ArrayView_test.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_ArrayView.hpp"
00030 #include "Teuchos_CommandLineProcessor.hpp"
00031 #include "Teuchos_GlobalMPISession.hpp"
00032 #include "Teuchos_VerboseObject.hpp"
00033 #include "Teuchos_StandardCatchMacros.hpp"
00034 #include "Teuchos_Version.hpp"
00035 #include "Teuchos_getConst.hpp"
00036 #include "Teuchos_as.hpp"
00037 #include "Teuchos_TestingHelpers.hpp"
00038 
00039 
00040 // Uncomment to show compile errors from invalid usage
00041 //#define SHOW_INVALID_COPY_CONSTRUCTION
00042 //#define SHOW_INVALID_CONST_ASSIGN
00043 //#define SHOW_INVALID_CONST_ITER_MODIFICATION
00044 
00045 //
00046 // Define local macros to make defining tests easier for this particular test
00047 // code.
00048 //
00049 // Note, macros with these types of names should only exist in a *.cpp file
00050 // after all #includes are done!
00051 //
00052 
00053 
00054 #define TEST_EQUALITY_CONST( v1, v2 ) \
00055   TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
00056 
00057 #define TEST_EQUALITY( v1, v2 ) \
00058   TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
00059 
00060 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
00061   TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
00062 
00063 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
00064    TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
00065 
00066 #define TEST_COMPARE( v1, comp, v2 ) \
00067   TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
00068 
00069 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
00070   { \
00071     const bool result = compareArrays(a1,#a1,a2,#a2,out); \
00072     if (!result) success = false; \
00073   }
00074 
00075 #define TEST_THROW( code, ExceptType  ) \
00076   TEUCHOS_TEST_THROW( code, ExceptType, out, success  )
00077 
00078 #define TEST_NOTHROW( code  ) \
00079   TEUCHOS_TEST_NOTHROW( code, out, success  )
00080 
00081 
00082 //
00083 // Main templated array test function
00084 //
00085 
00086 
00087 template<class T>
00088 bool testArrayView( const int n, Teuchos::FancyOStream &out )
00089 {
00090   
00091   using Teuchos::ArrayView;
00092   using Teuchos::arrayView;
00093   using Teuchos::arrayViewFromVector;
00094   using Teuchos::outArg;
00095   using Teuchos::NullIteratorTraits;
00096   using Teuchos::TypeNameTraits;
00097   using Teuchos::getConst;
00098   using Teuchos::as;
00099   typedef typename ArrayView<T>::size_type size_type;
00100 
00101   bool success = true;
00102  
00103   out
00104     << "\n***"
00105     << "\n*** Testing "<<TypeNameTraits<ArrayView<T> >::name()<<" of size = "<<n
00106     << "\n***\n";
00107   
00108   Teuchos::OSTab tab(out);
00109 
00110   //
00111   out << "\nA) Initial setup testing ...\n\n";
00112   //
00113   
00114   {
00115     out << "\nTesting basic null construction!\n\n";
00116     ArrayView<T> av2 = Teuchos::null;
00117     TEST_EQUALITY_CONST(is_null(av2),true);
00118     TEST_EQUALITY_CONST(av2.size(),0);
00119     TEST_EQUALITY_CONST(av2.getRawPtr(),0);
00120     TEST_ITER_EQUALITY(av2.begin(),av2.end());
00121 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00122     TEST_THROW(av2[0],Teuchos::NullReferenceError);
00123     TEST_THROW(*av2.begin(), Teuchos::NullReferenceError);
00124     TEST_THROW(*av2.end(), Teuchos::NullReferenceError);
00125     TEST_THROW(av2.assign(av2), Teuchos::NullReferenceError);
00126     TEST_THROW(av2.front(), Teuchos::NullReferenceError);
00127     TEST_THROW(av2.back(), Teuchos::NullReferenceError);
00128 #endif
00129     ArrayView<const T> cav2(av2); // Tests copy constructor and implicit conversion operator! 
00130     TEST_EQUALITY_CONST(cav2.size(),0);
00131     TEST_EQUALITY_CONST(cav2.getRawPtr(),0);
00132     TEST_ITER_EQUALITY(cav2.begin(),av2.end());
00133 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00134     TEST_THROW(cav2[0],Teuchos::NullReferenceError);
00135     TEST_THROW(*cav2.begin(), Teuchos::NullReferenceError);
00136     TEST_THROW(*cav2.end(), Teuchos::NullReferenceError);
00137     TEST_THROW(cav2.back(), Teuchos::NullReferenceError);
00138 #endif
00139 #ifdef SHOW_INVALID_CONST_ASSIGN
00140     TEST_NOTHROW(cav2.assign(av2)); // Should not compile!
00141 #endif
00142   }
00143 
00144   std::vector<T> v(n);
00145   const ArrayView<T> av = arrayViewFromVector(v);
00146   const ArrayView<const T> cav = arrayViewFromVector(getConst(v));
00147 
00148   TEST_EQUALITY_CONST(is_null(av), false);
00149   TEST_EQUALITY( as<int>(av.size()), n );
00150  
00151   {
00152     out << "\nInitializing data for std::vector v through view av ...\n";
00153     for( int i = 0; i < n; ++i )
00154       av[i] = i; // tests non-const operator[](i)
00155   }
00156 
00157   {
00158     out << "\nTest that v[i] == i through ArrayView<const T> ...\n";
00159     const ArrayView<const T> cav2 = arrayViewFromVector(getConst(v));
00160     bool local_success = true;
00161     for( int i = 0; i < n; ++i ) {
00162       TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
00163     }
00164     if (local_success) out << "passed\n";
00165     else success = false;
00166   }
00167 
00168   {
00169     out << "\nTest explicit copy to std::vector from non-const array view ...\n";
00170     std::vector<T> v2 = Teuchos::createVector(av);
00171     TEST_COMPARE_ARRAYS( v2, v );
00172   }
00173 
00174   {
00175     out << "\nTest explicit copy to std::vector from const array view ...\n";
00176     std::vector<T> v2 = Teuchos::createVector(cav);
00177     TEST_COMPARE_ARRAYS( v2, v );
00178   }
00179 
00180   {
00181     out << "\nTest shallow implicit conversion from ArrayView<T> to ArrayView<T> ...\n";
00182     ArrayView<T> av2(av);
00183     TEST_COMPARE_ARRAYS( av2, av );
00184   }
00185 
00186   {
00187     out << "\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<const T> ...\n";
00188     ArrayView<const T> cav2(cav);
00189     TEST_COMPARE_ARRAYS( cav2, cav );
00190   }
00191 
00192   {
00193     out << "\nTest shallow implicit conversion from ArrayView<const T> to ArrayView<T> ...\n";
00194     ArrayView<const T> cav2(av);
00195     TEST_COMPARE_ARRAYS( cav2, av );
00196   }
00197 
00198   {
00199     out << "\nTest shallow implicit conversion from std::vector<T> to ArrayView<T> ...\n";
00200     std::vector<T> v2 = Teuchos::createVector(cav);
00201     ArrayView<T> cav2(v2);
00202     TEST_COMPARE_ARRAYS( cav2, av );
00203   }
00204 
00205   {
00206     out << "\nTest shallow implicit conversion from const std::vector<T> to ArrayView<const T> ...\n";
00207     std::vector<T> v2 = Teuchos::createVector(cav);
00208     ArrayView<const T> cav2(getConst(v2));
00209     TEST_COMPARE_ARRAYS( cav2, av );
00210   }
00211 
00212   {
00213     // Try to copy construct from ArrayView<const T> to ArrayView<T> ..
00214 #ifdef SHOW_INVALID_COPY_CONSTRUCTION
00215     ArrayView<T> cav2(cav); // should not compile!
00216 #endif
00217   }
00218 
00219   {
00220     out << "\ntest assign(...) ...\n";
00221     std::vector<T> v2(n);
00222     ArrayView<T> av2(v2);
00223     av2.assign(av);
00224     TEST_COMPARE_ARRAYS( v2, v );
00225   }
00226 
00227   //
00228   out << "\nB) Test element access ...\n";
00229   //
00230 
00231 
00232   TEST_EQUALITY_CONST( av.front(), as<T>(0) );
00233   TEST_EQUALITY( av.back(), as<T>(n-1) );
00234   TEST_EQUALITY_CONST( cav.front(), as<T>(0) );
00235   TEST_EQUALITY( cav.back(), as<T>(n-1) );
00236 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00237   TEST_THROW( av[-1], Teuchos::RangeError );
00238   TEST_THROW( av[n], Teuchos::RangeError );
00239   TEST_THROW( cav[-1], Teuchos::RangeError );
00240   TEST_THROW( cav[n], Teuchos::RangeError );
00241 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00242 
00243   //
00244   out << "\nC) Test iterator access ...\n";
00245   //
00246 
00247 
00248   {
00249     out << "\nTest non-const forward iterator access ...\n";
00250     std::vector<T> v2(n);
00251     ArrayView<T> av2(v2);
00252     typedef typename ArrayView<T>::iterator iter_t;
00253     iter_t iter = av2.begin();
00254     for ( int i = 0; iter != av2.end(); ++i )
00255       *iter++ = i;
00256     TEST_COMPARE_ARRAYS( v2, v );
00257   }
00258 
00259   {
00260     out << "\nTest const forward iterator access ... ";
00261     bool local_success = true;
00262     typedef typename ArrayView<const T>::iterator iter_t;
00263     const ArrayView<const T> cav2 = av.getConst();
00264     iter_t iter = cav2.begin();
00265     for ( int i = 0; i < n; ++i, ++iter ) {
00266       TEST_ARRAY_ELE_EQUALITY( av, i, *iter );
00267 
00268 #ifdef SHOW_INVALID_CONST_ITER_MODIFICATION
00269       *iter = as<T>(i); // Should not compile!
00270 #endif
00271     }
00272     iter = NullIteratorTraits<iter_t>::getNull();
00273     if (local_success) out << "passed\n";
00274     else success = false;
00275   }
00276 
00277   //
00278   out << "\nD) Test sub-views ...\n";
00279   //
00280 
00281   {
00282     out << "\nTest full non-const subview ...\n";
00283     const ArrayView<T> av2 = av(0,n);
00284     TEST_COMPARE_ARRAYS( av2, av );
00285   }
00286 
00287   {
00288     out << "\nTest full shorthand non-const subview ...\n";
00289     const ArrayView<T> av2 = av();
00290     TEST_COMPARE_ARRAYS( av2, av );
00291   }
00292 
00293   {
00294     out << "\nTest full const subview ...\n";
00295     const ArrayView<const T> cav2 = cav(0,n);
00296     TEST_COMPARE_ARRAYS( cav2, cav );
00297   }
00298 
00299   {
00300     out << "\nTest full non-const to const subview ...\n";
00301     const ArrayView<const T> cav2 = av(0,n);
00302     TEST_COMPARE_ARRAYS( cav2, cav );
00303   }
00304 
00305   {
00306     out << "\nTest full short-hand const subview ...\n";
00307     const ArrayView<const T> cav2 = cav();
00308     TEST_COMPARE_ARRAYS( cav2, cav );
00309   }
00310 
00311   {
00312     out << "\nTest non-const initial range view ...\n";
00313     std::vector<T> v2(n,as<T>(-1));
00314     const ArrayView<T> av2(v2);
00315     const ArrayView<T> av2_init = av2(0,n-1);
00316     TEST_EQUALITY( av2_init.size(), n-1 );
00317     av2_init.assign( av(0,n-1) );
00318     av2.back() = as<T>(n-1);
00319     TEST_COMPARE_ARRAYS( v2, v );
00320   }
00321 
00322   {
00323     out << "\nTest non-const final range view ...\n";
00324     std::vector<T> v2(n,as<T>(-1));
00325     const ArrayView<T> av2(v2);
00326     const ArrayView<T> av2_init = av2(1,n-1);
00327     TEST_EQUALITY( av2_init.size(), n-1 );
00328     av2_init.assign( av(1,n-1) );
00329     av2.front() = as<T>(0);
00330     TEST_COMPARE_ARRAYS( v2, v );
00331   }
00332 
00333   {
00334     out << "\nTest non-const middle range view ...\n";
00335     std::vector<T> v2(n,as<T>(-1));
00336     const ArrayView<T> av2(v2);
00337     const ArrayView<T> av2_init = av2(1,n-2);
00338     TEST_EQUALITY( av2_init.size(), n-2 );
00339     av2_init.assign( av(1,n-2) );
00340     av2.front() = as<T>(0);
00341     av2.back() = as<T>(n-1);
00342     TEST_COMPARE_ARRAYS( v2, v );
00343   }
00344 
00345   // ToDo: Test requesting views outside of valid range!
00346 
00347   return success;
00348 
00349 }
00350 
00351 
00352 //
00353 // Main testing program
00354 //
00355 
00356 int main( int argc, char* argv[] )
00357 {
00358 
00359   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00360 
00361   using Teuchos::CommandLineProcessor;
00362   
00363   bool success = true;
00364   bool result;
00365  
00366   Teuchos::RCP<Teuchos::FancyOStream>
00367     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00368  
00369   try {
00370     
00371     //
00372     // Read options from the commandline
00373     //
00374 
00375     CommandLineProcessor clp(false); // Don't throw exceptions
00376 
00377     int n = 4;
00378     clp.setOption( "n", &n, "Number of elements in the array" );
00379 
00380     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00381 
00382     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00383       *out << "\nEnd Result: TEST FAILED" << std::endl;
00384       return parse_return;
00385     }
00386 
00387     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00388  
00389     result = testArrayView<int>(n,*out);
00390     if (!result) success = false;
00391 
00392     result = testArrayView<float>(n,*out);
00393     if (!result) success = false;
00394 
00395     result = testArrayView<double>(n,*out);
00396     if (!result) success = false;
00397 
00398     result = testArrayView<std::complex<double> >(n,*out);
00399     if (!result) success = false;
00400  
00401   }
00402   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00403  
00404   if (success)
00405     *out << "\nEnd Result: TEST PASSED" << std::endl;
00406   else
00407     *out << "\nEnd Result: TEST FAILED" << std::endl;
00408  
00409   return ( success ? 0 : 1 );
00410  
00411 }
 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