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::optInArg;
00063   using Teuchos::constOptInArg;
00064   using Teuchos::CommandLineProcessor;
00065   
00066   bool success = true;
00067  
00068   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00069   //const int procRank = Teuchos::GlobalMPISession::getRank();
00070  
00071   Teuchos::RCP<Teuchos::FancyOStream>
00072     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00073  
00074   try {
00075     
00076     //
00077     // Read options from the commandline
00078     //
00079 
00080     CommandLineProcessor clp(false); // Don't throw exceptions
00081 
00082     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00083 
00084     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00085       *out << "\nEnd Result: TEST FAILED" << std::endl;
00086       return parse_return;
00087     }
00088 
00089     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00090 
00091     *out << "\nTesting Teuchos::Ptr class ...\n";
00092  
00093     {
00094       // Test null construction
00095       Ptr<A> a_ptr;
00096       *out << "\nNull a_ptr = " << a_ptr << "\n";
00097       TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.get() );
00098       TEUCHOS_ASSERT_EQUALITY( 0, a_ptr.getRawPtr() );
00099 #ifdef TEUCHOS_DEBUG
00100       try {
00101         A &a = *a_ptr; // Should throw!
00102         a.A_g();
00103         TEST_FOR_EXCEPTION( true, std::logic_error,
00104           "Error, Ptr::operator*() on null Ptr should have thrown exception!" );
00105       }
00106       catch( const Teuchos::NullReferenceError &except ) {
00107         // Caught expected exception!
00108       }
00109 #endif
00110 #ifdef TEUCHOS_DEBUG
00111       try {
00112         a_ptr->A_g(); // Should throw!
00113         TEST_FOR_EXCEPTION( true, std::logic_error,
00114           "Error, Ptr::operator->() on null Ptr should have thrown exception!" );
00115       }
00116       catch( const Teuchos::NullReferenceError &except ) {
00117         // Caught expected exception!
00118       }
00119 #endif
00120     }
00121  
00122     {
00123       // Test basic construction of Ptr
00124       A a;
00125       Ptr<A> a_ptr(&a);
00126       *out << "\nNon-null a_ptr = " << a_ptr << "\n";
00127       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00128       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00129       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.getRawPtr() );
00130     }
00131  
00132     {
00133       // Test copy constructor for Ptr
00134       A a;
00135       Ptr<A> a_ptr1(&a);
00136       Ptr<A> a_ptr2(a_ptr1);
00137       TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
00138     }
00139  
00140     {
00141       // Test implicit copy conversion
00142       C c;
00143       Ptr<C> c_ptr(&c);
00144       Ptr<A> a_ptr(c_ptr);
00145       TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
00146     }
00147  
00148     {
00149       // Test assignment operator
00150       C c;
00151       Ptr<C> c_ptr(&c);
00152       Ptr<A> a_ptr;
00153       a_ptr = c_ptr;
00154       TEUCHOS_ASSERT_EQUALITY( &*a_ptr, &*c_ptr );
00155     }
00156  
00157     {
00158       // Test construction of Ptr from ptr()
00159       A a;
00160       Ptr<A> a_ptr = ptr(&a);
00161       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00162       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00163     }
00164  
00165     {
00166       // Test construction of Ptr from ptrFromRef()
00167       A a;
00168       Ptr<A> a_ptr = ptrFromRef(a);
00169       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00170       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00171     }
00172  
00173     {
00174       // Test construction of Ptr from constPtr()
00175       A a;
00176       Ptr<const A> a_ptr = constPtr(a);
00177       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00178       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00179     }
00180  
00181     {
00182       // Test construction of Ptr from outArg()
00183       A a;
00184       Ptr<A> a_ptr = outArg(a);
00185       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00186       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00187     }
00188  
00189     {
00190       // Test construction of Ptr from inOutArg()
00191       A a;
00192       Ptr<A> a_ptr = inOutArg(a);
00193       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00194       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00195     }
00196  
00197     {
00198       // Test construction of Ptr from optInArg()
00199       A a;
00200       Ptr<const A> a_ptr = optInArg(a);
00201       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00202       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00203     }
00204  
00205     {
00206       // Test construction of Ptr from optInArg()
00207       A a;
00208       Ptr<const A> a_ptr = constOptInArg(a);
00209       TEUCHOS_ASSERT_EQUALITY( &a, &*a_ptr );
00210       TEUCHOS_ASSERT_EQUALITY( &a, a_ptr.get() );
00211     }
00212  
00213     {
00214       // Test ptr_implicit_cast()
00215       C c;
00216       Ptr<C> c_ptr(&c);
00217       Ptr<A> a_ptr1 = c_ptr;
00218       Ptr<A> a_ptr2 = Teuchos::ptr_implicit_cast<A>(c_ptr);
00219       TEUCHOS_ASSERT_EQUALITY( &*a_ptr1, &*a_ptr2 );
00220     }
00221  
00222     {
00223       // Test ptr_static_cast()
00224       E e;
00225       Ptr<D> d_ptr(&e);
00226       Ptr<E> e_ptr = Teuchos::ptr_static_cast<E>(d_ptr);
00227       TEUCHOS_ASSERT_EQUALITY( &*e_ptr, &e );
00228     }
00229  
00230     {
00231       // Test ptr_const_cast()
00232       C c;
00233       Ptr<const C> c_ptr1(&c);
00234       Ptr<C> c_ptr2 = Teuchos::ptr_const_cast<C>(c_ptr1);
00235       TEUCHOS_ASSERT_EQUALITY( &*c_ptr2, &*c_ptr1 );
00236     }
00237  
00238     {
00239       // Test null ptr_dynamic_cast()
00240       Ptr<A> a_ptr;
00241       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
00242       TEUCHOS_ASSERT_EQUALITY( c_ptr.get(), 0 );
00243     }
00244  
00245     {
00246       // Test non-throw non-null ptr_dynamic_cast()
00247       C c;
00248       Ptr<A> a_ptr(&c);
00249       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr);
00250       TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
00251     }
00252  
00253     {
00254       // Test good throwing non-null ptr_dynamic_cast()
00255       C c;
00256       Ptr<A> a_ptr(&c);
00257       Ptr<C> c_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
00258       TEUCHOS_ASSERT_EQUALITY( &*c_ptr, &c );
00259     }
00260  
00261     {
00262       // Test bad throwing non-null ptr_dynamic_cast()
00263       B1 b1;
00264       Ptr<A> a_ptr(&b1);
00265       try {
00266         Ptr<C> b2_ptr = Teuchos::ptr_dynamic_cast<C>(a_ptr,true);
00267         TEST_FOR_EXCEPTION( true, std::logic_error,
00268           "If you get here then the test failed!" );
00269       }
00270       catch ( const Teuchos::m_bad_cast &except ) {
00271         // Test passed!
00272       }
00273     }
00274  
00275   }
00276   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00277  
00278   if (success)
00279     *out << "\nEnd Result: TEST PASSED" << std::endl;
00280   else
00281     *out << "\nEnd Result: TEST FAILED" << std::endl;
00282  
00283   return ( success ? 0 : 1 );
00284  
00285 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines