SOFA API  c76874b7
Open source framework for multi-physics simuation
sofa::helper::UnitTest Class Referenceabstract

#include <UnitTest.h>

Detailed Description

Base class for performing series of unit tests and issuing test reports.

There are three types of reports: the total number of unit tests, the number of warnings, and the number of errors. Warnings can be used as alternative to errors.

Log and warning/error messages can be output using output streams given by sout() and serr(), respectively. The messages are displayed or not, depending on the verbosity level.

To implement a new test, derive this class and implement method void runTests( unsigned& numTests, unsigned& numWarnings, unsigned& numErrors ).

Public Member Functions

 UnitTest (std::string shortTestName, VerbosityLevel verb)
 
virtual ~UnitTest ()
 
virtual void runTests (unsigned &numTests, unsigned &numWarnings, unsigned &numErrors)=0
 

Protected Member Functions

bool checkIf (bool testSucceeded, std::string testDescription, unsigned &numTests, unsigned &numErrors)
 
virtual std::ostream & sout ()
 Basic output stream, displayed only if the verbosity level is ALL_MESSAGES. More...
 
virtual std::ostream & serr ()
 Error and warning output stream, skipped only if the verbosity level is SILENT. More...
 
VerbosityLevel verbosityLevel ()
 
const std::string & getName () const
 

Static Protected Member Functions

static void initClass ()
 General-purpose initializations, such as the random number generator. More...
 

Helpers

Helper Functions to more easily create tests and check the results.

virtual double epsilon () const
 A very small value. Can be used to check if an error is small enough. More...
 
static double drand ()
 random double in [0,1[. It is recommended to use initClass() to initialize the random number generator before using this method. More...
 
template<typename Matrix1 , typename Matrix2 >
static bool matricesAreEqual (const Matrix1 &m1, const Matrix2 &m2, double tolerance=std::numeric_limits< double >::epsilon())
 return true if the matrices have same size and all their entries are equal within the given tolerance More...
 
template<typename Vector1 , typename Vector2 >
static bool vectorsAreEqual (const Vector1 &m1, const Vector2 &m2, double tolerance=std::numeric_limits< double >::epsilon())
 return true if the matrices have same size and all their entries are equal within the given tolerance More...
 

Constructor details

sofa::helper::UnitTest::UnitTest ( std::string  shortTestName,
VerbosityLevel  verb 
)
virtual sofa::helper::UnitTest::~UnitTest ( )
inlinevirtual

Function details

bool sofa::helper::UnitTest::checkIf ( bool  testSucceeded,
std::string  testDescription,
unsigned &  numTests,
unsigned &  numErrors 
)
protected

Helper to register the result of unit test.

Parameters
testSucceededis the result of the test, true if successfull
testDescriptionis the description of the test, printed depending on the test result and verbosity level
numTestsis incremented by 1
numErrorsis incremented if testSucceeded is false
static double sofa::helper::UnitTest::drand ( )
inlinestaticprotected

random double in [0,1[. It is recommended to use initClass() to initialize the random number generator before using this method.

virtual double sofa::helper::UnitTest::epsilon ( ) const
inlineprotectedvirtual

A very small value. Can be used to check if an error is small enough.

const std::string& sofa::helper::UnitTest::getName ( ) const
inlineprotected
void sofa::helper::UnitTest::initClass ( )
staticprotected

General-purpose initializations, such as the random number generator.

template<typename Matrix1 , typename Matrix2 >
static bool sofa::helper::UnitTest::matricesAreEqual ( const Matrix1 &  m1,
const Matrix2 m2,
double  tolerance = std::numeric_limits<double>::epsilon() 
)
inlinestaticprotected

return true if the matrices have same size and all their entries are equal within the given tolerance

virtual void sofa::helper::UnitTest::runTests ( unsigned &  numTests,
unsigned &  numWarnings,
unsigned &  numErrors 
)
pure virtual

Run a series of unit tests. Increment

Parameters
numTestswith the number of unit tests performed. Increment
numWarningswith the number of warnings issued. Increment
numErrorswith the number of errors detected. For each unit test, method checkIf(bool testSucceeded, std::string testDescription, unsigned& numTests, unsigned& numErrors) can be used to automatically increment numTests and numErrors, as well as issuing log and error messages.
virtual std::ostream& sofa::helper::UnitTest::serr ( )
inlineprotectedvirtual

Error and warning output stream, skipped only if the verbosity level is SILENT.

virtual std::ostream& sofa::helper::UnitTest::sout ( )
inlineprotectedvirtual

Basic output stream, displayed only if the verbosity level is ALL_MESSAGES.

template<typename Vector1 , typename Vector2 >
static bool sofa::helper::UnitTest::vectorsAreEqual ( const Vector1 &  m1,
const Vector2 &  m2,
double  tolerance = std::numeric_limits<double>::epsilon() 
)
inlinestaticprotected

return true if the matrices have same size and all their entries are equal within the given tolerance

VerbosityLevel sofa::helper::UnitTest::verbosityLevel ( )
inlineprotected

Enum details

Enumerator
SILENT 
WARNINGS_ONLY 
ALL_MESSAGES