ArrayView_UnitTests.cpp

Go to the documentation of this file.
00001 #include "Array_UnitTest_helpers.hpp"
00002 
00003 #include "Teuchos_implicit_cast.hpp"
00004 
00005 
00006 namespace {
00007 
00008 
00009 using ArrayUnitTestHelpers::n;
00010 using ArrayUnitTestHelpers::generateArray;
00011 using Teuchos::RCP;
00012 using Teuchos::rcp;
00013 using Teuchos::Array;
00014 using Teuchos::ArrayRCP;
00015 using Teuchos::arcp;
00016 using Teuchos::ArrayView;
00017 using Teuchos::arrayView;
00018 using Teuchos::av_reinterpret_cast;
00019 using Teuchos::DanglingReferenceError;
00020 using Teuchos::as;
00021 using Teuchos::null;
00022 using Teuchos::implicit_ptr_cast;
00023 
00024 
00025 TEUCHOS_UNIT_TEST( ArrayView, assignSelf )
00026 {
00027   ArrayView<int> av;
00028   av = av;
00029 }
00030 
00031 
00032 TEUCHOS_UNIT_TEST( ArrayView, assignFuncSelf )
00033 {
00034   Array<int> a = generateArray<int>(n);
00035   ArrayView<int> av = a;
00036   av.assign(av);
00037 }
00038 
00039 
00040 TEUCHOS_UNIT_TEST( ArrayRCP, av_reinterpret_cast_null )
00041 {
00042   ArrayView<char> av_char = null;
00043   ArrayView<int> av_int = av_reinterpret_cast<int>(av_char);
00044   TEST_ASSERT(is_null(av_int));
00045 }
00046 
00047 TEUCHOS_UNIT_TEST( ArrayRCP, av_reinterpret_cast_char_to_int )
00048 {
00049 
00050   const int sizeOfInt = sizeof(int);
00051   const int sizeOfChar = sizeof(char);
00052   const int num_ints = n;
00053   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00054   out << "num_ints = " << num_ints << "\n";
00055   out << "num_chars = " << num_chars << "\n";
00056 
00057   ArrayRCP<char> arcp_char = arcp<char>(num_chars);
00058   ArrayView<int> av_int = av_reinterpret_cast<int>(arcp_char());
00059   TEST_EQUALITY(av_int.size(), num_ints);
00060   TEST_EQUALITY(implicit_ptr_cast<void>(&av_int[0]),
00061     implicit_ptr_cast<void>(&arcp_char[0]));
00062   TEST_EQUALITY(implicit_ptr_cast<void>((&av_int[num_ints-1])+1),
00063     implicit_ptr_cast<void>((&arcp_char[num_chars-1])+1));
00064 
00065 }
00066 
00067 
00068 TEUCHOS_UNIT_TEST( ArrayRCP, av_reinterpret_cast_int_to_char )
00069 {
00070 
00071   const int sizeOfInt = sizeof(int);
00072   const int sizeOfChar = sizeof(char);
00073   const int num_ints = n;
00074   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00075   out << "num_ints = " << num_ints << "\n";
00076   out << "num_chars = " << num_chars << "\n";
00077 
00078   ArrayRCP<int> arcp_int = arcp<int>(num_ints);
00079   ArrayView<char> av_char = av_reinterpret_cast<char>(arcp_int());
00080   TEST_EQUALITY(av_char.size(), num_chars);
00081   TEST_EQUALITY(implicit_ptr_cast<void>(&arcp_int[0]),
00082     implicit_ptr_cast<void>(&av_char[0]));
00083   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00084     implicit_ptr_cast<void>((&av_char[num_chars-1])+1));
00085   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00086     implicit_ptr_cast<void>((&av_char[num_chars-1])+1));
00087 
00088 }
00089 
00090 
00091 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, arrayView, T )
00092 {
00093   Array<T> a = generateArray<T>(n);
00094   const ArrayView<T> av = arrayView(&a[0], a.size());
00095   TEST_COMPARE_ARRAYS( a, av );
00096 }
00097 
00098 
00099 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, assignmentOperator, T )
00100 {
00101   Array<T> a = generateArray<T>(n);
00102   ArrayView<T> av1;
00103   av1 = a;
00104   ArrayView<T> av2;
00105   av2 = av1;
00106   TEST_EQUALITY( av1.getRawPtr(), a.getRawPtr() );
00107   TEST_EQUALITY( av1.size(), as<int>(a.size()) );
00108   TEST_EQUALITY( av1.getRawPtr(), av2.getRawPtr() );
00109   TEST_EQUALITY( av1.size(), av2.size() );
00110   TEST_COMPARE_ARRAYS( av1, a );
00111   TEST_COMPARE_ARRAYS( av1, av2 );
00112   av1 = null;
00113   TEST_EQUALITY_CONST( av1.getRawPtr(), 0 );
00114   TEST_EQUALITY_CONST( av1.size(), 0 );
00115   av2 = null;
00116   TEST_EQUALITY_CONST( av2.getRawPtr(), 0 );
00117   TEST_EQUALITY_CONST( av2.size(), 0 );
00118 }
00119 
00120 
00121 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, iterators, T )
00122 {
00123   typedef typename ArrayView<T>::iterator iter_t;
00124   typedef Teuchos::ScalarTraits<T> ST;
00125   ECHO(Array<T> a = generateArray<T>(n));
00126   ECHO(ArrayView<T> av = a);
00127   ECHO(const iter_t av_begin = av.begin());
00128   ECHO(const iter_t av_end = av.end());
00129 #ifdef TEUCHOS_DEBUG
00130   TEST_ASSERT(av_begin.shares_resource(av_end));
00131 #endif
00132   ECHO(std::fill(av_begin, av_end, ST::random()));
00133   ECHO(Array<T> a2 = generateArray<T>(n));
00134   ECHO(ArrayView<T> av2 = a2);
00135   ECHO(std::copy(av.begin(), av.end(), av2.begin()));
00136   TEST_COMPARE_ARRAYS(a, a2);
00137 }
00138 
00139 
00140 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, danglingView_std_vector, T )
00141 {
00142   ArrayView<T> av;
00143   T* badPtr = 0;
00144   {
00145     std::vector<T> v(n);
00146     av = v;
00147     badPtr = &v[0];
00148   }
00149   // Access the raw pointer but it now points to invalid memory!
00150   TEST_EQUALITY(av.getRawPtr(), badPtr);
00151   // Above, we have no way to detect that the underlying std::vector object
00152   // has gone away.  This is the whole point of needing Teuchos::Array and
00153   // having an integrated set of utility classes that all work together!
00154 }
00155 
00156 
00157 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, danglingView_rcp_std_vector, T )
00158 {
00159   ArrayView<T> av;
00160   {
00161     ArrayRCP<T> ap = arcp(rcp(new std::vector<T>(n)));
00162     av = ap;
00163   }
00164 #ifdef TEUCHOS_DEBUG
00165   TEST_THROW(av.getRawPtr(), DanglingReferenceError);
00166 #endif
00167   // Above, because we wrapped the initial std::vector in an RCP object, we
00168   // can sucessfully detect when the object goes away in debug mode!
00169 }
00170 
00171 
00172 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00173 
00174 
00175 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00176 
00177 
00178 //
00179 // Instantiations
00180 //
00181 
00182 
00183 
00184 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00185 
00186 #  define DEBUG_UNIT_TEST_GROUP( T )
00187 
00188 #else // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00189 
00190 #  define DEBUG_UNIT_TEST_GROUP( T )
00191 
00192 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00193 
00194 
00195 #define UNIT_TEST_GROUP( T ) \
00196   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, arrayView, T ) \
00197   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, assignmentOperator, T ) \
00198   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, iterators, T ) \
00199   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, danglingView_std_vector, T ) \
00200   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, danglingView_rcp_std_vector, T ) \
00201   DEBUG_UNIT_TEST_GROUP( T )
00202 
00203 
00204 UNIT_TEST_GROUP(int)
00205 UNIT_TEST_GROUP(float)
00206 UNIT_TEST_GROUP(double)
00207 
00208 
00209 } // 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