Teuchos Package Browser (Single Doxygen Collection) Version of the Day
test/Containers/cxx_main.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_Hashtable.hpp"
00030 #include "Teuchos_HashSet.hpp"
00031 #include "Teuchos_GlobalMPISession.hpp"
00032 #include "Teuchos_MPIContainerComm.hpp"
00033 #include "Teuchos_ErrorPolling.hpp"
00034 #include "Teuchos_StrUtils.hpp"
00035 #include "Teuchos_Version.hpp"
00036 
00037 using namespace Teuchos;
00038 using std::string;
00039 
00040 /* Test of Teuchos container classes */
00041 
00042 int main( int argc, char* argv[] )
00043 {
00044   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00045 
00046   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00047 
00048   try
00049     {
00050 
00051       if (MPIComm::world().getRank() == 0)
00052         {
00053           /*------- do several tests of the Array class --------------- */
00054 
00055           /* You can create an empty array and append to it. */
00056           Array<double> x;
00057           x.append(1.0);
00058           x.append(4.0);
00059           x.append(9.0);
00060 
00061           /* there is a toString() method of Array that writes a list bounded by
00062            * curly braces */
00063           std::cerr << "x = " << x.toString() << std::endl;
00064 
00065 
00066           /* You can create an array of a specified size */
00067           Array<double> y(3);
00068 
00069           /* Array elements can be set using the [] indexing operator */
00070           y[0] = 3.14;
00071           y[1] = 2.72;
00072           y[2] = 1.42;
00073 
00074           /* Array elements can be read using the const [] indexing operator */
00075           for (int i=0; i<y.length(); i++)
00076             {
00077               std::fprintf(stderr, "%d %g\n", i, y[i]);
00078             }
00079 
00080           /* If compiled with boundschecking, Array will catch bounds violations. */
00081           if (Array<double>::hasBoundsChecking())
00082             {
00083               bool caughtBoundsError = false;
00084               try
00085                 {
00086                   double z = y[10];
00087                   (void)z;
00088                 }
00089               catch(std::exception& eb)
00090                 {
00091                   caughtBoundsError = true;
00092                   std::cerr << "caught [expected] bounds error: \n" <<  eb.what() << std::endl;
00093                 }
00094               if (!caughtBoundsError)
00095                 {
00096                   std::cerr << "FAILED TO CATCH BOUNDS ERROR" << std::endl;
00097                 }
00098             }
00099           else
00100             {
00101               std::cerr << "Teuchos compiled w/o array boundschecking" << std::endl;
00102             }
00103 
00104 
00105           /*------------ test packing of Arrays ------------------- */
00106 
00107           std::cerr << "testing packing of arrays..." << std::endl;
00108           Array<std::string> schools;
00109           schools.append("Cornell");
00110           schools.append("Princeton");
00111           schools.append("Carnegie Mellon");
00112           schools.append("North Carolina");
00113           schools.append("Maryland");
00114           schools.append("Caltech");
00115           schools.append("Chicago");
00116 
00117           Array<char> packed;
00118           MPIContainerComm<std::string>::pack(schools, packed);
00119 
00120           Array<std::string> unpacked;
00121           MPIContainerComm<std::string>::unpack(packed, unpacked);
00122 
00123           bool ok = true;
00124           if (unpacked.size() != schools.size())
00125             {
00126               ok = false;
00127             }
00128           else
00129             {
00130               for (unsigned int i=0; i<schools.size(); i++)
00131                 {
00132                   if (unpacked[i] != schools[i])
00133                     {
00134                       ok = false;
00135                     }
00136                 }
00137             }
00138       
00139           if (!ok)
00140             {
00141               std::cerr << "pack/unpack FAILED!" << std::endl;
00142               std::cerr << "original: " << schools << std::endl;
00143               std::cerr << "unpacked: " << unpacked << std::endl;
00144             }
00145           else
00146             {
00147               std::cerr << "pack/unpack OK" << std::endl;
00148             }
00149         }
00150 
00151       /*------------ test gathering of Arrays ------------------- */
00152       
00153       Array<std::string> teams;
00154       int rank = MPIComm::world().getRank();
00155       if (rank==0)
00156         {
00157           teams.append("Orioles");
00158           teams.append("Yankees");
00159           teams.append("Pirates");
00160         }
00161       else if (rank==1)
00162         {
00163           teams.append("Ravens");
00164           teams.append("Jets");
00165           teams.append("Steelers");
00166           teams.append("Colts");
00167           teams.append("Raiders");
00168         }
00169       else 
00170         {
00171           teams.append("Bruins");
00172           teams.append("Rangers");
00173         }
00174 
00175       Array<Array<std::string> > allTeams;
00176       MPIContainerComm<std::string>::gatherv(teams, allTeams, 0, MPIComm::world());
00177 
00178       if (rank==0)
00179         {
00180           std::cout << "all teams = " << allTeams << std::endl;
00181         }
00182       
00183 
00184       /*------------ test polling of exceptions across procs --------- */
00185       
00186       try
00187         {
00188           try
00189             {
00190               TEST_FOR_EXCEPTION(MPIComm::world().getRank()==1,
00191                               std::runtime_error, 
00192                               "std::exception [expected]");
00193             }
00194           catch(std::exception& ex1)
00195             {
00196               std::cerr << "successful detection of std::exception on proc="
00197                    << MPIComm::world().getRank() << std::endl;
00198               ErrorPolling::reportFailure(MPIComm::world());
00199               TEUCHOS_TRACE(ex1);
00200             }
00201           TEUCHOS_POLL_FOR_FAILURES(MPIComm::world());
00202         }
00203       catch(std::exception& ex)
00204         {
00205           std::cerr << ex.what() << std::endl;
00206         }
00207       std::cerr << "p=" << MPIComm::world().getRank() 
00208            << ": std::exception polling successful" << std::endl;
00209 
00210       if (MPIComm::world().getRank()==0)
00211         {
00212       
00213 
00214           /*-------- do several tests of the HashSet class ------------- */
00215       
00216           /* Add entries to a set using the put method */
00217           HashSet<std::string> trilinosPackages;
00218           trilinosPackages.put("epetra");
00219           trilinosPackages.put("ml");
00220           trilinosPackages.put("TSF");
00221           trilinosPackages.put("nox");
00222           trilinosPackages.put("meros");
00223       
00224           /* count entries using the size() method */
00225           std::fprintf(stderr, "trilinos has %d packages\n", trilinosPackages.size());
00226 
00227           /* write to a std::string using the toString() method */
00228           std::cerr << "trilinos packages are: " << trilinosPackages.toString() << std::endl;
00229 
00230           /* test for the presence of a member using the containsKey() method */
00231       
00232           if (trilinosPackages.containsKey("epetra"))
00233             {
00234               std::cerr << "epetra is in the list of trilinos packages" << std::endl;
00235             }
00236           else
00237             {
00238               std::cerr << "epetra is not in the list of trilinos packages" << std::endl;
00239             }
00240 
00241           if (trilinosPackages.containsKey("Space Invaders"))
00242             {
00243               std::cerr << "Space Invaders is in the list of trilinos packages" << std::endl;
00244             }
00245           else
00246             {
00247               std::cerr << "Space Invaders is not in the list of trilinos packages" << std::endl;
00248             }
00249 
00250 
00251           /*-------------- do several tests of the Hashtable class -------- */
00252 
00253           /* add entries using the put() method */
00254           Hashtable<std::string, int> battles;
00255       
00256           battles.put("hastings",    1066);
00257           battles.put("waterloo",    1815);
00258           battles.put("gettysburg",  1863);
00259           battles.put("verdun",      1916);
00260           battles.put("midway",      1942);
00261           battles.put("normandy",    1944);
00262 
00263           /* write to a std::string using the toString() method */
00264           std::cerr << "hashtable is: " << battles.toString() << std::endl;
00265       
00266           /* test for the presence of a key using the containsKey() method */
00267           if (battles.containsKey("cannae"))
00268             {
00269               std::fprintf(stderr, "the battle of cannae occured in %d\n", battles.get("cannae"));
00270             }
00271           else
00272             {
00273               std::cerr << "cannae is not in our hashtable" << std::endl;
00274             }
00275 
00276           /* test for the presence of a key using the containsKey() method */
00277           if (battles.containsKey("verdun"))
00278             {
00279               std::fprintf(stderr, "the battle of verdun occured in %d\n", battles.get("verdun"));
00280             }
00281           else
00282             {
00283               std::cerr << "verdun is not in our hashtable" << std::endl;
00284             }
00285 
00286           /* remove a member of the hashtable (bug# 2983)*/
00287           battles.remove( "waterloo" );
00288 
00289           /* write to a std::string using the toString() method */
00290           std::cerr << "hashtable is (after removal of waterloo): " << battles.toString() << std::endl;
00291    
00292 
00293           /*-------------- do several tests of the StrUtils class --------- */
00294 
00295           /* stringTokenizer() splits a std::string into whitespace-delimited tokens */
00296           std::string test = "Sandia National Laboratories";
00297 
00298           Array<std::string> tokens = StrUtils::stringTokenizer(test);
00299 
00300           std::cerr << "tokens = " << tokens.toString() << std::endl;
00301 
00302           /* atof() converts a std::string to its double value */
00303           double pi = StrUtils::atof("3.14159265358");
00304           std::fprintf(stderr, "pi = %g, tan(pi/4)=%g\n", pi, std::tan(pi/4.0));
00305 
00306           /* atoi() converts a std::string to its integer value */
00307           int a = StrUtils::atoi("-101");
00308           std::fprintf(stderr, "a = %d\n", a);
00309 
00310           /* allCaps() converts to upper case */
00311           std::cerr << "all caps: " << StrUtils::allCaps(test) << std::endl;
00312         }      
00313     }
00314   catch(std::exception& e)
00315     {
00316       std::cerr << e.what() << std::endl;
00317     }
00318 
00319 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines