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