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