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::as;
00019 using Teuchos::getConst;
00020 using Teuchos::DanglingReferenceError;
00021 using Teuchos::fromStringToArray;
00022 using Teuchos::InvalidArrayStringRepresentation;
00023 
00024 
00025 TEUCHOS_UNIT_TEST( Array, stringToArray )
00026 {
00027 
00028   {
00029     Array<std::string> arrayVal = fromStringToArray<std::string>("{}");
00030     Array<std::string> arrayVal_exp;
00031     TEST_EQUALITY(arrayVal, arrayVal_exp);
00032   }
00033 
00034   {
00035     Array<std::string> arrayVal = fromStringToArray<std::string>("{ a, b, c, d }");
00036     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("a", "b", "c", "d" );
00037     TEST_EQUALITY(arrayVal, arrayVal_exp);
00038   }
00039 
00040   {
00041     Array<std::string> arrayVal = fromStringToArray<std::string>("{ (a), b, c, (d) }");
00042     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("(a)", "b", "c", "(d)" );
00043     TEST_EQUALITY(arrayVal, arrayVal_exp);
00044   }
00045 
00046   {
00047     Array<std::string> arrayVal = fromStringToArray<std::string>("{ (a ), b, c, (d ) }");
00048     Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("(a )", "b", "c", "(d )" );
00049     TEST_EQUALITY(arrayVal, arrayVal_exp);
00050   }
00051 
00052   // This should work but does not.  I should fix this!
00053 //  {
00054 //    Array<std::string> arrayVal = fromStringToArray<std::string>("{ {a}, 'b', {c }, d }");
00055 //    Array<std::string> arrayVal_exp = Teuchos::tuple<std::string>("{a}", "'b'", "{c }", "d" );
00056 //    TEST_EQUALITY(arrayVal, arrayVal_exp);
00057 //  }
00058 
00059 }
00060 
00061 
00062 TEUCHOS_UNIT_TEST( Array, stringToArray_invalid )
00063 {
00064   TEST_THROW(fromStringToArray<std::string>("{ a, b, c"),
00065     InvalidArrayStringRepresentation);
00066   TEST_THROW(fromStringToArray<std::string>("a, b, c}"),
00067     InvalidArrayStringRepresentation);
00068   TEST_THROW(fromStringToArray<std::string>("a, b, c"),
00069     InvalidArrayStringRepresentation);
00070 }
00071 
00072 
00073 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, defaultConstruct, T )
00074 {
00075   Array<T> a2;
00076   TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
00077   TEST_EQUALITY_CONST( as<int>(a2.empty()), true );
00078   TEST_EQUALITY_CONST( a2.getRawPtr(), 0 );
00079   TEST_EQUALITY_CONST( getConst(a2).getRawPtr(), 0 );
00080 }
00081 
00082 
00083 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, sizedConstruct, T )
00084 {
00085   typedef typename Array<T>::size_type size_type;
00086   Array<T> a(n);
00087   TEST_EQUALITY_CONST( a.empty(), false );
00088   TEST_EQUALITY( a.length(), n );
00089   TEST_EQUALITY( as<int>(a.size()), n );
00090   TEST_EQUALITY( a.getRawPtr(), &a[0] );
00091   TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
00092   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00093   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00094 }
00095 
00096 
00097 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, operatorBracket, T )
00098 {
00099   out << "\nTest that a[i] == i ... ";
00100   Array<T> a = generateArray<T>(n);
00101   bool local_success = true;
00102   for( int i = 0; i < n; ++i ) {
00103     TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
00104   }
00105   if (local_success) out << "passed\n";
00106   else success = false;
00107 }
00108 
00109 
00110 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, constAt, T )
00111 {
00112   out << "\nTest that a.at(i) == i ...\n";
00113   Array<T> a = generateArray<T>(n);
00114   bool local_success = true;
00115   for( int i = 0; i < n; ++i ) {
00116     TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
00117   }
00118   if (local_success) out << "passed\n";
00119   else success = false;
00120 }
00121 
00122 
00123 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter_before_block_end, T )
00124 {
00125   typedef typename ArrayView<T>::iterator iter_t;
00126   typedef Teuchos::NullIteratorTraits<iter_t> NIT;
00127   iter_t iter = NIT::getNull();
00128   {
00129     ECHO(Array<T> a(n, as<T>(0)));
00130     ECHO(ArrayView<T> av = a);
00131     ECHO(iter = av.begin());
00132     ECHO(av = null);
00133     TEST_EQUALITY( *iter, a[0] );
00134     // Above, the iterator to the ArrayView object is still valid even through
00135     // the ArrayView object is was created from is gone now.  This is just
00136     // fine since the underlying data is still there in the original Array object.
00137     iter = NIT::getNull();
00138   }
00139 }
00140 
00141 
00142 
00143 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPArray_to_ArrayRCP, T )
00144 {
00145   const Array<T> a_const = generateArray<T>(n);
00146   const RCP<Array<T> > a_rcp = Teuchos::rcp( new  Array<T>(a_const));
00147   const ArrayRCP<T> a_arcp = arcp(a_rcp);
00148   TEST_COMPARE_ARRAYS( a_const(), a_arcp() );
00149 }
00150 
00151 
00152 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, RCPArray_to_ArrayRCP_null, T )
00153 {
00154   const RCP<Array<T> > a_rcp = null;
00155   const ArrayRCP<T> a_arcp = arcp(a_rcp);
00156   TEST_ASSERT( a_arcp == null );
00157 }
00158 
00159 
00160 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, toVector, T )
00161 {
00162   const Array<T> a = generateArray<T>(n);
00163   const std::vector<T> v = a.toVector();
00164   TEST_COMPARE_ARRAYS( a, v );
00165 }
00166 
00167 
00168 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, toVector_empty, T )
00169 {
00170   const Array<T> a;
00171   const std::vector<T> v = a.toVector();
00172   TEST_EQUALITY_CONST( as<int>(v.size()), 0 );
00173 }
00174 
00175 
00176 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, view_empty, T )
00177 {
00178   Array<T> a;
00179   const ArrayView<T> av = a.view(0, 0);
00180   TEST_ASSERT(is_null(av));
00181 }
00182 
00183 
00184 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, view_const_empty, T )
00185 {
00186   const Array<T> a;
00187   const ArrayView<const T> av = a.view(0, 0);
00188   TEST_ASSERT(is_null(av));
00189 }
00190 
00191 
00192 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, implicit_to_ArrayView_empty, T )
00193 {
00194   Array<T> a;
00195   const ArrayView<T> av = a();
00196   TEST_ASSERT(is_null(av));
00197 }
00198 
00199 
00200 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, implicit_to_ArrayView_const_empty, T )
00201 {
00202   const Array<T> a;
00203   const ArrayView<const T> av = a();
00204   TEST_ASSERT(is_null(av));
00205 }
00206 
00207 
00208 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00209 
00210 
00211 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_implicit, T )
00212 {
00213   ArrayView<T> av;
00214   TEST_THROW( { Array<T> a(n); av = a; },
00215     DanglingReferenceError );
00216 }
00217 
00218 
00219 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_implicit_const, T )
00220 {
00221   ArrayView<const T> av;
00222   TEST_THROW( { Array<T> a(n); av = getConst(a); },
00223     DanglingReferenceError );
00224 }
00225 
00226 
00227 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_explicit, T )
00228 {
00229   ArrayView<T> av;
00230   TEST_THROW( { Array<T> a(n); av = a(); },
00231     DanglingReferenceError );
00232 }
00233 
00234 
00235 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_explicit_const, T )
00236 {
00237   ArrayView<const T> av;
00238   TEST_THROW( { Array<T> a(n); av = getConst(a)(); },
00239     DanglingReferenceError );
00240 }
00241 
00242 
00243 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_subview, T )
00244 {
00245   ArrayView<T> av;
00246   TEST_THROW( { Array<T> a(n); av = a(0,1); },
00247     DanglingReferenceError );
00248 }
00249 
00250 
00251 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayView_subview_const, T )
00252 {
00253   ArrayView<const T> av;
00254   TEST_THROW( { Array<T> a(n); av = getConst(a)(0,1); },
00255     DanglingReferenceError );
00256 }
00257 
00258 
00259 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter, T )
00260 {
00261   typedef typename ArrayView<T>::iterator iter_t;
00262   ECHO(Array<T> a(n));
00263   ECHO(ArrayView<T> av = a);
00264   ECHO(iter_t iter = av.begin());
00265   ECHO(av = null);
00266   TEST_THROW( a.resize(0), DanglingReferenceError );
00267   // The way that Array::resize() is able to detect that there is still a
00268   // dangling iterator comes from the way in which all of this is implemented.
00269   // The reason that this throws is that the RCP<std::vector<T> > object
00270   // embedded in the Array object gets embedded inside of the dealloc object
00271   // which is attached to the node.  Therefore, even though the weak
00272   // ArrayRCP<T> object that was created and embedded in the ArrayView<T>
00273   // object has gone away, this same weak ArrayRCP<T> object was used to
00274   // create another weak ArrayRCP<T> object which *is* the iterator object
00275   // iter above.  What this means is that any reference counted object that
00276   // gets created for whatever reason based on the underlying
00277   // RCP<std::vector<T> > object will result in all the Array functions that
00278   // alter the underlying array memory to throw an exception right away.  I
00279   // think I really like this "early warning" behavior.  The only disadvantage
00280   // is that we don't get a very good error message.  It would be nice to find
00281   // a way so that it was the dangling reference object itself that threw the
00282   // exception message and was able to provide better debug feedback.
00283 }
00284 
00285 
00286 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, danglingArrayViewIter_const, T )
00287 {
00288   typedef typename ArrayView<const T>::iterator iter_t;
00289   ECHO(Array<T> a(n));
00290   ECHO(ArrayView<T> av = a);
00291   ECHO(iter_t iter = av.begin());
00292   ECHO(av = null);
00293   TEST_THROW( a.resize(0), DanglingReferenceError );
00294   // See comments above.
00295 }
00296 
00297 
00298 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, structuralChangeArrayView, T )
00299 {
00300   Array<T> a = generateArray<T>(n);
00301   ArrayView<T> av = a;
00302   TEST_THROW( a.push_back(a[0]), 
00303     DanglingReferenceError );
00304 }
00305 
00306 
00307 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( Array, structuralChangeArrayView_const, T )
00308 {
00309   Array<T> a = generateArray<T>(n);
00310   ArrayView<const T> av = getConst(a);
00311   TEST_THROW( a.push_back(a[0]), 
00312     DanglingReferenceError );
00313 }
00314 
00315 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00316 
00317 
00318 
00319 //
00320 // Instantiations
00321 //
00322 
00323 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00324 
00325 #  define DEBUG_UNIT_TEST_GROUP( T ) \
00326     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_implicit, T ) \
00327     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_implicit_const, T ) \
00328     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_explicit, T ) \
00329     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_explicit_const, T ) \
00330     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_subview, T ) \
00331     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayView_subview_const, T ) \
00332     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter, T ) \
00333     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter_const, T ) \
00334     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, structuralChangeArrayView, T ) \
00335     TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, structuralChangeArrayView_const, T )
00336 
00337 #else // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00338 
00339 #  define DEBUG_UNIT_TEST_GROUP( T )
00340 
00341 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00342 
00343 
00344 #define UNIT_TEST_GROUP( T ) \
00345   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, defaultConstruct, T ) \
00346   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, sizedConstruct, T ) \
00347   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, operatorBracket, T ) \
00348   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, constAt, T ) \
00349   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, danglingArrayViewIter_before_block_end, T ) \
00350   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPArray_to_ArrayRCP, T ) \
00351   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, RCPArray_to_ArrayRCP_null, T ) \
00352   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, toVector, T ) \
00353   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, toVector_empty, T ) \
00354   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, view_empty, T ) \
00355   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, view_const_empty, T ) \
00356   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( Array, implicit_to_ArrayView_empty, T ) \
00357   DEBUG_UNIT_TEST_GROUP( T )
00358 
00359 UNIT_TEST_GROUP(int)
00360 UNIT_TEST_GROUP(float)
00361 UNIT_TEST_GROUP(double)
00362 
00363 
00364 } // 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