PlasCom2  1.0
XPACC Multi-physics simluation application
PC2TestDriver.C
Go to the documentation of this file.
1 #include "ComLine.H"
11 #include "TestPlasCom2.H"
12 
13 namespace plascom2 {
14 
15 
45  int Test(int argc,char *argv[])
46  {
47 
48  // The default verbosity is 0
49  int verblevel = 0;
50 
51  // This line creates the PlasCom2::TestComLine object and passes in
52  // the command line arguments as a (const char **).
53  TestComLine comline((const char **)(argv));
54  // The call to comline.Initialize() reads the command line arguments
55  // from the array passed in the previous line.
56  comline.Initialize();
57  // The ProcessOptions() call does detailed examination of the command
58  // line arguments to check for user errors or other problems. This call
59  // will return non-zero if there were errors on the commandline.
60  int clerr = comline.ProcessOptions();
61  // Check if the user just wanted to get the help and exit
62  if(!comline.GetOption("help").empty()){
63  // Print out the "long usage" (i.e. help) message to stdout
64  std::cout << comline.LongUsage() << std::endl;
65  if(verblevel > 2)
66  std::cout << "PlasCom2::Test: Exiting test function (success)" << std::endl;
67  return(0);
68  }
69  if(clerr){
70  std::cout << comline.ErrorReport() << std::endl
71  << std::endl << comline.ShortUsage() << std::endl;
72  if(verblevel > 2)
73  std::cout << "PlasCom2::Test: Exiting test function (fail)" << std::endl;
74  return(1);
75  }
76  // These outstreams allow the output to file to be set up and separated
77  // from the stdout.
78  std::ofstream Ouf;
79  std::ostream *Out = &std::cout;
80  std::ostringstream outStream; // if a file is specified, buffer output
81 
82  // The next few lines populate some strings based on the
83  // users input from the commandline.
84  std::string OutFileName(comline.GetOption("output"));
85  std::string TestName(comline.GetOption("name"));
86  std::string ListName(comline.GetOption("list"));
87  std::string sverb(comline.GetOption("verblevel"));
88 
89  // The following block parses and sets the verbosity level
90  if(!sverb.empty()){
91  verblevel = 1;
92  if(sverb != ".true."){
93  std::istringstream Istr(sverb);
94  Istr >> verblevel;
95  if(verblevel < 0)
96  verblevel = 1;
97  }
98  }
99 
100  // This block sets up the output file if the user specified one
101  if(!OutFileName.empty()){
102  Out = &outStream;
103  }
104 
105  if(verblevel > 2)
106  std::cout << "PlasCom2::Test: Entering test function" << std::endl;
107 
108  // Make an instance of the PlasCom2 testing object, PlasCom2::TestingObject
110  // Make an instance of the PlasCom2 results object, PlasCom2::TestResults
111  plascom2::TestResults results;
112 
113  // If the user specified a name, then run only the named test
114  if(!TestName.empty()){
115  // This call runs a test by name
116  test.RunTest(TestName,results);
117  }
118  // Otherwise, if the user specified a list, then read the list and
119  // run the listed tests.
120  else if(!ListName.empty()){
121  std::ifstream ListInf;
122  ListInf.open(ListName.c_str());
123  if(!ListInf){
124  std::cout << "PlasCom2::Test> Error: Could not open list of tests in file "
125  << ListName << ". Exiting (fail)." << std::endl;
126  return(1);
127  }
128  std::string testname;
129  while(std::getline(ListInf,testname))
130  test.RunTest(testname,results);
131  ListInf.close();
132  }
133  else {
134  // This call runs all the tests for the PlasCom2 namespace.
135  test.Process(results);
136  }
137  *Out << results << std::endl;
138  if(verblevel > 2)
139  *Out << "PlasCom2::Test: Exiting test function (success)" << std::endl;
140 
141  // Don't open until the end - race condition resolution
142  if(!OutFileName.empty()){
143  Ouf.open(OutFileName.c_str(),std::ios::app);
144  if(!Ouf){
145  std::cout << "PlasCom2::Test> Error: Could not open output file, "
146  << OutFileName << " for test output. Exiting (fail)." << std::endl;
147  return(1);
148  }
149  Ouf << outStream.str();
150  Ouf.flush();
151  Ouf.close();
152  }
153  // if(Ouf)
154  // Ouf.close();
155 
156  return(0);
157  }
158 };
159 
160 int main(int argc,char *argv[])
161 {
162  return(plascom2::Test(argc,argv));
163 }
int ProcessOptions()
Processes all command line tokens.
Definition: ComLine.C:330
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
Definition: PC2IO.H:10
Encapsulating class for collections of test results.
Definition: Testing.H:18
virtual void Process(ResultsType &result)
Runs all tests implemented by the PlasCom2::TestingObject.
Definition: PlasCom2Test.H:186
int main(int argc, char *argv[])
ComLineObject header.
void Initialize()
virtual function for program specific Initialization.
Definition: TestPlasCom2.H:25
ComLineObject for PlasCom2 testing command-line interface.
Definition: TestPlasCom2.H:16
Testing utilities for PlasCom2.
std::string LongUsage()
Generate long usage string.
Definition: ComLine.C:188
Project-specific testing object.
Definition: PlasCom2Test.H:74
virtual void RunTest(const std::string &name, ResultsType &result)
Runs a test specified by name.
Definition: PlasCom2Test.H:246
int Test(int argc, char *argv[])
Drives the plascom2::TestObject.
Definition: PC2TestDriver.C:45
std::string ShortUsage()
Generate short usage string.
Definition: ComLine.C:244