RCP_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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_RCP.hpp"
00030 #include "Teuchos_GlobalMPISession.hpp"
00031 #include "Teuchos_oblackholestream.hpp"
00032 #include "Teuchos_CommandLineProcessor.hpp"
00033 #include "Teuchos_StandardCatchMacros.hpp"
00034 #include "Teuchos_Assert.hpp"
00035 #include "Teuchos_getConst.hpp"
00036 #include "Teuchos_Version.hpp"
00037 
00038 #ifdef HAVE_TEUCHOS_BOOST
00039 #  include "Teuchos_RCPBoostSharedPtrConversions.hpp"
00040 #endif
00041 
00042 #include "TestClasses.hpp"
00043 
00044 
00045 // 
00046 // Uncomment these macros to see example errors
00047 //
00048 
00049 //#define SHOW_COMPILE_TIME_ERRORS
00050 //#define SHOW_RUN_TIME_ERROR_1
00051 //#define SHOW_RUN_TIME_ERROR_2
00052 //#define SHOW_RUN_TIME_ERROR_3
00053 //#define SHOW_RUN_TIME_ERROR_4
00054 #define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
00055 #define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
00056 //#define SHOW_MEMORY_LEAK_1
00057 
00058 //
00059 // This program prints minimal output to standard error
00060 //
00061 
00062 int main( int argc, char* argv[] ) {
00063 
00064   using Teuchos::RCP;
00065   using Teuchos::DeallocDelete;
00066   using Teuchos::deallocFunctorDelete;
00067   using Teuchos::deallocFunctorHandleDelete;
00068   using Teuchos::null;
00069   using Teuchos::rcp;
00070   using Teuchos::rcpFromRef;
00071   using Teuchos::inOutArg;
00072   using Teuchos::is_null;
00073   using Teuchos::rcp_implicit_cast;
00074   using Teuchos::rcp_const_cast;
00075   using Teuchos::rcp_static_cast;
00076   using Teuchos::rcp_dynamic_cast;
00077   using Teuchos::set_extra_data;
00078   using Teuchos::get_extra_data;
00079   using Teuchos::get_nonconst_extra_data;
00080   using Teuchos::get_optional_extra_data;
00081   using Teuchos::get_optional_nonconst_extra_data;
00082   using Teuchos::get_dealloc;
00083   using Teuchos::get_nonconst_dealloc;
00084   using Teuchos::get_optional_dealloc;
00085   using Teuchos::get_optional_nonconst_dealloc;
00086   using Teuchos::rcpWithEmbeddedObj;
00087   using Teuchos::rcpWithEmbeddedObjPreDestroy;
00088   using Teuchos::rcpWithEmbeddedObjPostDestroy;
00089   using Teuchos::getEmbeddedObj;
00090   using Teuchos::getNonconstEmbeddedObj;
00091   using Teuchos::getConst;
00092   using Teuchos::CommandLineProcessor;
00093   
00094   bool success = true, verbose = true;
00095 
00096   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00097   const int procRank = Teuchos::GlobalMPISession::getRank();
00098 
00099   Teuchos::oblackholestream blackhole;
00100   std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
00101 
00102   try {
00103 
00104     // Read options from the commandline
00105     CommandLineProcessor clp(false); // Don't throw exceptions
00106     clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00107     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00108     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00109       out << "\nEnd Result: TEST FAILED" << std::endl;
00110       return parse_return;
00111     }
00112 
00113     blackhole << "\nThis should not print anywhere.\n";
00114 
00115     if(verbose)
00116       out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00117 
00118     if(verbose)
00119       out << "\nTesting basic RCP functionality ...\n";
00120 
00121     // Create some smart pointers
00122 
00123     RCP<A> a_ptr1 = rcp(new C);
00124     if(verbose)
00125       out << "\na_ptr1 = " << a_ptr1 << "\n";
00126 #ifndef __sun
00127     // RAB: 2003/11/24: The Sun compiler ("Forte Developer 7 C++
00128     // 5.4 2002/03/09" returned from CC -V) does not seem to be
00129     // following the standard when it comes to the handling of
00130     // temporary objects and therefore the count() is not currect.
00131     // In the above statement, at least one and perhaps two
00132     // temporary RCP objects are created before the object
00133     // a_ptr1 is initialized. However, the standard says that the
00134     // lifetime of temprary objects must not extend past the
00135     // statement in which it was created (see section 10.4.10 in
00136     // Stroustroup, 3ed edition). This compiler stinks!!!!!
00137     TEST_FOR_EXCEPT( a_ptr1.count() != 1 );
00138     TEST_FOR_EXCEPT( !a_ptr1.shares_resource(a_ptr1) );
00139 #endif // __sun
00140     TEST_FOR_EXCEPT( a_ptr1.ptr() == null );
00141     TEST_FOR_EXCEPT( a_ptr1 == null );
00142     TEST_FOR_EXCEPT( !(a_ptr1 != null) );
00143     TEST_FOR_EXCEPT( is_null(a_ptr1) );
00144     RCP<D> d_ptr1 = rcp(new E);
00145     TEST_FOR_EXCEPT( d_ptr1.shares_resource(a_ptr1) );
00146 #ifndef __sun
00147     TEST_FOR_EXCEPT( d_ptr1.count() != 1 );
00148 #endif
00149     TEST_FOR_EXCEPT( d_ptr1.get() == NULL);
00150     TEST_FOR_EXCEPT( d_ptr1.getRawPtr() == NULL);
00151 
00152     {
00153       // Create a weak RCP
00154       RCP<A> weak_a_ptr1 = rcpFromRef(*a_ptr1);
00155       TEUCHOS_ASSERT( weak_a_ptr1.ptr() == a_ptr1.ptr() );
00156       TEUCHOS_ASSERT( weak_a_ptr1.has_ownership() == false );
00157     }
00158 
00159     {
00160 
00161       // Create some more smart points (no new memory!)
00162 
00163       const RCP<const A> ca_ptr1 = rcp_const_cast<const A>(a_ptr1); 
00164       TEST_FOR_EXCEPT( !(ca_ptr1 == a_ptr1) );
00165       TEST_FOR_EXCEPT( ca_ptr1 != a_ptr1 );
00166       TEST_FOR_EXCEPT( !ca_ptr1.shares_resource(a_ptr1) );
00167 #ifndef __sun
00168       TEST_FOR_EXCEPT( a_ptr1.count() != 2 );
00169 #endif
00170       TEST_FOR_EXCEPT( ca_ptr1.ptr() == null );
00171 #ifndef __sun
00172       TEST_FOR_EXCEPT( ca_ptr1.count() != 2 );
00173 #endif
00174       const RCP<const D> cd_ptr1 = rcp_const_cast<const D>(d_ptr1);
00175 #ifndef __sun
00176       TEST_FOR_EXCEPT( d_ptr1.count() != 2 );
00177 #endif
00178       TEST_FOR_EXCEPT( cd_ptr1.ptr() == null );
00179 #ifndef __sun
00180       TEST_FOR_EXCEPT( cd_ptr1.count() != 2 );
00181 #endif
00182 
00183 #ifdef SHOW_RUN_TIME_ERROR_1
00184       // Conversion using get() is a no no! When a_ptr2 is deleted so will the allocated
00185       // object and then a_ptr1 will be corrupted after this block ends!
00186       const RCP<A> a_ptr2 = a_ptr1.get();
00187 #endif
00188 
00189       // Test assignment functions
00190 
00191       a_ptr1 = rcp_const_cast<A>(ca_ptr1.assert_not_null()); // Should be okay, assignment to self
00192 
00193 #ifdef SHOW_COMPILE_TIME_ERRORS
00194       ca_ptr1 = ca_ptr1; // Should not compile since ca_ptr1 is declared constant
00195       ca_ptr1->A_g(); // Should not compile since A_g() is a non-const member function
00196 #endif
00197 
00198       // Test function calls through operaor->(...)
00199 
00200       TEST_FOR_EXCEPT( a_ptr1->A_g() != A_g_return );
00201       TEST_FOR_EXCEPT( a_ptr1->A_f() != A_f_return );
00202       TEST_FOR_EXCEPT( ca_ptr1->A_f() != A_f_return );
00203       TEST_FOR_EXCEPT( d_ptr1->D_g() != D_g_return );
00204       TEST_FOR_EXCEPT( d_ptr1->D_f() != D_f_return );
00205       TEST_FOR_EXCEPT( cd_ptr1->D_f() != D_f_return );
00206     
00207       // Test funciton calls through operator*(...)
00208 
00209       TEST_FOR_EXCEPT( (*a_ptr1).A_g() != A_g_return );
00210       TEST_FOR_EXCEPT( (*a_ptr1).A_f() != A_f_return );
00211       TEST_FOR_EXCEPT( (*ca_ptr1).A_f() != A_f_return );
00212       TEST_FOR_EXCEPT( (*d_ptr1).D_g() != D_g_return );
00213       TEST_FOR_EXCEPT( (*d_ptr1).D_f() != D_f_return );
00214       TEST_FOR_EXCEPT( (*cd_ptr1).D_f() != D_f_return );
00215 
00216       // Test dynamic and static conversions
00217 
00218       // Cast down the inheritance hiearchy (const A -> const B1)
00219       const RCP<const B1> cb1_ptr1 = rcp_dynamic_cast<const B1>(ca_ptr1);
00220       TEST_FOR_EXCEPT( cb1_ptr1.ptr() == null );
00221 #ifndef __sun
00222       TEST_FOR_EXCEPT( cb1_ptr1.count() != 3 );
00223       TEST_FOR_EXCEPT( ca_ptr1.count() != 3 );
00224       TEST_FOR_EXCEPT( a_ptr1.count() != 3 );
00225 #endif
00226 
00227       // Cast up the inheritance hiearchy (const B1 -> const A)
00228       TEST_FOR_EXCEPT( rcp_implicit_cast<const A>(cb1_ptr1)->A_f() != A_f_return );
00229       TEST_FOR_EXCEPT( RCP<const A>(cb1_ptr1)->A_f() != A_f_return );
00230       // Implicit cast from const to non-const (A -> const A)
00231       TEST_FOR_EXCEPT( rcp_implicit_cast<const A>(a_ptr1)->A_f() != A_f_return );
00232       TEST_FOR_EXCEPT( RCP<const A>(a_ptr1)->A_f() != A_f_return );
00233       // Cast away constantness (const B1 -> B1)
00234       TEST_FOR_EXCEPT( rcp_const_cast<B1>(cb1_ptr1)->B1_g() != B1_g_return );
00235       // Cast across the inheritance hiearchy (const B1 -> const B2)
00236       TEST_FOR_EXCEPT( rcp_dynamic_cast<const B2>(cb1_ptr1)->B2_f() != B2_f_return );
00237       // Cast down the inheritance hiearchy (const B1 -> const C)
00238       TEST_FOR_EXCEPT( rcp_dynamic_cast<const C>(cb1_ptr1)->C_f() != C_f_return );
00239 
00240       // Cast away constantness (const C -> C)
00241       const RCP<C>
00242         c_ptr1 = rcp_const_cast<C>(rcp_dynamic_cast<const C>(ca_ptr1));
00243       TEST_FOR_EXCEPT( c_ptr1.ptr() == null );
00244 #ifndef __sun
00245       TEST_FOR_EXCEPT( c_ptr1.count() != 4 );
00246       TEST_FOR_EXCEPT( ca_ptr1.count() != 4 );
00247       TEST_FOR_EXCEPT( a_ptr1.count() != 4 );
00248 #endif
00249 
00250       // Cast down the inheritance hiearchy using static_cast<...> (const D -> const E)
00251       const RCP<const E>
00252         ce_ptr1 = rcp_static_cast<const E>(cd_ptr1); // This is not checked at runtime!
00253       TEST_FOR_EXCEPT( ce_ptr1.ptr() == null);
00254 #ifndef __sun
00255       TEST_FOR_EXCEPT( ce_ptr1.count() != 3 );
00256       TEST_FOR_EXCEPT( cd_ptr1.count() != 3 );
00257       TEST_FOR_EXCEPT( d_ptr1.count() != 3 );
00258 #endif
00259 
00260       // Cast up the inheritance hiearchy (const E -> const D)
00261       TEST_FOR_EXCEPT( rcp_implicit_cast<const D>(ce_ptr1)->D_f() != D_f_return ); 
00262       // Cast away constantness (const E -> E)
00263       TEST_FOR_EXCEPT( rcp_const_cast<E>(ce_ptr1)->E_g() != E_g_return );
00264       TEST_FOR_EXCEPT( ce_ptr1->D_f() != D_f_return );
00265 
00266 #ifdef SHOW_COMPILE_TIME_ERRORS
00267       // Try to cast down inheritance hiearchy using dynamic_cast<...> (const D -> const E)
00268       rcp_dynamic_cast<const E>( cd_ptr1 )->E_f(); // This should not compile since D and E are not polymophic
00269 #endif
00270 
00271 #ifndef _INTEL // Intel compiler does not seem to be doing dynamic cast correctly?
00272 #ifdef TEUCHOS_DEBUG // operator->() only throws std::exception when TEUCHOS_DEBUG is defined
00273       try {
00274         // Try to cast form one interface to another that is not supported (B2 -> B1).
00275         // The RCP<B1> returned from rcp_dynamic_cast<...> should be null!
00276         // Note that RCP<...>::optertor->() should throw an std::exception in debug
00277         // mode (i.e. TEUCHOS_DEBUG is defined) but even so no memory leak occurs. If you
00278         // don't believe me then step through with a debugger and see for yourself.
00279         TEST_FOR_EXCEPT( rcp_dynamic_cast<B1>( rcp(new B2) )->B1_g() != B1_g_return );
00280         return -1; // Should not be executed!
00281       }
00282       catch( const std::logic_error &excpt )
00283       {}
00284 #endif
00285       try {
00286         // Try to cast form one interface to another that is not supported (B2 -> B1).
00287         // Note that rcp_dynamic_cast<B1>(...,true) should throw an std::exception but even
00288         // so no memory leak occurs. If you don't believe me then step through with a
00289         // debugger and see for yourself.
00290         rcp_dynamic_cast<B1>( rcp(new B2), true );
00291         return -1; // Should not be executed!
00292       }
00293       catch( const std::bad_cast &excpt )
00294       {}
00295 #endif
00296 
00297       // Manually clean up some memory
00298 
00299       delete d_ptr1.release().get(); // Now d_ptr1.get() no longer points to a valid object but okay
00300       // as long as no other access to this object is attempted! (see below)
00301 #ifdef SHOW_RUN_TIME_ERROR_2
00302       TEST_FOR_EXCEPT( d_ptr1->D_g() == D_g_return ); // Should cause a segmentation fault since d_ptr.get() was deleted!
00303 #endif
00304 
00305 #ifdef SHOW_MEMORY_LEAK_1
00306       a_ptr1.release(); // If we release but do not delete manually then this is a memory leak!
00307 #endif
00308     
00309       // Here at the end of the block, all of the other smart pointers are deleted!
00310     }
00311     // Check that all of the other references where removed but these
00312 #ifndef __sun
00313     TEST_FOR_EXCEPT( a_ptr1.count() != 1 );
00314     TEST_FOR_EXCEPT( d_ptr1.count() != 1 );
00315 #endif
00316 
00317     // Assign some other dynamically created objects.
00318   
00319     a_ptr1 = rcp(new A); // In each case the current dynamically allocated object is deleted ...
00320     a_ptr1 = rcp(new B1); // before the new reference is set.
00321     a_ptr1 = rcp(new B2); // ""
00322     a_ptr1 = rcp(new C); // ""
00323     d_ptr1 = rcp(new D); // ""
00324     d_ptr1 = rcp(new E); // ""
00325 
00326     // Assign pointers to some automatic objects that do not need deleted.
00327     // We can do this but we need to remove ownership of the pointer
00328     // from the smart pointer objects so that they do not try to
00329     // delete them. If we forget then delete will be called on these
00330     // pointers and will cause a runtime error.
00331 
00332     C c; // Automatic object what will be deleted by compiler at end of block
00333     a_ptr1 = rcp(&c);
00334 #ifndef SHOW_RUN_TIME_ERROR_3
00335     // Release ownership so that a_ptr1 will not try to delete &c when a_ptr1 goes out of scope
00336     a_ptr1.release();
00337 #endif  
00338 
00339     E e; // Automatic object what will be deleted by compiler at end of block
00340     d_ptr1 = rcp(&e);
00341 #ifndef SHOW_RUN_TIME_ERROR_4
00342     // Release ownership so that d_ptr1 will not try to delete &e when a_ptr1 goes out of scope
00343     d_ptr1.release();
00344 #endif
00345 
00346 #ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
00347     // Allocate an using new and then store the non-base address in in
00348     // a RCP and then try to delete (this is a no-no usually).
00349     C *c_ptr5 = new C; // Okay, no type info lost and address should be same as returned from malloc(...)
00350 #ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
00351     const void *c_ptr5_base = dynamic_cast<void*>(c_ptr5);
00352     if(verbose) {
00353       out << "\nSize of C = " << sizeof(C) << std::endl;
00354       out << "Base address of object of type C = " << dynamic_cast<void*>(c_ptr5) << std::endl;
00355       out << "Offset to address of object of type C = " << ((long int)c_ptr5 - (long int)c_ptr5_base) << std::endl;
00356       out << "Offset of B1 object in object of type C = " << ((long int)static_cast<B1*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
00357       out << "Offset of B2 object in object of type C = " << ((long int)static_cast<B2*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
00358       out << "Offset of A object in object of type C = " << ((long int)static_cast<A*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
00359     }
00360 #endif // SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
00361     A *a_rptr5 = c_ptr5; // Here the address has changed and is no longer the same as the base address
00362     a_ptr1 = rcp(a_rptr5); // This is a no-no and could cause trouble!
00363     a_ptr1 = null; // This will cause a segmentation fault in free(...) on many platforms
00364 #endif // SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
00365   
00366     // Test out getting the deallocator object
00367     a_ptr1 = rcp( new C, DeallocDelete<C>(), true );
00368     get_dealloc<DeallocDelete<C> >(a_ptr1);
00369     get_nonconst_dealloc<DeallocDelete<C> >(a_ptr1);
00370     TEST_FOR_EXCEPT( get_optional_nonconst_dealloc<DeallocDelete<C> >(a_ptr1)==null );
00371     TEST_FOR_EXCEPT( get_optional_nonconst_dealloc<DeallocDelete<A> >(a_ptr1)!=null );
00372     TEST_FOR_EXCEPT( get_optional_dealloc<DeallocDelete<C> >(const_cast<const RCP<A>&>(a_ptr1))==null );
00373     TEST_FOR_EXCEPT( get_optional_dealloc<DeallocDelete<A> >(const_cast<const RCP<A>&>(a_ptr1))!=null );
00374  
00375     // Test storing extra data and then getting it out again
00376     TEST_FOR_EXCEPT( get_optional_nonconst_extra_data<RCP<B1> >(a_ptr1,"blahblah") != null );
00377     TEST_FOR_EXCEPT( get_optional_extra_data<int>(const_cast<const RCP<A>&>(a_ptr1),"blahblah") != null ); // test const version
00378     set_extra_data( int(-5), "int", inOutArg(a_ptr1) );
00379     TEST_FOR_EXCEPT( get_extra_data<int>(a_ptr1,"int") != -5 );
00380     TEST_FOR_EXCEPT( get_nonconst_extra_data<int>(a_ptr1,"int") != -5 );
00381     set_extra_data( rcp(new B1), "B1", inOutArg(a_ptr1) );
00382     TEST_FOR_EXCEPT( get_extra_data<RCP<B1> >(a_ptr1,"B1")->B1_f() != B1_f_return );
00383     TEST_FOR_EXCEPT( get_extra_data<int>(const_cast<const RCP<A>&>(a_ptr1),"int") != -5 ); // test const version
00384     TEST_FOR_EXCEPT( (*get_optional_extra_data<RCP<B1> >(a_ptr1,"B1"))->B1_f() != B1_f_return );
00385     TEST_FOR_EXCEPT( *get_optional_extra_data<int>(const_cast<const RCP<A>&>(a_ptr1),"int") != -5 ); // test const version
00386     TEST_FOR_EXCEPT( get_optional_extra_data<RCP<B1> >(a_ptr1,"blahblah") != null );
00387     TEST_FOR_EXCEPT( get_optional_extra_data<int>(const_cast<const RCP<A>&>(a_ptr1),"blahblah") != null ); // test const version
00388 
00389     // Test storage of extra data as embedded objects and then getting it out
00390     // again
00391  
00392     {
00393       RCP<A> a_ptr = rcpWithEmbeddedObj(new C,int(-5));
00394       const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
00395       TEST_FOR_EXCEPT( intRtn1 != -5 );
00396       getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
00397       const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
00398       TEST_FOR_EXCEPT( intRtn2 != -4 );
00399     }
00400  
00401     {
00402       RCP<A> a_ptr = rcpWithEmbeddedObjPreDestroy(new C,int(-5));
00403       const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
00404       TEST_FOR_EXCEPT( intRtn1 != -5 );
00405       getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
00406       const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
00407       TEST_FOR_EXCEPT( intRtn2 != -4 );
00408     }
00409  
00410     {
00411       RCP<A> a_ptr = rcpWithEmbeddedObjPostDestroy(new C,int(-5));
00412       const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
00413       TEST_FOR_EXCEPT( intRtn1 != -5 );
00414       getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
00415       const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
00416       TEST_FOR_EXCEPT( intRtn2 != -4 );
00417     }
00418 
00419     // Test pre-destruction of extra data
00420     int a_f_return = -2;
00421     set_extra_data( rcp(new Get_A_f_return(&*a_ptr1,&a_f_return)),
00422       "a_f_return", inOutArg(a_ptr1), Teuchos::PRE_DESTROY );
00423 
00424     // Set pointers to null to force releasing any owned memory
00425     a_ptr1 = null;
00426     d_ptr1 = null;
00427 
00428 #ifndef __sun
00429     // RAB: 2004/08/12: It appears that SUN compiler is not deleting the piece of extra
00430     // data properly and therefore the destructor of the above Get_A_f_return object
00431     // is not being called (which sets the value of af_return). This compiler stinks!
00432     TEST_FOR_EXCEPT( a_f_return != A_f_return ); // Should be been called in destructor of a_ptr1 but before the A object is destroyed!
00433 #endif
00434 
00435     // Testing the deallocFunctorDelete function and DeallocFunctorDelete class
00436     a_ptr1 = rcp( new C, deallocFunctorDelete<A>(deallocA), true );
00437     a_ptr1 = null;
00438 
00439     // Testing the deallocFunctorHandleDelete function and DeallocFunctorHandleDelete class
00440     a_ptr1 = rcp( new C, deallocFunctorHandleDelete<A>(deallocHandleA), true );
00441     a_ptr1 = null;
00442 
00443 #ifdef TEUCHOS_DEBUG
00444  
00445     if(verbose)
00446       out << "\nCreate a circular reference that will cause a memory leak! ...\n";
00447     {
00448       Teuchos::setTracingActiveRCPNodes(true);
00449       RCP<A> a = rcp(new A());
00450       RCP<C> c2 = rcp(new C());
00451       a->set_C(c2);
00452       c2->set_A(a);
00453     }
00454 
00455 #endif // TEUCHOS_DEBUG
00456 
00457 #ifndef TEUCHOS_DEBUG 
00458 
00459     if(verbose)
00460       out << "\nTesting using RCP to wrap an undefined opaque object (no TNT) ...\n";
00461     {
00462       RCP<UndefinedType> op_ptr = rcp( createOpaque(),
00463         deallocFunctorHandleDelete<UndefinedType>(destroyOpaque), true );
00464       TEUCHOS_ASSERT_EQUALITY( getOpaqueValue(&*op_ptr), getOpaqueValue_return );
00465     }
00466     // 2008/08/01: rabartl: Above, we can only wrap an undefined type in
00467     // nondebug mode since there is no TypeNameTraits class defined for it and
00468     // the default uses typeid(...) which you can't call on an undefined type.
00469     // If you define a specialization of TypeNameTraits for this class, then
00470     // it will compile just fine!  This is related to bug 4016.
00471 
00472 #endif // not TEUCHOS_DEBUG
00473 
00474     if(verbose)
00475       out << "\nTesting using RCP to wrap an undefined opaque object (with TNT) ...\n";
00476     {
00477       RCP<UndefinedType2> op_ptr = rcp( createOpaque2(),
00478         deallocFunctorHandleDelete<UndefinedType2>(destroyOpaque2), true );
00479       TEUCHOS_ASSERT_EQUALITY( getOpaque2Value(&*op_ptr), getOpaque2Value_return );
00480     }
00481     // 2008/08/01: rabartl: Above, we can wrap an undefined type in debug mode
00482     // as long as we have a TypeNameTraits specialization of it to avoid
00483     // calling typeid(...).
00484 
00485 #ifdef HAVE_TEUCHOS_BOOST
00486 
00487     if(verbose)
00488       out << "\nTesting basic RCP compatibility with boost::shared_ptr ...\n";
00489 
00490     boost::shared_ptr<A> a_sptr1(new C());
00491     RCP<A> a_rsptr1 = rcp(a_sptr1);
00492     TEST_FOR_EXCEPT( a_rsptr1.get() != a_sptr1.get() );
00493     TEST_FOR_EXCEPT( a_rsptr1.getRawPtr() != a_sptr1.get() );
00494     TEST_FOR_EXCEPT( a_rsptr1.get() != a_rsptr1.getRawPtr() );
00495     boost::shared_ptr<A> a_sptr2 = shared_pointer(a_rsptr1);
00496     TEST_FOR_EXCEPT( a_sptr2.get() != a_sptr1.get() );
00497     RCP<A> a_rsptr2 = rcp(a_sptr2);
00498     TEST_FOR_EXCEPT( a_rsptr2.ptr() != a_rsptr1.ptr() );
00499     //TEST_FOR_EXCEPT( a_rsptr2 != a_rsptr1 ); // This should work if boost::get_deleter() works correctly!
00500     boost::shared_ptr<A> a_sptr3 = shared_pointer(a_rsptr2);
00501     TEST_FOR_EXCEPT( a_sptr3.get() != a_rsptr2.get() );
00502 
00503 #endif // HAVE_TEUCHOS_BOOST
00504 
00505     if(verbose)
00506       out << "\nAll tests for RCP seem to check out!\n";
00507 
00508   } // end try
00509   TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
00510  
00511   try {
00512     // In debug mode, this should show that the A and C RCP objects are still
00513     // around!
00514     Teuchos::printActiveRCPNodes(out);
00515 #ifdef TEUCHOS_DEBUG
00516     TEUCHOS_ASSERT_EQUALITY( 2, Teuchos::numActiveRCPNodes() );
00517 #endif
00518   } // end try
00519   TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
00520  
00521   if(success)
00522     out << "\nEnd Result: TEST PASSED" << std::endl;
00523 
00524   return ( success ? 0 : 1 );
00525 
00526 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1