Tests for IntrusivePtr and deleted ScopedPointer
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-IntrusivePtr.cpp
1 /*
2  * Copyright (c) 2015 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
19 #include <iostream>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 namespace
27 {
28
29 const int REPEAT = 1000;
30
31 size_t g_creationCount = 0;
32 size_t g_destructionCount = 0;
33 size_t g_creationCountSubclass = 0;
34 size_t g_destructionCountSubclass = 0;
35 size_t g_creationCountUnrelated = 0;
36 size_t g_destructionCountUnrelated = 0;
37
38 class Counted : public RefObject
39 {
40 public:
41   Counted()
42   {
43     ++g_creationCount;
44   }
45   ~Counted()
46   {
47     ++g_destructionCount;
48   }
49 };
50
51 class CountedSubclass : public Counted
52 {
53 public:
54   CountedSubclass()
55   {
56     ++g_creationCountSubclass;
57   }
58   ~CountedSubclass()
59   {
60     ++g_destructionCountSubclass;
61   }
62 };
63
64 class UnrelatedCounted : public RefObject
65 {
66 public:
67   UnrelatedCounted()
68   {
69     ++g_creationCountUnrelated;
70   }
71   ~UnrelatedCounted()
72   {
73     ++g_destructionCountUnrelated;
74   }
75 };
76 }
77
78 /**
79  * Test that a default constructed pointer is null and harmless.
80  */
81 int UtcDaliIntrusivePtrIntrusivePtr(void)
82 {
83   tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" );
84
85   g_creationCount = g_destructionCount = 0;
86
87   IntrusivePtr<Counted> counted;
88   DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
89   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
90   // Test the pointer is null
91   DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION );
92   DALI_TEST_EQUALS( &(*counted), (Counted*) 0, TEST_LOCATION );
93   // Check destruction of the null smart pointer does nothing:
94   counted = IntrusivePtr<Counted>();
95   DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
96   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
97
98   END_TEST;
99 }
100
101 int UtcDaliIntrusivePtrIntrusivePtrTP(void)
102 {
103   tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" );
104
105   g_creationCount = g_destructionCount = 0;
106
107   IntrusivePtr<Counted> counted( new Counted );
108   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
109   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
110   counted = 0;
111   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
112   DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
113
114   END_TEST;
115 }
116
117 // Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void)
118
119 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUP(void)
120 {
121   tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr<U> const &)" );
122
123   g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0;
124
125   IntrusivePtr<CountedSubclass> countedSubclass( new CountedSubclass );
126   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
127   DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION );
128   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
129   DALI_TEST_EQUALS( g_destructionCountSubclass, 0u, TEST_LOCATION );
130
131   IntrusivePtr<Counted> counted( countedSubclass );
132   DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
133
134   // Make loads more references:
135   std::vector< IntrusivePtr<Counted> > intrusivePtrs;
136   for( int i = 0; i < REPEAT; ++i )
137   {
138     intrusivePtrs.push_back( IntrusivePtr<Counted>( countedSubclass ) );
139   }
140   DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
141
142   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
143   DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION );
144   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
145
146   END_TEST;
147 }
148
149 // The negative version of this test would fail at compile time:
150 // int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void)
151
152 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrP(void)
153 {
154   tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" );
155
156   // Pass a pointer to a constructed second object:
157   // Pass a pointer to null:
158
159   g_creationCount = g_destructionCount = 0;
160
161   IntrusivePtr<Counted> counted( new Counted );
162   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
163   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
164   DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
165
166   IntrusivePtr<Counted> counted2( counted );
167   DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
168   DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
169
170   // Make loads more references:
171   std::vector< IntrusivePtr<Counted> > intrusivePtrs;
172   for( int i = 0; i < REPEAT; ++i )
173   {
174     intrusivePtrs.push_back( IntrusivePtr<Counted>( counted ) );
175   }
176   DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
177
178   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
179   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
180
181   intrusivePtrs.clear();
182
183   DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
184
185   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
186   DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
187
188   counted.Reset();
189   DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
190   counted2.Reset();
191
192   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
193   DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
194
195   END_TEST;
196 }
197
198 int UtcDaliIntrusivePtrGetP(void)
199 {
200   tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
201
202   IntrusivePtr<Counted> counted( new Counted );
203   DALI_TEST_CHECK( counted.Get() != 0 );
204   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
205   DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
206
207   END_TEST;
208 }
209
210 int UtcDaliIntrusivePtrGetN(void)
211 {
212   tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
213
214   g_creationCount = 0;
215
216   IntrusivePtr<Counted> counted( 0 );
217   DALI_TEST_CHECK( counted.Get() == 0 );
218   DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
219
220   END_TEST;
221 }
222
223 int UtcDaliIntrusivePtrArrowOperatorP(void)
224 {
225   tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" );
226
227   IntrusivePtr<Counted> counted( new Counted );
228   DALI_TEST_CHECK( (counted.operator->()) != 0 );
229   DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
230
231   END_TEST;
232 }
233
234 int UtcDaliIntrusivePtrArrowOperatorN(void)
235 {
236   tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" );
237
238   IntrusivePtr<Counted> counted;
239   DALI_TEST_CHECK( (counted.operator->()) == 0 );
240
241   END_TEST;
242 }
243
244 int UtcDaliIntrusivePtrIndirectionOperatorP(void)
245 {
246   tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
247
248   IntrusivePtr<Counted> counted( new Counted );
249   DALI_TEST_CHECK( &(counted.operator*()) != 0 );
250   DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION );
251
252   END_TEST;
253 }
254
255 int UtcDaliIntrusivePtrIndirectionOperatorN(void)
256 {
257   tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" );
258
259   IntrusivePtr<Counted> counted;
260   DALI_TEST_CHECK( &(counted.operator*()) == 0 );
261
262   END_TEST;
263 }
264
265 int UtcDaliIntrusivePtrResetP(void)
266 {
267   tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
268
269   IntrusivePtr<Counted> counted( new Counted );
270   DALI_TEST_CHECK( counted.Get() != 0 );
271   counted.Reset();
272   DALI_TEST_CHECK( counted.Get() == 0 );
273
274   END_TEST;
275 }
276
277 int UtcDaliIntrusivePtrResetN(void)
278 {
279   tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" );
280
281   IntrusivePtr<Counted> counted;
282   Counted* firstGet = counted.Get();
283   counted.Reset();
284   DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION );
285
286   END_TEST;
287 }
288
289 int UtcDaliIntrusivePtrResetTP(void)
290 {
291   tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" );
292
293   g_creationCount = g_destructionCount = 0;
294
295   IntrusivePtr<Counted> counted( new Counted );
296
297   IntrusivePtr<Counted> counted2( new Counted );
298
299   DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
300   DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
301
302   counted.Reset( counted2.Get() );
303
304   DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
305   DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION );
306
307   DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
308
309   DALI_TEST_EQUALS( g_creationCount, 2u, TEST_LOCATION );
310   DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
311
312   counted2.Reset( (Counted*) 0 );
313   counted.Reset( counted2.Get() );
314   DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
315
316   // Check that reseting nulls is harmless:
317   counted2.Reset( counted.Get() );
318   counted.Reset( counted2.Get() );
319
320   DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
321
322   END_TEST;
323 }
324
325
326 int UtcDaliIntrusivePtrResetTN(void)
327 {
328   tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" );
329
330   g_creationCount = g_destructionCount = 0;
331
332   IntrusivePtr<Counted> counted( new Counted );
333
334   counted.Reset( (Counted*) 0 );
335
336   DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION );
337   DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
338   DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
339
340   END_TEST;
341 }
342
343 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
344 {
345   tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
346
347   IntrusivePtr<Counted> counted( new Counted );
348   DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
349   DALI_TEST_CHECK( counted );
350   counted.Reset();
351   DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
352
353   END_TEST;
354 }
355
356 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
357 {
358   tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
359
360   IntrusivePtr<Counted> counted;
361   DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
362   DALI_TEST_CHECK( !counted );
363   END_TEST;
364 }
365
366 /** Equality of two different types*/
367 int UtcDaliIntrusivePtrOperatorEqualTU(void)
368 {
369   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
370
371   IntrusivePtr<Counted> counted1( new Counted );
372   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
373   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
374   IntrusivePtr<Counted> counted2( countedSubclass2 );
375
376   DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
377   DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
378   END_TEST;
379 }
380
381 /** Inequality of two different types*/
382 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
383 {
384   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
385
386   IntrusivePtr<Counted> counted1( new Counted );
387   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
388   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
389   IntrusivePtr<Counted> counted2( countedSubclass2 );
390
391   DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
392   DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
393   END_TEST;
394 }
395
396 /** Equality of two different types where right hand side is a raw pointer */
397 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
398 {
399   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
400
401   IntrusivePtr<Counted> counted1( new Counted );
402   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
403   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
404   IntrusivePtr<Counted> counted2( countedSubclass2 );
405
406   DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
407   DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
408   END_TEST;
409 }
410
411 /** Inequality of two different types where the right hand side is a raw pointer */
412 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
413 {
414   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
415
416   IntrusivePtr<Counted> counted1( new Counted );
417   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
418   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
419   IntrusivePtr<Counted> counted2( countedSubclass2 );
420
421   DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
422   DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
423   END_TEST;
424 }
425
426 /** Equality of two different types where left hand side is a raw pointer */
427 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
428 {
429   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
430
431   IntrusivePtr<Counted> counted1( new Counted );
432   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
433   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
434   IntrusivePtr<Counted> counted2( countedSubclass2 );
435
436   DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
437   DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
438   END_TEST;
439 }
440
441 /** Inequality of two different types where the left hand side is a raw pointer */
442 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
443 {
444   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
445
446   IntrusivePtr<Counted> counted1( new Counted );
447   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
448   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
449   IntrusivePtr<Counted> counted2( countedSubclass2 );
450
451   DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
452   DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );
453   END_TEST;
454 }
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480