Tuple_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_Tuple.hpp"
00030 #include "Teuchos_GlobalMPISession.hpp"
00031 #include "Teuchos_CommandLineProcessor.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, int N>
00088 bool testTuple( Teuchos::FancyOStream &out )
00089 {
00090   
00091   using Teuchos::Tuple;
00092   using Teuchos::tuple;
00093   using Teuchos::ArrayView;
00094   using Teuchos::arrayView;
00095   using Teuchos::arrayViewFromVector;
00096   using Teuchos::outArg;
00097   using Teuchos::NullIteratorTraits;
00098   using Teuchos::TypeNameTraits;
00099   using Teuchos::getConst;
00100   using Teuchos::as;
00101   typedef typename ArrayView<T>::size_type size_type;
00102 
00103   bool success = true;
00104  
00105   out
00106     << "\n***"
00107     << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
00108     << "\n***\n";
00109   
00110   Teuchos::OSTab tab(out);
00111 
00112   //
00113   out << "\nA) Initial setup testing ...\n\n";
00114   //
00115 
00116   Tuple<T,N> t;
00117   TEST_EQUALITY_CONST(t.size(),N);
00118   for( int i = 0; i < N; ++i )
00119     t[i] = i; // tests non-const operator[](i)
00120 
00121   {
00122     out << "\nTest that t[i] == i ... ";
00123     const ArrayView<const T> cav2 = t;
00124     bool local_success = true;
00125     for( int i = 0; i < N; ++i ) {
00126       TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
00127     }
00128     if (local_success) out << "passed\n";
00129     else success = false;
00130   }
00131 
00132   {
00133     const int n = 1;
00134     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00135     Tuple<T,n> tn = tuple<T>(0);
00136     TEST_EQUALITY_CONST(tn.size(),n);
00137     out << "Test that tn[i] == i ... ";
00138     bool local_success = true;
00139     for( int i = 0; i < n; ++i ) {
00140       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00141     }
00142     if (local_success) out << "passed\n";
00143     else success = false;
00144   }
00145 
00146   {
00147     const int n = 2;
00148     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00149     Tuple<T,n> tn = tuple<T>(0,1);
00150     TEST_EQUALITY_CONST(tn.size(),n);
00151     out << "Test that tn[i] == i ... ";
00152     bool local_success = true;
00153     for( int i = 0; i < n; ++i ) {
00154       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00155     }
00156     if (local_success) out << "passed\n";
00157     else success = false;
00158   }
00159 
00160   {
00161     const int n = 3;
00162     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00163     Tuple<T,n> tn = tuple<T>(0,1,2);
00164     TEST_EQUALITY_CONST(tn.size(),n);
00165     out << "Test that tn[i] == i ... ";
00166     bool local_success = true;
00167     for( int i = 0; i < n; ++i ) {
00168       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00169     }
00170     if (local_success) out << "passed\n";
00171     else success = false;
00172   }
00173 
00174   {
00175     const int n = 4;
00176     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00177     Tuple<T,n> tn = tuple<T>(0,1,2,3);
00178     TEST_EQUALITY_CONST(tn.size(),n);
00179     out << "Test that tn[i] == i ... ";
00180     bool local_success = true;
00181     for( int i = 0; i < n; ++i ) {
00182       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00183     }
00184     if (local_success) out << "passed\n";
00185     else success = false;
00186   }
00187 
00188   {
00189     const int n = 5;
00190     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00191     Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
00192     TEST_EQUALITY_CONST(tn.size(),n);
00193     out << "Test that tn[i] == i ... ";
00194     bool local_success = true;
00195     for( int i = 0; i < n; ++i ) {
00196       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00197     }
00198     if (local_success) out << "passed\n";
00199     else success = false;
00200   }
00201 
00202   {
00203     const int n = 6;
00204     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00205     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
00206     TEST_EQUALITY_CONST(tn.size(),n);
00207     out << "Test that tn[i] == i ... ";
00208     bool local_success = true;
00209     for( int i = 0; i < n; ++i ) {
00210       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00211     }
00212     if (local_success) out << "passed\n";
00213     else success = false;
00214   }
00215 
00216   {
00217     const int n = 7;
00218     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00219     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
00220     TEST_EQUALITY_CONST(tn.size(),n);
00221     out << "Test that tn[i] == i ... ";
00222     bool local_success = true;
00223     for( int i = 0; i < n; ++i ) {
00224       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00225     }
00226     if (local_success) out << "passed\n";
00227     else success = false;
00228   }
00229 
00230   {
00231     const int n = 8;
00232     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00233     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
00234     TEST_EQUALITY_CONST(tn.size(),n);
00235     out << "Test that tn[i] == i ... ";
00236     bool local_success = true;
00237     for( int i = 0; i < n; ++i ) {
00238       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00239     }
00240     if (local_success) out << "passed\n";
00241     else success = false;
00242   }
00243 
00244   {
00245     const int n = 9;
00246     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00247     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
00248     TEST_EQUALITY_CONST(tn.size(),n);
00249     out << "Test that tn[i] == i ... ";
00250     bool local_success = true;
00251     for( int i = 0; i < n; ++i ) {
00252       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00253     }
00254     if (local_success) out << "passed\n";
00255     else success = false;
00256   }
00257 
00258   {
00259     const int n = 10;
00260     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00261     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
00262     TEST_EQUALITY_CONST(tn.size(),n);
00263     out << "Test that tn[i] == i ... ";
00264     bool local_success = true;
00265     for( int i = 0; i < n; ++i ) {
00266       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00267     }
00268     if (local_success) out << "passed\n";
00269     else success = false;
00270   }
00271 
00272   {
00273     const int n = 11;
00274     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00275     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
00276     TEST_EQUALITY_CONST(tn.size(),n);
00277     out << "Test that tn[i] == i ... ";
00278     bool local_success = true;
00279     for( int i = 0; i < n; ++i ) {
00280       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00281     }
00282     if (local_success) out << "passed\n";
00283     else success = false;
00284   }
00285 
00286   {
00287     const int n = 12;
00288     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00289     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
00290     TEST_EQUALITY_CONST(tn.size(),n);
00291     out << "Test that tn[i] == i ... ";
00292     bool local_success = true;
00293     for( int i = 0; i < n; ++i ) {
00294       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00295     }
00296     if (local_success) out << "passed\n";
00297     else success = false;
00298   }
00299 
00300   {
00301     const int n = 13;
00302     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00303     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
00304     TEST_EQUALITY_CONST(tn.size(),n);
00305     out << "Test that tn[i] == i ... ";
00306     bool local_success = true;
00307     for( int i = 0; i < n; ++i ) {
00308       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00309     }
00310     if (local_success) out << "passed\n";
00311     else success = false;
00312   }
00313 
00314   {
00315     const int n = 14;
00316     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00317     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
00318     TEST_EQUALITY_CONST(tn.size(),n);
00319     out << "Test that tn[i] == i ... ";
00320     bool local_success = true;
00321     for( int i = 0; i < n; ++i ) {
00322       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00323     }
00324     if (local_success) out << "passed\n";
00325     else success = false;
00326   }
00327 
00328   {
00329     const int n = 15;
00330     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00331     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
00332     TEST_EQUALITY_CONST(tn.size(),n);
00333     out << "Test that tn[i] == i ... ";
00334     bool local_success = true;
00335     for( int i = 0; i < n; ++i ) {
00336       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00337     }
00338     if (local_success) out << "passed\n";
00339     else success = false;
00340   }
00341 
00342   {
00343     out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
00344     const ArrayView<const T> av2 = t;
00345     TEST_COMPARE_ARRAYS( av2, t );
00346   }
00347   
00348   // ToDo: Add more tests!
00349 
00350   return success;
00351 
00352 }
00353 
00354 
00355 //
00356 // Main testing program
00357 //
00358 
00359 int main( int argc, char* argv[] ) {
00360 
00361   using Teuchos::CommandLineProcessor;
00362   
00363   bool success = true;
00364   bool result;
00365  
00366   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00367  
00368   Teuchos::RCP<Teuchos::FancyOStream>
00369     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00370  
00371   try {
00372     
00373     //
00374     // Read options from the commandline
00375     //
00376 
00377     CommandLineProcessor clp(false); // Don't throw exceptions
00378 
00379     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00380 
00381     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00382       *out << "\nEnd Result: TEST FAILED" << std::endl;
00383       return parse_return;
00384     }
00385 
00386     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00387 
00388     const int N = 8;
00389  
00390     result = testTuple<int,N>(*out);
00391     if (!result) success = false;
00392 
00393     result = testTuple<float,N>(*out);
00394     if (!result) success = false;
00395 
00396     result = testTuple<double,N>(*out);
00397     if (!result) success = false;
00398 
00399     result = testTuple<std::complex<double> ,N>(*out);
00400     if (!result) success = false;
00401  
00402   }
00403   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00404  
00405   if (success)
00406     *out << "\nEnd Result: TEST PASSED" << std::endl;
00407   else
00408     *out << "\nEnd Result: TEST FAILED" << std::endl;
00409  
00410   return ( success ? 0 : 1 );
00411  
00412 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:14:01 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1