Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
50namespace {
51
52
53using Teuchos::null;
54using Teuchos::Ptr;
55using Teuchos::RCP;
56using Teuchos::rcp;
57using Teuchos::ptrFromRef;
58using Teuchos::rcpFromPtr;
63
64
65TEUCHOS_UNIT_TEST( Ptr, nonnull )
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
77TEUCHOS_UNIT_TEST( Ptr, getConst )
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
86TEUCHOS_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
107TEUCHOS_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
116TEUCHOS_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
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
#define ECHO(statement)
Echo the given statement before it is executed.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Dangling reference error exception class.
Null reference error exception class.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const boost::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
Definition PackageA.cpp:3
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.