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 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
68  long long longLongOption = 42;
69  clp.setOption( "longLongOption", &longLongOption, "An option of type long long." );
70 #endif // HAVE_TEUCHOS_LONG_LONG_INT
71 
72  // Parse the current input, which should return succesful.
73  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
74  if (verbose)
75  std::cout << "Test 1: CommandLineProcessor - No exceptions - All extra options ignored: ";
76  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
77  {
78  parse_successful = false;
79  if (verbose) std::cout << "FAILED" << std::endl;
80  }
81  else
82  if (verbose) std::cout << "PASSED" << std::endl;
83 
84  // Add a new option that is required
85  int num = 1;
86  clp.setOption( "num", &num, "Number of memory blocks created (required option).", true );
87 
88  // Now parse with this new option (which should not be passed in on the command line)
89  parse_return = clp.parse(argc,argv);
90  if (verbose)
91  std::cout << "Test 2: CommandLineProcessor - No exceptions - All extra options ignored - 1 required: ";
92  if( parse_return != CommandLineProcessor::PARSE_ERROR )
93  {
94  parse_successful = false;
95  if (verbose) std::cout << "FAILED" << std::endl;
96  }
97  else
98  if (verbose) std::cout << "PASSED" << std::endl;
99 
100  }
101  catch( ... ) {
102  if(verbose)
103  std::cerr << "*** Caught UNEXPECTED unknown exception\n";
104  parse_successful = false; // No exceptions should be thrown for this command line processor.
105  }
106 
107  // Next create tests for a command line processor that does throw exceptions.
108  // Read options from the commandline
109  try {
110  CommandLineProcessor clp2(true, false); // Throw exceptions
111 
112  clp2.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
113 
114  double rel_proc_speed = 1e-5; // Should
115  clp2.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
116 
117  int size = 1;
118  clp2.setOption( "size", &size, "Size of memory blocks created." );
119 
120  // Add a new option that is required
121  int num = 1;
122  clp2.setOption( "num", &num, "Number of memory blocks created (required option).", true );
123 
124  // Parse the argument line and see if we get an exception thrown
125  clp2.parse(argc,argv);
126  }
127  catch( CommandLineProcessor::ParseError &excpt ) {
128  if(verbose)
129  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
130  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: PASSED" << std::endl;
131  }
132  catch( ... ) {
133  if(verbose)
134  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
135  << "Test 3: CommandLineProcessor - Throw exceptions - All extra options ignored - 1 required: FAILED" << std::endl;
136  parse_successful = false; // No exceptions should be thrown for this command line processor.
137  }
138 
139  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
140  try {
141  CommandLineProcessor clp3(false, true); // Don't recognize all options
142 
143  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
144  CommandLineProcessor::EParseCommandLineReturn parse_return = clp3.parse(argc,argv);
145  if (verbose)
146  std::cout << "Test 4 : CommandLineProcessor - No exceptions - Extra options not recognized: ";
147  if( parse_return != CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION )
148  {
149  parse_successful = false;
150  if (verbose) std::cout << "FAILED" << std::endl;
151  }
152  else
153  if (verbose) std::cout << "PASSED" << std::endl;
154 
155  // Now add the verbose option back in and add a required option.
156  clp3.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
157 
158  int num = 1;
159  clp3.setOption( "num", &num, "Number of memory blocks created (required option).", true );
160 
161  parse_return = clp3.parse(argc,argv);
162  if (verbose)
163  std::cout << "Test 5 : CommandLineProcessor - No exceptions - Extra options not recognized - 1 required: ";
164  if( parse_return != CommandLineProcessor::PARSE_ERROR )
165  {
166  parse_successful = false;
167  if (verbose) std::cout << "FAILED" << std::endl;
168  }
169  else
170  if (verbose) std::cout << "PASSED" << std::endl;
171  }
172  catch( ... ) {
173  if(verbose)
174  std::cerr << "*** Caught UNEXPECTED unknown exception" << std::endl;
175  parse_successful = false; // No exceptions should be thrown for this command line processor.
176  }
177 
178  // Next create tests for a command line processor that doesn't throw exceptions, and doesn't recognize all options.
179  try {
180  if (verbose)
181  std::cout << "Test 6 : CommandLineProcessor - Throw exceptions - Extra options not recognized: ";
182 
183  CommandLineProcessor clp4(true, true); // Don't recognize all options AND throw exceptions (default mode)
184 
185  // Parse the current input, which should not be successful because the test is run with "--verbose" argument.
186  clp4.parse(argc,argv);
187  }
188  catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
189  if(verbose)
190  std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
191  << "Test 6: CommandLineProcessor - Throw exceptions - Extra options not recognized: PASSED" << std::endl;
192  }
193  catch( ... ) {
194  if(verbose)
195  std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
196  << "Test 5: CommandLineProcessor - Throw exceptions - Extra options not recognized: FAILED" << std::endl;
197  parse_successful = false; // No exceptions should be thrown for this command line processor.
198  }
199 
200  // Return whether the command line processor tests passed.
201  if (parse_successful) {
202  std::cout << "End Result: TEST PASSED" << std::endl;
203  return 0;
204  }
205  else {
206  std::cout << "End Result: TEST FAILED" << std::endl;
207  return 1;
208  }
209 }
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.