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 /*
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 
45 #include "Teuchos_Ptr.hpp"
46 #include "Teuchos_getConst.hpp"
47 #include "TestClasses.hpp"
48 
49 
50 namespace {
51 
52 
53 using Teuchos::null;
54 using Teuchos::Ptr;
55 using Teuchos::RCP;
56 using Teuchos::rcp;
57 using Teuchos::ptrFromRef;
58 using Teuchos::rcpFromPtr;
62 using Teuchos::RCP_WEAK;
63 
64 
66 {
67  ECHO(A a);
68  ECHO(Ptr<A> a_ptr = ptrFromRef(a));
69  TEST_EQUALITY_CONST(is_null(a_ptr), false);
70  TEST_EQUALITY_CONST(nonnull(a_ptr), true);
71  ECHO(a_ptr = null);
72  TEST_EQUALITY_CONST(is_null(a_ptr), true);
73  TEST_EQUALITY_CONST(nonnull(a_ptr), false);
74 }
75 
76 
78 {
79  RCP<A> a_rcp(new A);
80  Ptr<A> a_ptr = a_rcp.ptr();
81  Ptr<const A> ca_ptr = a_ptr.getConst();
82  TEST_EQUALITY(a_ptr.getRawPtr(), ca_ptr.getRawPtr());
83 }
84 
85 
86 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_weakRef )
87 {
88  ECHO(RCP<A> a_rcp = rcp(new A));
89  ECHO(Ptr<A> a_ptr = a_rcp.ptr());
90  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
91  TEST_EQUALITY(a_rcp2.getRawPtr(), a_rcp.getRawPtr());
92 #ifdef TEUCHOS_DEBUG
93  TEST_ASSERT(a_rcp2.shares_resource(a_rcp));
94 #else
95  // In an optimized build, the object a_rcp2 has its own RCPNode object that
96  // is unrelated to the orgininal a_rcp object. This cuts down on overhead.
97 #endif
98  ECHO(a_rcp = null);
99 #ifdef TEUCHOS_DEBUG
100  TEST_THROW(a_ptr.getRawPtr(), DanglingReferenceError);
101  TEST_THROW(a_rcp2.getRawPtr(), DanglingReferenceError);
102 #endif
103 
104 }
105 
106 
107 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_rawRef )
108 {
109  ECHO(A a);
110  ECHO(Ptr<A> a_ptr = ptrFromRef(a));
111  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
112  TEST_EQUALITY(a_rcp2.getRawPtr(), &a);
113 }
114 
115 
116 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_null )
117 {
118  ECHO(Ptr<A> a_ptr);
119  ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
120  TEST_EQUALITY(a_rcp2, null);
121 }
122 
123 
124 } // 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.