2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <dali-test-suite-utils.h>
23 #include <dali/internal/common/owner-pointer.h>
25 using namespace Dali::Internal;
27 void utc_dali_internal_owner_pointer_startup(void)
29 test_return_value = TET_UNDEF;
32 void utc_dali_internal_owner_pointer_cleanup(void)
34 test_return_value = TET_PASS;
37 ///////////////////////////////////////////////////////////////////////////////
41 /// Takes in a reference to a bool which is set to true when the destructor is called
46 OwnedClass( bool& destructorCalled )
47 : mDestructorCalled( destructorCalled )
49 mDestructorCalled = false;
54 mDestructorCalled = true;
59 bool& mDestructorCalled;
62 /// Just a simple class with a function that marks a member boolean to true if that function is called
63 class ClassWithFunction
66 ClassWithFunction() = default;
70 functionCalled = true;
73 bool functionCalled{ false };
78 ///////////////////////////////////////////////////////////////////////////////
80 int UtcDaliOwnerPointerEnsureDeletion(void)
82 // Ensure that the object owned by the owner-pointer is deleted.
87 OwnerPointer< OwnedClass > pointer( new OwnedClass( deleted ) );
88 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
91 // OwnerPointer out-of-scope, object should be deleted.
93 DALI_TEST_EQUALS( deleted, true, TEST_LOCATION );
98 int UtcDaliOwnerPointerDefaultConstructor(void)
100 // Ensure the default constructor is created as expected.
102 OwnerPointer< OwnedClass > pointer;
103 DALI_TEST_CHECK( pointer.Get() == nullptr );
108 int UtcDaliOwnerPointerCopy(void)
110 // Call copy constructor and assignment operator
112 bool deleted = false;
113 OwnedClass* owned = new OwnedClass( deleted );
115 OwnerPointer< OwnedClass > first( owned );
116 DALI_TEST_CHECK( first.Get() == owned );
119 // Copy constructor, first should have a nullptr now, no object deletion
120 OwnerPointer< OwnedClass > second( first );
121 DALI_TEST_CHECK( first.Get() == nullptr );
122 DALI_TEST_CHECK( second.Get() == owned);
123 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
125 // Self assignment, nothing should change or be deleted.
128 DALI_TEST_CHECK( first.Get() == nullptr );
129 DALI_TEST_CHECK( second.Get() == owned );
130 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
132 // Assign second to first, no deletion, second should have a nullptr now
134 DALI_TEST_CHECK( first.Get() == owned );
135 DALI_TEST_CHECK( second.Get() == nullptr );
136 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
139 // second is out-of-scope now, no object deletion
140 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
142 // Assign to an empty pointer, owned object should be deleted
143 OwnerPointer< OwnedClass > empty;
145 DALI_TEST_EQUALS( deleted, true, TEST_LOCATION );
146 DALI_TEST_CHECK( first.Get() == nullptr );
147 DALI_TEST_CHECK( empty.Get() == nullptr );
152 int UtcDaliOwnerPointerMove(void)
154 // Call move constructor and move assignment operator
156 bool deleted = false;
157 OwnedClass* owned = new OwnedClass( deleted );
159 OwnerPointer< OwnedClass > first( owned );
160 DALI_TEST_CHECK( first.Get() == owned );
163 // Move constructor, first should have a nullptr now, no object deletion
164 OwnerPointer< OwnedClass > second( std::move( first ) );
165 DALI_TEST_CHECK( first.Get() == nullptr );
166 DALI_TEST_CHECK( second.Get() == owned);
167 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
169 // Self assignment, nothing should change or be deleted.
170 first = std::move( first );
171 second = std::move( second );
172 DALI_TEST_CHECK( first.Get() == nullptr );
173 DALI_TEST_CHECK( second.Get() == owned );
174 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
176 // Assign second to first, no deletion, second should have a nullptr now
177 first = std::move( second );
178 DALI_TEST_CHECK( first.Get() == owned );
179 DALI_TEST_CHECK( second.Get() == nullptr );
180 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
183 // second is out-of-scope now, no object deletion
184 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
186 // Assign to an empty pointer, owned object should be deleted
187 OwnerPointer< OwnedClass > empty;
188 first = std::move( empty );
189 DALI_TEST_EQUALS( deleted, true, TEST_LOCATION );
190 DALI_TEST_CHECK( first.Get() == nullptr );
191 DALI_TEST_CHECK( empty.Get() == nullptr );
196 int UtcDaliOwnerPointerIndirection(void)
198 // Check the indirection operators
200 using Ptr = OwnerPointer< int >;
203 int* rawIntPtr( new int( 200 ) );
204 Ptr nonConstPtr( rawIntPtr );
205 DALI_TEST_CHECK( rawIntPtr == &( *nonConstPtr ) );
206 DALI_TEST_EQUALS( *nonConstPtr, 200, TEST_LOCATION );
210 int* rawIntPtr2( new int( 300 ) );
211 const Ptr constPtr( rawIntPtr2 );
212 DALI_TEST_CHECK( rawIntPtr2 == &( *constPtr ) );
213 DALI_TEST_EQUALS( *constPtr, 300, TEST_LOCATION );
219 int UtcDaliOwnerPointerPointerOperator(void)
221 // Check the pointer operators
223 using Ptr = OwnerPointer< ClassWithFunction >;
225 // Check if function is called as expected when using a const OwnerPointer
227 ClassWithFunction* rawPtr( new ClassWithFunction );
228 Ptr nonConstPtr( rawPtr );
229 DALI_TEST_EQUALS( rawPtr->functionCalled, false, TEST_LOCATION );
230 nonConstPtr->MyFunction();
231 DALI_TEST_EQUALS( rawPtr->functionCalled, true, TEST_LOCATION );
235 // Check if function is called as expected when using a const OwnerPointer
237 ClassWithFunction* rawPtr2( new ClassWithFunction );
238 const Ptr constPtr( rawPtr2 );
239 DALI_TEST_EQUALS( rawPtr2->functionCalled, false, TEST_LOCATION );
240 constPtr->MyFunction();
241 DALI_TEST_EQUALS( rawPtr2->functionCalled, true, TEST_LOCATION );
246 int UtcDaliOwnerPointerComparisonOperator(void)
248 // Check the comparison operator
250 using Ptr = OwnerPointer< int >;
252 int* rawIntPtr( new int( 200 ) );
253 Ptr ownerPtr( rawIntPtr );
254 DALI_TEST_CHECK( ownerPtr == rawIntPtr );
255 DALI_TEST_CHECK( ! ( ownerPtr == nullptr ) );
260 int UtcDaliOwnerPointerReset(void)
262 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
264 bool deleted = false;
266 OwnerPointer< OwnedClass > pointer( new OwnedClass( deleted ) );
267 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
269 DALI_TEST_EQUALS( deleted, true, TEST_LOCATION );
270 DALI_TEST_CHECK( pointer.Get() == nullptr );
272 // Reset the empty pointer, should have no effect but there shouldn't be any crash
274 DALI_TEST_CHECK( pointer.Get() == nullptr );
279 int UtcDaliOwnerPointerRelease(void)
281 // Ensure that calling Release does NOT delete the object but still sets the owner-pointer to NULL
283 bool deleted = false;
285 OwnedClass* rawPtr = new OwnedClass( deleted );
286 OwnerPointer< OwnedClass > pointer( rawPtr );
288 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
289 DALI_TEST_CHECK( pointer.Release() == rawPtr );
290 DALI_TEST_EQUALS( deleted, false, TEST_LOCATION );
291 DALI_TEST_CHECK( pointer.Get() == nullptr );
293 // Release the empty pointer, should have no effect but there shouldn't be any crash
294 DALI_TEST_CHECK( pointer.Release() == nullptr );
295 DALI_TEST_CHECK( pointer.Get() == nullptr );
300 int UtcDaliOwnerPointerGet(void)
302 // Check the Get method
304 using Ptr = OwnerPointer< int >;
306 int* rawIntPtr( new int( 200 ) );
307 Ptr ownerPtr( rawIntPtr );
308 DALI_TEST_CHECK( ownerPtr.Get() == rawIntPtr );
313 int UtcDaliOwnerPointerSwap(void)
315 // Ensure the Swap method swaps the pointers and doesn't delete any objects
317 using Ptr = OwnerPointer< OwnedClass >;
319 bool firstObjectDeleted = false;
320 bool secondObjectDeleted = false;
322 OwnedClass* firstRawPtr = new OwnedClass( firstObjectDeleted );
323 OwnedClass* secondRawPtr = new OwnedClass( secondObjectDeleted );
325 Ptr firstPtr( firstRawPtr );
326 Ptr secondPtr( secondRawPtr );
328 // Check initial values
329 DALI_TEST_EQUALS( firstObjectDeleted, false, TEST_LOCATION );
330 DALI_TEST_EQUALS( secondObjectDeleted, false, TEST_LOCATION );
331 DALI_TEST_CHECK( firstPtr == firstRawPtr );
332 DALI_TEST_CHECK( secondPtr == secondRawPtr );
334 // Call Swap on first and ensure swap is done and there's no deletion
335 firstPtr.Swap( secondPtr );
336 DALI_TEST_EQUALS( firstObjectDeleted, false, TEST_LOCATION );
337 DALI_TEST_EQUALS( secondObjectDeleted, false, TEST_LOCATION );
338 DALI_TEST_CHECK( firstPtr == secondRawPtr );
339 DALI_TEST_CHECK( secondPtr == firstRawPtr );
341 // Swap back using second, again no deletion
342 secondPtr.Swap( firstPtr );
343 DALI_TEST_EQUALS( firstObjectDeleted, false, TEST_LOCATION );
344 DALI_TEST_EQUALS( secondObjectDeleted, false, TEST_LOCATION );
345 DALI_TEST_CHECK( firstPtr == firstRawPtr );
346 DALI_TEST_CHECK( secondPtr == secondRawPtr );
348 // Swap with self, nothing should change or be deleted
349 firstPtr.Swap( firstPtr );
350 DALI_TEST_EQUALS( firstObjectDeleted, false, TEST_LOCATION );
351 DALI_TEST_CHECK( firstPtr == firstRawPtr );
353 // Swap with an empty OwnerPointer, no deletion but firstPtr should be empty now
355 firstPtr.Swap( emptyPtr );
356 DALI_TEST_EQUALS( firstObjectDeleted, false, TEST_LOCATION );
357 DALI_TEST_CHECK( firstPtr == nullptr );
358 DALI_TEST_CHECK( emptyPtr == firstRawPtr );