Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
RCP_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "Teuchos_RCP.hpp"
45 #include "Teuchos_getConst.hpp"
47 #ifdef HAVE_TEUCHOS_BOOST
49 #endif
50 
51 #include "TestClasses.hpp"
52 
54 
55 
56 namespace {
57 
58 
59 using Teuchos::as;
60 using Teuchos::null;
61 using Teuchos::Ptr;
62 using Teuchos::RCP;
63 using Teuchos::rcp;
64 using Teuchos::rcpFromRef;
65 using Teuchos::rcpFromUndefRef;
66 using Teuchos::outArg;
67 using Teuchos::rcpWithEmbeddedObj;
68 using Teuchos::getEmbeddedObj;
69 using Teuchos::getOptionalEmbeddedObj;
70 using Teuchos::getOptionalNonconstEmbeddedObj;
71 using Teuchos::set_extra_data;
72 using Teuchos::get_optional_nonconst_extra_data;
73 using Teuchos::getConst;
78 using Teuchos::RCP_WEAK;
80 
81 
82 TEUCHOS_UNIT_TEST( DeallocNull, free )
83 {
85  d.free(0);
86 }
87 
88 
89 TEUCHOS_UNIT_TEST( RCP, assignSelf_null )
90 {
91  RCP<A> a_rcp;
92  a_rcp = a_rcp;
93  TEST_ASSERT(is_null(a_rcp));
94 }
95 
96 
97 TEUCHOS_UNIT_TEST( RCP, assignSelf_nonnull )
98 {
99  RCP<A> a_rcp(new A);
100  A *a_raw_ptr = a_rcp.getRawPtr();
101  a_rcp = a_rcp;
102  TEST_ASSERT(nonnull(a_rcp));
103  TEST_EQUALITY(a_rcp.getRawPtr(), a_raw_ptr);
104 }
105 
106 
108 {
109  RCP<A> a_rcp(new A);
110  RCP<const A> ca_rcp = a_rcp.getConst();
111  TEST_EQUALITY(a_rcp.getRawPtr(), ca_rcp.getRawPtr());
112 }
113 
114 
115 TEUCHOS_UNIT_TEST( RCP, explicit_null )
116 {
117  RCP<A> a_rcp(0);
118  TEST_ASSERT(is_null(a_rcp));
119 }
120 
121 
122 TEUCHOS_UNIT_TEST( RCP, explicit_dealloc_null )
123 {
124  RCP<A> a_rcp = rcpWithDealloc(static_cast<A*>(0), Teuchos::DeallocNull<A>(), false);
125  TEST_ASSERT(is_null(a_rcp));
126 }
127 
128 
129 TEUCHOS_UNIT_TEST( RCP, explicit_null_null )
130 {
131  RCP<A> a_rcp(0, null);
132  TEST_ASSERT(is_null(a_rcp));
133 }
134 
135 
136 TEUCHOS_UNIT_TEST( RCP, explicit_null_nonnull )
137 {
138  A *a = new A;
139  RCP<A> a_rcp(a, null);
140  TEST_ASSERT(nonnull(a_rcp));
141  delete a;
142 }
143 
144 
145 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_raw_ref )
146 {
147  A a;
148  RCP<A> a_rcp = rcpFromRef(a);
149  TEST_EQUALITY(a_rcp.getRawPtr(), &a);
150  TEST_ASSERT(nonnull(a_rcp));
151 }
152 
153 
154 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_from_rcp )
155 {
156  RCP<A> a_rcp1 = rcp<A>(new A);
157  RCP<A> a_rcp2 = rcpFromRef(*a_rcp1);
158  TEST_EQUALITY(a_rcp2.getRawPtr(), a_rcp1.getRawPtr());
159  if (RCPNodeTracer::isTracingActiveRCPNodes())
160  {
161  TEST_EQUALITY_CONST(a_rcp2.strong_count(), 1);
162  TEST_EQUALITY_CONST(a_rcp2.weak_count(), 1);
163  TEST_EQUALITY_CONST(a_rcp2.has_ownership(), true);
164  }
165  else {
166  TEST_EQUALITY_CONST(a_rcp2.strong_count(), 1);
167  TEST_EQUALITY_CONST(a_rcp2.weak_count(), 0);
168  TEST_EQUALITY_CONST(a_rcp2.has_ownership(), false);
169  }
170 }
171 
172 
174 {
175  A a;
176  RCP<A> a_rcp = rcpFromUndefRef(a);
177  TEST_ASSERT(nonnull(a_rcp));
178 }
179 
180 
181 //
182 // Test rcpCloneNode(...)
183 //
184 
185 
186 TEUCHOS_UNIT_TEST( RCP, rcpCloneNode_null )
187 {
188  ECHO(RCP<RCP<int> > rcp1 = null);
189  ECHO(RCP<RCP<int> > rcp2 = rcpCloneNode(rcp1));
190  TEST_EQUALITY(rcp2, null);
191 }
192 
193 
194 TEUCHOS_UNIT_TEST( RCP, rcpCloneNode_basic )
195 {
196 
197  ECHO(RCP<int> rcp1 = rcp(new int(0)));
198 
199  ECHO(RCP<int> rcp2 = rcpCloneNode(rcp1));
200  TEST_ASSERT(nonnull(rcp2));
201  TEST_EQUALITY(rcp1.strong_count(), 2);
202  TEST_EQUALITY(rcp2.strong_count(), 1);
203 
204  ECHO(RCP<int> rcp3 = rcp2);
205  TEST_EQUALITY(rcp1.strong_count(), 2);
206  TEST_EQUALITY(rcp2.strong_count(), 2);
207  TEST_EQUALITY(rcp3.strong_count(), 2);
208 
209  ECHO(RCP <int> rcp4 = rcp1);
210  TEST_EQUALITY(rcp1.strong_count(), 3);
211  TEST_EQUALITY(rcp2.strong_count(), 2);
212  TEST_EQUALITY(rcp3.strong_count(), 2);
213 
214  ECHO(rcp4 = null);
215  TEST_EQUALITY(rcp1.strong_count(), 2);
216  TEST_EQUALITY(rcp2.strong_count(), 2);
217  TEST_EQUALITY(rcp3.strong_count(), 2);
218  TEST_EQUALITY(rcp4.strong_count(), 0);
219 
220  ECHO(rcp1 = null);
221  TEST_EQUALITY(rcp1.strong_count(), 0);
222  TEST_EQUALITY(rcp2.strong_count(), 2);
223  TEST_EQUALITY(rcp3.strong_count(), 2);
224  TEST_EQUALITY(rcp4.strong_count(), 0);
225 
226  ECHO(rcp2 = null);
227  TEST_EQUALITY(rcp2.strong_count(), 0);
228  TEST_EQUALITY(rcp3.strong_count(), 1);
229 
230  ECHO(rcp3 = null);
231  TEST_EQUALITY(rcp3.strong_count(), 0);
232 
233 }
234 
235 
236 //
237 // Test duplicate owning RCP objects
238 //
239 
240 
241 // Test that shows that we can detect trying to create two owning RCPs
242 // pointing to the same polymorphic object but having different interfaces
243 // with different addresses. This happens due to virtual base classes. Only
244 // works when we have a working getBaseObjVoidPtr(...) function.
245 TEUCHOS_UNIT_TEST( RCP, duplicate_rcp_owning_polymorphic )
246 {
248  ECHO(C *c_ptr = new C);
249  ECHO(A *a_ptr = c_ptr);
250  ECHO(RCP<C> c_rcp = rcp(c_ptr)); // Okay
251 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
252  // With determine they are pointed to the same object!
253  TEST_THROW(RCP<A> a_rcp = rcp(a_ptr), DuplicateOwningRCPError);
254 #else
255  // Will not determine they are point to the same object!
256  ECHO(RCP<A> a_rcp = rcp(a_ptr));
257  TEST_EQUALITY(a_rcp.getRawPtr(), a_ptr);
258  ECHO(a_rcp.release()); // Better or we will get a segfault!
259 #endif
260 }
261 
262 
263 // Test that shows that we can detect trying to create two owning RCPs
264 // pointing to the same polymorphic object with the same type and therefore
265 // the same address. This works even if these use virtual base classes. This
266 // works even without a working getBaseObjVoidPtr(...) function.
267 TEUCHOS_UNIT_TEST( RCP, duplicate_rcp_owning_polymorphic_different_addr )
268 {
270  ECHO(A *a_ptr1 = new C);
271  ECHO(A *a_ptr2 = a_ptr1);
272  ECHO(RCP<A> a_rcp1 = rcp(a_ptr1)); // Okay
273 #if defined(TEUCHOS_DEBUG)
274  // With determine they are pointed to the same object!
275  TEST_THROW(RCP<A> a_rcp2 = rcp(a_ptr2), DuplicateOwningRCPError);
276 #else
277  // Will not determine they are point to the same object!
278  ECHO(RCP<A> a_rcp2 = rcp(a_ptr2));
279  TEST_EQUALITY(a_rcp2.getRawPtr(), a_ptr2);
280  ECHO(a_rcp2.release()); // Better or we will get a segfault!
281 #endif
282 }
283 
284 
285 // Test that shows that we can always detect trying to create two owning RCPs
286 // pointing to the same nonpolymorphic object having different interfaces but
287 // the same address (single non-virtual inheritance). Works just fine without
288 // a working getBaseObjVoidPtr(...) function.
289 TEUCHOS_UNIT_TEST( RCP, duplicate_rcp_owning_nonpolymorphic_same_addr )
290 {
292  ECHO(E *e_ptr = new E);
293  ECHO(E *d_ptr = e_ptr);
294  ECHO(RCP<E> e_rcp = rcp(e_ptr)); // Okay
295 #if defined(TEUCHOS_DEBUG)
296  // With determine they are pointed to the same object even without support
297  // for getBaseObjVoidPtr(...) because no dynamic_cast is needed.
298  TEST_THROW(RCP<D> d_rcp = rcp(d_ptr), DuplicateOwningRCPError);
299 #else
300  // Will not determine they are point to the same object!
301  ECHO(RCP<D> d_rcp = rcp(d_ptr));
302  TEST_EQUALITY(d_rcp.getRawPtr(), d_ptr);
303  ECHO(d_rcp.release()); // Better or we will get a segfault!
304 #endif
305 }
306 
307 
308 //
309 // These next tests shows that we can detect when two RCPs are create to the same
310 // object, one owning and the other non-owning. When we have a working
311 // getBaseObjVoidPtr(...) function, the new non-owning RCP will actually be a
312 // weak RCP that can be used to detect circular dependencies.
313 //
314 
315 
316 // rcp
317 
318 
319 TEUCHOS_UNIT_TEST( RCP, rcp_duplicate_rcp_nonowning_polymorphic_different_addr )
320 {
322  ECHO(RCP<C> c_rcp(new C));
323  ECHO(A &a_ref = *c_rcp);
324  ECHO(RCP<A> a_rcp = rcp(&a_ref, false));
325  ECHO(c_rcp = null);
326 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
327  TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
328 #else
329  TEST_NOTHROW(a_rcp.getRawPtr());
330 #endif
331 }
332 
333 
334 TEUCHOS_UNIT_TEST( RCP, rcp_duplicate_rcp_nonowning_polymorphic_same_addr )
335 {
337  ECHO(RCP<A> a_rcp1(new C));
338  ECHO(A &a_ref = *a_rcp1);
339  ECHO(RCP<A> a_rcp2 = rcp(&a_ref, false));
340  ECHO(a_rcp1 = null);
341 #if defined(TEUCHOS_DEBUG)
342  TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
343 #else
344  TEST_NOTHROW(a_rcp2.getRawPtr());
345 #endif
346 }
347 
348 
349 TEUCHOS_UNIT_TEST( RCP, rcp_duplicate_rcp_nonowning_nonpolymorphic )
350 {
352  ECHO(RCP<E> e_rcp(new E));
353  ECHO(D &d_ref = *e_rcp);
354  ECHO(RCP<D> d_rcp = rcp(&d_ref, false));
355  ECHO(e_rcp = null);
356 #if defined(TEUCHOS_DEBUG)
357  TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
358 #else
359  TEST_NOTHROW(d_rcp.getRawPtr());
360 #endif
361 }
362 
363 
364 // rcpFromRef
365 
366 
367 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_different_addr )
368 {
370  ECHO(RCP<C> c_rcp(new C));
371  ECHO(A &a_ref = *c_rcp);
372  ECHO(RCP<A> a_rcp = rcpFromRef(a_ref));
373  ECHO(c_rcp = null);
374 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
375  TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
376 #else
377  TEST_NOTHROW(a_rcp.getRawPtr());
378 #endif
379 }
380 
381 
382 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_same_addr )
383 {
385  ECHO(RCP<A> a_rcp1(new C));
386  ECHO(A &a_ref = *a_rcp1);
387  ECHO(RCP<A> a_rcp2 = rcpFromRef(a_ref));
388  ECHO(a_rcp1 = null);
389 #if defined(TEUCHOS_DEBUG)
390  TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
391 #else
392  TEST_NOTHROW(a_rcp2.getRawPtr());
393 #endif
394 }
395 
396 
397 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_nonpolymorphic )
398 {
400  ECHO(RCP<E> e_rcp(new E));
401  ECHO(D &d_ref = *e_rcp);
402  ECHO(RCP<D> d_rcp = rcpFromRef(d_ref));
403  ECHO(e_rcp = null);
404 #if defined(TEUCHOS_DEBUG)
405  TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
406 #else
407  TEST_NOTHROW(d_rcp.getRawPtr());
408 #endif
409 }
410 
411 
412 // rcpFromUndefRef (Can never detect dangling references)
413 
414 
415 TEUCHOS_UNIT_TEST( RCP, rcpFromUndefRef_duplicate_rcp_nonowning_polymorphic_same_addr )
416 {
418  ECHO(RCP<A> a_rcp1(new C));
419  ECHO(A &a_ref = *a_rcp1);
420  ECHO(RCP<A> a_rcp2 = rcpFromUndefRef(a_ref));
421  ECHO(a_rcp1 = null);
422  TEST_NOTHROW(a_rcp2.getRawPtr());
423 }
424 
425 
426 //
427 // extra data and embedded objects tests
428 //
429 
430 
432 {
433  RCP<A> a_rcp = rcp(new A);
434  set_extra_data( as<int>(1), "blob", outArg(a_rcp) );
435  TEST_EQUALITY_CONST(*get_optional_nonconst_extra_data<int>(a_rcp, "blob"), as<int>(1));
436 }
437 
438 
439 TEUCHOS_UNIT_TEST( RCP, getOptionalEmbeddedObj_null )
440 {
441  ECHO(RCP<A> a_rcp = rcp(new A));
442  const Ptr<const RCP<C> > c_ptr_rcp_1 =
443  getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
444  TEST_EQUALITY_CONST( c_ptr_rcp_1, null );
445  const Ptr<RCP<C> > c_ptr_rcp_2 =
446  getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
447  TEST_EQUALITY_CONST( c_ptr_rcp_2, null );
448 }
449 
450 
451 TEUCHOS_UNIT_TEST( RCP, getOptionalEmbeddedObj_default )
452 {
453 
454  ECHO(RCP<C> c_rcp = rcp(new C));
455  ECHO(RCP<A> a_rcp = rcpWithEmbeddedObj(new A, c_rcp));
456 
457  Ptr<const RCP<C> > c_ptr_rcp_1 =
458  getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
459  TEST_EQUALITY_CONST( is_null(c_ptr_rcp_1), false );
460  TEST_EQUALITY( (*c_ptr_rcp_1).getRawPtr(), c_rcp.getRawPtr() );
461  TEST_EQUALITY( (*c_ptr_rcp_1)->C_g(), C_g_return );
462 
463  Ptr<RCP<C> > c_ptr_rcp_2 =
464  getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
465  TEST_EQUALITY_CONST( is_null(c_ptr_rcp_2), false );
466  TEST_EQUALITY( (*c_ptr_rcp_2).getRawPtr(), c_rcp.getRawPtr() );
467  TEST_EQUALITY( (*c_ptr_rcp_2)->C_f(), C_f_return );
468 
469 }
470 
471 
472 TEUCHOS_UNIT_TEST( RCP, reset_null )
473 {
474  RCP<A> a_rcp = rcp(new A);
475  a_rcp.reset();
476  TEST_ASSERT(is_null(a_rcp));
477 }
478 
479 
480 TEUCHOS_UNIT_TEST( RCP, reset_nonnull )
481 {
482  RCP<A> a_rcp = rcp(new A);
483  C* c_rawp = new C;
484  a_rcp.reset(c_rawp);
485  A* a_rawp = c_rawp;
486  TEST_EQUALITY( a_rcp.getRawPtr(), a_rawp );
487 }
488 
489 
491 {
492  ECHO(RCP<A> a_rcp = rcp(new A));
493  TEST_EQUALITY_CONST(is_null(a_rcp), false);
494  TEST_EQUALITY_CONST(nonnull(a_rcp), true);
495  ECHO(a_rcp = null);
496  TEST_EQUALITY_CONST(is_null(a_rcp), true);
497  TEST_EQUALITY_CONST(nonnull(a_rcp), false);
498 }
499 
500 
501 #ifdef HAVE_TEUCHOS_BOOST
502 
503 
504 TEUCHOS_UNIT_TEST( shared_ptr, nonnull )
505 {
506  using boost::shared_ptr;
507  ECHO(shared_ptr<A> a_sptr(new A));
508  TEST_EQUALITY_CONST(is_null(a_sptr), false);
509  TEST_EQUALITY_CONST(nonnull(a_sptr), true);
510  ECHO(a_sptr = shared_ptr<A>());
511  TEST_EQUALITY_CONST(is_null(a_sptr), true);
512  TEST_EQUALITY_CONST(nonnull(a_sptr), false);
513 }
514 
515 
516 #endif // HAVE_TEUCHOS_BOOST
517 
518 
519 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( RCP, weakDelete, T )
520 {
521 
522  ECHO(RCP<T> rcp_strong = rcp(new T));
523 
524  TEST_EQUALITY_CONST( rcp_strong.strength(), RCP_STRONG );
525  TEST_EQUALITY_CONST( rcp_strong.is_null(), false );
526  TEST_EQUALITY_CONST( rcp_strong.strong_count(), 1 );
527  TEST_EQUALITY_CONST( rcp_strong.weak_count(), 0 );
528  TEST_EQUALITY_CONST( rcp_strong.total_count(), 1 );
529 
530  ECHO(RCP<T> rcp_weak1 = rcp_strong.create_weak());
531 
532  TEST_EQUALITY_CONST( rcp_weak1.strength(), RCP_WEAK );
533  TEST_EQUALITY_CONST( rcp_weak1.is_null(), false );
534  TEST_EQUALITY_CONST( rcp_weak1.strong_count(), 1 );
535  TEST_EQUALITY_CONST( rcp_weak1.weak_count(), 1 );
536  TEST_EQUALITY_CONST( rcp_weak1.total_count(), 2 );
537 
538  TEST_EQUALITY_CONST( rcp_strong.strong_count(), 1 );
539  TEST_EQUALITY_CONST( rcp_strong.is_null(), false );
540  TEST_EQUALITY_CONST( rcp_strong.weak_count(), 1 );
541  TEST_EQUALITY_CONST( rcp_strong.total_count(), 2 );
542 
543  TEST_EQUALITY_CONST( rcp_weak1.shares_resource(rcp_strong), true );
544 
545  TEST_EQUALITY( rcp_weak1.get(), rcp_weak1.getRawPtr() );
546  TEST_EQUALITY( rcp_weak1.get(), rcp_strong.get() );
547  TEST_EQUALITY( rcp_weak1.getRawPtr(), rcp_strong.getRawPtr() );
548 
549  ECHO(RCP<T> rcp_weak2 = rcp_weak1);
550 
551  TEST_EQUALITY_CONST( rcp_weak2.strength(), RCP_WEAK );
552  TEST_EQUALITY_CONST( rcp_weak2.is_null(), false );
553  TEST_EQUALITY_CONST( rcp_weak2.strong_count(), 1 );
554  TEST_EQUALITY_CONST( rcp_weak2.weak_count(), 2 );
555  TEST_EQUALITY_CONST( rcp_weak2.total_count(), 3 );
556 
557  TEST_EQUALITY_CONST( rcp_strong.strong_count(), 1 );
558  TEST_EQUALITY_CONST( rcp_strong.is_null(), false );
559  TEST_EQUALITY_CONST( rcp_strong.weak_count(), 2 );
560  TEST_EQUALITY_CONST( rcp_strong.total_count(), 3 );
561 
562  TEST_EQUALITY_CONST( rcp_weak1.shares_resource(rcp_strong), true );
563  TEST_EQUALITY_CONST( rcp_weak1.shares_resource(rcp_weak2), true );
564  TEST_EQUALITY_CONST( rcp_weak2.shares_resource(rcp_strong), true );
565 
566  TEST_EQUALITY( rcp_weak2.get(), rcp_strong.get() );
567  TEST_EQUALITY( rcp_weak2.getRawPtr(), rcp_strong.getRawPtr() );
568 
569  ECHO(rcp_strong = null); // This deletes the underlying object of type T!
570 
571  TEST_EQUALITY_CONST( rcp_strong.strength(), RCP_STRONG );
572  TEST_EQUALITY_CONST( rcp_strong.is_null(), true );
573  TEST_EQUALITY_CONST( rcp_strong.strong_count(), 0 );
574  TEST_EQUALITY_CONST( rcp_strong.strong_count(), 0 );
575  TEST_EQUALITY_CONST( rcp_strong.weak_count(), 0 );
576  TEST_EQUALITY_CONST( rcp_strong.total_count(), 0 );
577  TEST_EQUALITY_CONST( rcp_strong.is_valid_ptr(), true );
578 
579  TEST_EQUALITY_CONST( rcp_strong.shares_resource(rcp_weak1), false );
580  TEST_EQUALITY_CONST( rcp_strong.shares_resource(rcp_weak2), false );
581 
582  TEST_EQUALITY_CONST( rcp_weak1.has_ownership(), true );
583  TEST_EQUALITY_CONST( rcp_weak1.strong_count(), 0 );
584  TEST_EQUALITY_CONST( rcp_weak1.strong_count(), 0 );
585  TEST_EQUALITY_CONST( rcp_weak1.weak_count(), 2 );
586  TEST_EQUALITY_CONST( rcp_weak1.total_count(), 2 );
587  TEST_EQUALITY_CONST( rcp_weak1.is_valid_ptr(), false );
588 
589  TEST_EQUALITY_CONST( rcp_weak2.has_ownership(), true );
590  TEST_EQUALITY_CONST( rcp_weak2.strong_count(), 0 );
591  TEST_EQUALITY_CONST( rcp_weak2.strong_count(), 0 );
592  TEST_EQUALITY_CONST( rcp_weak2.weak_count(), 2 );
593  TEST_EQUALITY_CONST( rcp_weak2.total_count(), 2 );
594  TEST_EQUALITY_CONST( rcp_weak2.is_valid_ptr(), false );
595 
596  TEST_EQUALITY_CONST( rcp_weak1.shares_resource(rcp_weak2), true );
597 
598  ECHO(rcp_weak1.assert_not_null()); // Does not throw!
599  ECHO(rcp_weak2.assert_not_null()); // Does not throw!
600 
601  TEST_THROW( rcp_weak1.assert_valid_ptr(), DanglingReferenceError );
602 #ifdef TEUCHOS_DEBUG
603  TEST_THROW( rcp_weak1.operator->(), DanglingReferenceError );
604  TEST_THROW( *rcp_weak1, DanglingReferenceError );
605  TEST_THROW( rcp_weak1.create_weak(), DanglingReferenceError );
606  TEST_THROW( rcp_weak1.get(), DanglingReferenceError );
607  TEST_THROW( rcp_weak1.getRawPtr(), DanglingReferenceError );
608  TEST_THROW( rcp_weak1(), DanglingReferenceError );
609  TEST_THROW( rcp_weak1.release(), DanglingReferenceError );
610 #endif // TEUCHOS_DEBUG
611 
612  ECHO(rcp_weak1 = null); // Just deicrements weak count!
613 
614  TEST_EQUALITY_CONST( rcp_weak1.strength(), RCP_STRONG );
615  TEST_EQUALITY_CONST( rcp_weak1.is_null(), true );
616  TEST_EQUALITY_CONST( rcp_weak1.strong_count(), 0 );
617  TEST_EQUALITY_CONST( rcp_weak1.strong_count(), 0 );
618  TEST_EQUALITY_CONST( rcp_weak1.weak_count(), 0 );
619  TEST_EQUALITY_CONST( rcp_weak1.total_count(), 0 );
620  TEST_EQUALITY_CONST( rcp_weak1.is_valid_ptr(), true );
621 
622  TEST_EQUALITY_CONST( rcp_weak2.has_ownership(), true );
623  TEST_EQUALITY_CONST( rcp_weak2.strong_count(), 0 );
624  TEST_EQUALITY_CONST( rcp_weak2.strong_count(), 0 );
625  TEST_EQUALITY_CONST( rcp_weak2.weak_count(), 1 );
626  TEST_EQUALITY_CONST( rcp_weak2.total_count(), 1 );
627  TEST_EQUALITY_CONST( rcp_weak2.is_valid_ptr(), false );
628 
629  TEST_EQUALITY_CONST( rcp_weak1.shares_resource(rcp_weak2), false );
630 
631  TEST_THROW( rcp_weak2.assert_valid_ptr(), DanglingReferenceError );
632 #ifdef TEUCHOS_DEBUG
633  TEST_THROW( rcp_weak2.operator->(), DanglingReferenceError );
634  TEST_THROW( *rcp_weak2, DanglingReferenceError );
635  TEST_THROW( rcp_weak2.create_weak(), DanglingReferenceError );
636  TEST_THROW( rcp_weak2.get(), DanglingReferenceError );
637  TEST_THROW( rcp_weak2.getRawPtr(), DanglingReferenceError );
638  TEST_THROW( rcp_weak2(), DanglingReferenceError );
639  TEST_THROW( rcp_weak2.release(), DanglingReferenceError );
640 #endif // TEUCHOS_DEBUG
641 
642 }
643 
644 
645 TEUCHOS_UNIT_TEST( RCP, weak_strong )
646 {
647 
648  ECHO(RCP<A> rcp1(rcp(new A)));
649  TEST_EQUALITY_CONST( rcp1.strength(), RCP_STRONG );
650 
651  ECHO(RCP<A> rcp2 = rcp1.create_weak());
652 
653  TEST_EQUALITY_CONST( rcp2.strength(), RCP_WEAK );
654  TEST_EQUALITY_CONST( rcp1.strong_count(), 1 );
655  TEST_EQUALITY_CONST( rcp1.weak_count(), 1 );
656  TEST_EQUALITY_CONST( rcp2.strong_count(), 1 );
657  TEST_EQUALITY_CONST( rcp2.weak_count(), 1 );
658 
659  ECHO(RCP<A> rcp3 = rcp2.create_strong());
660 
661  TEST_EQUALITY_CONST( rcp3.strength(), RCP_STRONG );
662  TEST_EQUALITY_CONST( rcp1.strong_count(), 2 );
663  TEST_EQUALITY_CONST( rcp1.weak_count(), 1 );
664  TEST_EQUALITY_CONST( rcp2.strong_count(), 2 );
665  TEST_EQUALITY_CONST( rcp2.weak_count(), 1 );
666 
667  // This will make the underlying object A gets deleted!
668  ECHO(rcp1 = null);
669  ECHO(rcp3 = null);
670 
671  ECHO(rcp2 = null); // Should make the underlying node go away
672 
673 }
674 
675 
676 //
677 // circularReference
678 //
679 
680 
681 TEUCHOS_UNIT_TEST( RCP, circularReference_a_then_c )
682 {
683 
684  //TEST_EQUALITY_CONST(Teuchos::numActiveRCPNodes(), 0);
685 
686  {
687 
688  // Create objects a and c
689 
690  ECHO(RCP<A> a = rcp(new A));
691  ECHO(RCP<C> c = rcp(new C));
692 
693  // Create a circular reference where 'a' owns 'c' strongly but 'c' only
694  // owns 'a' weakly.
695 
696  ECHO(a->set_C(c));
697  ECHO(c->set_A(a.create_weak()));
698 
699 #ifdef TEUCHOS_DEBUG
700  ECHO(c->call_A_on_delete(true));
701  // Here, we set 'c' to call 'a' when it is deleted which will result in an
702  // exception being thrown in a call to delete. NOTE: It is *very* bad
703  // practice to allow exceptions to be thrown from destructors but I am
704  // allowing it so that I can detect such bad bahavior below!
705 #endif
706 
708  TEST_EQUALITY( c->call_A_g(), A_g_return );
709 
710  // Remove 'a' first and then remove 'c'. Since 'a' is only weakly held by
711  // 'c', this will result in 'a' being deleted right away. In this case,
712  // if anyone tries to access 'a' after this (like 'c' in its destructor),
713  // then an exception will get thrown in debug mode!
714 
715  ECHO(a = null);
716 
717  // Now, remove 'c'. In this case, since 'a' has already been deleted and
718  // 'c' is going to try to call 'a' on its way out, this will thrown an
719  // exception.
720 
721 #ifdef TEUCHOS_DEBUG
722 
723  TEST_THROW(c = null, DanglingReferenceError);
724  // NOTE: Above, operator==(...) exhibits the 'strong' guarantee?
725 
726  // Since an exception was thrown, the 'c' object never got deleted.
727  // Therefore, we need to disable 'c' calling 'a' on delete and the object
728  // will get cleaned up correctly when this function exists (I hope).
729  ECHO(c->call_A_on_delete(false));
730 
731  ECHO(c = null); // All memory should be cleaned up here!
732 
733 #endif // TEUCHOS_DEBUG
734 
735  }
736 
737 }
738 
739 
740 TEUCHOS_UNIT_TEST( RCP, circularReference_c_then_a )
741 {
742 
743  {
744 
745  // Create objects a and c
746 
747  ECHO(RCP<A> a = rcp(new A));
748  ECHO(RCP<C> c = rcp(new C));
749 
750  // Create a circular reference where 'a' owns 'c' strongly but 'c' only
751  // owns 'a' weakly.
752 
753  ECHO(a->set_C(c));
754  ECHO(c->set_A(a.create_weak()));
755 
756  ECHO(c->call_A_on_delete(false));
757  // Here, we set 'c' to not call 'a' when it is deleted. It turns out that
758  // the set of calls to delete and destructors that takes place is very
759  // complex and in order to avoid trouble, an object that holds an RCP to
760  // another object weakly should *never* try to call any members on the
761  // wrapped object as it gets deleted!
762 
764  TEST_EQUALITY( c->call_A_g(), A_g_return );
765 
766  // Remove 'c' first and then remove 'a' implicitly at the end of the
767  // block. Since 'c' is held strongly by 'a' and since we are keeping the
768  // strong pointer for 'a' alive, we can call functions on 'a' all we want
769  // with no fear of accessing dead memory.
770 
771  ECHO(c = null);
772 
773  TEST_EQUALITY( a->call_C_f(), C_f_return ); // C is still alive!
774 
775  // Finally, when 'a' goes away implicitly, it will take 'c' with it. In
776  // the complex set of nested calls that take place due to the circular
777  // reference, everything will get cleaned up correctly. Also, if any
778  // client code where to try to access an object as it is being deleted, an
779  // exception will get thrown and no memory error will occur (unless an
780  // abort(...) is called when an exception gets thrown from a destructor
781  // when an exception is already active).
782 
783  }
784 
785 }
786 
787 
788 TEUCHOS_UNIT_TEST( RCP, circularReference_self )
789 {
790  {
791  // Create one 'c' object
792  ECHO(RCP<C> c = rcp(new C));
793  // Create a weak circular reference where 'c' points back to itself
794  ECHO(c->set_A(c.create_weak()));
795  // Now, try to set 'c' to null.
796  ECHO(c = null); // All memory should be cleaned up here!
797  }
798 }
799 
800 
801 TEUCHOS_UNIT_TEST( RCP, danglingPtr )
802 {
803  ECHO(RCP<A> a_rcp = rcp(new A));
804  ECHO(Ptr<A> a_ptr = a_rcp());
805  ECHO(A *badPtr = a_rcp.getRawPtr());
806  ECHO(a_rcp = null);
807 #ifdef TEUCHOS_DEBUG
808  TEST_THROW( *a_ptr, DanglingReferenceError );
809  (void)badPtr;
810 #else
811  TEST_EQUALITY( a_ptr.getRawPtr(), badPtr );
812 #endif
813 }
814 
815 
816 #ifdef TEUCHOS_DEBUG
817 
818 /* ToDo: Comment this back in once I have everything working
819 
820 // Test that the RCPNode tracing machinary can detect if an owning RCPNode is
821 // being created that would result in a double delete.
822 TEUCHOS_UNIT_TEST( RCP, multiRcpCreateError )
823 {
824  C *c_ptr = new C;
825 #if !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
826  Teuchos::setTracingActiveRCPNodes(true);
827 #endif
828  RCP<C> c_rcp = rcp(c_ptr); // Okay
829  RCP<C> c_rcp2;
830  TEST_THROW(c_rcp2 = rcp(c_ptr), DuplicateOwningRCPError);
831 #if !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
832  Teuchos::setTracingActiveRCPNodes(false);
833 #endif
834  // Clean up memory so no leaks and not double deletes no matter what.
835  c_rcp.release();
836  c_rcp2.release();
837  delete c_ptr;
838 }
839 
840 */
841 
842 #endif // TEUCHOS_DEBUG
843 
844 
845 //
846 // invertObjectOwnership
847 //
848 
849 
850 RCP<C> createCAFactory()
851 {
852  RCP<C> c = rcp(new C);
853  c->set_A(rcp(new A));
854  return c;
855 }
856 
857 
858 RCP<A> createACFactory()
859 {
860  RCP<C> c = createCAFactory();
861  return Teuchos::rcpWithInvertedObjOwnership(c->get_A(), c);
862 }
863 
864 
865 RCP<C> extractCFromA(const RCP<A> &a)
866 {
867  return Teuchos::getInvertedObjOwnershipParent<C>(a);
868 }
869 
870 
871 TEUCHOS_UNIT_TEST( RCP, invertObjectOwnership_basic )
872 {
873  RCP<A> a = createACFactory();
874  RCP<C> c = extractCFromA(a);
875  TEST_EQUALITY_CONST( a.strong_count(), 1 );
876  TEST_EQUALITY_CONST( c->get_A().strong_count(), 3 );
877  TEST_ASSERT( !a.shares_resource(c->get_A()) );
878  TEST_EQUALITY( a.getRawPtr(), c->get_A().getRawPtr() );
879  TEST_EQUALITY( a->A_g(), A_g_return );
880  TEST_EQUALITY( c->C_g(), C_g_return );
881 }
882 
883 
884 // This unit test shows that you can remove the RCP in the C object
885 // and the A object will still live on.
886 TEUCHOS_UNIT_TEST( RCP, invertObjectOwnership_remove_A )
887 {
888  RCP<A> a = createACFactory();
889  extractCFromA(a)->set_A(null);
890  RCP<C> c = extractCFromA(a);
891  TEST_EQUALITY_CONST( a.strong_count(), 1 );
892  TEST_EQUALITY_CONST( c->get_A(), null );
893  TEST_EQUALITY( a->A_g(), A_g_return );
894  TEST_EQUALITY( c->C_g(), C_g_return );
895 }
896 
897 
898 //
899 // createRCPWithBadDealloc
900 //
901 
902 
903 RCP<A> createRCPWithBadDealloc()
904 {
905  return rcp(new A[1]); // Will use delete but should use delete []!
906 }
907 
908 
909 template<typename T>
910 class DeallocArrayDeleteExtraData {
911 public:
912  static RCP<DeallocArrayDeleteExtraData<T> > create(T *ptr)
913  { return rcp(new DeallocArrayDeleteExtraData(ptr)); }
914  ~DeallocArrayDeleteExtraData() { delete [] ptr_; }
915 private:
916  T *ptr_;
917  DeallocArrayDeleteExtraData(T *ptr) : ptr_(ptr) {}
918  // Not defined!
919  DeallocArrayDeleteExtraData();
920  DeallocArrayDeleteExtraData(const DeallocArrayDeleteExtraData&);
921  DeallocArrayDeleteExtraData& operator=(const DeallocArrayDeleteExtraData&);
922 };
923 
924 
925 // This unit test shows how you can use extra data to fix a bad deallocation
926 // policy
927 TEUCHOS_UNIT_TEST( RCP, Fix_createRCPWithBadDealloc )
928 {
929  using Teuchos::inOutArg;
930  using Teuchos::set_extra_data;
931  // Create object with bad deallocator
932  RCP<A> a = createRCPWithBadDealloc();
933  TEST_ASSERT(nonnull(a));
934  // Disable default (incorrect) dealloc and set a new deallocation policy as extra data!
935  a.release();
936  set_extra_data( DeallocArrayDeleteExtraData<A>::create(a.getRawPtr()), "dealloc",
937  inOutArg(a));
938 }
939 
940 
941 //
942 // Template Instantiations
943 //
944 
945 
946 #ifdef TEUCHOS_DEBUG
947 
948 # define DEBUG_UNIT_TEST_GROUP( T ) \
949 
950 #else
951 
952 # define DEBUG_UNIT_TEST_GROUP( T )
953 
954 #endif
955 
956 
957 #define UNIT_TEST_GROUP( T ) \
958  TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCP, weakDelete, T ) \
959  DEBUG_UNIT_TEST_GROUP(T)
960 
961 
966 
967 
968 } // namespace
Dangling reference error exception class.
Null reference error exception class.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
void set_extra_data(const T1 &extra_data, const std::string &name, const Ptr< ArrayRCP< T2 > > &p, EPrePostDestruction destroy_when=POST_DESTROY, bool force_unique=true)
Set extra data associated with a ArrayRCP object.
void set_C(const Teuchos::RCP< C > &c)
#define TEST_ASSERT(v1)
Assert the given statement is true.
Ptr< T1 > get_optional_nonconst_extra_data(RCP< T2 > &p, const std::string &name)
Get a pointer to non-const extra data (if it exists) associated with a RCP object.
int call_C_f()
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
const int C_g_return
Definition: TestClasses.hpp:56
const int C_f_return
Definition: TestClasses.hpp:57
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
RCP< T > rcpWithEmbeddedObj(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
void free(T *ptr)
Deallocates a pointer ptr using delete ptr (required).
Debug-mode RCPNode tracing class.
RCP< T > rcpCloneNode(const RCP< T > &p)
Allocate a new RCP object with a new RCPNode with memory pointing to the initial node.
#define SET_RCPNODE_TRACING()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition: Teuchos_as.hpp:287
bool nonnull(const boost::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
RCP< T > rcpFromUndefRef(T &r)
Return a non-owning weak RCP object from a raw object reference for an undefined type.
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
#define TEST_NOTHROW(code )
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
Policy class for deallocator for non-owned RCPs.
Smart reference counting pointer class for automatic garbage collection.
#define TEST_THROW(code, ExceptType )
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Thrown if a duplicate owning RCP is creatd the the same object.
Reference-counted pointer class and non-member templated function implementations.
const int A_g_return
Definition: TestClasses.hpp:50
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.
#define UNIT_TEST_GROUP(T)