Intrepid
/usr/src/RPM/BUILD/trilinos10-10.6.4/packages/intrepid/test/Shared/FieldContainer/test_02.cpp
00001 // @HEADER
00002 // ************************************************************************
00003 //
00004 //                           Intrepid Package
00005 //                 Copyright (2007) 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 Pavel Bochev (pbboche@sandia.gov) or
00025 //                    Denis Ridzal (dridzal@sandia.gov).
00026 //
00027 // ************************************************************************
00028 // @HEADER
00029 
00030 
00036 #include "Intrepid_FieldContainer.hpp"
00037 #include "Teuchos_oblackholestream.hpp"
00038 #include "Teuchos_RCP.hpp"
00039 #include "Teuchos_GlobalMPISession.hpp"
00040 
00041 
00042 using namespace Intrepid;
00043 
00044 int main(int argc, char *argv[]) {
00045 
00046   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00047   
00048   // This little trick lets us print to cout  only if a (dummy) command-line argument is provided.
00049   int iprint     = argc - 1;
00050   
00051   Teuchos::RCP<std::ostream> outStream;
00052   Teuchos::oblackholestream bhs; // outputs nothing
00053   
00054   if (iprint > 0)
00055     outStream = Teuchos::rcp(&std::cout, false);
00056   else
00057     outStream = Teuchos::rcp(&bhs, false);
00058   
00059   // Save the format state of the original cout .
00060   Teuchos::oblackholestream oldFormatState;
00061   oldFormatState.copyfmt(std::cout);
00062   
00063   *outStream  \
00064     << "===============================================================================\n" \
00065     << "|                                                                             |\n" \
00066     << "|                           Unit Test FieldContainer                          |\n" \
00067     << "|                                                                             |\n" \
00068     << "|     1) Testing exception handling                                           |\n" \
00069     << "|       requires intrepid to be configured with --enable-intrepid-debug       |\n" \
00070     << "|                                                                             |\n" \
00071     << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov) or                   |\n" \
00072     << "|                      Denis Ridzal (dridzal@sandia.gov).                     |\n" \
00073     << "|                                                                             |\n" \
00074     << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00075     << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00076     << "|                                                                             |\n" \
00077     << "===============================================================================\n";
00078   
00079   // Test initializations
00080   int errorFlag  = 0;
00081   
00082   // Define variables to create and use FieldContainers
00083   Teuchos::Array<int> dimensions;
00084   Teuchos::Array<int> multiIndex;
00085   
00086   // Initialize dimensions for rank-4 multi-index value
00087   dimensions.resize(4);
00088   dimensions[0] = 5;
00089   dimensions[1] = 3;
00090   dimensions[2] = 2;
00091   dimensions[3] = 7;
00092   
00093   // Create a FieldContainer
00094   FieldContainer<double> myContainer(dimensions);
00095   
00096   // These tests should only run if intrepid was configured with --enable-intrepid-debug option
00097   // Each test is designed to cause an exception. The total number of all caught exceptions should
00098   // be the same as the number of tests in this section.
00099 #ifdef HAVE_INTREPID_DEBUG
00100   
00101   *outStream << "\n" \
00102     << "===============================================================================\n"\
00103     << "| TEST 1: Catching exceptions                                                 |\n"\
00104     << "===============================================================================\n\n";
00105   
00106   int numTestException =16;
00107   int beginThrowNumber = TestForException_getThrowNumber();
00108   int endThrowNumber = beginThrowNumber + numTestException;
00109   
00110   try{ // Outer try block contains all tests for exception
00111     
00112     
00113     try{ // catch exception (1)
00114       
00115       //  Trying to  get enumeration using multi-index with the wrong rank:
00116       *outStream << "\n" \
00117       << "===============================================================================\n"\
00118       << "  Trying to  get enumeration using multi-index with the wrong rank: \n";
00119       multiIndex.resize(5);
00120       multiIndex[0] = 3; 
00121       multiIndex[1] = 1;
00122       multiIndex[2] = 2;
00123       multiIndex[3] = 2;
00124       multiIndex[4] = 6;
00125       myContainer.getEnumeration(multiIndex);
00126     }
00127     catch (std::logic_error err) {
00128       *outStream  << err.what() << "\n";
00129     };
00130     
00131     
00132     
00133     try{ // catch exception (2)
00134       
00135       // Trying to get enumeration using multi-index that is out of bounds: 3rd index is 4, must be <2
00136       *outStream << "\n" \
00137       << "===============================================================================\n"\
00138       << " Trying to get enumeration using multi-index that is out of bounds: \n";
00139       multiIndex.resize(4);
00140       multiIndex[0] = 3; 
00141       multiIndex[1] = 1;
00142       multiIndex[2] = 4;
00143       multiIndex[3] = 2;
00144       myContainer.getEnumeration(multiIndex);
00145     }
00146     catch (std::logic_error err) {
00147       *outStream  << err.what() << "\n";
00148     };
00149     
00150  
00151     
00152     try{  // catch exception (3)
00153       
00154       // Trying to set values from array whose size is less than FieldContainer's size:
00155       *outStream << "\n" \
00156       << "===============================================================================\n"\
00157       <<  " Trying to set values from array whose size is less than FieldContainer's size: \n";
00158       
00159       // Change one of the values of the dimensions to a lesser value: original value was 5 
00160       dimensions[0] = 4;
00161       
00162       // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00163       Teuchos::Array<double> dataTeuchosArray(4*3*2*7);
00164       
00165       // Fill with data
00166       int counter = 0;
00167       for(int i=0; i < dimensions[0]; i++){
00168         for(int j=0; j < dimensions[1]; j++){
00169           for(int k=0; k < dimensions[2]; k++){
00170             for(int l = 0; l < dimensions[3]; l++){
00171               dataTeuchosArray[counter] = (double)counter;
00172               counter++;
00173             }
00174           }
00175         }
00176       }
00177       
00178       // Now try to stuff this data into FieldContainer
00179       myContainer.setValues(dataTeuchosArray);
00180     }
00181     catch (std::logic_error err) {
00182       *outStream  << err.what() << "\n";
00183     };
00184     
00185     
00186     
00187     try{  // catch exception (4)
00188       
00189       // Trying to set values from array whose size is greater than FieldContainer's size:
00190       *outStream << "\n" \
00191       << "===============================================================================\n"\
00192       <<  " Trying to set values from array whose size is greater than FieldContainer's size: \n";
00193       
00194       // Change one of the values of the dimensions to a lesser value: restore dimensions[0] to the 
00195       // value used to construct the LexArray and change dimensions[2] to a greater value
00196       dimensions[0] = 5;
00197       dimensions[2] = 3;
00198       
00199       // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00200       Teuchos::ArrayRCP<double> dataTeuchosArray(5*3*3*7);
00201       
00202       // Fill with data
00203       int counter = 0;
00204       for(int i=0; i < dimensions[0]; i++){
00205         for(int j=0; j < dimensions[1]; j++){
00206           for(int k=0; k < dimensions[2]; k++){
00207             for(int l = 0; l < dimensions[3]; l++){
00208               dataTeuchosArray[counter] = (double)counter;
00209               counter++;
00210             }
00211           }
00212         }
00213       }
00214       
00215       // Now try to stuff this data into FieldContainer
00216       myContainer.setValues(dataTeuchosArray);
00217     }
00218     catch (std::logic_error err) {
00219       *outStream  << err.what() << "\n";
00220     };
00221     
00222     
00223     
00224     try{ // catch exception (5)
00225       
00226       // Trying to use [] with enumeration that is out of range:
00227       *outStream << "\n" \
00228       << "===============================================================================\n"\
00229       << " Trying to use [] with enumeration that is out of range: \n";
00230       myContainer[1000];
00231     }
00232     catch (std::logic_error err) {
00233       *outStream  << err.what() << "\n";
00234     }
00235     
00236     
00237     
00238     try{ // catch exception (6)
00239       
00240       // Trying to get multi-index from enumeration that is out of bounds:
00241       *outStream << "\n" \
00242       << "===============================================================================\n"\
00243       << " Trying to get multi-index from enumeration that is out of bounds: \n";
00244       myContainer.getMultiIndex(multiIndex,10000);
00245     }
00246     catch(std::logic_error err) {
00247      *outStream << err.what() << "\n";
00248     }
00249     
00250   
00251     
00252     try{ // catch exception (7)
00253       
00254       //Trying to self-assign FieldContainer
00255       *outStream << "\n" \
00256       << "===============================================================================\n"\
00257       << " Trying to self-assign FieldContainer \n";
00258       myContainer = myContainer;
00259     }
00260     catch(std::logic_error err) {
00261       *outStream << err.what() << "\n"; 
00262     }
00263 
00264     
00265     // Container of rank-1
00266     FieldContainer<double> rank1Container(3);
00267     
00268     // catch exception (8): method is for rank-2 container
00269     try{  
00270       *outStream << "\n" \
00271       << "===============================================================================\n"\
00272       << " using a method for rank-2 container \n";
00273       rank1Container.getEnumeration(1,1); 
00274     }
00275     catch(std::logic_error err){
00276       *outStream << err.what() << "\n";
00277     }
00278     
00279     // catch exception (9): method is for rank-3 container
00280     try{  
00281       *outStream << "\n" \
00282       << "===============================================================================\n"\
00283       << " using a method for rank-3 container \n";
00284       rank1Container.getEnumeration(1,1,1); 
00285     }
00286     catch(std::logic_error err){
00287       *outStream << err.what() << "\n";
00288     }
00289     
00290     // catch exception (10): method is for rank-4 container
00291     try{  
00292       *outStream << "\n" \
00293       << "===============================================================================\n"\
00294       << " using a method for rank-4 container \n";
00295       rank1Container.getEnumeration(1,1,1,1); 
00296     }
00297     catch(std::logic_error err){
00298       *outStream << err.what() << "\n";
00299     }
00300     
00301     // catch exception (11): method is for rank-5 container
00302     try{  
00303       *outStream << "\n" \
00304       << "===============================================================================\n"\
00305       << " using a method for rank-5 container \n";
00306       rank1Container.getEnumeration(1,1,1,1,1); 
00307     }
00308     catch(std::logic_error err){
00309       *outStream << err.what() << "\n";
00310     }
00311     
00312     // catch exception (12): 4 is out of bounds
00313     try{  
00314       *outStream << "\n" \
00315       << "===============================================================================\n"\
00316       << " The specified enumeration is out of bounds \n";
00317       int i0;
00318       rank1Container.getMultiIndex(i0,4); 
00319     }
00320     catch(std::logic_error err){
00321       *outStream << err.what() << "\n";
00322     }
00323     
00324     // catch exception (13): method for rank-2 container
00325     try{  
00326       *outStream << "\n" \
00327       << "===============================================================================\n"\
00328       << " Using a method for rank-2 containers \n";
00329       int i0,i1;
00330       rank1Container.getMultiIndex(i0,i1,2); 
00331     }
00332     catch(std::logic_error err){
00333       *outStream << err.what() << "\n";
00334     }
00335     
00336     // catch exception (14): method for rank-3 container
00337     try{  
00338       *outStream << "\n" \
00339       << "===============================================================================\n"\
00340       << " Using a method for rank-2 containers \n";
00341       int i0,i1,i2;
00342       rank1Container.getMultiIndex(i0,i1,i2,2); 
00343     }
00344     catch(std::logic_error err){
00345       *outStream << err.what() << "\n";
00346     }
00347     
00348     
00349     // catch exception (15): method for rank-4 container
00350     try{  
00351       *outStream << "\n" \
00352       << "===============================================================================\n"\
00353       << " Using a method for rank-4 containers \n";
00354       int i0,i1,i2,i3;
00355       rank1Container.getMultiIndex(i0,i1,i2,i3,2); 
00356     }
00357     catch(std::logic_error err){
00358       *outStream << err.what() << "\n";
00359     }
00360     
00361     
00362     // catch exception (16): method for rank-5 container
00363     try{  
00364       *outStream << "\n" \
00365       << "===============================================================================\n"\
00366       << " Using a method for rank-5 containers \n";
00367       int i0,i1,i2,i3,i4;
00368       rank1Container.getMultiIndex(i0,i1,i2,i3,i4,2); 
00369     }
00370     catch(std::logic_error err){
00371       *outStream << err.what() << "\n";
00372     }
00373     
00374     
00375     // Check if number of caught exceptions matches the expected number
00376     if (TestForException_getThrowNumber() != endThrowNumber) {
00377       errorFlag++;
00378       
00379     }
00380   } // outer try block
00381   catch (std::logic_error err) {
00382     *outStream << "UNEXPECTED ERROR !!! ----------------------------------------------------------\n";
00383     *outStream  << err.what() << "\n";
00384     *outStream << "-------------------------------------------------------------------------------" << "\n\n";
00385     errorFlag = -1000;
00386   }
00387 #endif
00388   
00389   if (errorFlag != 0)
00390     std::cout << "End Result: TEST FAILED\n";
00391   else
00392     std::cout << "End Result: TEST PASSED\n";
00393   
00394   // reset format state of std::cout
00395   std::cout.copyfmt(oldFormatState);
00396   
00397   return errorFlag;
00398 }