Teuchos Package Browser (Single Doxygen Collection) Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_Tuple.hpp"
00043 #include "Teuchos_GlobalMPISession.hpp"
00044 #include "Teuchos_CommandLineProcessor.hpp"
00045 #include "Teuchos_VerboseObject.hpp"
00046 #include "Teuchos_StandardCatchMacros.hpp"
00047 #include "Teuchos_Version.hpp"
00048 #include "Teuchos_getConst.hpp"
00049 #include "Teuchos_as.hpp"
00050 #include "Teuchos_TestingHelpers.hpp"
00051 
00052 
00053 // Uncomment to show compile errors from invalid usage
00054 //#define SHOW_INVALID_COPY_CONSTRUCTION
00055 //#define SHOW_INVALID_CONST_ASSIGN
00056 //#define SHOW_INVALID_CONST_ITER_MODIFICATION
00057 
00058 //
00059 // Define local macros to make defining tests easier for this particular test
00060 // code.
00061 //
00062 // Note, macros with these types of names should only exist in a *.cpp file
00063 // after all #includes are done!
00064 //
00065 
00066 
00067 #define TEST_EQUALITY_CONST( v1, v2 ) \
00068   TEUCHOS_TEST_EQUALITY_CONST( v1, v2, out, success )
00069 
00070 #define TEST_EQUALITY( v1, v2 ) \
00071   TEUCHOS_TEST_EQUALITY( v1, v2, out, success )
00072 
00073 #define TEST_ITER_EQUALITY( iter1, iter2 ) \
00074   TEUCHOS_TEST_ITER_EQUALITY( iter1, iter2, out, success )
00075 
00076 #define TEST_ARRAY_ELE_EQUALITY( a, i, val ) \
00077    TEUCHOS_TEST_ARRAY_ELE_EQUALITY( a, i, val, false, out, local_success )
00078 
00079 #define TEST_COMPARE( v1, comp, v2 ) \
00080   TEUCHOS_TEST_COMPARE( v1, comp, v2, out, success )
00081 
00082 #define TEST_COMPARE_ARRAYS( a1, a2 ) \
00083   { \
00084     const bool result = compareArrays(a1,#a1,a2,#a2,out); \
00085     if (!result) success = false; \
00086   }
00087 
00088 #define TEST_THROW( code, ExceptType  ) \
00089   TEUCHOS_TEST_THROW( code, ExceptType, out, success  )
00090 
00091 #define TEST_NOTHROW( code  ) \
00092   TEUCHOS_TEST_NOTHROW( code, out, success  )
00093 
00094 
00095 //
00096 // Main templated array test function
00097 //
00098 
00099 
00100 template<class T, int N>
00101 bool testTuple( Teuchos::FancyOStream &out )
00102 {
00103   
00104   using Teuchos::Tuple;
00105   using Teuchos::tuple;
00106   using Teuchos::ArrayView;
00107   using Teuchos::arrayView;
00108   using Teuchos::arrayViewFromVector;
00109   using Teuchos::outArg;
00110   using Teuchos::NullIteratorTraits;
00111   using Teuchos::TypeNameTraits;
00112   using Teuchos::getConst;
00113   using Teuchos::as;
00114   typedef typename ArrayView<T>::size_type size_type;
00115 
00116   bool success = true;
00117  
00118   out
00119     << "\n***"
00120     << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
00121     << "\n***\n";
00122   
00123   Teuchos::OSTab tab(out);
00124 
00125   //
00126   out << "\nA) Initial setup testing ...\n\n";
00127   //
00128 
00129   Tuple<T,N> t;
00130   TEST_EQUALITY_CONST(t.size(),N);
00131   for( int i = 0; i < N; ++i )
00132     t[i] = i; // tests non-const operator[](i)
00133 
00134   {
00135     out << "\nTest that t[i] == i ... ";
00136     const ArrayView<const T> cav2 = t;
00137     bool local_success = true;
00138     for( int i = 0; i < N; ++i ) {
00139       TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
00140     }
00141     if (local_success) out << "passed\n";
00142     else success = false;
00143   }
00144 
00145   {
00146     const int n = 1;
00147     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00148     Tuple<T,n> tn = tuple<T>(0);
00149     TEST_EQUALITY_CONST(tn.size(),n);
00150     out << "Test that tn[i] == i ... ";
00151     bool local_success = true;
00152     for( int i = 0; i < n; ++i ) {
00153       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00154     }
00155     if (local_success) out << "passed\n";
00156     else success = false;
00157   }
00158 
00159   {
00160     const int n = 2;
00161     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00162     Tuple<T,n> tn = tuple<T>(0,1);
00163     TEST_EQUALITY_CONST(tn.size(),n);
00164     out << "Test that tn[i] == i ... ";
00165     bool local_success = true;
00166     for( int i = 0; i < n; ++i ) {
00167       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00168     }
00169     if (local_success) out << "passed\n";
00170     else success = false;
00171   }
00172 
00173   {
00174     const int n = 3;
00175     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00176     Tuple<T,n> tn = tuple<T>(0,1,2);
00177     TEST_EQUALITY_CONST(tn.size(),n);
00178     out << "Test that tn[i] == i ... ";
00179     bool local_success = true;
00180     for( int i = 0; i < n; ++i ) {
00181       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00182     }
00183     if (local_success) out << "passed\n";
00184     else success = false;
00185   }
00186 
00187   {
00188     const int n = 4;
00189     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00190     Tuple<T,n> tn = tuple<T>(0,1,2,3);
00191     TEST_EQUALITY_CONST(tn.size(),n);
00192     out << "Test that tn[i] == i ... ";
00193     bool local_success = true;
00194     for( int i = 0; i < n; ++i ) {
00195       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00196     }
00197     if (local_success) out << "passed\n";
00198     else success = false;
00199   }
00200 
00201   {
00202     const int n = 5;
00203     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00204     Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
00205     TEST_EQUALITY_CONST(tn.size(),n);
00206     out << "Test that tn[i] == i ... ";
00207     bool local_success = true;
00208     for( int i = 0; i < n; ++i ) {
00209       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00210     }
00211     if (local_success) out << "passed\n";
00212     else success = false;
00213   }
00214 
00215   {
00216     const int n = 6;
00217     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00218     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
00219     TEST_EQUALITY_CONST(tn.size(),n);
00220     out << "Test that tn[i] == i ... ";
00221     bool local_success = true;
00222     for( int i = 0; i < n; ++i ) {
00223       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00224     }
00225     if (local_success) out << "passed\n";
00226     else success = false;
00227   }
00228 
00229   {
00230     const int n = 7;
00231     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00232     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
00233     TEST_EQUALITY_CONST(tn.size(),n);
00234     out << "Test that tn[i] == i ... ";
00235     bool local_success = true;
00236     for( int i = 0; i < n; ++i ) {
00237       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00238     }
00239     if (local_success) out << "passed\n";
00240     else success = false;
00241   }
00242 
00243   {
00244     const int n = 8;
00245     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00246     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
00247     TEST_EQUALITY_CONST(tn.size(),n);
00248     out << "Test that tn[i] == i ... ";
00249     bool local_success = true;
00250     for( int i = 0; i < n; ++i ) {
00251       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00252     }
00253     if (local_success) out << "passed\n";
00254     else success = false;
00255   }
00256 
00257   {
00258     const int n = 9;
00259     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00260     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
00261     TEST_EQUALITY_CONST(tn.size(),n);
00262     out << "Test that tn[i] == i ... ";
00263     bool local_success = true;
00264     for( int i = 0; i < n; ++i ) {
00265       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00266     }
00267     if (local_success) out << "passed\n";
00268     else success = false;
00269   }
00270 
00271   {
00272     const int n = 10;
00273     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00274     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
00275     TEST_EQUALITY_CONST(tn.size(),n);
00276     out << "Test that tn[i] == i ... ";
00277     bool local_success = true;
00278     for( int i = 0; i < n; ++i ) {
00279       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00280     }
00281     if (local_success) out << "passed\n";
00282     else success = false;
00283   }
00284 
00285   {
00286     const int n = 11;
00287     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00288     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
00289     TEST_EQUALITY_CONST(tn.size(),n);
00290     out << "Test that tn[i] == i ... ";
00291     bool local_success = true;
00292     for( int i = 0; i < n; ++i ) {
00293       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00294     }
00295     if (local_success) out << "passed\n";
00296     else success = false;
00297   }
00298 
00299   {
00300     const int n = 12;
00301     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00302     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
00303     TEST_EQUALITY_CONST(tn.size(),n);
00304     out << "Test that tn[i] == i ... ";
00305     bool local_success = true;
00306     for( int i = 0; i < n; ++i ) {
00307       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00308     }
00309     if (local_success) out << "passed\n";
00310     else success = false;
00311   }
00312 
00313   {
00314     const int n = 13;
00315     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00316     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
00317     TEST_EQUALITY_CONST(tn.size(),n);
00318     out << "Test that tn[i] == i ... ";
00319     bool local_success = true;
00320     for( int i = 0; i < n; ++i ) {
00321       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00322     }
00323     if (local_success) out << "passed\n";
00324     else success = false;
00325   }
00326 
00327   {
00328     const int n = 14;
00329     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00330     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
00331     TEST_EQUALITY_CONST(tn.size(),n);
00332     out << "Test that tn[i] == i ... ";
00333     bool local_success = true;
00334     for( int i = 0; i < n; ++i ) {
00335       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00336     }
00337     if (local_success) out << "passed\n";
00338     else success = false;
00339   }
00340 
00341   {
00342     const int n = 15;
00343     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00344     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
00345     TEST_EQUALITY_CONST(tn.size(),n);
00346     out << "Test that tn[i] == i ... ";
00347     bool local_success = true;
00348     for( int i = 0; i < n; ++i ) {
00349       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00350     }
00351     if (local_success) out << "passed\n";
00352     else success = false;
00353   }
00354 
00355   {
00356     out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
00357     const ArrayView<const T> av2 = t;
00358     TEST_COMPARE_ARRAYS( av2, t );
00359   }
00360   
00361   // ToDo: Add more tests!
00362 
00363   return success;
00364 
00365 }
00366 
00367 
00368 //
00369 // Main testing program
00370 //
00371 
00372 int main( int argc, char* argv[] ) {
00373 
00374   using Teuchos::CommandLineProcessor;
00375   
00376   bool success = true;
00377   bool result;
00378  
00379   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00380  
00381   Teuchos::RCP<Teuchos::FancyOStream>
00382     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00383  
00384   try {
00385     
00386     //
00387     // Read options from the commandline
00388     //
00389 
00390     CommandLineProcessor clp(false); // Don't throw exceptions
00391 
00392     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00393 
00394     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00395       *out << "\nEnd Result: TEST FAILED" << std::endl;
00396       return parse_return;
00397     }
00398 
00399     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00400 
00401     const int N = 8;
00402  
00403     result = testTuple<int,N>(*out);
00404     if (!result) success = false;
00405 
00406     result = testTuple<float,N>(*out);
00407     if (!result) success = false;
00408 
00409     result = testTuple<double,N>(*out);
00410     if (!result) success = false;
00411 
00412     result = testTuple<std::complex<double> ,N>(*out);
00413     if (!result) success = false;
00414  
00415   }
00416   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00417  
00418   if (success)
00419     *out << "\nEnd Result: TEST PASSED" << std::endl;
00420   else
00421     *out << "\nEnd Result: TEST FAILED" << std::endl;
00422  
00423   return ( success ? 0 : 1 );
00424  
00425 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines