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   // mfh 03 Apr 2014: The point of the above line of code is to ensure
00116   // that ArrayView<T> has a public size_type typedef.  However, the
00117   // above line of code in isolation causes some compilers to warn
00118   // about a declared but unused typedef.  We deal with this by
00119   // declaring a variable (actually, the oxymoron "const variable") of
00120   // type size_type, then using the "cast to void" trick to forestall
00121   // compiler warnings for the declared but unused variable.  (Fun
00122   // fact: "oxymoron" means "sharp dull" and is itself an oxymoron.)
00123   // The "cast to void" trick doesn't always work, but if it doesn't,
00124   // it's easy to make it go away by printing it to the output stream
00125   // 'out'.
00126   const size_type arbitrarySizeTypeValue = 0;
00127   (void) arbitrarySizeTypeValue;
00128 
00129   bool success = true;
00130 
00131   out
00132     << "\n***"
00133     << "\n*** Testing "<<TypeNameTraits<Tuple<T,N> >::name()<<" of size = "<<N
00134     << "\n***\n";
00135 
00136   Teuchos::OSTab tab(out);
00137 
00138   //
00139   out << "\nA) Initial setup testing ...\n\n";
00140   //
00141 
00142   Tuple<T,N> t;
00143   TEST_EQUALITY_CONST(t.size(),N);
00144   for( int i = 0; i < N; ++i )
00145     t[i] = i; // tests non-const operator[](i)
00146 
00147   {
00148     out << "\nTest that t[i] == i ... ";
00149     const ArrayView<const T> cav2 = t;
00150     bool local_success = true;
00151     for( int i = 0; i < N; ++i ) {
00152       TEST_ARRAY_ELE_EQUALITY( cav2, i, as<T>(i) );
00153     }
00154     if (local_success) out << "passed\n";
00155     else success = false;
00156   }
00157 
00158   {
00159     const int n = 1;
00160     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00161     Tuple<T,n> tn = tuple<T>(0);
00162     TEST_EQUALITY_CONST(tn.size(),n);
00163     out << "Test that tn[i] == i ... ";
00164     bool local_success = true;
00165     for( int i = 0; i < n; ++i ) {
00166       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00167     }
00168     if (local_success) out << "passed\n";
00169     else success = false;
00170   }
00171 
00172   {
00173     const int n = 2;
00174     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00175     Tuple<T,n> tn = tuple<T>(0,1);
00176     TEST_EQUALITY_CONST(tn.size(),n);
00177     out << "Test that tn[i] == i ... ";
00178     bool local_success = true;
00179     for( int i = 0; i < n; ++i ) {
00180       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00181     }
00182     if (local_success) out << "passed\n";
00183     else success = false;
00184   }
00185 
00186   {
00187     const int n = 3;
00188     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00189     Tuple<T,n> tn = tuple<T>(0,1,2);
00190     TEST_EQUALITY_CONST(tn.size(),n);
00191     out << "Test that tn[i] == i ... ";
00192     bool local_success = true;
00193     for( int i = 0; i < n; ++i ) {
00194       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00195     }
00196     if (local_success) out << "passed\n";
00197     else success = false;
00198   }
00199 
00200   {
00201     const int n = 4;
00202     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00203     Tuple<T,n> tn = tuple<T>(0,1,2,3);
00204     TEST_EQUALITY_CONST(tn.size(),n);
00205     out << "Test that tn[i] == i ... ";
00206     bool local_success = true;
00207     for( int i = 0; i < n; ++i ) {
00208       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00209     }
00210     if (local_success) out << "passed\n";
00211     else success = false;
00212   }
00213 
00214   {
00215     const int n = 5;
00216     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00217     Tuple<T,n> tn = tuple<T>(0,1,2,3,4);
00218     TEST_EQUALITY_CONST(tn.size(),n);
00219     out << "Test that tn[i] == i ... ";
00220     bool local_success = true;
00221     for( int i = 0; i < n; ++i ) {
00222       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00223     }
00224     if (local_success) out << "passed\n";
00225     else success = false;
00226   }
00227 
00228   {
00229     const int n = 6;
00230     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00231     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5);
00232     TEST_EQUALITY_CONST(tn.size(),n);
00233     out << "Test that tn[i] == i ... ";
00234     bool local_success = true;
00235     for( int i = 0; i < n; ++i ) {
00236       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00237     }
00238     if (local_success) out << "passed\n";
00239     else success = false;
00240   }
00241 
00242   {
00243     const int n = 7;
00244     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00245     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6);
00246     TEST_EQUALITY_CONST(tn.size(),n);
00247     out << "Test that tn[i] == i ... ";
00248     bool local_success = true;
00249     for( int i = 0; i < n; ++i ) {
00250       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00251     }
00252     if (local_success) out << "passed\n";
00253     else success = false;
00254   }
00255 
00256   {
00257     const int n = 8;
00258     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00259     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7);
00260     TEST_EQUALITY_CONST(tn.size(),n);
00261     out << "Test that tn[i] == i ... ";
00262     bool local_success = true;
00263     for( int i = 0; i < n; ++i ) {
00264       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00265     }
00266     if (local_success) out << "passed\n";
00267     else success = false;
00268   }
00269 
00270   {
00271     const int n = 9;
00272     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00273     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8);
00274     TEST_EQUALITY_CONST(tn.size(),n);
00275     out << "Test that tn[i] == i ... ";
00276     bool local_success = true;
00277     for( int i = 0; i < n; ++i ) {
00278       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00279     }
00280     if (local_success) out << "passed\n";
00281     else success = false;
00282   }
00283 
00284   {
00285     const int n = 10;
00286     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00287     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9);
00288     TEST_EQUALITY_CONST(tn.size(),n);
00289     out << "Test that tn[i] == i ... ";
00290     bool local_success = true;
00291     for( int i = 0; i < n; ++i ) {
00292       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00293     }
00294     if (local_success) out << "passed\n";
00295     else success = false;
00296   }
00297 
00298   {
00299     const int n = 11;
00300     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00301     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10);
00302     TEST_EQUALITY_CONST(tn.size(),n);
00303     out << "Test that tn[i] == i ... ";
00304     bool local_success = true;
00305     for( int i = 0; i < n; ++i ) {
00306       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00307     }
00308     if (local_success) out << "passed\n";
00309     else success = false;
00310   }
00311 
00312   {
00313     const int n = 12;
00314     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00315     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11);
00316     TEST_EQUALITY_CONST(tn.size(),n);
00317     out << "Test that tn[i] == i ... ";
00318     bool local_success = true;
00319     for( int i = 0; i < n; ++i ) {
00320       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00321     }
00322     if (local_success) out << "passed\n";
00323     else success = false;
00324   }
00325 
00326   {
00327     const int n = 13;
00328     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00329     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12);
00330     TEST_EQUALITY_CONST(tn.size(),n);
00331     out << "Test that tn[i] == i ... ";
00332     bool local_success = true;
00333     for( int i = 0; i < n; ++i ) {
00334       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00335     }
00336     if (local_success) out << "passed\n";
00337     else success = false;
00338   }
00339 
00340   {
00341     const int n = 14;
00342     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00343     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13);
00344     TEST_EQUALITY_CONST(tn.size(),n);
00345     out << "Test that tn[i] == i ... ";
00346     bool local_success = true;
00347     for( int i = 0; i < n; ++i ) {
00348       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00349     }
00350     if (local_success) out << "passed\n";
00351     else success = false;
00352   }
00353 
00354   {
00355     const int n = 15;
00356     out << "\nTest Tuple<T,"<<n<<"> = tuple(...)\n";
00357     Tuple<T,n> tn = tuple<T>(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14);
00358     TEST_EQUALITY_CONST(tn.size(),n);
00359     out << "Test that tn[i] == i ... ";
00360     bool local_success = true;
00361     for( int i = 0; i < n; ++i ) {
00362       TEST_ARRAY_ELE_EQUALITY( tn, i, as<T>(i) );
00363     }
00364     if (local_success) out << "passed\n";
00365     else success = false;
00366   }
00367 
00368   {
00369     out << "\nTest constructing Array<const T> from Tuple<T,N> ...\n";
00370     const ArrayView<const T> av2 = t;
00371     TEST_COMPARE_ARRAYS( av2, t );
00372   }
00373 
00374   // ToDo: Add more tests!
00375 
00376   return success;
00377 
00378 }
00379 
00380 
00381 //
00382 // Main testing program
00383 //
00384 
00385 int main( int argc, char* argv[] ) {
00386 
00387   using Teuchos::CommandLineProcessor;
00388 
00389   bool success = true;
00390   bool result;
00391 
00392   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00393 
00394   Teuchos::RCP<Teuchos::FancyOStream>
00395     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00396 
00397   try {
00398 
00399     //
00400     // Read options from the commandline
00401     //
00402 
00403     CommandLineProcessor clp(false); // Don't throw exceptions
00404 
00405     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00406 
00407     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00408       *out << "\nEnd Result: TEST FAILED" << std::endl;
00409       return parse_return;
00410     }
00411 
00412     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00413 
00414     const int N = 8;
00415 
00416     result = testTuple<int,N>(*out);
00417     if (!result) success = false;
00418 
00419     result = testTuple<float,N>(*out);
00420     if (!result) success = false;
00421 
00422     result = testTuple<double,N>(*out);
00423     if (!result) success = false;
00424 
00425     result = testTuple<std::complex<double> ,N>(*out);
00426     if (!result) success = false;
00427 
00428   }
00429   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00430 
00431   if (success)
00432     *out << "\nEnd Result: TEST PASSED" << std::endl;
00433   else
00434     *out << "\nEnd Result: TEST FAILED" << std::endl;
00435 
00436   return ( success ? 0 : 1 );
00437 
00438 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines