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 );
343 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
345 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
347 IntrusivePtr<Counted> counted( new Counted );
348 DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
349 DALI_TEST_CHECK( counted );
351 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
356 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
358 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
360 IntrusivePtr<Counted> counted;
361 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
362 DALI_TEST_CHECK( !counted );
366 /** Equality of two different types*/
367 int UtcDaliIntrusivePtrOperatorEqualTU(void)
369 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
371 IntrusivePtr<Counted> counted1( new Counted );
372 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
373 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
374 IntrusivePtr<Counted> counted2( countedSubclass2 );
376 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
377 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
381 /** Inequality of two different types*/
382 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
384 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
386 IntrusivePtr<Counted> counted1( new Counted );
387 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
388 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
389 IntrusivePtr<Counted> counted2( countedSubclass2 );
391 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
392 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
396 /** Equality of two different types where right hand side is a raw pointer */
397 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
399 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
401 IntrusivePtr<Counted> counted1( new Counted );
402 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
403 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
404 IntrusivePtr<Counted> counted2( countedSubclass2 );
406 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
407 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
411 /** Inequality of two different types where the right hand side is a raw pointer */
412 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
414 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
416 IntrusivePtr<Counted> counted1( new Counted );
417 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
418 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
419 IntrusivePtr<Counted> counted2( countedSubclass2 );
421 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
422 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
426 /** Equality of two different types where left hand side is a raw pointer */
427 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
429 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
431 IntrusivePtr<Counted> counted1( new Counted );
432 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
433 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
434 IntrusivePtr<Counted> counted2( countedSubclass2 );
436 DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
437 DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
441 /** Inequality of two different types where the left hand side is a raw pointer */
442 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
444 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
446 IntrusivePtr<Counted> counted1( new Counted );
447 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
448 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
449 IntrusivePtr<Counted> counted2( countedSubclass2 );
451 DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
452 DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );