Teuchos Package Browser (Single Doxygen Collection) Version of the Day
core/test/CommandLineProcessor/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 // 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_CommandLineProcessor.hpp"
00043 #include "Teuchos_GlobalMPISession.hpp"
00044 #include "Teuchos_Version.hpp"
00045 
00046 int main( int argc, char* argv[] )
00047 {
00048 
00049   using Teuchos::CommandLineProcessor;
00050 
00051   bool verbose = true;  
00052   bool parse_successful = true;
00053 
00054   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00055 
00056   // First create tests for a command line processor that doesn't throw exceptions.
00057   try {
00058     // Read options from the commandline
00059     CommandLineProcessor  clp(false, false); // Don't throw exceptions
00060 
00061     double rel_proc_speed = 1e-5; // Should 
00062     clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
00063 
00064     int size = 1;
00065     clp.setOption( "size", &size, "Size of memory blocks created." );
00066 
00067 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
00068     long long longLongOption = 42;
00069     clp.setOption( "longLongOption", &longLongOption, "An option of type long long." );
00070 #endif // HAVE_TEUCHOS_LONG_LONG_INT
00071 
00072     // Parse the current input, which should return succesful.
00073     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00074     if (verbose)
00075       std::cout << "Test 1:  CommandLineProcessor - No exceptions - All extra options ignored: ";
00076     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
00077     {
00078       parse_successful = false;
00079       if (verbose) std::cout << "FAILED" << std::endl;
00080     }
00081     else
00082       if (verbose) std::cout << "PASSED" << std::endl;
00083 
00084     // Add a new option that is required
00085     int num = 1;
00086     clp.setOption( "num", &num, "Number of memory blocks created (required option).", true );
00087    
00088     // Now parse with this new option (which should not be passed in on the command line) 
00089     parse_return = clp.parse(argc,argv);
00090     if (verbose)
00091       std::cout << "Test 2:  CommandLineProcessor - No exceptions - All extra options ignored - 1 required: ";
00092     if( parse_return != CommandLineProcessor::PARSE_ERROR )
00093     {
00094       parse_successful = false;
00095       if (verbose) std::cout << "FAILED" << std::endl;
00096     }
00097     else
00098       if (verbose) std::cout << "PASSED" << std::endl;
00099 
00100   }
00101   catch( ... ) {
00102     if(verbose)
00103       std::cerr << "*** Caught UNEXPECTED unknown exception\n";
00104     parse_successful = false;  // No exceptions should be thrown for this command line processor.
00105   }
00106 
00107   // Next create tests for a command line processor that does throw exceptions.
00108   // Read options from the commandline
00109   try {
00110     CommandLineProcessor  clp2(true, false); // Throw exceptions
00111 
00112     clp2.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00113 
00114     double rel_proc_speed = 1e-5; // Should 
00115     clp2.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
00116 
00117     int size = 1;
00118     clp2.setOption( "size", &size, "Size of memory blocks created." );
00119 
00120     // Add a new option that is required
00121     int num = 1;
00122     clp2.setOption( "num", &num, "Number of memory blocks created (required option).", true );
00123   
00124     // Parse the argument line and see if we get an exception thrown 
00125     clp2.parse(argc,argv);
00126   }
00127   catch( CommandLineProcessor::ParseError &excpt ) {
00128     if(verbose)
00129       std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
00130                 << "Test 3:  CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: PASSED" << std::endl;
00131   }
00132   catch( ... ) {
00133     if(verbose)
00134       std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
00135                 << "Test 3:  CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: FAILED" << std::endl;
00136     parse_successful = false;  // No exceptions should be thrown for this command line processor.
00137   }
00138 
00139   // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
00140   try {
00141     CommandLineProcessor  clp3(false, true); // Don't recognize all options
00142 
00143     // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
00144     CommandLineProcessor::EParseCommandLineReturn parse_return = clp3.parse(argc,argv);
00145     if (verbose)
00146       std::cout << "Test 4 :  CommandLineProcessor - No exceptions - Extra options not recognized: ";
00147     if( parse_return != CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION )
00148     {
00149       parse_successful = false;
00150       if (verbose) std::cout << "FAILED" << std::endl;
00151     }
00152     else
00153       if (verbose) std::cout << "PASSED" << std::endl;
00154 
00155     // Now add the verbose option back in and add a required option.
00156     clp3.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00157 
00158     int num = 1;
00159     clp3.setOption( "num", &num, "Number of memory blocks created (required option).", true );
00160 
00161     parse_return = clp3.parse(argc,argv);
00162     if (verbose)
00163       std::cout << "Test 5 :  CommandLineProcessor - No exceptions - Extra options not recognized - 1 required: ";
00164     if( parse_return != CommandLineProcessor::PARSE_ERROR )
00165     {
00166       parse_successful = false;
00167       if (verbose) std::cout << "FAILED" << std::endl;
00168     }
00169     else
00170       if (verbose) std::cout << "PASSED" << std::endl;
00171   } 
00172   catch( ... ) {
00173     if(verbose)
00174       std::cerr << "*** Caught UNEXPECTED unknown exception" << std::endl;
00175     parse_successful = false;  // No exceptions should be thrown for this command line processor.
00176   }
00177  
00178   // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
00179   try {
00180     if (verbose)
00181       std::cout << "Test 6 :  CommandLineProcessor - Throw exceptions - Extra options not recognized: ";
00182 
00183     CommandLineProcessor  clp4(true, true); // Don't recognize all options AND throw exceptions (default mode)
00184 
00185     // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
00186     clp4.parse(argc,argv);
00187   } 
00188   catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
00189     if(verbose)
00190       std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
00191                 << "Test 6:  CommandLineProcessor - Throw exceptions - Extra options not recognized: PASSED" << std::endl;
00192   }
00193   catch( ... ) {
00194     if(verbose)
00195       std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
00196                 << "Test 5:  CommandLineProcessor - Throw exceptions - Extra options not recognized: FAILED" << std::endl;
00197     parse_successful = false;  // No exceptions should be thrown for this command line processor.
00198   }
00199 
00200   // Return whether the command line processor tests passed. 
00201   if (parse_successful) {
00202     std::cout << "End Result: TEST PASSED" << std::endl;
00203     return 0;
00204   }
00205   else {
00206     std::cout << "End Result: TEST FAILED" << std::endl;
00207     return 1;
00208   }
00209 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines