PlasCom2  1.0
XPACC Multi-physics simluation application
TestingProgram.H
Go to the documentation of this file.
1 #ifndef __TESTING_PROGRAM_H__
9 #define __TESTING_PROGRAM_H__
10 
11 #include "Profiler.H"
12 #include "Global.H"
13 #include "ComLine.H"
14 #include "COMM.H"
15 #include <cmath>
16 
17 namespace ix { namespace test {
22  {
23  public:
25  {};
26  program_command_line(const char *args[])
27  : ix::util::ComLineObject(args)
28  {};
29  virtual void Initialize(){
30  AddOption('h',"help");
31  AddOption('l',"list");
32  AddOption('v',"verblevel",1,"level");
33  AddOption('o',"output",2,"filename");
34  AddOption('f',"file",2,"filename");
35  AddOption('n',"name",2,"TestName");
36  AddOption('d',"debuglevel",1,"level");
37  AddOption('p',"enable-profiling",1,"level");
38  AddHelp('h',"Print out long version of help and exit.");
39  AddHelp('l',"List the available tests and exit.");
40  AddHelp('o',"Set the output file to <filename>. (default = stdout)");
41  std::ostringstream Ostr;
42  Ostr << "Set the test list file to <filename>. (no default).\n\t\t"
43  << "The test list file should be a text file with one test\n\t\t"
44  << "name per line.";
45  AddHelp('f',Ostr.str());
46  AddHelp("name","Run test by name. (no default)");
47  Ostr.str("");
48  Ostr << "Command-line interface for a general testing program.";
49  _description.assign(Ostr.str());
50  AddHelp('d',"Set the debugging level for the program.(default=1)");
51  AddHelp('p',std::string("Enable profiling. Profiling level > 1 enables")+
52  std::string(" profiling\n\t\tbarriers. (default=1)"));
53  AddHelp('v',std::string("Set the verbosity level for the program.")+
54  std::string(" Anything\n\t\tlarger than 2 is debugging level.")+
55  std::string("\n\t\tExamples:\n\t\t\t-v (defaults to 1)\n\t\t\t")+
56  std::string("-v 0 (silence)\n\t\t\t--verblevel=2")+
57  std::string(" (high verbosity)"));
58  };
59  };
60 
65 
66  template<typename TestObjectType>
67  class program : public ProgramType
68  {
69  protected:
70  std::string outFileName;
71  std::string testName;
72  std::string listFileName;
73  TestObjectType testObject;
74  typename TestObjectType::Results testResult;
75  public:
76  program(int narg,char **args) : ProgramType(narg,args){};
77  program(Global &inglob) : ProgramType(inglob){};
78  program(CommandLineType &incom) : ProgramType(incom){};
79  program(CommandLineType &incom,Global &inglob) :
80  ProgramType(incom,inglob){};
81  virtual int Initialize(){
83  ErrOut("Failed to initialize.\n");
84  ErrOut(CommandLine().ErrorReport()+std::string("\n"));
85  ErrOut("Use -h,--help for usage help.\n\n");
86  ErrOut(CommandLine().ShortUsage()+std::string("\n"));
87  return(1);
88  }
89  int setexit = 0;
90  if(!CommandLine().GetOption('h').empty()){
91  StdOut(CommandLine().LongUsage());
92  setexit = 1;
93  }
94  if(!CommandLine().GetOption('l').empty()){
95  std::ostringstream outStream;
96  outStream << "Available tests are:" << std::endl;
97  testObject.ListTests(outStream);
98  StdOut(outStream.str());
99  setexit = 1;
100  }
101  if(setexit)
102  return(1);
103  // Verbosity level processing intent:
104  // 0 - be silent
105  // 1 - normal verbosity
106  // 2 - increased verbosity
107  // 3 - debug mode engaged (debug stream engaged for root process)
108  // 4+ - debug mode (with debugging logged on all processors)
109  std::string sverb(CommandLine().GetOption('v'));
110  if(!sverb.empty()){
111  if(sverb == ".true.")
112  SetVerbLevel(1);
113  else {
114  std::istringstream Istr(sverb);
115  int verbLevel;
116  Istr >> verbLevel;
117  SetVerbLevel(verbLevel);
118  if(verbLevel > 1){
119  SetDebugStream(ErrStream());
120  SetDebugLevel(verbLevel - 1);
121  }
122  }
123  }
124  // Process profiling control
125  std::string sprof(CommandLine().GetOption('p'));
126  if(!sprof.empty()){
127  if(sprof == ".true.")
128  Profiling(true);
129  else {
130  std::istringstream Istr(sprof);
131  int profLevel;
132  Istr >> profLevel;
133  if(profLevel == 0)
134  Profiling(false);
135  if(profLevel >= 1){
136  Profiling(true);
137  }
138  }
139  }
140  // Process profiling control
141  std::string sdebug(CommandLine().GetOption('d'));
142  if(!sdebug.empty()){
143  if(sdebug == ".true.")
144  SetDebugLevel(1);
145  else {
146  std::istringstream Istr(sdebug);
147  int debugLevel;
148  Istr >> debugLevel;
149  if(debugLevel >= 1)
150  SetDebugLevel(debugLevel);
151  }
152  }
153  outFileName = CommandLine().GetOption('o');
154  testName = CommandLine().GetOption('n');
155  listFileName = CommandLine().GetOption('l');
156  return(0);
157  }
158 
159  virtual int Run(){
160  FunctionEntry("Run");
161  // If the user specified a name, then run only the named test
162  if(!testName.empty()){
163  // This call runs a test by name
164  FunctionEntry(testName);
165  testObject.RunTest(testName,testResult);
166  FunctionExit(testName);
167  } else if(!listFileName.empty()){
168  std::ifstream listInf;
169  listInf.open(listFileName.c_str());
170  if(!listInf){
171  std::ostringstream errStream;
172  errStream << "Error: Could not open list of tests in file "
173  << listFileName << "." << std::endl;
174  ErrOut(errStream.str());
175  FunctionExit("Run");
176  return(1);
177  }
178  std::string testName;
179  while(std::getline(listInf,testName)){
180  FunctionEntry(testName);
181  testObject.RunTest(testName,testResult);
182  FunctionExit(testName);
183  }
184  listInf.close();
185  }
186  else {
187  // This call runs all the tests implemented in the testing object
188  FunctionEntry("AllTests");
189  testObject.Process(testResult);
190  FunctionExit("AllTests");
191  }
192  FunctionExit("Run");
193  return(0);
194  };
195 
196  virtual int Finalize(){
197  std::ostringstream outStream;
198  outStream << testResult << std::endl;
199  if(!outFileName.empty()){
200  std::ofstream Ouf;
201  Ouf.open(outFileName.c_str());
202  if(!Ouf){
203  std::ostringstream errStream;
204  errStream << "Error: Could not open output file, "
205  << outFileName << " for test ouput."
206  << std::endl;
207  ErrOut(errStream.str());
208  return(1);
209  }
210  Ouf << outStream.str();
211  Ouf.close();
212  } else {
213  StdOut(outStream.str());
214  }
215  std::ostringstream profileStream;
216  Report(profileStream);
217  if(Profiling())
218  StdOut(profileStream.str(),1);
219  return(0);
220  };
221  TestObjectType &TestObject(){return(testObject);};
222  virtual ~program(){};
223  };
224  }
225 }
226 #endif
program_command_line CommandLineType
virtual int Initialize()
std::string ErrorReport()
Error reporting.
Definition: ComLine.C:473
std::string GetOption(const char &s)
Get the value of an option.
Definition: ComLine.H:291
program(CommandLineType &incom, Global &inglob)
program(Global &inglob)
Performance profiling object.
Definition: Profiler.H:398
virtual int Run()
Communication utilities.
Defines MPI-specific parallel global and program classes.
ix::global::GlobalObj< std::string, std::string, ProfilerType > Global
virtual int Initialize()
Definition: Global.H:661
Performance Profiling interface definition.
std::string _description
application description.
Definition: ComLine.H:73
ix::profiler::ProfilerObj ProfilerType
virtual int Finalize()
program(CommandLineType &incom)
program(int narg, char **args)
TestObjectType::Results testResult
void AddOption(char s, const std::string &l, int=0)
User interface to describe simple option.
Definition: ComLine.C:295
Base global object header.
ComLineObject header.
TestObjectType testObject
virtual void Initialize()
virtual function for program specific Initialization.
program_command_line(const char *args[])
std::string outFileName
std::string listFileName
std::string testName
ComLineObject for testing program command-line interface.
ix::global::Program< Global, CommandLineType > ProgramType
TestObjectType & TestObject()
std::string LongUsage()
Generate long usage string.
Definition: ComLine.C:188
Base global object.
Definition: Global.H:25
Command line processing.
Definition: ComLine.H:62
ComLineObject()
Default constructor.
Definition: ComLine.H:102
void AddHelp(char s, const std::string &help)
Specify usage for an option.
Definition: ComLine.H:227
std::string ShortUsage()
Generate short usage string.
Definition: ComLine.C:244