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>
23 #include <test-native-image.h>
30 const int REPEAT = 1000;
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;
39 class Counted : public RefObject
52 class CountedSubclass : public Counted
57 ++g_creationCountSubclass;
61 ++g_destructionCountSubclass;
65 class UnrelatedCounted : public RefObject
70 ++g_creationCountUnrelated;
74 ++g_destructionCountUnrelated;
80 * Test that a default constructed pointer is null and harmless.
82 int UtcDaliIntrusivePtrIntrusivePtr(void)
84 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" );
86 g_creationCount = g_destructionCount = 0;
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 );
102 int UtcDaliIntrusivePtrIntrusivePtrTP(void)
104 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" );
106 g_creationCount = g_destructionCount = 0;
108 IntrusivePtr<Counted> counted( new Counted );
109 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
110 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
112 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
113 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
118 // Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void)
120 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUP(void)
122 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr<U> const &)" );
124 g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0;
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 );
132 IntrusivePtr<Counted> counted( countedSubclass );
133 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
135 // Make loads more references:
136 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
137 for( int i = 0; i < REPEAT; ++i )
139 intrusivePtrs.push_back( IntrusivePtr<Counted>( countedSubclass ) );
141 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
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 );
150 // The negative version of this test would fail at compile time:
151 // int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void)
153 int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrP(void)
155 tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" );
157 // Pass a pointer to a constructed second object:
158 // Pass a pointer to null:
160 g_creationCount = g_destructionCount = 0;
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 );
167 IntrusivePtr<Counted> counted2( counted );
168 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
169 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
171 // Make loads more references:
172 std::vector< IntrusivePtr<Counted> > intrusivePtrs;
173 for( int i = 0; i < REPEAT; ++i )
175 intrusivePtrs.push_back( IntrusivePtr<Counted>( counted ) );
177 DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
179 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
180 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
182 intrusivePtrs.clear();
184 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
186 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
187 DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION );
190 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
193 DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION );
194 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
199 int UtcDaliIntrusivePtrGetP(void)
201 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
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 );
211 int UtcDaliIntrusivePtrGetN(void)
213 tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
217 IntrusivePtr<Counted> counted( 0 );
218 DALI_TEST_CHECK( counted.Get() == 0 );
219 DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION );
224 int UtcDaliIntrusivePtrArrowOperatorP(void)
226 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" );
228 IntrusivePtr<Counted> counted( new Counted );
229 DALI_TEST_CHECK( (counted.operator->()) != 0 );
230 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
235 int UtcDaliIntrusivePtrArrowOperatorN(void)
237 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" );
239 IntrusivePtr<Counted> counted;
240 DALI_TEST_CHECK( (counted.operator->()) == 0 );
245 int UtcDaliIntrusivePtrIndirectionOperatorP(void)
247 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
249 IntrusivePtr<Counted> counted( new Counted );
250 DALI_TEST_CHECK( &(counted.operator*()) != 0 );
251 DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION );
256 int UtcDaliIntrusivePtrIndirectionOperatorN(void)
258 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" );
260 IntrusivePtr<Counted> counted;
261 DALI_TEST_CHECK( &(counted.operator*()) == 0 );
266 int UtcDaliIntrusivePtrResetP(void)
268 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
270 IntrusivePtr<Counted> counted( new Counted );
271 DALI_TEST_CHECK( counted.Get() != 0 );
273 DALI_TEST_CHECK( counted.Get() == 0 );
278 int UtcDaliIntrusivePtrResetN(void)
280 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" );
282 IntrusivePtr<Counted> counted;
283 Counted* firstGet = counted.Get();
285 DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION );
290 int UtcDaliIntrusivePtrResetTP(void)
292 tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" );
294 g_creationCount = g_destructionCount = 0;
296 IntrusivePtr<Counted> counted( new Counted );
298 IntrusivePtr<Counted> counted2( new Counted );
300 DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
301 DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
303 counted.Reset( counted2.Get() );
305 DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
306 DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION );
308 DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
310 DALI_TEST_EQUALS( g_creationCount, 2u, TEST_LOCATION );
311 DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION );
313 counted2.Reset( (Counted*) 0 );
314 counted.Reset( counted2.Get() );
315 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
317 // Check that reseting nulls is harmless:
318 counted2.Reset( counted.Get() );
319 counted.Reset( counted2.Get() );
321 DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION );
327 int UtcDaliIntrusivePtrResetTN(void)
329 tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" );
331 g_creationCount = g_destructionCount = 0;
333 IntrusivePtr<Counted> counted( new Counted );
335 counted.Reset( (Counted*) 0 );
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 );
346 int UtcDaliIntrusivePtrOperatorBooleanTypeP(void)
348 tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
350 IntrusivePtr<Counted> counted( new Counted );
351 DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
352 DALI_TEST_CHECK( counted );
354 typedef void (IntrusivePtr<Counted>::*BoolIdiomFunc)() const;
355 BoolIdiomFunc func = static_cast<BoolIdiomFunc>( counted.operator BooleanType() );
356 ((counted).*func)(); // purely for test coverage
359 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
364 int UtcDaliIntrusivePtrOperatorBooleanTypeN(void)
366 tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
368 IntrusivePtr<Counted> counted;
369 DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
370 DALI_TEST_CHECK( !counted );
374 /** Equality of two different types*/
375 int UtcDaliIntrusivePtrOperatorEqualTU(void)
377 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
379 IntrusivePtr<Counted> counted1( new Counted );
380 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
381 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
382 IntrusivePtr<Counted> counted2( countedSubclass2 );
384 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
385 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
389 /** Inequality of two different types*/
390 int UtcDaliIntrusivePtrOperatorNotEqualTU(void)
392 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
394 IntrusivePtr<Counted> counted1( new Counted );
395 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
396 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
397 IntrusivePtr<Counted> counted2( countedSubclass2 );
399 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
400 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
404 /** Equality of two different types where right hand side is a raw pointer */
405 int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void)
407 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
409 IntrusivePtr<Counted> counted1( new Counted );
410 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
411 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
412 IntrusivePtr<Counted> counted2( countedSubclass2 );
414 DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
415 DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
419 /** Inequality of two different types where the right hand side is a raw pointer */
420 int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void)
422 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
424 IntrusivePtr<Counted> counted1( new Counted );
425 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
426 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
427 IntrusivePtr<Counted> counted2( countedSubclass2 );
429 DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
430 DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
434 /** Equality of two different types where left hand side is a raw pointer */
435 int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void)
437 tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
439 IntrusivePtr<Counted> counted1( new Counted );
440 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
441 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
442 IntrusivePtr<Counted> counted2( countedSubclass2 );
444 DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
445 DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
449 /** Inequality of two different types where the left hand side is a raw pointer */
450 int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void)
452 tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
454 IntrusivePtr<Counted> counted1( new Counted );
455 IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
456 IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
457 IntrusivePtr<Counted> counted2( countedSubclass2 );
459 DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
460 DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );