Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Ptr_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_UnitTestHarness.hpp"
00032 #include "Teuchos_Ptr.hpp"
00033 #include "Teuchos_getConst.hpp"
00034 #include "TestClasses.hpp"
00035 
00036 
00037 namespace {
00038 
00039 
00040 using Teuchos::null;
00041 using Teuchos::Ptr;
00042 using Teuchos::RCP;
00043 using Teuchos::rcp;
00044 using Teuchos::ptrFromRef;
00045 using Teuchos::rcpFromPtr;
00046 using Teuchos::NullReferenceError;
00047 using Teuchos::DanglingReferenceError;
00048 using Teuchos::RCP_STRONG;
00049 using Teuchos::RCP_WEAK;
00050 using Teuchos::RCP_STRENGTH_INVALID;
00051 
00052 
00053 TEUCHOS_UNIT_TEST( Ptr, nonnull )
00054 {
00055   ECHO(A a);
00056   ECHO(Ptr<A> a_ptr = ptrFromRef(a));
00057   TEST_EQUALITY_CONST(is_null(a_ptr), false);
00058   TEST_EQUALITY_CONST(nonnull(a_ptr), true);
00059   ECHO(a_ptr = null);
00060   TEST_EQUALITY_CONST(is_null(a_ptr), true);
00061   TEST_EQUALITY_CONST(nonnull(a_ptr), false);
00062 }
00063 
00064 
00065 TEUCHOS_UNIT_TEST( Ptr, getConst )
00066 {
00067   RCP<A> a_rcp(new A);
00068   Ptr<A> a_ptr = a_rcp.ptr();
00069   Ptr<const A> ca_ptr = a_ptr.getConst();
00070   TEST_EQUALITY(a_ptr.getRawPtr(), ca_ptr.getRawPtr());
00071 }
00072 
00073 
00074 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_weakRef )
00075 {
00076   ECHO(RCP<A> a_rcp = rcp(new A));
00077   ECHO(Ptr<A> a_ptr = a_rcp.ptr());
00078   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00079   TEST_EQUALITY(a_rcp2.getRawPtr(), a_rcp.getRawPtr());
00080 #ifdef TEUCHOS_DEBUG
00081   TEST_ASSERT(a_rcp2.shares_resource(a_rcp));
00082 #else
00083   // In an optimized build, the object a_rcp2 has its own RCPNode object that
00084   // is unrelated to the orgininal a_rcp object.  This cuts down on overhead.
00085 #endif
00086   ECHO(a_rcp = null);
00087 #ifdef TEUCHOS_DEBUG
00088   TEST_THROW(a_ptr.getRawPtr(), DanglingReferenceError);
00089   TEST_THROW(a_rcp2.getRawPtr(), DanglingReferenceError);
00090 #endif
00091   
00092 }
00093 
00094 
00095 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_rawRef )
00096 {
00097   ECHO(A a);
00098   ECHO(Ptr<A> a_ptr = ptrFromRef(a));
00099   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00100   TEST_EQUALITY(a_rcp2.getRawPtr(), &a);
00101 }
00102 
00103 
00104 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_null )
00105 {
00106   ECHO(Ptr<A> a_ptr);
00107   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00108   TEST_EQUALITY(a_rcp2, null);
00109 }
00110 
00111 
00112 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines