Teuchos Package Browser (Single Doxygen Collection) Version of the Day
FancyOutputting_test.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_VerboseObject.hpp"
00045 #include "Teuchos_StandardCatchMacros.hpp"
00046 #include "Teuchos_GlobalMPISession.hpp"
00047 #include "Teuchos_CommandLineProcessor.hpp"
00048 #include "Teuchos_StandardParameterEntryValidators.hpp"
00049 #include "Teuchos_dyn_cast.hpp"
00050 #include "Teuchos_Version.hpp"
00051 
00052 #include "AlgorithmA.hpp"
00053 
00054 
00055 //
00056 // Here is a simple driver function that I call over and over to show
00057 // different features of FancyOStream
00058 //
00059 
00060 void doAlgorithmStuff( Teuchos::ParameterList *algoParams = 0 )
00061 {
00062 
00063   // Here I just create the algorithm object that derives from VerboseObject.
00064   // By default, this object will print to *Verbose::getDefaultOStream()
00065   AlgorithmA algoA;
00066   if (algoParams)
00067     algoA.setParameterList(Teuchos::rcp(algoParams,false));
00068   // Note that here I could change the stream just this object prints to
00069   // by calling algoA.setOStream(...).
00070   
00071   // Now I call the algorithm which will print to its default output stream
00072   algoA.doAlgorithm();
00073   
00074   *algoA.getOStream() << std::endl;
00075 
00076   TEUCHOS_ASSERT(algoA.getParameterList().getRawPtr() == algoParams);
00077 
00078 }
00079 
00080 //
00081 // Test that static initialization of VerboseObjectBase and VerboseObject works!
00082 //
00083 
00084 class TestVerboseObjectBaseInitialization {
00085 public:
00086   TestVerboseObjectBaseInitialization()
00087     {
00088       // Get the verbosity level for AlgorithmA
00089       Teuchos::EVerbosityLevel verbLevel = Teuchos::VerboseObject<AlgorithmA>::getDefaultVerbLevel();
00090       TEUCHOS_TEST_FOR_EXCEPT_PRINT(verbLevel!=Teuchos::VERB_DEFAULT,&std::cerr);
00091       // Print to the default default OStream to make sure that the initialization
00092       // trick worked!
00093       *Teuchos::VerboseObjectBase::getDefaultOStream()
00094         << "\n***\n*** Printing to default OStream before main() even starts!\n***\n\n"
00095         << std::flush;
00096     }
00097 };
00098 
00099 static TestVerboseObjectBaseInitialization testVerboseObjectBaseInitialization;
00100 
00101 //
00102 // Main driver program
00103 //
00104 
00105 int main(int argc, char* argv[])
00106 {
00107 
00108   using Teuchos::RCP;
00109   using Teuchos::rcp;
00110   using Teuchos::FancyOStream;
00111   using Teuchos::VerboseObjectBase;
00112   using Teuchos::OSTab;
00113   using Teuchos::dyn_cast;
00114   using Teuchos::CommandLineProcessor;
00115 
00116   bool success = true;
00117 
00118   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00119   const int numProcs = Teuchos::GlobalMPISession::getNProc();
00120 
00121   try {
00122 
00123     // Get some commandline options
00124     CommandLineProcessor  clp;
00125     clp.throwExceptions(false);
00126     clp.addOutputSetupOptions(true);
00127     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00128     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00129 
00130     // Here I am just grabbing the default output stream
00131     RCP<FancyOStream>
00132       out = VerboseObjectBase::getDefaultOStream();
00133     // Note that the VerboseObject manages FancyOStream objects and not just
00134     // std::ostream objects.  This is important to the design and very
00135     // resonable I think.
00136 
00137     *out << std::endl << Teuchos::Teuchos_Version() << std::endl << std::endl;
00138 
00139     //
00140     // Now I call doAlgorithmStuff() a bunch of times with different setups to
00141     // show the different kinds of line prefix options
00142     //
00143   
00144     *out << "\n***\n*** Testing VerboseObject base class use\n***\n";
00145   
00146     *out << "\n*** Algorithm output with default formatting\n\n";
00147     doAlgorithmStuff();
00148   
00149     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00150     *out << "\n*** Algorithm output with no front matter\n\n";
00151     out->setShowAllFrontMatter(false);
00152     doAlgorithmStuff();
00153   
00154     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00155     *out << "\n*** Algorithm output with processor ranks\n\n";
00156     out->setShowAllFrontMatter(false).setShowProcRank(true);
00157     doAlgorithmStuff();
00158   
00159     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00160     *out << "\n*** Algorithm output with line prefix names\n\n";
00161     out->setShowAllFrontMatter(false).setShowLinePrefix(true);
00162     doAlgorithmStuff();
00163   
00164     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00165     *out << "\n*** Algorithm output with tab counts\n\n";
00166     out->setShowAllFrontMatter(false).setShowTabCount(true);
00167     doAlgorithmStuff();
00168   
00169     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00170     *out << "\n*** Algorithm output with line prefix names and tab counts\n\n";
00171     out->setShowAllFrontMatter(false).setShowLinePrefix(true).setShowTabCount(true);
00172     doAlgorithmStuff();
00173   
00174     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00175     *out << "\n*** Algorithm output with processor ranks and line prefix names\n\n";
00176     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true);
00177     doAlgorithmStuff();
00178   
00179     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00180     *out << "\n*** Algorithm output with processor ranks and tab counts\n\n";
00181     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowTabCount(true);
00182     doAlgorithmStuff();
00183   
00184     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00185     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts\n\n";
00186     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00187     doAlgorithmStuff();
00188   
00189     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00190     *out << "\n*** Algorithm output with processor ranks, line prefix names, and tab counts but no output for AlgorithmA\n\n";
00191     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_NONE);
00192     out->setShowAllFrontMatter(false).setShowProcRank(true).setShowLinePrefix(true).setShowTabCount(true);
00193     doAlgorithmStuff();
00194     Teuchos::VerboseObject<AlgorithmA>::setDefaultVerbLevel(Teuchos::VERB_DEFAULT);
00195 
00196     *out << "\n*** Running the algorithm by setting parameters in the parameter list ...\n";
00197 
00198     Teuchos::ParameterList algoParams("AlgorithmA");
00199 
00200     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00201     *out << "\n*** Set AlgorithmA verbosity level to extreme through a parameter list\n\n";
00202     algoParams.sublist("VerboseObject").set("Verbosity Level","extreme");
00203     algoParams.set("Algo Type","Harry");
00204     algoParams.set("Algo Tol",0.3);
00205     doAlgorithmStuff(&algoParams);
00206 
00207     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00208     *out << "\n*** Set AlgorithmA verbosity level to medium and the output file \"AlgorithmA.out\" through a parameter list\n\n";
00209     algoParams.sublist("VerboseObject").set("Verbosity Level","medium");
00210     algoParams.sublist("VerboseObject").set("Output File","AlgorithmA.out");
00211     algoParams.set("Algo Type","John");
00212     algoParams.set("Algo Tol",10);
00213     doAlgorithmStuff(&algoParams);
00214 
00215     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00216     *out << "\n*** Set AlgorithmA verbosity level to low and the output back to default through a parameter list\n\n";
00217     algoParams.sublist("VerboseObject").set("Verbosity Level","low");
00218     algoParams.sublist("VerboseObject").set("Output File","none");
00219     algoParams.set("Algo Tol","20");
00220     doAlgorithmStuff(&algoParams);
00221 
00222     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1);
00223     *out << "\n***\n*** Do some more simple tests to make sure things work correctly\n***\n\n";
00224 
00225     //
00226     // Now I do some other simple tests just to see that FancyOStream is working
00227     // correctly
00228     //
00229 
00230     out->setShowAllFrontMatter(false).setShowProcRank(numProcs>1).setShowTabCount(true);
00231     out->setProcRankAndSize(mpiSession.getRank(),mpiSession.getNProc());
00232     
00233     *out << "\n***\n*** Testing basic FancyOStream and OSTab classes\n***\n\n";
00234     
00235     *out << "\nThis is very good output\nand I like it a lot!\n";
00236     *out << "";
00237     *out << "\n";
00238     *out << "This should";
00239     *out << " all be";
00240     *out << " printed on";
00241     *out << " the same";
00242     *out << " line two lines below the above output!\n";
00243     RCP<FancyOStream>
00244       out2 = rcp(new FancyOStream(rcp(new std::ostringstream),"  "));
00245     {
00246       OSTab tab1(out);
00247       *out << "This should be indented one tab!\n";
00248       {
00249         OSTab tab2(out);
00250         *out << "This should be indented two tabs!\n";
00251         *out2 << "This should be indented zero tabs from out2!\n";
00252         {
00253           OSTab tab3(out2);
00254           *out << "This should be indented two tabs!\n";
00255           *out2 << "This should be indented one tab from out2!\n";
00256         }
00257       }
00258       *out << "This should be indented one tab!\n";
00259     }
00260     *out << "This should be indented zero tabs!\n";
00261     
00262     *out << std::endl; // This required overflow() to be overridden!
00263 
00264     *out << "\n***\n*** Now outputting the latent output that was sent to out2\n***\n\n"
00265          << dyn_cast<std::ostringstream>(*out2->getOStream()).str();
00266 
00267     if(success)
00268       *out << "\nEnd Result: TEST PASSED" << std::endl;
00269     
00270   }
00271   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00272     
00273   return ( success ? 0 : 1 );
00274   
00275 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines