Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Ptr_test.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "TestClasses.hpp"
00043 #include "Teuchos_Ptr.hpp"
00044 #include "Teuchos_GlobalMPISession.hpp"
00045 #include "Teuchos_CommandLineProcessor.hpp"
00046 #include "Teuchos_VerboseObject.hpp"
00047 #include "Teuchos_StandardCatchMacros.hpp"
00048 #include "Teuchos_Version.hpp"
00049 #include "Teuchos_Assert.hpp"
00050 #include "Teuchos_getConst.hpp"
00051 #include "Teuchos_as.hpp"
00052 
00053 
00054 int main( int argc, char* argv[] ) {
00055 
00056   using Teuchos::Ptr;
00057   using Teuchos::ptr;
00058   using Teuchos::ptrFromRef;
00059   using Teuchos::constPtr;
00060   using Teuchos::outArg;
00061   using Teuchos::inOutArg;
00062   using Teuchos::inoutArg;
00063   using Teuchos::optInArg;
00064   using Teuchos::constOptInArg;
00065   using Teuchos::CommandLineProcessor;
00066 
00067         bool success = true;
00068 
00069   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00070   //const int procRank = Teuchos::GlobalMPISession::getRank();
00071 
00072   Teuchos::RCP<Teuchos::FancyOStream>
00073     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00074 
00075         try {
00076 
00077     //
00078                 // Read options from the commandline
00079     //
00080 
00081     CommandLineProcessor clp(false); // Don't throw exceptions
00082 
00083                 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00084 
00085                 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00086                         *out << "\nEnd Result: TEST FAILED" << std::endl;
00087                         return parse_return;
00088                 }
00089 
00090     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00091 
00092     *out << "\nTesting Teuchos::Ptr class ...\n";
00093 
00094     {
00095       // Test null construction
00096       Ptr<A> a_ptr;
00097       *out << "\nNull a_ptr = " << a_ptr << "\n";
00098       TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.get() );
00099       TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.getRawPtr() );
00100 #ifdef TEUCHOS_DEBUG
00101       try {
00102         A &a = *a_ptr; // Should throw!
00103         a.A_g();
00104         TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
00105           "Error, Ptr::operator*() on null Ptr should have thrown exception!" );
00106       }
00107       catch( const Teuchos::NullReferenceError &except ) {
00108         // Caught expected exception!
00109       }
00110 #endif
00111 #ifdef TEUCHOS_DEBUG
00112       try {
00113         a_ptr->A_g(); // Should throw!
00114         TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
00115           "Error, Ptr::operator->() on null Ptr should have thrown exception!" );
00116       }
00117       catch( const Teuchos::NullReferenceError &except ) {
00118         // Caught expected exception!
00119       }
00120 #endif
00121     }
00122 
00123     {
00124       // Test basic construction of Ptr
00125       A a;
00126       Ptr<A> a_ptr(&a);
00127       *out << "\nNon-null a_ptr = " << a_ptr << "\n";
00128       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00129       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00130       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.getRawPtr() );
00131     }
00132 
00133     {
00134       // Test copy constructor for Ptr
00135       A a;
00136       Ptr<A> a_ptr1(&a);
00137       Ptr<A> a_ptr2(a_ptr1);
00138       TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
00139     }
00140 
00141     {
00142       // Test implicit copy conversion
00143       C c;
00144       Ptr<C> c_ptr(&c);
00145       Ptr<A> a_ptr(c_ptr);
00146       TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
00147     }
00148 
00149     {
00150       // Test assignment operator
00151       C c;
00152       Ptr<C> c_ptr(&c);
00153       Ptr<A> a_ptr;
00154       a_ptr = c_ptr;
00155       TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
00156     }
00157 
00158     {
00159       // Test construction of Ptr from ptr()
00160       A a;
00161       Ptr<A> a_ptr = ptr(&a);
00162       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00163       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00164     }
00165 
00166     {
00167       // Test construction of Ptr from ptrFromRef()
00168       A a;
00169       Ptr<A> a_ptr = ptrFromRef(a);
00170       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00171       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00172     }
00173 
00174     {
00175       // Test construction of Ptr from constPtr()
00176       A a;
00177       Ptr<const A> a_ptr = constPtr(a);
00178       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00179       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00180     }
00181 
00182     {
00183       // Test construction of Ptr from outArg()
00184       A a;
00185       Ptr<A> a_ptr = outArg(a);
00186       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00187       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00188     }
00189 
00190     {
00191       // Test construction of Ptr from inOutArg()
00192       A a;
00193       Ptr<A> a_ptr = inOutArg(a);
00194       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00195       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00196     }
00197 
00198     {
00199       // Test construction of Ptr from inOutArg()
00200       A a;
00201       Ptr<A> a_ptr = inoutArg(a);
00202       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00203       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00204     }
00205 
00206     {
00207       // Test construction of Ptr from optInArg()
00208       A a;
00209       Ptr<const A> a_ptr = optInArg(a);
00210       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00211       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00212     }
00213 
00214     {
00215       // Test construction of Ptr from optInArg()
00216       A a;
00217       Ptr<const A> a_ptr = constOptInArg(a);
00218       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00219       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00220     }
00221 
00222     {
00223       // Test ptr_implicit_cast()
00224       C c;
00225       Ptr<C> c_ptr(&c);
00226       Ptr<A> a_ptr1 = c_ptr;
00227       Ptr<A> a_ptr2 = Teuchos::ptr_implicit_cast<A>(c_ptr);
00228       TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
00229     }
00230 
00231     {
00232       // Test ptr_static_cast()
00233       E e;
00234       Ptr<D> d_ptr(&e);
00235       Ptr<E> e_ptr = Teuchos::ptr_static_cast<E>(d_ptr);
00236       TEUCHOS_ASSERT_EQUALITY( &*e_ptr, &e );
00237     }
00238 
00239     {
00240       // Test ptr_const_cast()
00241       C c;
00242       Ptr<const C> c_ptr1(&c);
00243       Ptr<C> c_ptr2 = Teuchos::ptr_const_cast<C>(c_ptr1);
00244       TEUCHOS_ASSERT_EQUALITY( &*c_ptr2, &*c_ptr1 );
00245     }
00246 
00247     {
00248       // Test null ptr_dynamic_cast()
00249       Ptr<A> a_ptr;
00250       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
00251       TEUCHOS_ASSERT_EQUALITY( c_ptr.get(), 0 );
00252     }
00253 
00254     {
00255       // Test non-throw non-null ptr_dynamic_cast()
00256       C c;
00257       Ptr<A> a_ptr(&c);
00258       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
00259       TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
00260     }
00261 
00262     {
00263       // Test good throwing non-null ptr_dynamic_cast()
00264       C c;
00265       Ptr<A> a_ptr(&c);
00266       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
00267       TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
00268     }
00269 
00270     {
00271       // Test bad throwing non-null ptr_dynamic_cast()
00272       B1 b1;
00273       Ptr<A> a_ptr(&b1);
00274       try {
00275         Ptr<C> b2_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
00276         (void) b2_ptr; // Silence "set but not used" compiler warning.
00277         TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error,
00278           "If you get here then the test failed!" );
00279       }
00280       catch ( const Teuchos::m_bad_cast &except ) {
00281         // Test passed!
00282       }
00283     }
00284 
00285         }
00286   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00287 
00288   if (success)
00289     *out << "\nEnd Result: TEST PASSED" << std::endl;
00290   else
00291     *out << "\nEnd Result: TEST FAILED" << std::endl;
00292 
00293   return ( success ? 0 : 1 );
00294 
00295 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines