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