Revert "[3.0] Added test cases for public api."
[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
344
345 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
346 {
347   tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
348
349   IntrusivePtr<Counted> counted( new Counted );
350   DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
351   DALI_TEST_CHECK( counted );
352
353   typedef void (IntrusivePtr<Counted>::*BoolIdiomFunc)() const;
354   BoolIdiomFunc func = static_cast<BoolIdiomFunc>( counted.operator BooleanType() );
355   ((counted).*func)(); // purely for test coverage
356
357   counted.Reset();
358   DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
359
360   END_TEST;
361 }
362
363 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
364 {
365   tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
366
367   IntrusivePtr<Counted> counted;
368   DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
369   DALI_TEST_CHECK( !counted );
370   END_TEST;
371 }
372
373 /** Equality of two different types*/
374 int UtcDaliIntrusivePtrOperatorEqualTU(void)
375 {
376   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
377
378   IntrusivePtr<Counted> counted1( new Counted );
379   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
380   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
381   IntrusivePtr<Counted> counted2( countedSubclass2 );
382
383   DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
384   DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
385   END_TEST;
386 }
387
388 /** Inequality of two different types*/
389 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
390 {
391   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
392
393   IntrusivePtr<Counted> counted1( new Counted );
394   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
395   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
396   IntrusivePtr<Counted> counted2( countedSubclass2 );
397
398   DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
399   DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
400   END_TEST;
401 }
402
403 /** Equality of two different types where right hand side is a raw pointer */
404 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
405 {
406   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
407
408   IntrusivePtr<Counted> counted1( new Counted );
409   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
410   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
411   IntrusivePtr<Counted> counted2( countedSubclass2 );
412
413   DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
414   DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
415   END_TEST;
416 }
417
418 /** Inequality of two different types where the right hand side is a raw pointer */
419 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
420 {
421   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
422
423   IntrusivePtr<Counted> counted1( new Counted );
424   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
425   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
426   IntrusivePtr<Counted> counted2( countedSubclass2 );
427
428   DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
429   DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
430   END_TEST;
431 }
432
433 /** Equality of two different types where left hand side is a raw pointer */
434 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
435 {
436   tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
437
438   IntrusivePtr<Counted> counted1( new Counted );
439   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
440   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
441   IntrusivePtr<Counted> counted2( countedSubclass2 );
442
443   DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
444   DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
445   END_TEST;
446 }
447
448 /** Inequality of two different types where the left hand side is a raw pointer */
449 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
450 {
451   tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
452
453   IntrusivePtr<Counted> counted1( new Counted );
454   IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
455   IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
456   IntrusivePtr<Counted> counted2( countedSubclass2 );
457
458   DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
459   DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );
460   END_TEST;
461 }