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