Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Ptr_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Ptr.hpp"
12 #include "Teuchos_getConst.hpp"
13 #include "TestClasses.hpp"
14 
15 
16 namespace {
17 
18 
19 using Teuchos::null;
20 using Teuchos::Ptr;
21 using Teuchos::RCP;
22 using Teuchos::rcp;
23 using Teuchos::ptrFromRef;
24 using Teuchos::rcpFromPtr;
28 using Teuchos::RCP_WEAK;
29 
30 
32 {
33  ECHO(A a);
34  ECHO(Ptr<A> a_ptr = ptrFromRef(a));
35  TEST_EQUALITY_CONST(is_null(a_ptr), false);
36  TEST_EQUALITY_CONST(nonnull(a_ptr), true);
37  ECHO(a_ptr = null);
38  TEST_EQUALITY_CONST(is_null(a_ptr), true);
39  TEST_EQUALITY_CONST(nonnull(a_ptr), false);
40 }
41 
42 
44 {
45  RCP<A> a_rcp(new A);
46  Ptr<A> a_ptr = a_rcp.ptr();
47  Ptr<const A> ca_ptr = a_ptr.getConst();
48  TEST_EQUALITY(a_ptr.getRawPtr(), ca_ptr.getRawPtr());
49 }
50 
51 
52 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_weakRef )
53 {
54  ECHO(RCP<A> a_rcp = rcp(new A));
55  ECHO(Ptr<A> a_ptr = a_rcp.ptr());
56  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
57  TEST_EQUALITY(a_rcp2.getRawPtr(), a_rcp.getRawPtr());
58 #ifdef TEUCHOS_DEBUG
59  TEST_ASSERT(a_rcp2.shares_resource(a_rcp));
60 #else
61  // In an optimized build, the object a_rcp2 has its own RCPNode object that
62  // is unrelated to the orgininal a_rcp object. This cuts down on overhead.
63 #endif
64  ECHO(a_rcp = null);
65 #ifdef TEUCHOS_DEBUG
66  TEST_THROW(a_ptr.getRawPtr(), DanglingReferenceError);
67  TEST_THROW(a_rcp2.getRawPtr(), DanglingReferenceError);
68 #endif
69 
70 }
71 
72 
73 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_rawRef )
74 {
75  ECHO(A a);
76  ECHO(Ptr<A> a_ptr = ptrFromRef(a));
77  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
78  TEST_EQUALITY(a_rcp2.getRawPtr(), &a);
79 }
80 
81 
82 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_null )
83 {
84  ECHO(Ptr<A> a_ptr);
85  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
86  TEST_EQUALITY(a_rcp2, null);
87 }
88 
89 
90 } // 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...
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
#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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Ptr< T > ptrFromRef(T &arg)
Create a pointer to a object from an object reference.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
RCP< T > rcpFromPtr(const Ptr< T > &ptr)
Create an RCP&lt;T&gt; from a Ptr&lt;T&gt; object.
Smart reference counting pointer class for automatic garbage collection.
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.