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;
79 * Test that a default constructed pointer is null and harmless.
81 int UtcDaliIntrusivePtrIntrusivePtr(void)
83 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" );
85 g_creationCount = g_destructionCount = 0;
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 );
101 int UtcDaliIntrusivePtrIntrusivePtrTP(void)
103 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" );
105 g_creationCount = g_destructionCount = 0;
107 IntrusivePtr<Counted> counted( new Counted );
108 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
109 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
111 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
112 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
117 // Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void)
119 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUP(void)
121 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr<U> const &)" );
123 g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0;
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 );
131 IntrusivePtr<Counted> counted( countedSubclass );
132 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
134 // Make loads more references:
135 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
136 for( int i = 0; i < REPEAT; ++i )
138 intrusivePtrs.push_back( IntrusivePtr<Counted>( countedSubclass ) );
140 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
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 );
149 // The negative version of this test would fail at compile time:
150 // int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void)
152 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrP(void)
154 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" );
156 // Pass a pointer to a constructed second object:
157 // Pass a pointer to null:
159 g_creationCount = g_destructionCount = 0;
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 );
166 IntrusivePtr<Counted> counted2( counted );
167 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
168 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
170 // Make loads more references:
171 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
172 for( int i = 0; i < REPEAT; ++i )
174 intrusivePtrs.push_back( IntrusivePtr<Counted>( counted ) );
176 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
178 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
179 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
181 intrusivePtrs.clear();
183 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
185 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
186 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
189 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
192 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
193 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
198 int UtcDaliIntrusivePtrGetP(void)
200 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
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 );
210 int UtcDaliIntrusivePtrGetN(void)
212 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
216 IntrusivePtr<Counted> counted( 0 );
217 DALI_TEST_CHECK( counted.Get() == 0 );
218 DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
223 int UtcDaliIntrusivePtrArrowOperatorP(void)
225 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" );
227 IntrusivePtr<Counted> counted( new Counted );
228 DALI_TEST_CHECK( (counted.operator->()) != 0 );
229 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
234 int UtcDaliIntrusivePtrArrowOperatorN(void)
236 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" );
238 IntrusivePtr<Counted> counted;
239 DALI_TEST_CHECK( (counted.operator->()) == 0 );
244 int UtcDaliIntrusivePtrIndirectionOperatorP(void)
246 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
248 IntrusivePtr<Counted> counted( new Counted );
249 DALI_TEST_CHECK( &(counted.operator*()) != 0 );
250 DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION );
255 int UtcDaliIntrusivePtrIndirectionOperatorN(void)
257 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" );
259 IntrusivePtr<Counted> counted;
260 DALI_TEST_CHECK( &(counted.operator*()) == 0 );
265 int UtcDaliIntrusivePtrResetP(void)
267 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
269 IntrusivePtr<Counted> counted( new Counted );
270 DALI_TEST_CHECK( counted.Get() != 0 );
272 DALI_TEST_CHECK( counted.Get() == 0 );
277 int UtcDaliIntrusivePtrResetN(void)
279 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" );
281 IntrusivePtr<Counted> counted;
282 Counted* firstGet = counted.Get();
284 DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION );
289 int UtcDaliIntrusivePtrResetTP(void)
291 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" );
293 g_creationCount = g_destructionCount = 0;
295 IntrusivePtr<Counted> counted( new Counted );
297 IntrusivePtr<Counted> counted2( new Counted );
299 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
300 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
302 counted.Reset( counted2.Get() );
304 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
305 DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION );
307 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
309 DALI_TEST_EQUALS( g_creationCount, 2u, TEST_LOCATION );
310 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
312 counted2.Reset( (Counted*) 0 );
313 counted.Reset( counted2.Get() );
314 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
316 // Check that reseting nulls is harmless:
317 counted2.Reset( counted.Get() );
318 counted.Reset( counted2.Get() );
320 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
326 int UtcDaliIntrusivePtrResetTN(void)
328 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" );
330 g_creationCount = g_destructionCount = 0;
332 IntrusivePtr<Counted> counted( new Counted );
334 counted.Reset( (Counted*) 0 );
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 );
345 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
347 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
349 IntrusivePtr<Counted> counted( new Counted );
350 DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
351 DALI_TEST_CHECK( counted );
353 typedef void (IntrusivePtr<Counted>::*BoolIdiomFunc)() const;
354 BoolIdiomFunc func = static_cast<BoolIdiomFunc>( counted.operator BooleanType() );
355 ((counted).*func)(); // purely for test coverage
358 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
363 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
365 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
367 IntrusivePtr<Counted> counted;
368 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
369 DALI_TEST_CHECK( !counted );
373 /** Equality of two different types*/
374 int UtcDaliIntrusivePtrOperatorEqualTU(void)
376 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
378 IntrusivePtr<Counted> counted1( new Counted );
379 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
380 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
381 IntrusivePtr<Counted> counted2( countedSubclass2 );
383 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
384 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
388 /** Inequality of two different types*/
389 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
391 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
393 IntrusivePtr<Counted> counted1( new Counted );
394 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
395 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
396 IntrusivePtr<Counted> counted2( countedSubclass2 );
398 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
399 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
403 /** Equality of two different types where right hand side is a raw pointer */
404 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
406 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
408 IntrusivePtr<Counted> counted1( new Counted );
409 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
410 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
411 IntrusivePtr<Counted> counted2( countedSubclass2 );
413 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
414 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
418 /** Inequality of two different types where the right hand side is a raw pointer */
419 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
421 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
423 IntrusivePtr<Counted> counted1( new Counted );
424 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
425 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
426 IntrusivePtr<Counted> counted2( countedSubclass2 );
428 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
429 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
433 /** Equality of two different types where left hand side is a raw pointer */
434 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
436 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
438 IntrusivePtr<Counted> counted1( new Counted );
439 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
440 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
441 IntrusivePtr<Counted> counted2( countedSubclass2 );
443 DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
444 DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
448 /** Inequality of two different types where the left hand side is a raw pointer */
449 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
451 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
453 IntrusivePtr<Counted> counted1( new Counted );
454 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
455 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
456 IntrusivePtr<Counted> counted2( countedSubclass2 );
458 DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
459 DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );