Simpatico  v1.10
UnitTest.h
1 #ifndef UNIT_TEST_H
2 #define UNIT_TEST_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 "TestException.h"
12 
13 #include <stdio.h>
14 #include <fstream>
15 #include <string>
16 #ifdef TEST_MPI
17 #include <mpi.h>
18 #endif
19 
50 class UnitTest
51 {
52 
53 public:
54 
58  UnitTest();
59 
63  virtual ~UnitTest();
64 
68  virtual void setUp();
69 
73  virtual void tearDown();
74 
80  void setVerbose(int verbose);
81 
91  void setFilePrefix(const std::string& prefix);
92 
96  const std::string& filePrefix();
97 
101  bool isIoProcessor() const;
102 
103  #ifdef TEST_MPI
104 
109  void setCommunicator(MPI::Intracomm& communicator);
110 
114  int mpiRank();
115 
119  bool hasCommunicator();
120 
124  MPI::Intracomm& communicator();
125  #endif
126 
127 protected:
128 
134  void printMethod(const char* methodName);
135 
139  void printEndl();
140 
144  virtual void endMarker();
145 
155  void openInputFile(const std::string& name, std::ifstream& in) const;
156 
166  void openOutputFile(const std::string& name, std::ofstream& out) const;
167 
178  FILE* openFile(const std::string& name, const char* mode) const;
179 
183  int verbose() const;
184 
188  static bool eq(int s1, int s2);
189 
193  static bool eq(double s1, double s2);
194 
195 private:
196 
198  std::string filePrefix_;
199 
200  #ifdef TEST_MPI
201  MPI::Intracomm* communicatorPtr_;
203 
205  int mpiRank_;
206  #endif
207 
209  int verbose_;
210 
212  bool isIoProcessor_;
213 
214 };
215 
216 /*
217 * Constructor
218 */
220  #ifdef TEST_MPI
221  communicatorPtr_(0),
222  mpiRank_(-1),
223  #endif
224  verbose_(0),
225  isIoProcessor_(true)
226 {
227  #ifdef TEST_MPI
228  // Set the communicator to COMM_WORLD by default.
229  setCommunicator(MPI::COMM_WORLD);
230  #endif
231 }
232 
233 /*
234 * Destructor.
235 */
237 {}
238 
239 /*
240 * Set up before each test method (empty default implementation).
241 */
243 {}
244 
245 /*
246 * Tear down after each test method (empty default implementation).
247 */
249 {}
250 
251 /*
252 * Set verbosity level.
253 *
254 * \param verbose verbosity level (0 = silent).
255 */
257 { verbose_ = verbose; }
258 
259 /*
260 * Set file prefix.
261 */
262 void UnitTest::setFilePrefix(const std::string& prefix)
263 { filePrefix_ = prefix; }
264 
265 /*
266 * Get file prefix string
267 */
268 const std::string& UnitTest::filePrefix()
269 { return filePrefix_; }
270 
271 /*
272 * Should this processor read and write to file?
273 */
275 { return isIoProcessor_; }
276 
277 #ifdef TEST_MPI
278 /*
279 * Set the MPI communicator.
280 */
281 void UnitTest::setCommunicator(MPI::Intracomm& communicator)
282 {
283  communicatorPtr_ = &communicator;
284  mpiRank_ = communicator.Get_rank();
285  if (mpiRank_ == 0) {
286  isIoProcessor_ = true;
287  } else {
288  isIoProcessor_ = false;
289  }
290 }
291 
292 /*
293 * Return rank of this processor in communicator.
294 */
295 int UnitTest::mpiRank()
296 { return mpiRank_; }
297 
298 /*
299 * Does this test have a communicator?
300 */
301 bool UnitTest::hasCommunicator()
302 { return bool(communicatorPtr_ != 0); }
303 
304 /*
305 * Return the communicator by reference.
306 */
307 MPI::Intracomm& UnitTest::communicator()
308 { return *communicatorPtr_; }
309 #endif
310 
311 /*
312 * Write name of a class method, iff ioProcessor.
313 */
314 void UnitTest::printMethod(const char* methodName)
315 { if (isIoProcessor()) {
316  std::cout << std::endl;
317  std::cout << std::string(methodName);
318  }
319 }
320 
321 /*
322 * Write carriage return, iff isIoProcessor.
323 */
325 { if (isIoProcessor()) std::cout << std::endl; }
326 
327 /*
328 * Print a line of hashes, iff isIoProcessor.
329 */
331 {
332  if (isIoProcessor()) {
333  std::cout << std::endl;
334  std::cout << "----------------------------------------------------";
335  std::cout << std::endl << std::endl;
336  }
337 }
338 
339 /*
340 * Open input file.
341 */
342 void
343 UnitTest::openInputFile(const std::string& name, std::ifstream& in)
344 const
345 {
346  std::string filename = filePrefix_;
347  filename += name;
348  in.open(filename.c_str());
349  if (in.fail()) {
350  std::cout << std::endl;
351  std::cout << "Failure to open input file "
352  << filename << std::endl;
353  TEST_THROW("Failure to open file");
354  }
355 }
356 
357 /*
358 * Open output file stream.
359 */
360 void
361 UnitTest::openOutputFile(const std::string& name, std::ofstream& out)
362 const
363 {
364  std::string filename = filePrefix_;
365  filename += name;
366  out.open(filename.c_str());
367  if (out.fail()) {
368  std::cout << std::endl;
369  std::cout << "Failure to open output file "
370  << filename << std::endl;
371  TEST_THROW("Failure to open file");
372  }
373 }
374 
375 /*
376 * Open C file handle.
377 */
378 FILE*
379 UnitTest::openFile(const std::string& name, const char* mode)
380 const
381 {
382  std::string filename = filePrefix_;
383  filename += name;
384  FILE* fp = fopen(filename.c_str(), mode);
385  if (fp == NULL) {
386  std::cout << std::endl;
387  std::cout << "Failure of fopen to open file "
388  << filename << std::endl;
389  TEST_THROW("Failure to open file");
390  }
391  return fp;
392 }
393 
394 /*
395 * Return integer verbosity level (0 == silent).
396 */
397 inline int UnitTest::verbose() const
398 { return verbose_; }
399 
400 /*
401 * Return true if two integers are equal (static).
402 */
403 inline bool UnitTest::eq(int s1, int s2)
404 { return (s1 == s2); }
405 
406 /*
407 * Return true if two double precision floats are equal (static).
408 */
409 bool UnitTest::eq(double s1, double s2)
410 {
411  double epsilon = 1.0E-10;
412  return ( fabs(s1-s2) < epsilon );
413 }
414 
415 #endif
virtual ~UnitTest()
Destructor.
Definition: UnitTest.h:236
void setVerbose(int verbose)
Set verbosity level.
Definition: UnitTest.h:256
int verbose() const
Return integer verbosity level (0 == silent).
Definition: UnitTest.h:397
bool isIoProcessor() const
Should this processor read and write to file?
Definition: UnitTest.h:274
FILE * openFile(const std::string &name, const char *mode) const
Open C file handle with specified mode.
Definition: UnitTest.h:379
void printEndl()
Write carriage return, iff isIoProcessor.
Definition: UnitTest.h:324
void openOutputFile(const std::string &name, std::ofstream &out) const
Open C++ output file ofstream.
Definition: UnitTest.h:361
virtual void setUp()
Set up before each test method (empty default implementation).
Definition: UnitTest.h:242
UnitTest is a base class for classes that define unit tests.
Definition: UnitTest.h:50
void setFilePrefix(const std::string &prefix)
Set file prefix.
Definition: UnitTest.h:262
virtual void endMarker()
Print a line of hashes, iff isIoProcessor.
Definition: UnitTest.h:330
UnitTest()
Constructor.
Definition: UnitTest.h:219
virtual void tearDown()
Tear down after each test method (empty default implementation).
Definition: UnitTest.h:248
const std::string & filePrefix()
Get file prefix string.
Definition: UnitTest.h:268
void printMethod(const char *methodName)
Write name of a class method, iff ioProcessor.
Definition: UnitTest.h:314
static bool eq(int s1, int s2)
Return true if two integers are equal.
Definition: UnitTest.h:403
void openInputFile(const std::string &name, std::ifstream &in) const
Open C++ input file ifstream.
Definition: UnitTest.h:343