Formatting automated-tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-OwnerPointer.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <utility>
20
21 // INTERNAL INCLUDES
22 #include <dali-test-suite-utils.h>
23 #include <dali/internal/common/owner-pointer.h>
24
25 using namespace Dali::Internal;
26
27 void utc_dali_internal_owner_pointer_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_internal_owner_pointer_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38 namespace
39 {
40 /// Takes in a reference to a bool which is set to true when the destructor is called
41 class OwnedClass
42 {
43 public:
44   OwnedClass(bool& destructorCalled)
45   : mDestructorCalled(destructorCalled)
46   {
47     mDestructorCalled = false;
48   }
49
50   ~OwnedClass()
51   {
52     mDestructorCalled = true;
53   }
54
55 private:
56   bool& mDestructorCalled;
57 };
58
59 /// Just a simple class with a function that marks a member boolean to true if that function is called
60 class ClassWithFunction
61 {
62 public:
63   ClassWithFunction() = default;
64
65   void MyFunction()
66   {
67     functionCalled = true;
68   }
69
70   bool functionCalled{false};
71 };
72
73 } // namespace
74
75 ///////////////////////////////////////////////////////////////////////////////
76
77 int UtcDaliOwnerPointerEnsureDeletion(void)
78 {
79   // Ensure that the object owned by the owner-pointer is deleted.
80
81   bool deleted = false;
82
83   {
84     OwnerPointer<OwnedClass> pointer(new OwnedClass(deleted));
85     DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
86   }
87
88   // OwnerPointer out-of-scope, object should be deleted.
89
90   DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
91
92   END_TEST;
93 }
94
95 int UtcDaliOwnerPointerDefaultConstructor(void)
96 {
97   // Ensure the default constructor is created as expected.
98
99   OwnerPointer<OwnedClass> pointer;
100   DALI_TEST_CHECK(pointer.Get() == nullptr);
101
102   END_TEST;
103 }
104
105 int UtcDaliOwnerPointerCopy(void)
106 {
107   // Call copy constructor and assignment operator
108
109   bool        deleted = false;
110   OwnedClass* owned   = new OwnedClass(deleted);
111
112   OwnerPointer<OwnedClass> first(owned);
113   DALI_TEST_CHECK(first.Get() == owned);
114
115   {
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);
121
122     // Self assignment, nothing should change or be deleted.
123     first  = first;
124     second = second;
125     DALI_TEST_CHECK(first.Get() == nullptr);
126     DALI_TEST_CHECK(second.Get() == owned);
127     DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
128
129     // Assign second to first, no deletion, second should have a nullptr now
130     first = second;
131     DALI_TEST_CHECK(first.Get() == owned);
132     DALI_TEST_CHECK(second.Get() == nullptr);
133     DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
134   }
135
136   // second is out-of-scope now, no object deletion
137   DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
138
139   // Assign to an empty pointer, owned object should be deleted
140   OwnerPointer<OwnedClass> empty;
141   first = empty;
142   DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
143   DALI_TEST_CHECK(first.Get() == nullptr);
144   DALI_TEST_CHECK(empty.Get() == nullptr);
145
146   END_TEST;
147 }
148
149 int UtcDaliOwnerPointerMove(void)
150 {
151   // Call move constructor and move assignment operator
152
153   bool        deleted = false;
154   OwnedClass* owned   = new OwnedClass(deleted);
155
156   OwnerPointer<OwnedClass> first(owned);
157   DALI_TEST_CHECK(first.Get() == owned);
158
159   {
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);
165
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);
172
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);
178   }
179
180   // second is out-of-scope now, no object deletion
181   DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
182
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);
189
190   END_TEST;
191 }
192
193 int UtcDaliOwnerPointerIndirection(void)
194 {
195   // Check the indirection operators
196
197   using Ptr = OwnerPointer<int>;
198
199   {
200     int* rawIntPtr(new int(200));
201     Ptr  nonConstPtr(rawIntPtr);
202     DALI_TEST_CHECK(rawIntPtr == &(*nonConstPtr));
203     DALI_TEST_EQUALS(*nonConstPtr, 200, TEST_LOCATION);
204   }
205
206   {
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);
211   }
212
213   END_TEST;
214 }
215
216 int UtcDaliOwnerPointerPointerOperator(void)
217 {
218   // Check the pointer operators
219
220   using Ptr = OwnerPointer<ClassWithFunction>;
221
222   // Check if function is called as expected when using a const OwnerPointer
223   {
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);
229   }
230
231   // Check if function is called as expected when using a const OwnerPointer
232   {
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);
238   }
239   END_TEST;
240 }
241
242 int UtcDaliOwnerPointerComparisonOperator(void)
243 {
244   // Check the comparison operator
245
246   using Ptr = OwnerPointer<int>;
247
248   int* rawIntPtr(new int(200));
249   Ptr  ownerPtr(rawIntPtr);
250   DALI_TEST_CHECK(ownerPtr == rawIntPtr);
251   DALI_TEST_CHECK(!(ownerPtr == nullptr));
252
253   END_TEST;
254 }
255
256 int UtcDaliOwnerPointerReset(void)
257 {
258   // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
259
260   bool deleted = false;
261
262   OwnerPointer<OwnedClass> pointer(new OwnedClass(deleted));
263   DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
264   pointer.Reset();
265   DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
266   DALI_TEST_CHECK(pointer.Get() == nullptr);
267
268   // Reset the empty pointer, should have no effect but there shouldn't be any crash
269   pointer.Reset();
270   DALI_TEST_CHECK(pointer.Get() == nullptr);
271
272   END_TEST;
273 }
274
275 int UtcDaliOwnerPointerRelease(void)
276 {
277   // Ensure that calling Release does NOT delete the object but still sets the owner-pointer to NULL
278
279   bool deleted = false;
280
281   OwnedClass*              rawPtr = new OwnedClass(deleted);
282   OwnerPointer<OwnedClass> pointer(rawPtr);
283
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);
288
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);
292
293   END_TEST;
294 }
295
296 int UtcDaliOwnerPointerGet(void)
297 {
298   // Check the Get method
299
300   using Ptr = OwnerPointer<int>;
301
302   int* rawIntPtr(new int(200));
303   Ptr  ownerPtr(rawIntPtr);
304   DALI_TEST_CHECK(ownerPtr.Get() == rawIntPtr);
305
306   END_TEST;
307 }
308
309 int UtcDaliOwnerPointerSwap(void)
310 {
311   // Ensure the Swap method swaps the pointers and doesn't delete any objects
312
313   using Ptr = OwnerPointer<OwnedClass>;
314
315   bool firstObjectDeleted  = false;
316   bool secondObjectDeleted = false;
317
318   OwnedClass* firstRawPtr  = new OwnedClass(firstObjectDeleted);
319   OwnedClass* secondRawPtr = new OwnedClass(secondObjectDeleted);
320
321   Ptr firstPtr(firstRawPtr);
322   Ptr secondPtr(secondRawPtr);
323
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);
329
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);
336
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);
343
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);
348
349   // Swap with an empty OwnerPointer, no deletion but firstPtr should be empty now
350   Ptr emptyPtr;
351   firstPtr.Swap(emptyPtr);
352   DALI_TEST_EQUALS(firstObjectDeleted, false, TEST_LOCATION);
353   DALI_TEST_CHECK(firstPtr == nullptr);
354   DALI_TEST_CHECK(emptyPtr == firstRawPtr);
355
356   END_TEST;
357 }