2 * Copyright (c) 2020 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 ///////////////////////////////////////////////////////////////////////////////
40 /// Takes in a reference to a bool which is set to true when the destructor is called
44 OwnedClass(bool& destructorCalled)
45 : mDestructorCalled(destructorCalled)
47 mDestructorCalled = false;
52 mDestructorCalled = true;
56 bool& mDestructorCalled;
59 /// Just a simple class with a function that marks a member boolean to true if that function is called
60 class ClassWithFunction
63 ClassWithFunction() = default;
67 functionCalled = true;
70 bool functionCalled{false};
75 ///////////////////////////////////////////////////////////////////////////////
77 int UtcDaliOwnerPointerEnsureDeletion(void)
79 // Ensure that the object owned by the owner-pointer is deleted.
84 OwnerPointer<OwnedClass> pointer(new OwnedClass(deleted));
85 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
88 // OwnerPointer out-of-scope, object should be deleted.
90 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
95 int UtcDaliOwnerPointerDefaultConstructor(void)
97 // Ensure the default constructor is created as expected.
99 OwnerPointer<OwnedClass> pointer;
100 DALI_TEST_CHECK(pointer.Get() == nullptr);
105 int UtcDaliOwnerPointerCopy(void)
107 // Call copy constructor and assignment operator
109 bool deleted = false;
110 OwnedClass* owned = new OwnedClass(deleted);
112 OwnerPointer<OwnedClass> first(owned);
113 DALI_TEST_CHECK(first.Get() == owned);
116 // Copy constructor, first should have a nullptr now, no object deletion
117 OwnerPointer<OwnedClass> second(first);
118 DALI_TEST_CHECK(first.Get() == nullptr);
119 DALI_TEST_CHECK(second.Get() == owned);
120 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
122 // Self assignment, nothing should change or be deleted.
125 DALI_TEST_CHECK(first.Get() == nullptr);
126 DALI_TEST_CHECK(second.Get() == owned);
127 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
129 // Assign second to first, no deletion, second should have a nullptr now
131 DALI_TEST_CHECK(first.Get() == owned);
132 DALI_TEST_CHECK(second.Get() == nullptr);
133 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
136 // second is out-of-scope now, no object deletion
137 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
139 // Assign to an empty pointer, owned object should be deleted
140 OwnerPointer<OwnedClass> empty;
142 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
143 DALI_TEST_CHECK(first.Get() == nullptr);
144 DALI_TEST_CHECK(empty.Get() == nullptr);
149 int UtcDaliOwnerPointerMove(void)
151 // Call move constructor and move assignment operator
153 bool deleted = false;
154 OwnedClass* owned = new OwnedClass(deleted);
156 OwnerPointer<OwnedClass> first(owned);
157 DALI_TEST_CHECK(first.Get() == owned);
160 // Move constructor, first should have a nullptr now, no object deletion
161 OwnerPointer<OwnedClass> second(std::move(first));
162 DALI_TEST_CHECK(first.Get() == nullptr);
163 DALI_TEST_CHECK(second.Get() == owned);
164 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
166 // Self assignment, nothing should change or be deleted.
167 first = std::move(first);
168 second = std::move(second);
169 DALI_TEST_CHECK(first.Get() == nullptr);
170 DALI_TEST_CHECK(second.Get() == owned);
171 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
173 // Assign second to first, no deletion, second should have a nullptr now
174 first = std::move(second);
175 DALI_TEST_CHECK(first.Get() == owned);
176 DALI_TEST_CHECK(second.Get() == nullptr);
177 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
180 // second is out-of-scope now, no object deletion
181 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
183 // Assign to an empty pointer, owned object should be deleted
184 OwnerPointer<OwnedClass> empty;
185 first = std::move(empty);
186 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
187 DALI_TEST_CHECK(first.Get() == nullptr);
188 DALI_TEST_CHECK(empty.Get() == nullptr);
193 int UtcDaliOwnerPointerIndirection(void)
195 // Check the indirection operators
197 using Ptr = OwnerPointer<int>;
200 int* rawIntPtr(new int(200));
201 Ptr nonConstPtr(rawIntPtr);
202 DALI_TEST_CHECK(rawIntPtr == &(*nonConstPtr));
203 DALI_TEST_EQUALS(*nonConstPtr, 200, TEST_LOCATION);
207 int* rawIntPtr2(new int(300));
208 const Ptr constPtr(rawIntPtr2);
209 DALI_TEST_CHECK(rawIntPtr2 == &(*constPtr));
210 DALI_TEST_EQUALS(*constPtr, 300, TEST_LOCATION);
216 int UtcDaliOwnerPointerPointerOperator(void)
218 // Check the pointer operators
220 using Ptr = OwnerPointer<ClassWithFunction>;
222 // Check if function is called as expected when using a const OwnerPointer
224 ClassWithFunction* rawPtr(new ClassWithFunction);
225 Ptr nonConstPtr(rawPtr);
226 DALI_TEST_EQUALS(rawPtr->functionCalled, false, TEST_LOCATION);
227 nonConstPtr->MyFunction();
228 DALI_TEST_EQUALS(rawPtr->functionCalled, true, TEST_LOCATION);
231 // Check if function is called as expected when using a const OwnerPointer
233 ClassWithFunction* rawPtr2(new ClassWithFunction);
234 const Ptr constPtr(rawPtr2);
235 DALI_TEST_EQUALS(rawPtr2->functionCalled, false, TEST_LOCATION);
236 constPtr->MyFunction();
237 DALI_TEST_EQUALS(rawPtr2->functionCalled, true, TEST_LOCATION);
242 int UtcDaliOwnerPointerComparisonOperator(void)
244 // Check the comparison operator
246 using Ptr = OwnerPointer<int>;
248 int* rawIntPtr(new int(200));
249 Ptr ownerPtr(rawIntPtr);
250 DALI_TEST_CHECK(ownerPtr == rawIntPtr);
251 DALI_TEST_CHECK(!(ownerPtr == nullptr));
256 int UtcDaliOwnerPointerReset(void)
258 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
260 bool deleted = false;
262 OwnerPointer<OwnedClass> pointer(new OwnedClass(deleted));
263 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
265 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
266 DALI_TEST_CHECK(pointer.Get() == nullptr);
268 // Reset the empty pointer, should have no effect but there shouldn't be any crash
270 DALI_TEST_CHECK(pointer.Get() == nullptr);
275 int UtcDaliOwnerPointerRelease(void)
277 // Ensure that calling Release does NOT delete the object but still sets the owner-pointer to NULL
279 bool deleted = false;
281 OwnedClass* rawPtr = new OwnedClass(deleted);
282 OwnerPointer<OwnedClass> pointer(rawPtr);
284 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
285 DALI_TEST_CHECK(pointer.Release() == rawPtr);
286 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
287 DALI_TEST_CHECK(pointer.Get() == nullptr);
289 // Release the empty pointer, should have no effect but there shouldn't be any crash
290 DALI_TEST_CHECK(pointer.Release() == nullptr);
291 DALI_TEST_CHECK(pointer.Get() == nullptr);
296 int UtcDaliOwnerPointerGet(void)
298 // Check the Get method
300 using Ptr = OwnerPointer<int>;
302 int* rawIntPtr(new int(200));
303 Ptr ownerPtr(rawIntPtr);
304 DALI_TEST_CHECK(ownerPtr.Get() == rawIntPtr);
309 int UtcDaliOwnerPointerSwap(void)
311 // Ensure the Swap method swaps the pointers and doesn't delete any objects
313 using Ptr = OwnerPointer<OwnedClass>;
315 bool firstObjectDeleted = false;
316 bool secondObjectDeleted = false;
318 OwnedClass* firstRawPtr = new OwnedClass(firstObjectDeleted);
319 OwnedClass* secondRawPtr = new OwnedClass(secondObjectDeleted);
321 Ptr firstPtr(firstRawPtr);
322 Ptr secondPtr(secondRawPtr);
324 // Check initial values
325 DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
326 DALI_TEST_EQUALS(secondObjectDeleted, false, TEST_LOCATION);
327 DALI_TEST_CHECK(firstPtr == firstRawPtr);
328 DALI_TEST_CHECK(secondPtr == secondRawPtr);
330 // Call Swap on first and ensure swap is done and there's no deletion
331 firstPtr.Swap(secondPtr);
332 DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
333 DALI_TEST_EQUALS(secondObjectDeleted, false, TEST_LOCATION);
334 DALI_TEST_CHECK(firstPtr == secondRawPtr);
335 DALI_TEST_CHECK(secondPtr == firstRawPtr);
337 // Swap back using second, again no deletion
338 secondPtr.Swap(firstPtr);
339 DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
340 DALI_TEST_EQUALS(secondObjectDeleted, false, TEST_LOCATION);
341 DALI_TEST_CHECK(firstPtr == firstRawPtr);
342 DALI_TEST_CHECK(secondPtr == secondRawPtr);
344 // Swap with self, nothing should change or be deleted
345 firstPtr.Swap(firstPtr);
346 DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
347 DALI_TEST_CHECK(firstPtr == firstRawPtr);
349 // Swap with an empty OwnerPointer, no deletion but firstPtr should be empty now
351 firstPtr.Swap(emptyPtr);
352 DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
353 DALI_TEST_CHECK(firstPtr == nullptr);
354 DALI_TEST_CHECK(emptyPtr == firstRawPtr);