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_const_cast;
00019 using Teuchos::av_reinterpret_cast;
00020 using Teuchos::DanglingReferenceError;
00021 using Teuchos::as;
00022 using Teuchos::null;
00023 using Teuchos::implicit_ptr_cast;
00024 
00025 
00026 TEUCHOS_UNIT_TEST( ArrayView, assignSelf )
00027 {
00028   ArrayView<int> av;
00029   av = av;
00030   TEST_ASSERT(is_null(av));
00031   TEST_ASSERT(!nonnull(av));
00032 }
00033 
00034 
00035 TEUCHOS_UNIT_TEST( ArrayView, assignFuncSelf )
00036 {
00037   Array<int> a = generateArray<int>(n);
00038   ArrayView<int> av = a;
00039   av.assign(av);
00040 }
00041 
00042 
00043 TEUCHOS_UNIT_TEST( ArrayView, av_const_cast_null )
00044 {
00045   ArrayView<const int> av_int1 = null;
00046   ArrayView<int> av_int2 = av_const_cast<int>(av_int1);
00047   TEST_ASSERT(is_null(av_int2));
00048 }
00049 
00050 
00051 TEUCHOS_UNIT_TEST( ArrayView, av_const_cast )
00052 {
00053   ArrayRCP<const int> arcp_int = arcp<int>(n);
00054   ArrayView<const int> av_int1 = arcp_int();
00055   ArrayView<int> av_int2 = av_const_cast<int>(av_int1);
00056   TEST_ASSERT(nonnull(av_int2));
00057   TEST_EQUALITY(av_int2.getRawPtr(), av_int1.getRawPtr());
00058   TEST_EQUALITY(av_int2.getRawPtr(), arcp_int.getRawPtr());
00059 }
00060 
00061 
00062 TEUCHOS_UNIT_TEST( ArrayView, av_reinterpret_cast_null )
00063 {
00064   ArrayView<char> av_char = null;
00065   ArrayView<int> av_int = av_reinterpret_cast<int>(av_char);
00066   TEST_ASSERT(is_null(av_int));
00067 }
00068 
00069 
00070 TEUCHOS_UNIT_TEST( ArrayView, av_reinterpret_cast_char_to_int )
00071 {
00072 
00073   const int sizeOfInt = sizeof(int);
00074   const int sizeOfChar = sizeof(char);
00075   const int num_ints = n;
00076   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00077   out << "num_ints = " << num_ints << "\n";
00078   out << "num_chars = " << num_chars << "\n";
00079 
00080   ArrayRCP<char> arcp_char = arcp<char>(num_chars);
00081   ArrayView<int> av_int = av_reinterpret_cast<int>(arcp_char());
00082   TEST_EQUALITY(av_int.size(), num_ints);
00083   TEST_EQUALITY(implicit_ptr_cast<void>(&av_int[0]),
00084     implicit_ptr_cast<void>(&arcp_char[0]));
00085   TEST_EQUALITY(implicit_ptr_cast<void>((&av_int[num_ints-1])+1),
00086     implicit_ptr_cast<void>((&arcp_char[num_chars-1])+1));
00087 
00088 }
00089 
00090 
00091 TEUCHOS_UNIT_TEST( ArrayView, av_reinterpret_cast_int_to_char )
00092 {
00093 
00094   const int sizeOfInt = sizeof(int);
00095   const int sizeOfChar = sizeof(char);
00096   const int num_ints = n;
00097   const int num_chars = (num_ints*sizeOfInt)/sizeOfChar;
00098   out << "num_ints = " << num_ints << "\n";
00099   out << "num_chars = " << num_chars << "\n";
00100 
00101   ArrayRCP<int> arcp_int = arcp<int>(num_ints);
00102   ArrayView<char> av_char = av_reinterpret_cast<char>(arcp_int());
00103   TEST_EQUALITY(av_char.size(), num_chars);
00104   TEST_EQUALITY(implicit_ptr_cast<void>(&arcp_int[0]),
00105     implicit_ptr_cast<void>(&av_char[0]));
00106   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00107     implicit_ptr_cast<void>((&av_char[num_chars-1])+1));
00108   TEST_EQUALITY(implicit_ptr_cast<void>((&arcp_int[num_ints-1])+1),
00109     implicit_ptr_cast<void>((&av_char[num_chars-1])+1));
00110 
00111 }
00112 
00113 
00114 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, arrayView_zero_size, T )
00115 {
00116   Array<T> a;
00117   const ArrayView<T> av = arrayView(a.getRawPtr(), a.size());
00118   TEST_EQUALITY_CONST(av.size(), 0);
00119   TEST_ASSERT(is_null(av));
00120   TEST_ASSERT(!nonnull(av));
00121 }
00122 
00123 
00124 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, arrayView, T )
00125 {
00126   Array<T> a = generateArray<T>(n);
00127   const ArrayView<T> av = arrayView(&a[0], a.size());
00128   TEST_COMPARE_ARRAYS( a, av );
00129 }
00130 
00131 
00132 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, assignmentOperator, T )
00133 {
00134   Array<T> a = generateArray<T>(n);
00135   ArrayView<T> av1;
00136   av1 = a;
00137   ArrayView<T> av2;
00138   av2 = av1;
00139   TEST_EQUALITY( av1.getRawPtr(), a.getRawPtr() );
00140   TEST_EQUALITY( av1.size(), as<int>(a.size()) );
00141   TEST_EQUALITY( av1.getRawPtr(), av2.getRawPtr() );
00142   TEST_EQUALITY( av1.size(), av2.size() );
00143   TEST_COMPARE_ARRAYS( av1, a );
00144   TEST_COMPARE_ARRAYS( av1, av2 );
00145   av1 = null;
00146   TEST_EQUALITY_CONST( av1.getRawPtr(), 0 );
00147   TEST_EQUALITY_CONST( av1.size(), 0 );
00148   av2 = null;
00149   TEST_EQUALITY_CONST( av2.getRawPtr(), 0 );
00150   TEST_EQUALITY_CONST( av2.size(), 0 );
00151 }
00152 
00153 
00154 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, iterators, T )
00155 {
00156   typedef typename ArrayView<T>::iterator iter_t;
00157   typedef Teuchos::ScalarTraits<T> ST;
00158   ECHO(Array<T> a = generateArray<T>(n));
00159   ECHO(ArrayView<T> av = a);
00160   ECHO(const iter_t av_begin = av.begin());
00161   ECHO(const iter_t av_end = av.end());
00162 #ifdef TEUCHOS_DEBUG
00163   TEST_ASSERT(av_begin.shares_resource(av_end));
00164 #endif
00165   ECHO(std::fill(av_begin, av_end, ST::random()));
00166   ECHO(Array<T> a2 = generateArray<T>(n));
00167   ECHO(ArrayView<T> av2 = a2);
00168   ECHO(std::copy(av.begin(), av.end(), av2.begin()));
00169   TEST_COMPARE_ARRAYS(a, a2);
00170 }
00171 
00172 
00173 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, danglingView_std_vector, T )
00174 {
00175   ArrayView<T> av;
00176   T* badPtr = 0;
00177   {
00178     std::vector<T> v(n);
00179     av = v;
00180     badPtr = &v[0];
00181   }
00182   // Access the raw pointer but it now points to invalid memory!
00183   TEST_EQUALITY(av.getRawPtr(), badPtr);
00184   // Above, we have no way to detect that the underlying std::vector object
00185   // has gone away.  This is the whole point of needing Teuchos::Array and
00186   // having an integrated set of utility classes that all work together!
00187 }
00188 
00189 
00190 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArrayView, danglingView_rcp_std_vector, T )
00191 {
00192   ArrayView<T> av;
00193   {
00194     ArrayRCP<T> ap = arcp(rcp(new std::vector<T>(n)));
00195     av = ap();
00196   }
00197 #ifdef TEUCHOS_DEBUG
00198   TEST_THROW(av.getRawPtr(), DanglingReferenceError);
00199 #endif
00200   // Above, because we wrapped the initial std::vector in an RCP object, we
00201   // can sucessfully detect when the object goes away in debug mode!
00202 }
00203 
00204 
00205 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00206 
00207 
00208 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00209 
00210 
00211 //
00212 // Instantiations
00213 //
00214 
00215 
00216 
00217 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00218 
00219 #  define DEBUG_UNIT_TEST_GROUP( T )
00220 
00221 #else // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00222 
00223 #  define DEBUG_UNIT_TEST_GROUP( T )
00224 
00225 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00226 
00227 
00228 #define UNIT_TEST_GROUP( T ) \
00229   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, arrayView_zero_size, T ) \
00230   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, arrayView, T ) \
00231   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, assignmentOperator, T ) \
00232   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, iterators, T ) \
00233   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, danglingView_std_vector, T ) \
00234   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ArrayView, danglingView_rcp_std_vector, T ) \
00235   DEBUG_UNIT_TEST_GROUP( T )
00236 
00237 
00238 UNIT_TEST_GROUP(int)
00239 UNIT_TEST_GROUP(float)
00240 UNIT_TEST_GROUP(double)
00241 
00242 
00243 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:28 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3