Array_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Array_UnitTest_helpers.hpp"
00002 #include "Teuchos_Tuple.hpp"
00003 #include "Teuchos_ArrayRCP.hpp"
00004 
00005 
00006 namespace {
00007 
00008 
00009 using ArrayUnitTestHelpers::n;
00010 using ArrayUnitTestHelpers::generateArray;
00011 using Teuchos::null;
00012 using Teuchos::tuple;
00013 using Teuchos::RCP;
00014 using Teuchos::Array;
00015 using Teuchos::ArrayView;
00016 using Teuchos::ArrayRCP;
00017 using Teuchos::arcp;
00018 using Teuchos::arcpFromArray;
00019 using Teuchos::as;
00020 using Teuchos::getConst;
00021 using Teuchos::DanglingReferenceError;
00022 using Teuchos::fromStringToArray;
00023 using Teuchos::InvalidArrayStringRepresentation;
00024 
00025 
00026 TEUCHOS_UNIT_TEST( Array, stringToArray )
00027 {
00028 
00029   {
00030     Array<std::string> arrayVal = fromStringToArray<std::string>("{}");
00031     Array<std::string> arrayVal_exp;
00032     TEST_EQUALITY(arrayVal, arrayVal_exp);
00033   }
00034 
00035   {
00036     Array<std::string> arrayVal = fromStringToArray<std::string>("{ a, b, c, d }");
00037     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("a", "b", "c", "d" );
00038     TEST_EQUALITY(arrayVal, arrayVal_exp);
00039   }
00040 
00041   {
00042     Array<std::string> arrayVal = fromStringToArray<std::string>("{ (a), b, c, (d) }");
00043     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("(a)", "b", "c", "(d)" );
00044     TEST_EQUALITY(arrayVal, arrayVal_exp);
00045   }
00046 
00047   {
00048     Array<std::string> arrayVal = fromStringToArray<std::string>("{ (a ), b, c, (d ) }");
00049     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("(a )", "b", "c", "(d )" );
00050     TEST_EQUALITY(arrayVal, arrayVal_exp);
00051   }
00052 
00053   // This should work but does not.  I should fix this!
00054 //  {
00055 //    Array<std::string> arrayVal = fromStringToArray<std::string>("{ {a}, 'b', {c }, d }");
00056 //    Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("{a}", "'b'", "{c }", "d" );
00057 //    TEST_EQUALITY(arrayVal, arrayVal_exp);
00058 //  }
00059 
00060 }
00061 
00062 
00063 TEUCHOS_UNIT_TEST( Array, stringToArray_invalid )
00064 {
00065   TEST_THROW(fromStringToArray<std::string>("{ a, b, c"),
00066     InvalidArrayStringRepresentation);
00067   TEST_THROW(fromStringToArray<std::string>("a, b, c}"),
00068     InvalidArrayStringRepresentation);
00069   TEST_THROW(fromStringToArray<std::string>("a, b, c"),
00070     InvalidArrayStringRepresentation);
00071 }
00072 
00073 
00074 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, defaultConstruct, T )
00075 {
00076   Array<T> a2;
00077   TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
00078   TEST_EQUALITY_CONST( as<int>(a2.empty()), true );
00079   TEST_EQUALITY_CONST( a2.getRawPtr(), 0 );
00080   TEST_EQUALITY_CONST( getConst(a2).getRawPtr(), 0 );
00081 }
00082 
00083 
00084 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, sizedConstruct, T )
00085 {
00086   typedef typename Array<T>::size_type size_type;
00087   Array<T> a(n);
00088   TEST_EQUALITY_CONST( a.empty(), false );
00089   TEST_EQUALITY( a.length(), n );
00090   TEST_EQUALITY( as<int>(a.size()), n );
00091   TEST_EQUALITY( a.getRawPtr(), &a[0] );
00092   TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
00093   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00094   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00095 }
00096 
00097 
00098 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, operatorBracket, T )
00099 {
00100   out << "\nTest that a[i] == i ... ";
00101   Array<T> a = generateArray<T>(n);
00102   bool local_success = true;
00103   for( int i = 0; i < n; ++i ) {
00104     TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
00105   }
00106   if (local_success) out << "passed\n";
00107   else success = false;
00108 }
00109 
00110 
00111 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, constAt, T )
00112 {
00113   out << "\nTest that a.at(i) == i ...\n";
00114   Array<T> a = generateArray<T>(n);
00115   bool local_success = true;
00116   for( int i = 0; i < n; ++i ) {
00117     TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
00118   }
00119   if (local_success) out << "passed\n";
00120   else success = false;
00121 }
00122 
00123 
00124 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter_before_block_end, T )
00125 {
00126   typedef typename ArrayView<T>::iterator iter_t;
00127   typedef Teuchos::NullIteratorTraits<iter_t> NIT;
00128   iter_t iter = NIT::getNull();
00129   {
00130     ECHO(Array<T> a(n, as<T>(0)));
00131     ECHO(ArrayView<T> av = a);
00132     ECHO(iter = av.begin());
00133     ECHO(av = null);
00134     TEST_EQUALITY( *iter, a[0] );
00135     // Above, the iterator to the ArrayView object is still valid even through
00136     // the ArrayView object is was created from is gone now.  This is just
00137     // fine since the underlying data is still there in the original Array object.
00138     iter = NIT::getNull();
00139   }
00140 }
00141 
00142 
00143 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPArray_to_ArrayRCP_null, T )
00144 {
00145   const RCP<Array<T> > a_rcp = null;
00146   const ArrayRCP<T> a_arcp = arcp(a_rcp);
00147   TEST_ASSERT( a_arcp == null );
00148 }
00149 
00150 
00151 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPconstArray_to_ArrayRCP_null, T )
00152 {
00153   const RCP<const Array<T> > a_rcp = null;
00154   const ArrayRCP<const T> a_arcp = arcp(a_rcp);
00155   TEST_ASSERT( a_arcp == null );
00156 }
00157 
00158 
00159 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPArray_to_ArrayRCP, T )
00160 {
00161   const Array<T> a_const = generateArray<T>(n);
00162   const RCP<Array<T> > a_rcp = Teuchos::rcp( new  Array<T>(a_const));
00163   const ArrayRCP<T> a_arcp = arcp(a_rcp);
00164   TEST_COMPARE_ARRAYS( a_const(), a_arcp() );
00165 }
00166 
00167 
00168 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPconstArray_to_ArrayRCP, T )
00169 {
00170   const Array<T> a_const = generateArray<T>(n);
00171   const RCP<const Array<T> > a_rcp = Teuchos::rcp( new  Array<T>(a_const));
00172   const ArrayRCP<const T> a_arcp = arcp(a_rcp);
00173   TEST_COMPARE_ARRAYS( a_const(), a_arcp() );
00174 }
00175 
00176 
00177 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, Array_to_ArrayRCP_null, T )
00178 {
00179   Array<T> a;
00180   const ArrayRCP<T> a_arcp = arcpFromArray(a);
00181   TEST_ASSERT(a_arcp == null);
00182 }
00183 
00184 
00185 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, constArray_to_ArrayRCP_null, T )
00186 {
00187   const Array<T> a;
00188   const ArrayRCP<const T> a_arcp = arcpFromArray(a);
00189   TEST_ASSERT(a_arcp == null);
00190 }
00191 
00192 
00193 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, Array_to_ArrayRCP, T )
00194 {
00195   Array<T> a = generateArray<T>(n);
00196   const ArrayRCP<T> a_arcp = arcpFromArray(a);
00197   TEST_COMPARE_ARRAYS( a(), a_arcp() );
00198 }
00199 
00200 
00201 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, constArray_to_ArrayRCP, T )
00202 {
00203   const Array<T> a = generateArray<T>(n);
00204   const ArrayRCP<const T> a_arcp = arcpFromArray(a);
00205   TEST_COMPARE_ARRAYS( a(), a_arcp() );
00206 }
00207 
00208 
00209 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, Array_to_ArrayRCP_dangling, T )
00210 {
00211   ArrayRCP<T> a_arcp;
00212   {
00213     Array<T> a = generateArray<T>(n);
00214     a_arcp = arcpFromArray(a);
00215   }
00216 #ifdef TEUCHOS_DEBUG
00217   TEST_THROW(a_arcp[0], DanglingReferenceError);
00218 #endif
00219 }
00220 
00221 
00222 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, constArray_to_ArrayRCP_dangling, T )
00223 {
00224   ArrayRCP<const T> a_arcp;
00225   {
00226     const Array<T> a = generateArray<T>(n);
00227     a_arcp = arcpFromArray(a);
00228   }
00229 #ifdef TEUCHOS_DEBUG
00230   TEST_THROW(a_arcp[0], DanglingReferenceError);
00231 #endif
00232 }
00233 
00234 
00235 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, toVector, T )
00236 {
00237   const Array<T> a = generateArray<T>(n);
00238   const std::vector<T> v = a.toVector();
00239   TEST_COMPARE_ARRAYS( a, v );
00240 }
00241 
00242 
00243 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, toVector_empty, T )
00244 {
00245   const Array<T> a;
00246   const std::vector<T> v = a.toVector();
00247   TEST_EQUALITY_CONST( as<int>(v.size()), 0 );
00248 }
00249 
00250 
00251 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, view_empty, T )
00252 {
00253   Array<T> a;
00254   const ArrayView<T> av = a.view(0, 0);
00255   TEST_ASSERT(is_null(av));
00256 }
00257 
00258 
00259 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, view_const_empty, T )
00260 {
00261   const Array<T> a;
00262   const ArrayView<const T> av = a.view(0, 0);
00263   TEST_ASSERT(is_null(av));
00264 }
00265 
00266 
00267 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, implicit_to_ArrayView_empty, T )
00268 {
00269   Array<T> a;
00270   const ArrayView<T> av = a();
00271   TEST_ASSERT(is_null(av));
00272 }
00273 
00274 
00275 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, implicit_to_ArrayView_const_empty, T )
00276 {
00277   const Array<T> a;
00278   const ArrayView<const T> av = a();
00279   TEST_ASSERT(is_null(av));
00280 }
00281 
00282 
00283 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_implicit, T )
00284 {
00285   ArrayView<T> av;
00286   { Array<T> a(n); av = a; }
00287 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00288   TEST_THROW( av[0] = 0, DanglingReferenceError );
00289 #endif
00290 }
00291 
00292 
00293 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_implicit_const, T )
00294 {
00295   ArrayView<const T> av;
00296   { Array<T> a(n); av = getConst(a); }
00297 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00298   TEST_THROW( av[0], DanglingReferenceError );
00299 #endif
00300 }
00301 
00302 
00303 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_explicit, T )
00304 {
00305   ArrayView<T> av;
00306   { Array<T> a(n); av = a(); }
00307 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00308   TEST_THROW( av[0] = 0, DanglingReferenceError );
00309 #endif
00310 }
00311 
00312 
00313 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_explicit_const, T )
00314 {
00315   ArrayView<const T> av;
00316   { Array<T> a(n); av = getConst(a)(); }
00317 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00318   TEST_THROW( av[0], DanglingReferenceError );
00319 #endif
00320 }
00321 
00322 
00323 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_subview, T )
00324 {
00325   ArrayView<T> av;
00326   { Array<T> a(n); av = a(0,1); }
00327 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00328   TEST_THROW( av[0] = 0, DanglingReferenceError );
00329 #endif
00330 }
00331 
00332 
00333 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_subview_const, T )
00334 {
00335   ArrayView<const T> av;
00336   { Array<T> a(n); av = getConst(a)(0,1); }
00337 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00338   TEST_THROW( av[0], DanglingReferenceError );
00339 #endif
00340 }
00341 
00342 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter, T )
00343 {
00344   typedef typename ArrayView<T>::iterator iter_t;
00345   ECHO(Array<T> a(n));
00346   ECHO(ArrayView<T> av = a);
00347   ECHO(iter_t iter = av.begin());
00348   ECHO(av = null);
00349   ECHO(a.resize(0));
00350 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00351   TEST_THROW( *iter = 0, DanglingReferenceError );
00352 #else
00353   (void)iter;
00354 #endif
00355 }
00356 
00357 
00358 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter_const, T )
00359 {
00360   typedef typename ArrayView<const T>::iterator iter_t;
00361   ECHO(Array<T> a(n));
00362   ECHO(ArrayView<T> av = a);
00363   ECHO(iter_t iter = av.begin());
00364   ECHO(av = null);
00365   ECHO(a.resize(0));
00366 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00367   TEST_THROW( *iter, DanglingReferenceError );
00368 #else
00369   (void)iter;
00370 #endif
00371 }
00372 
00373 
00374 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, structuralChangeArrayView, T )
00375 {
00376   Array<T> a = generateArray<T>(n);
00377   ArrayView<T> av = a;
00378   a.push_back(a[0]);
00379 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00380   TEST_THROW( av[0] = 0, DanglingReferenceError );
00381 #endif
00382 }
00383 
00384 
00385 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, structuralChangeArrayView_const, T )
00386 {
00387   Array<T> a = generateArray<T>(n);
00388   ArrayView<const T> av = getConst(a);
00389   a.push_back(a[0]);
00390 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00391   TEST_THROW( av[0], DanglingReferenceError );
00392 #endif
00393 }
00394 
00395 
00396 //
00397 // Instantiations
00398 //
00399 
00400 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00401 
00402 #  define DEBUG_UNIT_TEST_GROUP( T )
00403 
00404 #else // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00405 
00406 #  define DEBUG_UNIT_TEST_GROUP( T )
00407 
00408 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00409 
00410 
00411 #define UNIT_TEST_GROUP( T ) \
00412   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, defaultConstruct, T ) \
00413   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, sizedConstruct, T ) \
00414   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, operatorBracket, T ) \
00415   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, constAt, T ) \
00416   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter_before_block_end, T ) \
00417   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPArray_to_ArrayRCP_null, T ) \
00418   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPconstArray_to_ArrayRCP_null, T ) \
00419   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPArray_to_ArrayRCP, T ) \
00420   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPconstArray_to_ArrayRCP, T ) \
00421   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, Array_to_ArrayRCP_null, T ) \
00422   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, constArray_to_ArrayRCP_null, T ) \
00423   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, Array_to_ArrayRCP, T ) \
00424   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, constArray_to_ArrayRCP, T ) \
00425   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, Array_to_ArrayRCP_dangling, T ) \
00426   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, constArray_to_ArrayRCP_dangling, T ) \
00427   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, toVector, T ) \
00428   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, toVector_empty, T ) \
00429   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, view_empty, T ) \
00430   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, view_const_empty, T ) \
00431   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, implicit_to_ArrayView_empty, T ) \
00432   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_implicit, T ) \
00433   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_implicit_const, T ) \
00434   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_explicit, T ) \
00435   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_explicit_const, T ) \
00436   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_subview, T ) \
00437   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_subview_const, T ) \
00438   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter, T ) \
00439   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter_const, T ) \
00440   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, structuralChangeArrayView, T ) \
00441   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, structuralChangeArrayView_const, T )
00442   DEBUG_UNIT_TEST_GROUP( T )
00443 
00444 UNIT_TEST_GROUP(int)
00445 UNIT_TEST_GROUP(float)
00446 UNIT_TEST_GROUP(double)
00447 
00448 
00449 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:27 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3