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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_UnitTestHarness.hpp"
00045 #include "Teuchos_Ptr.hpp"
00046 #include "Teuchos_getConst.hpp"
00047 #include "TestClasses.hpp"
00048 
00049 
00050 namespace {
00051 
00052 
00053 using Teuchos::null;
00054 using Teuchos::Ptr;
00055 using Teuchos::RCP;
00056 using Teuchos::rcp;
00057 using Teuchos::ptrFromRef;
00058 using Teuchos::rcpFromPtr;
00059 using Teuchos::NullReferenceError;
00060 using Teuchos::DanglingReferenceError;
00061 using Teuchos::RCP_STRONG;
00062 using Teuchos::RCP_WEAK;
00063 
00064 
00065 TEUCHOS_UNIT_TEST( Ptr, nonnull )
00066 {
00067   ECHO(A a);
00068   ECHO(Ptr<A> a_ptr = ptrFromRef(a));
00069   TEST_EQUALITY_CONST(is_null(a_ptr), false);
00070   TEST_EQUALITY_CONST(nonnull(a_ptr), true);
00071   ECHO(a_ptr = null);
00072   TEST_EQUALITY_CONST(is_null(a_ptr), true);
00073   TEST_EQUALITY_CONST(nonnull(a_ptr), false);
00074 }
00075 
00076 
00077 TEUCHOS_UNIT_TEST( Ptr, getConst )
00078 {
00079   RCP<A> a_rcp(new A);
00080   Ptr<A> a_ptr = a_rcp.ptr();
00081   Ptr<const A> ca_ptr = a_ptr.getConst();
00082   TEST_EQUALITY(a_ptr.getRawPtr(), ca_ptr.getRawPtr());
00083 }
00084 
00085 
00086 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_weakRef )
00087 {
00088   ECHO(RCP<A> a_rcp = rcp(new A));
00089   ECHO(Ptr<A> a_ptr = a_rcp.ptr());
00090   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00091   TEST_EQUALITY(a_rcp2.getRawPtr(), a_rcp.getRawPtr());
00092 #ifdef TEUCHOS_DEBUG
00093   TEST_ASSERT(a_rcp2.shares_resource(a_rcp));
00094 #else
00095   // In an optimized build, the object a_rcp2 has its own RCPNode object that
00096   // is unrelated to the orgininal a_rcp object.  This cuts down on overhead.
00097 #endif
00098   ECHO(a_rcp = null);
00099 #ifdef TEUCHOS_DEBUG
00100   TEST_THROW(a_ptr.getRawPtr(), DanglingReferenceError);
00101   TEST_THROW(a_rcp2.getRawPtr(), DanglingReferenceError);
00102 #endif
00103   
00104 }
00105 
00106 
00107 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_rawRef )
00108 {
00109   ECHO(A a);
00110   ECHO(Ptr<A> a_ptr = ptrFromRef(a));
00111   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00112   TEST_EQUALITY(a_rcp2.getRawPtr(), &a);
00113 }
00114 
00115 
00116 TEUCHOS_UNIT_TEST( Ptr, rcpFromPtr_null )
00117 {
00118   ECHO(Ptr<A> a_ptr);
00119   ECHO(RCP<A> a_rcp2 = rcpFromPtr(a_ptr));
00120   TEST_EQUALITY(a_rcp2, null);
00121 }
00122 
00123 
00124 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines