2 * Copyright (c) 2015 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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
29 const int REPEAT = 1000;
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;
38 class Counted : public RefObject
51 class CountedSubclass : public Counted
56 ++g_creationCountSubclass;
60 ++g_destructionCountSubclass;
64 class UnrelatedCounted : public RefObject
69 ++g_creationCountUnrelated;
73 ++g_destructionCountUnrelated;
77 class TestObject : public RefObject
85 TestObject(const TestObject& testObject)
86 : RefObject(testObject),
91 TestObject& Assign(const TestObject& testObject)
93 RefObject::operator=(testObject);
94 data = testObject.data;
102 } // Anonymous namespace
105 * Test that a default constructed pointer is null and harmless.
107 int UtcDaliIntrusivePtrIntrusivePtr(void)
109 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" );
111 g_creationCount = g_destructionCount = 0;
113 IntrusivePtr<Counted> counted;
114 DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
115 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
116 // Test the pointer is null
117 DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION );
118 DALI_TEST_EQUALS( &(*counted), (Counted*) 0, TEST_LOCATION );
119 // Check destruction of the null smart pointer does nothing:
120 counted = IntrusivePtr<Counted>();
121 DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
122 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
127 int UtcDaliIntrusivePtrIntrusivePtrTP(void)
129 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" );
131 g_creationCount = g_destructionCount = 0;
133 IntrusivePtr<Counted> counted( new Counted );
134 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
135 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
137 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
138 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
143 // Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void)
145 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUP(void)
147 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr<U> const &)" );
149 g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0;
151 IntrusivePtr<CountedSubclass> countedSubclass( new CountedSubclass );
152 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
153 DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION );
154 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
155 DALI_TEST_EQUALS( g_destructionCountSubclass, 0u, TEST_LOCATION );
157 IntrusivePtr<Counted> counted( countedSubclass );
158 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
160 // Make loads more references:
161 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
162 for( int i = 0; i < REPEAT; ++i )
164 intrusivePtrs.push_back( IntrusivePtr<Counted>( countedSubclass ) );
166 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
168 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
169 DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION );
170 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
175 // The negative version of this test would fail at compile time:
176 // int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void)
178 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrP(void)
180 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" );
182 // Pass a pointer to a constructed second object:
183 // Pass a pointer to null:
185 g_creationCount = g_destructionCount = 0;
187 IntrusivePtr<Counted> counted( new Counted );
188 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
189 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
190 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
192 IntrusivePtr<Counted> counted2( counted );
193 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
194 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
196 // Make loads more references:
197 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
198 for( int i = 0; i < REPEAT; ++i )
200 intrusivePtrs.push_back( IntrusivePtr<Counted>( counted ) );
202 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
204 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
205 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
207 intrusivePtrs.clear();
209 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
211 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
212 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
215 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
218 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
219 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
224 int UtcDaliIntrusivePtrGetP(void)
226 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
228 IntrusivePtr<Counted> counted( new Counted );
229 DALI_TEST_CHECK( counted.Get() != 0 );
230 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
231 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
236 int UtcDaliIntrusivePtrGetN(void)
238 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
242 IntrusivePtr<Counted> counted( 0 );
243 DALI_TEST_CHECK( counted.Get() == 0 );
244 DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
249 int UtcDaliIntrusivePtrArrowOperatorP(void)
251 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" );
253 IntrusivePtr<Counted> counted( new Counted );
254 DALI_TEST_CHECK( (counted.operator->()) != 0 );
255 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
260 int UtcDaliIntrusivePtrArrowOperatorN(void)
262 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" );
264 IntrusivePtr<Counted> counted;
265 DALI_TEST_CHECK( (counted.operator->()) == 0 );
270 int UtcDaliIntrusivePtrIndirectionOperatorP(void)
272 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
274 IntrusivePtr<Counted> counted( new Counted );
275 DALI_TEST_CHECK( &(counted.operator*()) != 0 );
276 DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION );
281 int UtcDaliIntrusivePtrIndirectionOperatorN(void)
283 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" );
285 IntrusivePtr<Counted> counted;
286 DALI_TEST_CHECK( &(counted.operator*()) == 0 );
291 int UtcDaliIntrusivePtrResetP(void)
293 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
295 IntrusivePtr<Counted> counted( new Counted );
296 DALI_TEST_CHECK( counted.Get() != 0 );
298 DALI_TEST_CHECK( counted.Get() == 0 );
303 int UtcDaliIntrusivePtrResetN(void)
305 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" );
307 IntrusivePtr<Counted> counted;
308 Counted* firstGet = counted.Get();
310 DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION );
315 int UtcDaliIntrusivePtrResetTP(void)
317 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" );
319 g_creationCount = g_destructionCount = 0;
321 IntrusivePtr<Counted> counted( new Counted );
323 IntrusivePtr<Counted> counted2( new Counted );
325 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
326 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
328 counted.Reset( counted2.Get() );
330 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
331 DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION );
333 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
335 DALI_TEST_EQUALS( g_creationCount, 2u, TEST_LOCATION );
336 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
338 counted2.Reset( (Counted*) 0 );
339 counted.Reset( counted2.Get() );
340 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
342 // Check that reseting nulls is harmless:
343 counted2.Reset( counted.Get() );
344 counted.Reset( counted2.Get() );
346 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
352 int UtcDaliIntrusivePtrResetTN(void)
354 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" );
356 g_creationCount = g_destructionCount = 0;
358 IntrusivePtr<Counted> counted( new Counted );
360 counted.Reset( (Counted*) 0 );
362 DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION );
363 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
364 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
371 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
373 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
375 IntrusivePtr<Counted> counted( new Counted );
376 DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
377 DALI_TEST_CHECK( counted );
379 typedef void (IntrusivePtr<Counted>::*BoolIdiomFunc)() const;
380 BoolIdiomFunc func = static_cast<BoolIdiomFunc>( counted.operator BooleanType() );
381 ((counted).*func)(); // purely for test coverage
384 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
389 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
391 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
393 IntrusivePtr<Counted> counted;
394 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
395 DALI_TEST_CHECK( !counted );
399 /** Equality of two different types*/
400 int UtcDaliIntrusivePtrOperatorEqualTU(void)
402 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
404 IntrusivePtr<Counted> counted1( new Counted );
405 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
406 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
407 IntrusivePtr<Counted> counted2( countedSubclass2 );
409 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
410 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
414 /** Inequality of two different types*/
415 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
417 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
419 IntrusivePtr<Counted> counted1( new Counted );
420 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
421 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
422 IntrusivePtr<Counted> counted2( countedSubclass2 );
424 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
425 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
429 /** Equality of two different types where right hand side is a raw pointer */
430 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
432 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
434 IntrusivePtr<Counted> counted1( new Counted );
435 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
436 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
437 IntrusivePtr<Counted> counted2( countedSubclass2 );
439 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
440 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
444 /** Inequality of two different types where the right hand side is a raw pointer */
445 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
447 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
449 IntrusivePtr<Counted> counted1( new Counted );
450 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
451 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
452 IntrusivePtr<Counted> counted2( countedSubclass2 );
454 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
455 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
459 /** Equality of two different types where left hand side is a raw pointer */
460 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
462 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
464 IntrusivePtr<Counted> counted1( new Counted );
465 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
466 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
467 IntrusivePtr<Counted> counted2( countedSubclass2 );
469 DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
470 DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
474 /** Inequality of two different types where the left hand side is a raw pointer */
475 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
477 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
479 IntrusivePtr<Counted> counted1( new Counted );
480 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
481 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
482 IntrusivePtr<Counted> counted2( countedSubclass2 );
484 DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
485 DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );
489 int UtcDaliRefObjectCopyConstructor(void)
491 tet_infoline("Test for Dali::RefObject(const RefObject&)");
494 IntrusivePtr<TestObject> testPtr( new TestObject );
495 DALI_TEST_EQUALS( testPtr->ReferenceCount(), 1, TEST_LOCATION );
497 const TestObject& testObject=*testPtr.Get();
499 IntrusivePtr<TestObject> testPtr2( new TestObject(testObject) );
500 DALI_TEST_EQUALS( testPtr2->ReferenceCount(), 1, TEST_LOCATION );
502 DALI_TEST_EQUALS( testPtr->ReferenceCount(), 1, TEST_LOCATION );
508 int UtcDaliRefObjectAssignmentOperator(void)
510 tet_infoline("Test for Dali::RefObject::operator=(const RefObject&)");
513 IntrusivePtr<TestObject> testPtr( new TestObject );
514 DALI_TEST_EQUALS( testPtr->ReferenceCount(), 1, TEST_LOCATION );
516 const TestObject& testObject=*testPtr.Get();
518 IntrusivePtr<TestObject> testPtr2( new TestObject() );
520 IntrusivePtr<TestObject> testPtr3 = testPtr2;
521 DALI_TEST_EQUALS( testPtr2->ReferenceCount(), 2, TEST_LOCATION );
522 DALI_TEST_EQUALS( testPtr2->data, 201, TEST_LOCATION );
524 TestObject& testObject2 = *testPtr2.Get();
525 testObject2 = testObject;
527 DALI_TEST_EQUALS( testPtr->ReferenceCount(), 1, TEST_LOCATION );
528 DALI_TEST_EQUALS( testPtr2->ReferenceCount(), 2, TEST_LOCATION );
530 DALI_TEST_EQUALS( testPtr->ReferenceCount(), 1, TEST_LOCATION );