PSCF v1.1
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
51{
52
53public:
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
109 void setCommunicator(MPI::Intracomm& communicator);
110
114 int mpiRank();
115
119 bool hasCommunicator();
120
124 MPI::Intracomm& communicator();
125 #endif
126
127protected:
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
195private:
196
198 std::string filePrefix_;
199
200 #ifdef TEST_MPI
202 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*/
256void UnitTest::setVerbose(int verbose)
257{ verbose_ = verbose; }
258
259/*
260* Set file prefix.
261*/
262void UnitTest::setFilePrefix(const std::string& prefix)
263{ filePrefix_ = prefix; }
264
265/*
266* Get file prefix string
267*/
268const 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*/
281void 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*/
295int UnitTest::mpiRank()
296{ return mpiRank_; }
297
298/*
299* Does this test have a communicator?
300*/
301bool UnitTest::hasCommunicator()
302{ return bool(communicatorPtr_ != 0); }
303
304/*
305* Return the communicator by reference.
306*/
307MPI::Intracomm& UnitTest::communicator()
308{ return *communicatorPtr_; }
309#endif
310
311/*
312* Write name of a class method, iff ioProcessor.
313*/
314void UnitTest::printMethod(const char* methodName)
315{ if (isIoProcessor()) {
316 std::cout << std::endl;
317 std::cout << std::string(methodName) << std::flush;
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*/
342void
343UnitTest::openInputFile(const std::string& name, std::ifstream& in)
344const
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*/
360void
361UnitTest::openOutputFile(const std::string& name, std::ofstream& out)
362const
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*/
378FILE*
379UnitTest::openFile(const std::string& name, const char* mode)
380const
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*/
397inline int UnitTest::verbose() const
398{ return verbose_; }
399
400/*
401* Return true if two integers are equal (static).
402*/
403inline 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*/
409bool UnitTest::eq(double s1, double s2)
410{
411 double epsilon = 1.0E-10;
412 return ( fabs(s1-s2) < epsilon );
413}
414
415#endif
UnitTest is a base class for classes that define unit tests.
Definition: UnitTest.h:51
void openInputFile(const std::string &name, std::ifstream &in) const
Open C++ input file ifstream.
Definition: UnitTest.h:343
static bool eq(int s1, int s2)
Return true if two integers are equal.
Definition: UnitTest.h:403
bool isIoProcessor() const
Should this processor read and write to file?
Definition: UnitTest.h:274
virtual void tearDown()
Tear down after each test method (empty default implementation).
Definition: UnitTest.h:248
void printMethod(const char *methodName)
Write name of a class method, iff ioProcessor.
Definition: UnitTest.h:314
void printEndl()
Write carriage return, iff isIoProcessor.
Definition: UnitTest.h:324
virtual void endMarker()
Print a line of hashes, iff isIoProcessor.
Definition: UnitTest.h:330
void setVerbose(int verbose)
Set verbosity level.
Definition: UnitTest.h:256
UnitTest()
Constructor.
Definition: UnitTest.h:219
void openOutputFile(const std::string &name, std::ofstream &out) const
Open C++ output file ofstream.
Definition: UnitTest.h:361
FILE * openFile(const std::string &name, const char *mode) const
Open C file handle with specified mode.
Definition: UnitTest.h:379
void setFilePrefix(const std::string &prefix)
Set file prefix.
Definition: UnitTest.h:262
int verbose() const
Return integer verbosity level (0 == silent).
Definition: UnitTest.h:397
const std::string & filePrefix()
Get file prefix string.
Definition: UnitTest.h:268
virtual void setUp()
Set up before each test method (empty default implementation).
Definition: UnitTest.h:242
virtual ~UnitTest()
Destructor.
Definition: UnitTest.h:236