PSCF v1.1
Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
UnitTest Class Reference

UnitTest is a base class for classes that define unit tests. More...

#include <UnitTest.h>

Inheritance diagram for UnitTest:
ParamFileTest TestA TestA TestA TestB

Public Member Functions

 UnitTest ()
 Constructor. More...
 
virtual ~UnitTest ()
 Destructor. More...
 
virtual void setUp ()
 Set up before each test method (empty default implementation). More...
 
virtual void tearDown ()
 Tear down after each test method (empty default implementation). More...
 
void setVerbose (int verbose)
 Set verbosity level. More...
 
void setFilePrefix (const std::string &prefix)
 Set file prefix. More...
 
const std::string & filePrefix ()
 Get file prefix string. More...
 
bool isIoProcessor () const
 Should this processor read and write to file? More...
 

Protected Member Functions

void printMethod (const char *methodName)
 Write name of a class method, iff ioProcessor. More...
 
void printEndl ()
 Write carriage return, iff isIoProcessor. More...
 
virtual void endMarker ()
 Print a line of hashes, iff isIoProcessor. More...
 
void openInputFile (const std::string &name, std::ifstream &in) const
 Open C++ input file ifstream. More...
 
void openOutputFile (const std::string &name, std::ofstream &out) const
 Open C++ output file ofstream. More...
 
FILE * openFile (const std::string &name, const char *mode) const
 Open C file handle with specified mode. More...
 
int verbose () const
 Return integer verbosity level (0 == silent). More...
 

Static Protected Member Functions

static bool eq (int s1, int s2)
 Return true if two integers are equal. More...
 
static bool eq (double s1, double s2)
 Return true if two double precision floats are equal. More...
 

Detailed Description

UnitTest is a base class for classes that define unit tests.

Each subclass of UnitTest should define one or more test methods. Each test method must be a zero parameter function that returns void. Test methods may be given arbitrary names. Individual test methods should use the preprocessor macro TEST_ASSERT(expression) defined in TextException.h to assert the truth of logical expressions.

The test methods defined by a UnitTest are run by an associated subclass of TestRunner. Each test method of a UnitTest must be added to the associated TestRunner. The run() method of a TestRunner calls all of the associated test methods in the order in which they were added, and counts the number of successful and failed tests.

The TestRunner associated with a single UnitTest is defined by a class template UnitTestRunner, which takes a UnitTest subclass as a template argument. For example, the TestRunner associated with a UnitTest subclass named TestA is a template instantiation UnitTestRunner<TestA>.

Preprocessor macros defined in the file UnitTestRunner.h should be used to create the boiler-plate code necessary to define a unit test runner and to add test methods to it.

Definition at line 50 of file UnitTest.h.

Constructor & Destructor Documentation

◆ UnitTest()

UnitTest::UnitTest ( )

Constructor.

Definition at line 219 of file UnitTest.h.

◆ ~UnitTest()

UnitTest::~UnitTest ( )
virtual

Destructor.

Definition at line 236 of file UnitTest.h.

Member Function Documentation

◆ setUp()

void UnitTest::setUp ( )
virtual

Set up before each test method (empty default implementation).

Definition at line 242 of file UnitTest.h.

◆ tearDown()

void UnitTest::tearDown ( )
virtual

Tear down after each test method (empty default implementation).

Reimplemented in ParamFileTest.

Definition at line 248 of file UnitTest.h.

◆ setVerbose()

void UnitTest::setVerbose ( int  verbose)

Set verbosity level.

Parameters
verboseverbosity level (0 = silent).

Definition at line 256 of file UnitTest.h.

References verbose().

◆ setFilePrefix()

void UnitTest::setFilePrefix ( const std::string &  prefix)

Set file prefix.

This function is called by the UnitTestRunner::method(int i) function to set the filePrefix of the unit test equal to that of the runner after construction but before running the relevant test method.

Parameters
prefixstring to be prepended to input and output file names.

Definition at line 262 of file UnitTest.h.

◆ filePrefix()

const std::string & UnitTest::filePrefix ( )

Get file prefix string.

Definition at line 268 of file UnitTest.h.

◆ isIoProcessor()

bool UnitTest::isIoProcessor ( ) const

Should this processor read and write to file?

Definition at line 274 of file UnitTest.h.

Referenced by endMarker(), ParamFileTest::openFile(), printEndl(), and printMethod().

◆ printMethod()

void UnitTest::printMethod ( const char *  methodName)
protected

Write name of a class method, iff ioProcessor.

Parameters
methodNamename of class test method

Definition at line 314 of file UnitTest.h.

References isIoProcessor().

◆ printEndl()

void UnitTest::printEndl ( )
protected

Write carriage return, iff isIoProcessor.

Definition at line 324 of file UnitTest.h.

References isIoProcessor().

◆ endMarker()

void UnitTest::endMarker ( )
protectedvirtual

Print a line of hashes, iff isIoProcessor.

Definition at line 330 of file UnitTest.h.

References isIoProcessor().

◆ openInputFile()

void UnitTest::openInputFile ( const std::string &  name,
std::ifstream &  in 
) const
protected

Open C++ input file ifstream.

This function adds the filePrefix before the name parameter. It does not check if this node isIoProcessor.

Parameters
namebase file name (added to filePrefix).
ininput file (opened on return).

Definition at line 343 of file UnitTest.h.

Referenced by ParamFileTest::openFile().

◆ openOutputFile()

void UnitTest::openOutputFile ( const std::string &  name,
std::ofstream &  out 
) const
protected

Open C++ output file ofstream.

This function adds the filePrefix before the name parameter. It does not check if this node isIoProcessor.

Parameters
namebase file name (added to filePrefix)
outoutput file (opened on return)

Definition at line 361 of file UnitTest.h.

◆ openFile()

FILE * UnitTest::openFile ( const std::string &  name,
const char *  mode 
) const
protected

Open C file handle with specified mode.

This function adds the filePrefix before the name parameter. It does not check if this node isIoProcessor.

Parameters
namebase file name (added to filePrefix)
modestring that specified read or write mode
Returns
C file handle, opened for reading or writing

Definition at line 379 of file UnitTest.h.

◆ verbose()

int UnitTest::verbose ( ) const
inlineprotected

Return integer verbosity level (0 == silent).

Definition at line 397 of file UnitTest.h.

Referenced by setVerbose().

◆ eq() [1/2]

bool UnitTest::eq ( int  s1,
int  s2 
)
inlinestaticprotected

Return true if two integers are equal.

Definition at line 403 of file UnitTest.h.

◆ eq() [2/2]

bool UnitTest::eq ( double  s1,
double  s2 
)
staticprotected

Return true if two double precision floats are equal.

Definition at line 409 of file UnitTest.h.


The documentation for this class was generated from the following file: