Simpatico  v1.10
TestRunner.h
1 #ifndef TEST_RUNNER_H
2 #define TEST_RUNNER_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <string>
12 #include <iostream>
13 
14 #ifdef TEST_MPI
15 #include <mpi.h>
16 #endif
17 
74 {
75 
76 public:
77 
81  TestRunner();
82 
86  virtual ~TestRunner();
87 
93  virtual int run() = 0;
94 
98  void recordFailure();
99 
103  void recordSuccess();
104 
110  void setParent(TestRunner& parent);
111 
115  TestRunner& parent();
116 
120  bool hasParent() const;
121 
125  int nSuccess() const;
126 
130  int nFailure() const;
131 
135  void report() const;
136 
140  bool isIoProcessor() const;
141 
142  #ifdef TEST_MPI
143 
146  int mpiRank() const;
147 
151  int mpiSize() const;
152  #endif
153 
157  virtual void addFilePrefix(const std::string& prefix);
158 
162  const std::string& filePrefix() const;
163 
164 protected:
165 
167  std::string filePrefix_;
168 
169 private:
170 
172  TestRunner* parentPtr_;
173 
175  int nSuccess_;
176 
178  int nFailure_;
179 
182  bool isIoProcessor_;
183 
184  #ifdef TEST_MPI
185  int mpiRank_;
187 
189  int mpiSize_;
190  #endif
191 
192 };
193 
194 // Inline methods
195 
196 /*
197 * Set another TestRunner as the parent.
198 */
200 { parentPtr_ = &parent; }
201 
202 /*
203 * Return the parent object, if any.
204 */
206 { return *parentPtr_; }
207 
208 /*
209 * Does this object have a parent?
210 */
211 inline bool TestRunner::hasParent() const
212 { return (parentPtr_ != 0); }
213 
214 /*
215 * Return number of successful tests run.
216 */
217 inline int TestRunner::nSuccess() const
218 { return nSuccess_; }
219 
220 /*
221 * Return number of failed tests run.
222 */
223 inline int TestRunner::nFailure() const
224 { return nFailure_; }
225 
226 /*
227 * Return file prefix by const reference.
228 */
229 inline
230 const std::string& TestRunner::filePrefix() const
231 { return filePrefix_; }
232 
233 /*
234 * Is this an Io processor? (always true without MPI)
235 */
236 inline bool TestRunner::isIoProcessor() const
237 { return isIoProcessor_; }
238 
239 #ifdef TEST_MPI
240 inline int TestRunner::mpiRank() const
241 { return mpiRank_; }
242 
243 inline int TestRunner::mpiSize() const
244 { return mpiSize_; }
245 #endif
246 
247 // Non-inline methods
248 
249 /*
250 * Constructor.
251 */
253  : parentPtr_(0),
254  nSuccess_(0),
255  nFailure_(0),
256  isIoProcessor_(0)
257  #ifdef TEST_MPI
258  ,mpiRank_(0),
259  mpiSize_(0)
260  #endif
261 {
262  #ifndef TEST_MPI
263  isIoProcessor_ = true;
264  #else
265  mpiRank_ = MPI::COMM_WORLD.Get_rank();
266  mpiSize_ = MPI::COMM_WORLD.Get_size();
267  if (mpiRank_ == 0) {
268  isIoProcessor_ = true;
269  } else {
270  isIoProcessor_ = false;
271  }
272  #endif
273 }
274 
275 /*
276 * Destructor.
277 */
279 {}
280 
281 /*
282 * Increment counter for failed tests, and that of parent (if any).
283 */
285 {
286  if (isIoProcessor()) {
287  ++nFailure_;
288  if (hasParent()) {
289  parent().recordFailure();
290  }
291  }
292 }
293 
294 /*
295 * Increment counter for successful tests, and that of parent (if any).
296 */
298 {
299  if (isIoProcessor()) {
300  ++nSuccess_;
301  if (hasParent()) {
302  parent().recordSuccess();
303  }
304  }
305 }
306 
307 /*
308 * If this object has no parent, report success and failure counters.
309 */
310 void TestRunner::report() const
311 {
312  if (!hasParent() && isIoProcessor()) {
313  std::cout << std::endl;
314  std::cout << nSuccess_ << " successful tests " << std::endl;
315  std::cout << nFailure_ << " failed tests " << std::endl;
316  std::cout << std::endl;
317  }
318 }
319 
320 /*
321 * Prepend argument prefix to existing filePrefix (virtual).
322 */
323 void TestRunner::addFilePrefix(const std::string& prefix)
324 {
325  std::string newPrefix = prefix;
326  newPrefix += filePrefix_;
327  filePrefix_ = newPrefix;
328 }
329 
330 #endif
TestRunner()
Constructor.
Definition: TestRunner.h:252
virtual ~TestRunner()
Destructor.
Definition: TestRunner.h:278
bool hasParent() const
Does this object have a parent?
Definition: TestRunner.h:211
void recordFailure()
Increment counter for failed tests, and that of parent (if any).
Definition: TestRunner.h:284
int nSuccess() const
Return number of successful tests run.
Definition: TestRunner.h:217
std::string filePrefix_
Prefix added to file names.
Definition: TestRunner.h:167
Abstract base class for classes that run tests.
Definition: TestRunner.h:73
bool isIoProcessor() const
Is this the IO processor of an MPI communicator?
Definition: TestRunner.h:236
int nFailure() const
Return number of failed tests run.
Definition: TestRunner.h:223
const std::string & filePrefix() const
Return file prefix by const reference.
Definition: TestRunner.h:230
void report() const
If this object has no parent, report success and failure counters.
Definition: TestRunner.h:310
virtual void addFilePrefix(const std::string &prefix)
Prepend argument prefix to existing filePrefix.
Definition: TestRunner.h:323
void recordSuccess()
Increment counter for successful tests, and that of parent (if any).
Definition: TestRunner.h:297
void setParent(TestRunner &parent)
Set another TestRunner as the parent.
Definition: TestRunner.h:199
TestRunner & parent()
Return the parent object, if any.
Definition: TestRunner.h:205
virtual int run()=0
Run all tests.