Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
core/test/CommandLineProcessor/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
44 #include "Teuchos_Version.hpp"
45 
46 int main( int argc, char* argv[] )
47 {
48 
50 
51  bool verbose = true;
52  bool parse_successful = true;
53 
54  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
55 
56  // First create tests for a command line processor that doesn't throw exceptions.
57  try {
58  // Read options from the commandline
59  CommandLineProcessor clp(false, false); // Don't throw exceptions
60 
61  double rel_proc_speed = 1e-5; // Should
62  clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
63 
64  int size = 1;
65  clp.setOption( "size", &size, "Size of memory blocks created." );
66 
67  size_t sizetOption = 10;
68  clp.setOption( "sizeTOption", &sizetOption, "An option of type size_t.");
69 
70 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
71  long long longLongOption = 42;
72  clp.setOption( "longLongOption", &longLongOption, "An option of type long long." );
73 #endif // HAVE_TEUCHOS_LONG_LONG_INT
74 
75  // Parse the current input, which should return succesful.
76  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
77  if (verbose)
78  std::cout << "Test 1: CommandLineProcessor - No exceptions - All extra options ignored: ";
79  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
80  {
81  parse_successful = false;
82  if (verbose) std::cout << "FAILED" << std::endl;
83  }
84  else
85  if (verbose) std::cout << "PASSED" << std::endl;
86 
87  // Add a new option that is required
88  int num = 1;
89  clp.setOption( "num", &num, "Number of memory blocks created (required option).", true );
90 
91  // Now parse with this new option (which should not be passed in on the command line)
92  parse_return = clp.parse(argc,argv);
93  if (verbose)
94  std::cout << "Test 2: CommandLineProcessor - No exceptions - All extra options ignored - 1 required: ";
95  if( parse_return != CommandLineProcessor::PARSE_ERROR )
96  {
97  parse_successful = false;
98  if (verbose) std::cout << "FAILED" << std::endl;
99  }
100  else
101  if (verbose) std::cout << "PASSED" << std::endl;
102 
103  }
104  catch( ... ) {
105  if(verbose)
106  std::cerr << "*** Caught UNEXPECTED unknown exception\n";
107  parse_successful = false; // No exceptions should be thrown for this command line processor.
108  }
109 
110  // Next create tests for a command line processor that does throw exceptions.
111  // Read options from the commandline
112  try {
113  CommandLineProcessor clp2(true, false); // Throw exceptions
114 
115  clp2.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
116 
117  double rel_proc_speed = 1e-5; // Should
118  clp2.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
119 
120  int size = 1;
121  clp2.setOption( "size", &size, "Size of memory blocks created." );
122 
123  // Add a new option that is required
124  int num = 1;
125  clp2.setOption( "num", &num, "Number of memory blocks created (required option).", true );
126 
127  // Parse the argument line and see if we get an exception thrown
128  clp2.parse(argc,argv);
129  }
130  catch( CommandLineProcessor::ParseError &excpt ) {
131  if(verbose)
132  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
133  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: PASSED" << std::endl;
134  }
135  catch( ... ) {
136  if(verbose)
137  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
138  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: FAILED" << std::endl;
139  parse_successful = false; // No exceptions should be thrown for this command line processor.
140  }
141 
142  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
143  try {
144  CommandLineProcessor clp3(false, true); // Don't recognize all options
145 
146  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
147  CommandLineProcessor::EParseCommandLineReturn parse_return = clp3.parse(argc,argv);
148  if (verbose)
149  std::cout << "Test 4 : CommandLineProcessor - No exceptions - Extra options not recognized: ";
150  if( parse_return != CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION )
151  {
152  parse_successful = false;
153  if (verbose) std::cout << "FAILED" << std::endl;
154  }
155  else
156  if (verbose) std::cout << "PASSED" << std::endl;
157 
158  // Now add the verbose option back in and add a required option.
159  clp3.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
160 
161  int num = 1;
162  clp3.setOption( "num", &num, "Number of memory blocks created (required option).", true );
163 
164  parse_return = clp3.parse(argc,argv);
165  if (verbose)
166  std::cout << "Test 5 : CommandLineProcessor - No exceptions - Extra options not recognized - 1 required: ";
167  if( parse_return != CommandLineProcessor::PARSE_ERROR )
168  {
169  parse_successful = false;
170  if (verbose) std::cout << "FAILED" << std::endl;
171  }
172  else
173  if (verbose) std::cout << "PASSED" << std::endl;
174  }
175  catch( ... ) {
176  if(verbose)
177  std::cerr << "*** Caught UNEXPECTED unknown exception" << std::endl;
178  parse_successful = false; // No exceptions should be thrown for this command line processor.
179  }
180 
181  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
182  try {
183  if (verbose)
184  std::cout << "Test 6 : CommandLineProcessor - Throw exceptions - Extra options not recognized: ";
185 
186  CommandLineProcessor clp4(true, true); // Don't recognize all options AND throw exceptions (default mode)
187 
188  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
189  clp4.parse(argc,argv);
190  }
191  catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
192  if(verbose)
193  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
194  << "Test 6: CommandLineProcessor - Throw exceptions - Extra options not recognized: PASSED" << std::endl;
195  }
196  catch( ... ) {
197  if(verbose)
198  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
199  << "Test 5: CommandLineProcessor - Throw exceptions - Extra options not recognized: FAILED" << std::endl;
200  parse_successful = false; // No exceptions should be thrown for this command line processor.
201  }
202 
203  // Return whether the command line processor tests passed.
204  if (parse_successful) {
205  std::cout << "End Result: TEST PASSED" << std::endl;
206  return 0;
207  }
208  else {
209  std::cout << "End Result: TEST FAILED" << std::endl;
210  return 1;
211  }
212 }
int main(int argc, char *argv[])
Initialize, finalize, and query the global MPI session.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
Class that helps parse command line input arguments from (argc,argv[]) and set options.