ArrayRCP_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Teuchos_UnitTestHarness.hpp"
00002 #include "Array_UnitTest_helpers.hpp"
00003 #include "TestClasses.hpp"
00004 #include "Teuchos_ArrayRCP.hpp"
00005 #include "Teuchos_implicit_cast.hpp"
00006 #include "Teuchos_as.hpp"
00007 #include "Teuchos_getRawPtr.hpp"
00008 
00009 namespace {
00010 
00011 using ArrayUnitTestHelpers::n;
00012 using ArrayUnitTestHelpers::generateArray;
00013 
00014 typedef Teuchos_Ordinal Ordinal;
00015 using Teuchos::getRawPtr;
00016 using Teuchos::as;
00017 using Teuchos::null;
00018 using Teuchos::rcp;
00019 using Teuchos::RCP;
00020 using Teuchos::ArrayRCP;
00021 using Teuchos::Array;
00022 using Teuchos::arcp;
00023 using Teuchos::arcp_reinterpret_cast;
00024 using Teuchos::ArrayView;
00025 using Teuchos::getConst;
00026 using Teuchos::NullReferenceError;
00027 using Teuchos::DanglingReferenceError;
00028 using Teuchos::RangeError;
00029 using Teuchos::RCP_STRONG;
00030 using Teuchos::RCP_WEAK;
00031 using Teuchos::RCP_STRENGTH_INVALID;
00032 using Teuchos::implicit_ptr_cast;
00033 using Teuchos::getRawPtr;
00034 
00035 
00036 //
00037 // Templated unit tests
00038 //
00039 
00040 
00041 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, construct_n, T )
00042 {
00043   std::vector<T> a(n, as<T>(1));
00044   ArrayRCP<T> a_arcp(n, as<T>(1));
00045   TEST_COMPARE_ARRAYS(a, a_arcp);
00046 }
00047 
00048 
00049 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, assignSelf, T )
00050 {
00051   ArrayRCP<T> a_arcp;
00052   a_arcp = a_arcp;
00053 }
00054 
00055 
00056 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, assign_n_val, T )
00057 {
00058   const T val = as<T>(1);
00059   std::vector<T> a;
00060   a.assign(n, val);
00061   ArrayRCP<T> a_arcp;
00062   a_arcp.assign(n, val);
00063   TEST_COMPARE_ARRAYS(a, a_arcp);
00064 }
00065 
00066 
00067 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, assign_begin_end, T )
00068 {
00069   const T val = as<T>(1);
00070   std::vector<T> a;
00071   a.assign(n, val);
00072   ArrayRCP<T> a_arcp;
00073   a_arcp.assign(a.begin(), a.end());
00074   TEST_COMPARE_ARRAYS(a, a_arcp);
00075 }
00076 
00077 
00078 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, deepCopy, T )
00079 {
00080   const T val = as<T>(1);
00081   std::vector<T> a;
00082   a.assign(n, val);
00083   ArrayRCP<T> a_arcp = arcp<T>(n);
00084   ArrayRCP<T> a_arcp_cpy = a_arcp;
00085   a_arcp.deepCopy(Teuchos::arrayViewFromVector(a));
00086   TEST_COMPARE_ARRAYS(a, a_arcp);
00087   TEST_EQUALITY(a_arcp.getRawPtr(), a_arcp_cpy.getRawPtr());
00088 }
00089 
00090 
00091 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, resize, T )
00092 {
00093   const T val1 = as<T>(1);
00094   const T val2 = as<T>(2);
00095 
00096   std::vector<T> a;
00097   ArrayRCP<T> a_arcp;
00098 
00099   out << "\nChecking resize(n, val1) ...\n"; 
00100   a.resize(n, val1);
00101   a_arcp.resize(n, val1);
00102   TEST_COMPARE_ARRAYS(a, a_arcp);
00103 
00104   out << "\nChecking resize(2*n, val2) ...\n"; 
00105   a.resize(2*n, val2);
00106   a_arcp.resize(2*n, val2);
00107   TEST_COMPARE_ARRAYS(a, a_arcp);
00108 
00109   out << "\nChecking resize(n/2) ...\n"; 
00110   a.resize(n/2);
00111   a_arcp.resize(n/2);
00112   TEST_COMPARE_ARRAYS(a, a_arcp);
00113 
00114   out << "\nChecking resize(0) ...\n"; 
00115   a.resize(0);
00116   a_arcp.resize(0);
00117   TEST_COMPARE_ARRAYS(a, a_arcp);
00118 
00119 #ifdef TEUCHOS_DEBUG
00120   a_arcp = arcp<T>(n);
00121   ++a_arcp;
00122   TEST_THROW(a_arcp.resize(1), std::out_of_range);
00123 #endif
00124 }
00125 
00126 
00127 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, clear, T )
00128 {
00129   ArrayRCP<T> a_arcp = arcp<T>(n);
00130   TEST_EQUALITY( a_arcp.size(), n );
00131   a_arcp.clear();
00132   TEST_EQUALITY( a_arcp.size(), 0 );
00133 }
00134 
00135 
00136 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, nullIterator, T )
00137 {
00138   typedef ArrayRCP<T> iter_t;
00139   ArrayRCP<T> arcp1 = Teuchos::NullIteratorTraits<iter_t>::getNull();
00140   TEST_EQUALITY_CONST(arcp1, Teuchos::null); 
00141 }
00142 
00143 
00144 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, implicitConversions, T )
00145 {
00146 
00147   ECHO(ArrayRCP<T> arcp1 = arcp<T>(n));
00148   ECHO(ArrayRCP<const T> arcp2 = arcp1);
00149   TEST_ASSERT(arcp1.shares_resource(arcp2));
00150 
00151 }
00152 
00153 
00154 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, weakDelete, T )
00155 {
00156 
00157   ECHO(ArrayRCP<T> arcp_strong = arcp<T>(n));
00158 
00159   TEST_EQUALITY_CONST( arcp_strong.strength(), RCP_STRONG );
00160   TEST_EQUALITY_CONST( arcp_strong.is_null(), false );
00161   TEST_EQUALITY_CONST( arcp_strong.strong_count(), 1 );
00162   TEST_EQUALITY_CONST( arcp_strong.weak_count(), 0 );
00163   TEST_EQUALITY_CONST( arcp_strong.total_count(), 1 );
00164 
00165   ECHO(ArrayRCP<T> arcp_weak1 = arcp_strong.create_weak());
00166 
00167   TEST_EQUALITY_CONST( arcp_weak1.strength(), RCP_WEAK );
00168   TEST_EQUALITY_CONST( arcp_weak1.is_null(), false );
00169   TEST_EQUALITY_CONST( arcp_weak1.strong_count(), 1 );
00170   TEST_EQUALITY_CONST( arcp_weak1.weak_count(), 1 );
00171   TEST_EQUALITY_CONST( arcp_weak1.total_count(), 2 );
00172 
00173   TEST_EQUALITY_CONST( arcp_strong.strong_count(), 1 );
00174   TEST_EQUALITY_CONST( arcp_strong.is_null(), false );
00175   TEST_EQUALITY_CONST( arcp_strong.weak_count(), 1 );
00176   TEST_EQUALITY_CONST( arcp_strong.total_count(), 2 );
00177 
00178   TEST_EQUALITY_CONST( arcp_weak1.shares_resource(arcp_strong), true );
00179 
00180   TEST_EQUALITY( arcp_weak1.get(), arcp_weak1.getRawPtr() );
00181   TEST_EQUALITY( arcp_weak1.get(), arcp_strong.get() );
00182   TEST_EQUALITY( arcp_weak1.getRawPtr(), arcp_strong.getRawPtr() );
00183 
00184   ECHO(ArrayRCP<T> arcp_weak2 = arcp_weak1);
00185 
00186   TEST_EQUALITY_CONST( arcp_weak2.strength(), RCP_WEAK );
00187   TEST_EQUALITY_CONST( arcp_weak2.is_null(), false );
00188   TEST_EQUALITY_CONST( arcp_weak2.strong_count(), 1 );
00189   TEST_EQUALITY_CONST( arcp_weak2.weak_count(), 2 );
00190   TEST_EQUALITY_CONST( arcp_weak2.total_count(), 3 );
00191 
00192   TEST_EQUALITY_CONST( arcp_strong.strong_count(), 1 );
00193   TEST_EQUALITY_CONST( arcp_strong.is_null(), false );
00194   TEST_EQUALITY_CONST( arcp_strong.weak_count(), 2 );
00195   TEST_EQUALITY_CONST( arcp_strong.total_count(), 3 );
00196 
00197   TEST_EQUALITY_CONST( arcp_weak1.shares_resource(arcp_strong), true );
00198   TEST_EQUALITY_CONST( arcp_weak1.shares_resource(arcp_weak2), true );
00199   TEST_EQUALITY_CONST( arcp_weak2.shares_resource(arcp_strong), true );
00200 
00201   TEST_EQUALITY( arcp_weak2.get(), arcp_strong.get() );
00202   TEST_EQUALITY( arcp_weak2.getRawPtr(), arcp_strong.getRawPtr() );
00203 
00204   ECHO(arcp_strong = null); // This deletes the underlying object of type T!
00205 
00206   TEST_EQUALITY_CONST( arcp_strong.strength(), RCP_STRENGTH_INVALID );
00207   TEST_EQUALITY_CONST( arcp_strong.is_null(), true );
00208   TEST_EQUALITY_CONST( arcp_strong.count(), 0 );
00209   TEST_EQUALITY_CONST( arcp_strong.strong_count(), 0 );
00210   TEST_EQUALITY_CONST( arcp_strong.weak_count(), 0 );
00211   TEST_EQUALITY_CONST( arcp_strong.total_count(), 0 );
00212   TEST_EQUALITY_CONST( arcp_strong.is_valid_ptr(), true );
00213 
00214   TEST_EQUALITY_CONST( arcp_strong.shares_resource(arcp_weak1), false );
00215   TEST_EQUALITY_CONST( arcp_strong.shares_resource(arcp_weak2), false );
00216 
00217   TEST_EQUALITY_CONST( arcp_weak1.has_ownership(), true );
00218   TEST_EQUALITY_CONST( arcp_weak1.count(), 0 );
00219   TEST_EQUALITY_CONST( arcp_weak1.strong_count(), 0 );
00220   TEST_EQUALITY_CONST( arcp_weak1.weak_count(), 2 );
00221   TEST_EQUALITY_CONST( arcp_weak1.total_count(), 2 );
00222   TEST_EQUALITY_CONST( arcp_weak1.is_valid_ptr(), false );
00223 
00224   TEST_EQUALITY_CONST( arcp_weak2.has_ownership(), true );
00225   TEST_EQUALITY_CONST( arcp_weak2.count(), 0 );
00226   TEST_EQUALITY_CONST( arcp_weak2.strong_count(), 0 );
00227   TEST_EQUALITY_CONST( arcp_weak2.weak_count(), 2 );
00228   TEST_EQUALITY_CONST( arcp_weak2.total_count(), 2 );
00229   TEST_EQUALITY_CONST( arcp_weak2.is_valid_ptr(), false );
00230 
00231   TEST_EQUALITY_CONST( arcp_weak1.shares_resource(arcp_weak2), true );
00232 
00233   ECHO(arcp_weak1.assert_not_null()); // Does not throw!
00234   ECHO(arcp_weak2.assert_not_null()); // Does not throw!
00235 
00236   TEST_THROW( arcp_weak1.assert_valid_ptr(), DanglingReferenceError );
00237 #ifdef TEUCHOS_DEBUG
00238   TEST_THROW( arcp_weak1.operator->(), DanglingReferenceError );
00239   TEST_THROW( *arcp_weak1, DanglingReferenceError );
00240   TEST_THROW( arcp_weak1.create_weak(), DanglingReferenceError );
00241   TEST_THROW( arcp_weak1.get(), DanglingReferenceError );
00242   TEST_THROW( arcp_weak1.getRawPtr(), DanglingReferenceError );
00243   TEST_THROW( arcp_weak1[0], DanglingReferenceError );
00244   TEST_THROW( ++arcp_weak1, DanglingReferenceError );
00245   TEST_THROW( arcp_weak1++, DanglingReferenceError );
00246   TEST_THROW( --arcp_weak1, DanglingReferenceError );
00247   TEST_THROW( arcp_weak1--, DanglingReferenceError );
00248   TEST_THROW( arcp_weak1+=1, DanglingReferenceError );
00249   TEST_THROW( arcp_weak1-=1, DanglingReferenceError );
00250   TEST_THROW( arcp_weak1+1, DanglingReferenceError );
00251   TEST_THROW( arcp_weak1-1, DanglingReferenceError );
00252   TEST_THROW( arcp_weak1.getConst(), DanglingReferenceError );
00253   TEST_THROW( arcp_weak1.persistingView(0,n), DanglingReferenceError );
00254   TEST_THROW( arcp_weak1.lowerOffset(), DanglingReferenceError );
00255   TEST_THROW( arcp_weak1.upperOffset(), DanglingReferenceError );
00256   TEST_THROW( arcp_weak1.size(), DanglingReferenceError );
00257   TEST_THROW( arcp_weak1.begin(), DanglingReferenceError );
00258   TEST_THROW( arcp_weak1.end(), DanglingReferenceError );
00259   TEST_THROW( arcp_weak1.view(0,n), DanglingReferenceError );
00260   TEST_THROW( arcp_weak1(0,n), DanglingReferenceError );
00261   TEST_THROW( arcp_weak1(), DanglingReferenceError );
00262   TEST_THROW( {ArrayView<T> av = arcp_weak1;}, DanglingReferenceError );
00263   TEST_THROW( {ArrayRCP<const T> ap = getConst(arcp_weak1);},
00264     DanglingReferenceError );
00265   TEST_THROW( arcp_weak1.release(), DanglingReferenceError );
00266 #endif // TEUCHOS_DEBUG
00267 
00268   ECHO(arcp_weak1 = null); // Just deicrements weak count!
00269 
00270   TEST_EQUALITY_CONST( arcp_weak1.strength(), RCP_STRENGTH_INVALID );
00271   TEST_EQUALITY_CONST( arcp_weak1.is_null(), true );
00272   TEST_EQUALITY_CONST( arcp_weak1.count(), 0 );
00273   TEST_EQUALITY_CONST( arcp_weak1.strong_count(), 0 );
00274   TEST_EQUALITY_CONST( arcp_weak1.weak_count(), 0 );
00275   TEST_EQUALITY_CONST( arcp_weak1.total_count(), 0 );
00276   TEST_EQUALITY_CONST( arcp_weak1.is_valid_ptr(), true );
00277 
00278   TEST_EQUALITY_CONST( arcp_weak2.has_ownership(), true );
00279   TEST_EQUALITY_CONST( arcp_weak2.count(), 0 );
00280   TEST_EQUALITY_CONST( arcp_weak2.strong_count(), 0 );
00281   TEST_EQUALITY_CONST( arcp_weak2.weak_count(), 1 );
00282   TEST_EQUALITY_CONST( arcp_weak2.total_count(), 1 );
00283   TEST_EQUALITY_CONST( arcp_weak2.is_valid_ptr(), false );
00284 
00285   TEST_EQUALITY_CONST( arcp_weak1.shares_resource(arcp_weak2), false );
00286 
00287   TEST_THROW( arcp_weak2.assert_valid_ptr(), DanglingReferenceError );
00288 #ifdef TEUCHOS_DEBUG
00289   // ToDo: Fill in
00290 #endif // TEUCHOS_DEBUG
00291 
00292 }
00293 
00294 
00295 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, danglingArrayView, T )
00296 {
00297   ArrayView<T> av;
00298   {
00299     ArrayRCP<T> arcp1 = arcp<T>(n);
00300     av = arcp1();
00301   }
00302 #ifdef TEUCHOS_DEBUG
00303   TEST_THROW( av.size(), DanglingReferenceError );
00304   TEST_THROW( av.toString(), DanglingReferenceError );
00305   TEST_THROW( av.getRawPtr(), DanglingReferenceError );
00306   TEST_THROW( av[0], DanglingReferenceError );
00307   TEST_THROW( av.front(), DanglingReferenceError );
00308   TEST_THROW( av.back(), DanglingReferenceError );
00309   TEST_THROW( av.view(0, n), DanglingReferenceError );
00310   TEST_THROW( av(0, n), DanglingReferenceError );
00311   TEST_THROW( av(), DanglingReferenceError );
00312   TEST_THROW( av.getConst(), DanglingReferenceError );
00313   TEST_THROW( av.begin(), DanglingReferenceError );
00314   TEST_THROW( av.end(), DanglingReferenceError );
00315 #endif  
00316 }
00317 
00318 
00319 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, getRawPtr, T )
00320 {
00321   ArrayRCP<const T> cptr;
00322   ArrayRCP<T> ptr;
00323   TEST_EQUALITY_CONST( getRawPtr(cptr), (const T*)NULL );
00324   TEST_EQUALITY_CONST( getRawPtr(ptr), (T*)NULL );
00325   cptr = arcp<T>(n);
00326   ptr  = arcp<T>(n);
00327   TEST_EQUALITY( getRawPtr(cptr), &cptr[0]);
00328   TEST_EQUALITY( getRawPtr(ptr),  &ptr[0] );
00329 }
00330 
00331 
00332 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( CPtr, getRawPtr, T )
00333 {
00334   const T *cptr = NULL;
00335   T *ptr = NULL;
00336   TEST_EQUALITY_CONST( getRawPtr(cptr), (const T*)NULL );
00337   TEST_EQUALITY_CONST( getRawPtr(ptr),  (T*)NULL );
00338   cptr = new T[n];
00339   ptr  = new T[n];
00340   TEST_EQUALITY( getRawPtr(cptr), &cptr[0]);
00341   TEST_EQUALITY( getRawPtr(ptr),  &ptr[0] );
00342   delete [] cptr;
00343   delete [] ptr;
00344 }
00345 
00346 
00347 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, arcp_zero, T )
00348 {
00349   ArrayRCP<T> arcp_strong = arcp<T>(0);
00350   TEST_EQUALITY(arcp_strong.size(), as<Ordinal>(0));
00351 }
00352 
00353 
00354 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, arcpFromArrayView, T )
00355 {
00356   Array<T> a = generateArray<T>(n);
00357   ArrayView<T> av = a;
00358   ArrayRCP<T> arcp1 = Teuchos::arcpFromArrayView(av);
00359   TEST_COMPARE_ARRAYS(arcp1, av);
00360 }
00361 
00362 
00363 #ifdef TEUCHOS_DEBUG
00364 
00365 
00366 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, arcp_neg, T )
00367 {
00368   TEST_THROW(ArrayRCP<T> arcp_strong = arcp<T>(-1),
00369     std::out_of_range);
00370 }
00371 
00372 
00373 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayRCP, outOfBounds, T )
00374 {
00375   ECHO(ArrayRCP<T> arcp1 = arcp<T>(n));
00376   TEST_THROW(arcp1(-1,n), RangeError);
00377   TEST_THROW(arcp1(0,n+1), RangeError);
00378   TEST_THROW(arcp1(0,-1), RangeError);
00379   TEST_THROW(arcp1.view(-1,n), RangeError);
00380   TEST_THROW(arcp1.view(0,n+1), RangeError);
00381   TEST_THROW(arcp1.view(0,-1), RangeError);
00382   TEST_THROW(arcp1.persistingView(-1,n), RangeError);
00383   TEST_THROW(arcp1.persistingView(0,n+1), RangeError);
00384   TEST_THROW(arcp1.persistingView(0,-1), RangeError);
00385 }
00386 
00387 
00388 #endif // TEUCHOS_DEBUG
00389 
00390 //
00391 // Template Instantiations
00392 //
00393 
00394 
00395 #ifdef TEUCHOS_DEBUG
00396 
00397 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00398   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, arcp_neg, T ) \
00399   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, outOfBounds, T ) \
00400 
00401 #else
00402 
00403 #  define DEBUG_UNIT_TEST_GROUP( T )
00404 
00405 #endif
00406 
00407 
00408 #define UNIT_TEST_GROUP( T ) \
00409   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, construct_n, T ) \
00410   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, assignSelf, T ) \
00411   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, assign_n_val, T ) \
00412   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, assign_begin_end, T ) \
00413   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, deepCopy, T ) \
00414   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, resize, T ) \
00415   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, clear, T ) \
00416   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, nullIterator, T ) \
00417   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, implicitConversions, T ) \
00418   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, weakDelete, T ) \
00419   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, danglingArrayView, T ) \
00420   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, getRawPtr, T) \
00421   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( CPtr, getRawPtr, T) \
00422   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, arcp_zero, T ) \
00423   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayRCP, arcpFromArrayView, T ) \
00424   DEBUG_UNIT_TEST_GROUP(T)
00425 
00426 
00427 UNIT_TEST_GROUP(int)
00428 UNIT_TEST_GROUP(double)
00429 UNIT_TEST_GROUP(float)
00430 
00431 
00432 //
00433 // Non templated unit tests
00434 //
00435 
00436 
00437 TEUCHOS_UNIT_TEST( ArrayRCP, memberPointer )
00438 {
00439   ArrayRCP<A> a_arcp = arcp<A>(1);
00440   TEST_EQUALITY_CONST( a_arcp->A_f(), A_f_return );
00441 }
00442 
00443 
00444 TEUCHOS_UNIT_TEST( ArrayRCP, getConst_null )
00445 {
00446   const ArrayRCP<A> a1_arcp;
00447   const ArrayRCP<const A> a2_arcp = a1_arcp.getConst();
00448   TEST_ASSERT(is_null(a2_arcp));
00449 }
00450 
00451 
00452 TEUCHOS_UNIT_TEST( ArrayRCP, operator_parenth_ArrayView_null )
00453 {
00454   const ArrayRCP<A> a_arcp;
00455   const ArrayView<A> av = a_arcp();
00456   TEST_ASSERT(is_null(av));
00457 }
00458 
00459 
00460 TEUCHOS_UNIT_TEST( ArrayRCP, operator_parenth_ArrayView_const_null )
00461 {
00462   const ArrayRCP<const A> a_arcp;
00463   const ArrayView<const A> av = a_arcp();
00464   TEST_ASSERT(is_null(av));
00465 }
00466 
00467 
00468 TEUCHOS_UNIT_TEST( ArrayRCP, implicit_ArrayRCP_const )
00469 {
00470   const ArrayRCP<A> a_arcp;
00471   const ArrayRCP<const A> ac_arcp = a_arcp;
00472   TEST_ASSERT(is_null(ac_arcp));
00473 }
00474 
00475 
00476 TEUCHOS_UNIT_TEST( ArrayRCP, release )
00477 {
00478   ArrayRCP<A> a_arcp = arcp<A>(1);
00479   delete [] a_arcp.release();
00480 }
00481 
00482 
00483 TEUCHOS_UNIT_TEST( ArrayRCP, arcp_null )
00484 {
00485   ArrayRCP<A> a_arcp = arcp<A>(0, 0, -1, false);
00486   TEST_ASSERT(is_null(a_arcp));
00487 }
00488 
00489 
00490 TEUCHOS_UNIT_TEST( ArrayRCP, arcp_dealloc_null )
00491 {
00492   ArrayRCP<A> a_arcp = arcp<A, Teuchos::DeallocNull<A> >(0, 0, -1,
00493     Teuchos::DeallocNull<A>(), false);
00494   TEST_ASSERT(is_null(a_arcp));
00495 }
00496 
00497 
00498 TEUCHOS_UNIT_TEST( ArrayRCP, convert_from_vector_null )
00499 {
00500   const RCP<std::vector<int> > v_rcp;
00501   const ArrayRCP<int> a_arcp = arcp(v_rcp);
00502   TEST_ASSERT(is_null(a_arcp));
00503 }
00504 
00505 
00506 TEUCHOS_UNIT_TEST( ArrayRCP, convert_from_const_vector_null )
00507 {
00508   const RCP<const std::vector<int> > v_rcp;
00509   const ArrayRCP<const int> a_arcp = arcp(v_rcp);
00510   TEST_ASSERT(is_null(a_arcp));
00511 }
00512 
00513 
00514 TEUCHOS_UNIT_TEST( ArrayRCP, convert_from_vector_unsized )
00515 {
00516   const RCP<std::vector<int> > v_rcp = rcp(new std::vector<int>);
00517   const ArrayRCP<int> a_arcp = arcp(v_rcp);
00518   TEST_ASSERT(is_null(a_arcp));
00519 }
00520 
00521 
00522 TEUCHOS_UNIT_TEST( ArrayRCP, convert_from_const_vector_unsized )
00523 {
00524   const RCP<const std::vector<int> > v_rcp = rcp(new std::vector<int>);
00525   const ArrayRCP<const int> a_arcp = arcp(v_rcp);
00526   TEST_ASSERT(is_null(a_arcp));
00527 }
00528 
00529 
00530 TEUCHOS_UNIT_TEST( ArrayRCP, arcpWithEmbeddedObj )
00531 {
00532   const ArrayRCP<const int> a_arcp =
00533     Teuchos::arcpWithEmbeddedObj<int>(new int[1], 0, 1, as<int>(1), true);
00534   const int embeddedObj = Teuchos::getEmbeddedObj<int,int>(a_arcp); 
00535   TEST_EQUALITY_CONST( embeddedObj, as<int>(1) );
00536 }
00537 
00538 
00539 TEUCHOS_UNIT_TEST( ArrayRCP, nonnull )
00540 {
00541   ECHO(ArrayRCP<int> a_arcp = arcp<int>(10));
00542   TEST_EQUALITY_CONST(is_null(a_arcp), false);
00543   TEST_EQUALITY_CONST(nonnull(a_arcp), true);
00544   ECHO(a_arcp = null);
00545   TEST_EQUALITY_CONST(is_null(a_arcp), true);
00546   TEST_EQUALITY_CONST(nonnull(a_arcp), false);
00547 }
00548 
00549 
00550 TEUCHOS_UNIT_TEST( ArrayRCP, weak_strong )
00551 {
00552 
00553   ECHO(ArrayRCP<int> arcp1 = arcp<int>(10));
00554   TEST_EQUALITY_CONST( arcp1.strength(), RCP_STRONG );
00555 
00556   ECHO(ArrayRCP<int> arcp2 = arcp1.create_weak());
00557 
00558   TEST_EQUALITY_CONST( arcp2.strength(), RCP_WEAK );
00559   TEST_EQUALITY_CONST( arcp1.strong_count(), 1 );
00560   TEST_EQUALITY_CONST( arcp1.weak_count(), 1 );
00561   TEST_EQUALITY_CONST( arcp2.strong_count(), 1 );
00562   TEST_EQUALITY_CONST( arcp2.weak_count(), 1 );
00563 
00564   ECHO(ArrayRCP<int> arcp3 = arcp2.create_strong());
00565 
00566   TEST_EQUALITY_CONST( arcp3.strength(), RCP_STRONG );
00567   TEST_EQUALITY_CONST( arcp1.strong_count(), 2 );
00568   TEST_EQUALITY_CONST( arcp1.weak_count(), 1 );
00569   TEST_EQUALITY_CONST( arcp2.strong_count(), 2 );
00570   TEST_EQUALITY_CONST( arcp2.weak_count(), 1 );
00571 
00572   // This will make the underlying object A gets deleted!
00573   ECHO(arcp1 = null);
00574   ECHO(arcp3 = null);
00575 
00576   ECHO(arcp2 = null); // Should make the underlying node go away
00577 
00578 }
00579 
00580 
00581 TEUCHOS_UNIT_TEST( ArrayRCP, arcp_reinterpret_cast_null )
00582 {
00583   ECHO(ArrayRCP<char> arcp_char = null);
00584   ECHO(ArrayRCP<int> arcp_int = arcp_reinterpret_cast<int>(arcp_char));
00585   TEST_EQUALITY_CONST(arcp_int, null);
00586 }
00587 
00588 
00589 TEUCHOS_UNIT_TEST( ArrayRCP, arcp_reinterpret_cast_char_to_int )
00590 {
00591 
00592   const int sizeOfInt = sizeof(int);
00593   const int sizeOfChar = sizeof(char);
00594   const int num_ints = n;
00595   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00596   out << "num_ints = " << num_ints << "\n";
00597   out << "num_chars = " << num_chars << "\n";
00598 
00599   ECHO(ArrayRCP<char> arcp_char = arcp<char>(num_chars));
00600   ECHO(ArrayRCP<int> arcp_int = arcp_reinterpret_cast<int>(arcp_char));
00601   TEST_EQUALITY(arcp_int.size(), num_ints);
00602   TEST_EQUALITY(implicit_ptr_cast<void>(&arcp_int[0]),
00603     implicit_ptr_cast<void>(&arcp_char[0]));
00604   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00605     implicit_ptr_cast<void>((&arcp_char[num_chars-1])+1));
00606 
00607   ECHO(arcp_char+=sizeOfInt);
00608   ECHO(arcp_int = arcp_reinterpret_cast<int>(arcp_char));
00609   TEST_EQUALITY(arcp_int.size(), num_ints);
00610   TEST_EQUALITY_CONST( arcp_int.lowerOffset(), -1);
00611   TEST_EQUALITY( arcp_int.upperOffset(), num_ints-2);
00612   TEST_EQUALITY( implicit_ptr_cast<void>(&arcp_int[-1]),
00613     implicit_ptr_cast<void>(&arcp_char[-sizeOfInt])
00614     );
00615   TEST_EQUALITY( implicit_ptr_cast<void>((&arcp_int[num_ints-2])+1),
00616     implicit_ptr_cast<void>((&arcp_char[num_chars-1-sizeOfInt])+1));
00617 
00618 }
00619 
00620 
00621 TEUCHOS_UNIT_TEST( ArrayRCP, arcp_reinterpret_cast_int_to_char )
00622 {
00623 
00624   const int sizeOfInt = sizeof(int);
00625   const int sizeOfChar = sizeof(char);
00626   const int num_ints = n;
00627   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00628   out << "num_ints = " << num_ints << "\n";
00629   out << "num_chars = " << num_chars << "\n";
00630 
00631   ECHO(ArrayRCP<int> arcp_int = arcp<int>(num_ints));
00632   ECHO(ArrayRCP<char> arcp_char = arcp_reinterpret_cast<char>(arcp_int));
00633   TEST_EQUALITY(arcp_char.size(), num_chars);
00634   TEST_EQUALITY(implicit_ptr_cast<void>(&arcp_int[0]),
00635     implicit_ptr_cast<void>(&arcp_char[0]));
00636   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00637     implicit_ptr_cast<void>((&arcp_char[num_chars-1])+1));
00638   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00639     implicit_ptr_cast<void>((&arcp_char[num_chars-1])+1));
00640 
00641   ECHO(++arcp_int);
00642   ECHO(arcp_char = arcp_reinterpret_cast<char>(arcp_int));
00643   TEST_EQUALITY(as<int>(arcp_char.lowerOffset()), as<int>(-sizeOfInt));
00644   TEST_EQUALITY(as<int>(arcp_char.upperOffset()), as<int>(num_chars-1-sizeOfInt));
00645   TEST_EQUALITY(implicit_ptr_cast<void>(&arcp_int[-1]),
00646     implicit_ptr_cast<void>(&arcp_char[-sizeOfInt]));
00647   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-2])+1),
00648     implicit_ptr_cast<void>((&arcp_char[num_chars-1-sizeOfInt])+1));
00649 
00650 }
00651 
00652 
00653 TEUCHOS_UNIT_TEST( ArrayRCP, evil_reinterpret_cast )
00654 {
00655   ECHO(ArrayRCP<ArrayRCP<int> > arcp1 = arcp<ArrayRCP<int> >(n));
00656   ECHO(ArrayRCP<ArrayRCP<const int> > arcp2 =
00657     arcp_reinterpret_cast<ArrayRCP<const int> >(arcp1));
00658   TEST_EQUALITY(arcp2.size(), arcp1.size());
00659   TEST_EQUALITY(implicit_ptr_cast<const void>(&arcp1[0]),
00660     implicit_ptr_cast<const void>(&arcp2[0]));
00661   ECHO(ArrayRCP<const ArrayRCP<const int> > arcp3 = arcp2);
00662   TEST_EQUALITY(arcp3.size(), arcp1.size());
00663   TEST_EQUALITY(implicit_ptr_cast<const void>(&arcp1[0]),
00664     implicit_ptr_cast<const void>(&arcp3[0]));
00665   out << "arcp3 = " << arcp3 << "\n";
00666 }
00667 
00668 
00669 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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