Dali 3D User Interface Engine
utc-Dali-IntrusivePtr.cpp
Go to the documentation of this file.
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>
22 #include <dali-test-suite-utils.h>
23 
24 using namespace Dali;
25 
26 namespace
27 {
28 
29 const int REPEAT = 1000;
30 
31 size_t g_creationCount = 0;
32 size_t g_destructionCount = 0;
37 
38 class Counted : public RefObject
39 {
40 public:
42  {
44  }
46  {
48  }
49 };
50 
51 class CountedSubclass : public Counted
52 {
53 public:
55  {
57  }
59  {
61  }
62 };
63 
65 {
66 public:
68  {
70  }
72  {
74  }
75 };
76 }
77 
82 {
83  tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" );
84 
86 
87  IntrusivePtr<Counted> counted;
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>();
97 
98  END_TEST;
99 }
100 
102 {
103  tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" );
104 
106 
107  IntrusivePtr<Counted> counted( new Counted );
110  counted = 0;
113 
114  END_TEST;
115 }
116 
117 // Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void)
118 
120 {
121  tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr<U> const &)" );
122 
124 
125  IntrusivePtr<CountedSubclass> countedSubclass( new CountedSubclass );
130 
131  IntrusivePtr<Counted> counted( countedSubclass );
132  DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
133 
134  // Make loads more references:
135  std::vector< IntrusivePtr<Counted> > intrusivePtrs;
136  for( int i = 0; i < REPEAT; ++i )
137  {
138  intrusivePtrs.push_back( IntrusivePtr<Counted>( countedSubclass ) );
139  }
140  DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
141 
145 
146  END_TEST;
147 }
148 
149 // The negative version of this test would fail at compile time:
150 // int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void)
151 
153 {
154  tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" );
155 
156  // Pass a pointer to a constructed second object:
157  // Pass a pointer to null:
158 
160 
161  IntrusivePtr<Counted> counted( new Counted );
164  DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
165 
166  IntrusivePtr<Counted> counted2( counted );
167  DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
168  DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
169 
170  // Make loads more references:
171  std::vector< IntrusivePtr<Counted> > intrusivePtrs;
172  for( int i = 0; i < REPEAT; ++i )
173  {
174  intrusivePtrs.push_back( IntrusivePtr<Counted>( counted ) );
175  }
176  DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION );
177 
180 
181  intrusivePtrs.clear();
182 
183  DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
184 
187 
188  counted.Reset();
189  DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
190  counted2.Reset();
191 
194 
195  END_TEST;
196 }
197 
199 {
200  tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
201 
202  IntrusivePtr<Counted> counted( new Counted );
203  DALI_TEST_CHECK( counted.Get() != 0 );
205  DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
206 
207  END_TEST;
208 }
209 
211 {
212  tet_infoline( "Testing Dali::IntrusivePtr::Get()" );
213 
214  g_creationCount = 0;
215 
216  IntrusivePtr<Counted> counted( 0 );
217  DALI_TEST_CHECK( counted.Get() == 0 );
219 
220  END_TEST;
221 }
222 
224 {
225  tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" );
226 
227  IntrusivePtr<Counted> counted( new Counted );
228  DALI_TEST_CHECK( (counted.operator->()) != 0 );
229  DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
230 
231  END_TEST;
232 }
233 
235 {
236  tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" );
237 
238  IntrusivePtr<Counted> counted;
239  DALI_TEST_CHECK( (counted.operator->()) == 0 );
240 
241  END_TEST;
242 }
243 
245 {
246  tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
247 
248  IntrusivePtr<Counted> counted( new Counted );
249  DALI_TEST_CHECK( &(counted.operator*()) != 0 );
250  DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION );
251 
252  END_TEST;
253 }
254 
256 {
257  tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" );
258 
259  IntrusivePtr<Counted> counted;
260  DALI_TEST_CHECK( &(counted.operator*()) == 0 );
261 
262  END_TEST;
263 }
264 
266 {
267  tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
268 
269  IntrusivePtr<Counted> counted( new Counted );
270  DALI_TEST_CHECK( counted.Get() != 0 );
271  counted.Reset();
272  DALI_TEST_CHECK( counted.Get() == 0 );
273 
274  END_TEST;
275 }
276 
278 {
279  tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" );
280 
281  IntrusivePtr<Counted> counted;
282  Counted* firstGet = counted.Get();
283  counted.Reset();
284  DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION );
285 
286  END_TEST;
287 }
288 
290 {
291  tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" );
292 
294 
295  IntrusivePtr<Counted> counted( new Counted );
296 
297  IntrusivePtr<Counted> counted2( new Counted );
298 
299  DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION );
300  DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION );
301 
302  counted.Reset( counted2.Get() );
303 
304  DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
305  DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION );
306 
307  DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION );
308 
311 
312  counted2.Reset( (Counted*) 0 );
313  counted.Reset( counted2.Get() );
315 
316  // Check that reseting nulls is harmless:
317  counted2.Reset( counted.Get() );
318  counted.Reset( counted2.Get() );
319 
321 
322  END_TEST;
323 }
324 
325 
327 {
328  tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" );
329 
331 
332  IntrusivePtr<Counted> counted( new Counted );
333 
334  counted.Reset( (Counted*) 0 );
335 
336  DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION );
339 
340  END_TEST;
341 }
342 
343 
344 
346 {
347  tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" );
348 
349  IntrusivePtr<Counted> counted( new Counted );
350  DALI_TEST_CHECK( counted.operator BooleanType() != 0 );
351  DALI_TEST_CHECK( counted );
352 
353  typedef void (IntrusivePtr<Counted>::*BoolIdiomFunc)() const;
354  BoolIdiomFunc func = static_cast<BoolIdiomFunc>( counted.operator BooleanType() );
355  ((counted).*func)(); // purely for test coverage
356 
357  counted.Reset();
358  DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
359 
360  END_TEST;
361 }
362 
364 {
365  tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" );
366 
367  IntrusivePtr<Counted> counted;
368  DALI_TEST_CHECK( counted.operator BooleanType() == 0 );
369  DALI_TEST_CHECK( !counted );
370  END_TEST;
371 }
372 
375 {
376  tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" );
377 
378  IntrusivePtr<Counted> counted1( new Counted );
379  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
380  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
381  IntrusivePtr<Counted> counted2( countedSubclass2 );
382 
383  DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION );
384  DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION );
385  END_TEST;
386 }
387 
390 {
391  tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" );
392 
393  IntrusivePtr<Counted> counted1( new Counted );
394  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
395  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
396  IntrusivePtr<Counted> counted2( countedSubclass2 );
397 
398  DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION );
399  DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION );
400  END_TEST;
401 }
402 
405 {
406  tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" );
407 
408  IntrusivePtr<Counted> counted1( new Counted );
409  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
410  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
411  IntrusivePtr<Counted> counted2( countedSubclass2 );
412 
413  DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION );
414  DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION );
415  END_TEST;
416 }
417 
420 {
421  tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" );
422 
423  IntrusivePtr<Counted> counted1( new Counted );
424  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
425  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
426  IntrusivePtr<Counted> counted2( countedSubclass2 );
427 
428  DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION );
429  DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION );
430  END_TEST;
431 }
432 
435 {
436  tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" );
437 
438  IntrusivePtr<Counted> counted1( new Counted );
439  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
440  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
441  IntrusivePtr<Counted> counted2( countedSubclass2 );
442 
443  DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION );
444  DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION );
445  END_TEST;
446 }
447 
450 {
451  tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" );
452 
453  IntrusivePtr<Counted> counted1( new Counted );
454  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
455  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
456  IntrusivePtr<Counted> counted2( countedSubclass2 );
457 
458  DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION );
459  DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION );
460  END_TEST;
461 }
Dali Docs Home
Read more about Dali