FancyOutputting_test.cpp

00001 #include "Teuchos_VerboseObject.hpp"
00002 #include "Teuchos_StandardCatchMacros.hpp"
00003 #include "Teuchos_GlobalMPISession.hpp"
00004 #include "Teuchos_CommandLineProcessor.hpp"
00005 #include "Teuchos_StandardParameterEntryValidators.hpp"
00006 #include "Teuchos_dyn_cast.hpp"
00007 #include "Teuchos_Version.hpp"
00008 
00009 #include "AlgorithmA.hpp"
00010 
00011 
00012 //
00013 // Here is a simple driver function that I call over and over to show
00014 // different features of FancyOStream
00015 //
00016 
00017 void doAlgorithmStuff( Teuchos::ParameterList *algoParams = 0 )
00018 {
00019 
00020   // Here I just create the algorithm object that derives from VerboseObject.
00021   // By default, this object will print to *Verbose::getDefaultOStream()
00022   AlgorithmA algoA;
00023   if (algoParams)
00024     algoA.setParameterList(Teuchos::rcp(algoParams,false));
00025   // Note that here I could change the stream just this object prints to
00026   // by calling algoA.setOStream(...).
00027   
00028   // Now I call the algorithm which will print to its default output stream
00029   algoA.doAlgorithm();
00030   
00031   *algoA.getOStream() << std::endl;
00032 
00033   TEUCHOS_ASSERT(algoA.getParameterList().getRawPtr() == algoParams);
00034 
00035 }
00036 
00037 //
00038 // Test that static initailziation of VerboseObjectBase and VerboseObject works!
00039 //
00040 
00041 class TestVerboseObjectBaseInitialization {
00042 public:
00043   TestVerboseObjectBaseInitialization()
00044     {
00045       // Get the verbosity level for AlgorithmA
00046       Teuchos::EVerbosityLevel verbLevel = Teuchos::VerboseObject<AlgorithmA>::getDefaultVerbLevel();
00047       TEST_FOR_EXCEPT_PRINT(verbLevel!=Teuchos::VERB_DEFAULT,&std::cerr);
00048       // Print to the default default OStream to make sure that the initialization
00049       // trick worked!
00050       *Teuchos::VerboseObjectBase::getDefaultOStream()
00051         << "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
00052         << std::flush;
00053     }
00054 };
00055 
00056 static TestVerboseObjectBaseInitialization testVerboseObjectBaseInitialization;
00057 
00058 //
00059 // Main driver program
00060 //
00061 
00062 int main(int argc, char* argv[])
00063 {
00064 
00065   using Teuchos::RCP;
00066   using Teuchos::rcp;
00067   using Teuchos::FancyOStream;
00068   using Teuchos::VerboseObjectBase;
00069   using Teuchos::OSTab;
00070   using Teuchos::dyn_cast;
00071   using Teuchos::CommandLineProcessor;
00072 
00073   bool success = true;
00074 
00075   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00076   const int numProcs = Teuchos::GlobalMPISession::getNProc();
00077 
00078   try {
00079 
00080     // Get some commandline options
00081     CommandLineProcessor  clp;
00082     clp.throwExceptions(false);
00083     clp.addOutputSetupOptions(true);
00084     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00085     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00086 
00087     // Here I am just grabbing the default output stream
00088     RCP<FancyOStream>
00089       out = VerboseObjectBase::getDefaultOStream();
00090     // Note that the VerboseObject manages FancyOStream objects and not just
00091     // std::ostream objects.  This is important to the design and very
00092     // resonable I think.
00093 
00094     *out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
00095 
00096     //
00097     // Now I call doAlgorithmStuff() a bunch of times with different setups to
00098     // show the different kinds of line prefix options
00099     //
00100   
00101     *out << "\n***\n*** Testing VerboseObject base class use\n***\n";
00102   
00103     *out << "\n*** Algorithm output with default formatting\n\n";
00104     doAlgorithmStuff();
00105   
00106     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00107     *out << "\n*** Algorithm output with no front matter\n\n";
00108     out->setShowAllFrontMatter(false);
00109     doAlgorithmStuff();
00110   
00111     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00112     *out << "\n*** Algorithm output with processor ranks\n\n";
00113     out->setShowAllFrontMatter(false).setShowProcRank(true);
00114     doAlgorithmStuff();
00115   
00116     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00117     *out << "\n*** Algorithm output with line prefix names\n\n";
00118     out->setShowAllFrontMatter(false).setShowLinePrefix(true);
00119     doAlgorithmStuff();
00120   
00121     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00122     *out << "\n*** Algorithm output with tab counts\n\n";
00123     out->setShowAllFrontMatter(false).setShowTabCount(true);
00124     doAlgorithmStuff();
00125   
00126     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00127     *out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
00128     out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
00129     doAlgorithmStuff();
00130   
00131     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00132     *out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
00133     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
00134     doAlgorithmStuff();
00135   
00136     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00137     *out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
00138     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
00139     doAlgorithmStuff();
00140   
00141     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00142     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
00143     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00144     doAlgorithmStuff();
00145   
00146     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00147     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
00148     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_NONE);
00149     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00150     doAlgorithmStuff();
00151     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_DEFAULT);
00152 
00153     *out << "\n*** Running the algorithm by setting parameters in the parameter list ...\n";
00154 
00155     Teuchos::ParameterList algoParams("AlgorithmA");
00156 
00157     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00158     *out << "\n*** Set AlgorithmA verbosity level to extreme through a parameter list\n\n";
00159     algoParams.sublist("VerboseObject").set("Verbosity Level","extreme");
00160     algoParams.set("Algo Type","Harry");
00161     algoParams.set("Algo Tol",0.3);
00162     doAlgorithmStuff(&algoParams);
00163 
00164     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00165     *out << "\n*** Set AlgorithmA verbosity level to medium and the output file \"AlgorithmA.out\" through a parameter list\n\n";
00166     algoParams.sublist("VerboseObject").set("Verbosity Level","medium");
00167     algoParams.sublist("VerboseObject").set("Output File","AlgorithmA.out");
00168     algoParams.set("Algo Type","John");
00169     algoParams.set("Algo Tol",10);
00170     doAlgorithmStuff(&algoParams);
00171 
00172     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00173     *out << "\n*** Set AlgorithmA verbosity level to low and the output back to default through a parameter list\n\n";
00174     algoParams.sublist("VerboseObject").set("Verbosity Level","low");
00175     algoParams.sublist("VerboseObject").set("Output File","none");
00176     algoParams.set("Algo Tol","20");
00177     doAlgorithmStuff(&algoParams);
00178 
00179     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00180     *out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
00181 
00182     //
00183     // Now I do some other simple tests just to see that FancyOStream is working
00184     // correctly
00185     //
00186 
00187     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
00188     out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
00189     
00190     *out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
00191     
00192     *out << "\nThis is very good output\nand I like it a lot!\n";
00193     *out << "";
00194     *out << "\n";
00195     *out << "This should";
00196     *out << " all be";
00197     *out << " printed on";
00198     *out << " the same";
00199     *out << " line two lines below the above output!\n";
00200     RCP<FancyOStream>
00201       out2 = rcp(new FancyOStream(rcp(new std::ostringstream),"  "));
00202     {
00203       OSTab tab1(out);
00204       *out << "This should be indented one tab!\n";
00205       {
00206         OSTab tab2(out);
00207         *out << "This should be indented two tabs!\n";
00208         *out2 << "This should be indented zero tabs from out2!\n";
00209         {
00210           OSTab tab3(out2);
00211           *out << "This should be indented two tabs!\n";
00212           *out2 << "This should be indented one tab from out2!\n";
00213         }
00214       }
00215       *out << "This should be indented one tab!\n";
00216     }
00217     *out << "This should be indented zero tabs!\n";
00218     
00219     *out << std::endl; // This required overflow() to be overridden!
00220 
00221     *out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
00222          << dyn_cast<std::ostringstream>(*out2->getOStream()).str();
00223 
00224     if(success)
00225       *out << "\nEnd Result: TEST PASSED" << std::endl;
00226     
00227   }
00228   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00229     
00230   return ( success ? 0 : 1 );
00231   
00232 }

Generated on Wed Jul 14 14:35:25 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7