GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GTestSuite Class Referenceabstract

Abstract test suite class for unit testing on GammaLib fixtures. More...

#include <GTestSuite.hpp>

Inheritance diagram for GTestSuite:
GBase

Public Member Functions

 GTestSuite (void)
 Void constructor. More...
 
 GTestSuite (const GTestSuite &testsuite)
 Copy constructor. More...
 
 GTestSuite (const std::string &name)
 Name constructor. More...
 
virtual ~GTestSuite (void)
 Destructor. More...
 
GTestSuiteoperator= (const GTestSuite &testsuite)
 Assignment operator. More...
 
GTestCaseoperator[] (const int &index)
 Returns reference to test case. More...
 
const GTestCaseoperator[] (const int &index) const
 Returns reference to test case. More...
 
virtual GTestSuiteclone (void) const =0
 Clones object. More...
 
virtual std::string classname (void) const =0
 Return class name. More...
 
virtual void set (void)=0
 
void clear (void)
 Clear test suite. More...
 
int size (void) const
 Return number of tests in test suite. More...
 
void append (pfunction function, const std::string &name)
 Append test functions to test suite. More...
 
bool run (void)
 Run all tests in test suite. More...
 
const std::string & name (void) const
 Return test suite name. More...
 
void name (const std::string &name)
 Set Test Suite name. More...
 
void cout (const bool &flag)
 Enables/disables logging into standard output stream. More...
 
void test_assert (const bool &result, const std::string &name, const std::string &message="")
 Test an assert. More...
 
void test_value (const int &value, const int &expected, const std::string &name="", const std::string &message="")
 Test an integer value. More...
 
void test_value (const double &value, const double &expected, const std::string &name="", const std::string &message="")
 Test a double precision value. More...
 
void test_value (const double &value, const double &expected, const double &eps, const std::string &name="", const std::string &message="")
 Test a double precision value. More...
 
void test_value (const std::complex< double > &value, const std::complex< double > &expected, const std::string &name="", const std::string &message="")
 Test a complex value. More...
 
void test_value (const std::complex< double > &value, const std::complex< double > &expected, const double &eps, const std::string &name="", const std::string &message="")
 Test a complex value. More...
 
void test_value (const std::string &value, const std::string &expected, const std::string &name="", const std::string &message="")
 Test a string value. More...
 
void test_try (const std::string &name)
 Test an try block. More...
 
void test_try_success (void)
 Notice when a try block succeeded. More...
 
void test_try_failure (const std::string &message="", const std::string &type="")
 Notice when a try block failed. More...
 
void test_try_failure (const std::exception &e)
 Notice when a try block failed. More...
 
GException::test_failureexception_failure (const std::string &message)
 Return a failure exception. More...
 
GException::test_errorexception_error (const std::string &message)
 Return an error exception. More...
 
const int & errors (void) const
 Return the number of errors. More...
 
const int & failures (void) const
 Return the number of failures. More...
 
int success (void) const
 Return the number of successful tests. More...
 
const time_t & timestamp (void) const
 Return the timestamp. More...
 
double duration (void) const
 Return the total duration of all tests. More...
 
std::string print (const GChatter &chatter=NORMAL) const
 Print test suite information. More...
 
- Public Member Functions inherited from GBase
virtual ~GBase (void)
 Destructor. More...
 

Protected Member Functions

void init_members (void)
 Initialise class members. More...
 
void copy_members (const GTestSuite &testsuite)
 Copy class members. More...
 
void free_members (void)
 Delete class members. More...
 
std::string format_name (const std::string &name)
 Format Name. More...
 

Protected Attributes

std::string m_name
 Name of the test suite. More...
 
std::vector< std::string > m_names
 Names of test functions. More...
 
std::vector< pfunctionm_functions
 Test functions of this suite. More...
 
std::vector< GTestCase * > m_tests
 List of test results. More...
 
std::vector< GTestCase * > m_stack_try
 Stack for nested try blocks. More...
 
int m_index
 Index of actual test function. More...
 
int m_failures
 Number of failures. More...
 
int m_errors
 Number of errors. More...
 
GLog m_log
 Log. More...
 
time_t m_timestamp
 Timestamp. More...
 

Detailed Description

Abstract test suite class for unit testing on GammaLib fixtures.

Todo:
Detailed explanation.

Definition at line 53 of file GTestSuite.hpp.

Constructor & Destructor Documentation

GTestSuite::GTestSuite ( void  )

Void constructor.

Definition at line 68 of file GTestSuite.cpp.

References init_members().

GTestSuite::GTestSuite ( const GTestSuite suite)

Copy constructor.

Parameters
[in]suiteTest Suite.

Definition at line 83 of file GTestSuite.cpp.

References copy_members(), and init_members().

GTestSuite::GTestSuite ( const std::string &  name)

Name constructor.

Parameters
[in]nameTest suite name.

Definition at line 101 of file GTestSuite.cpp.

References init_members(), m_name, and name().

GTestSuite::~GTestSuite ( void  )
virtual

Destructor.

Definition at line 117 of file GTestSuite.cpp.

References free_members().

Member Function Documentation

void GTestSuite::append ( pfunction  function,
const std::string &  name 
)

Append test functions to test suite.

Parameters
[in]functionTest function pointer.
[in]nameTest name.

This method adds test functions to the test suite. The test functions will be executed when the run method is called.

Definition at line 234 of file GTestSuite.cpp.

References m_functions, and m_names.

virtual std::string GTestSuite::classname ( void  ) const
pure virtual

Return class name.

Returns
String containing the class name.

Returns the class name for non-abstract classes in a human readable way.

Implements GBase.

void GTestSuite::clear ( void  )
virtual

Clear test suite.

Implements GBase.

Definition at line 212 of file GTestSuite.cpp.

References free_members(), and init_members().

virtual GTestSuite* GTestSuite::clone ( void  ) const
pure virtual

Clones object.

Returns
Pointer to deep copy of object.

Creates a deep copy of the object and returns a pointer to the object.

Implements GBase.

Referenced by GTestSuites::append(), GTestSuites::insert(), and GTestSuites::set().

void GTestSuite::copy_members ( const GTestSuite suite)
protected

Copy class members.

Parameters
[in]suiteTest suite.

This method just clone the container not the test case.

Definition at line 1038 of file GTestSuite.cpp.

References m_errors, m_failures, m_functions, m_index, m_log, m_name, m_names, m_stack_try, m_tests, and m_timestamp.

Referenced by GTestSuite(), and operator=().

void GTestSuite::cout ( const bool &  flag)
inline

Enables/disables logging into standard output stream.

Parameters
[in]flagEnable/disable logging (true/false).

Enables or disables logging into the standard output stream.

Definition at line 185 of file GTestSuite.hpp.

References GLog::cout(), and m_log.

Referenced by init_members().

double GTestSuite::duration ( void  ) const

Return the total duration of all tests.

This method sums up all test durations and returns the result.

Definition at line 950 of file GTestSuite.cpp.

References m_tests.

Referenced by GTestSuites::write().

const int & GTestSuite::errors ( void  ) const
inline

Return the number of errors.

Returns
Number of errors.

Definition at line 198 of file GTestSuite.hpp.

References m_errors.

Referenced by print(), run(), and GTestSuites::write().

GException::test_error & GTestSuite::exception_error ( const std::string &  message)

Return an error exception.

Parameters
[in]messageMessage.
See Also
test_try()
test_failure(const std::string& message)

It can be use in a try test

Example: test_try("Test a try block"); try { throw exception_error("an error"); test_try_success(); } catch(exception& e) { test_try_failure(e); }

Definition at line 928 of file GTestSuite.cpp.

References m_stack_try.

GException::test_failure & GTestSuite::exception_failure ( const std::string &  message)

Return a failure exception.

Parameters
[in]messageMessage.
See Also
test_try()
test_error(const std::string& message)

It can be use in a try test

Example: test_try("Test a try block"); try { throw exception_failure("a failure"); test_try_success(); } catch(exception& e) { test_try_failure(e); }

Definition at line 901 of file GTestSuite.cpp.

References m_stack_try.

const int & GTestSuite::failures ( void  ) const
inline

Return the number of failures.

Returns
Number of failures.

Definition at line 210 of file GTestSuite.hpp.

References m_failures.

Referenced by print(), run(), and GTestSuites::write().

std::string GTestSuite::format_name ( const std::string &  name)
protected

Format Name.

Return a string with the format "TestFunctionName:TestTryname1:TestTryName2: name"

Definition at line 1096 of file GTestSuite.cpp.

References m_index, m_names, m_stack_try, and name().

Referenced by test_assert(), test_try(), and test_value().

void GTestSuite::free_members ( void  )
protected

Delete class members.

Definition at line 1068 of file GTestSuite.cpp.

References GLog::close(), m_log, m_stack_try, and m_tests.

Referenced by clear(), operator=(), and ~GTestSuite().

void GTestSuite::init_members ( void  )
protected

Initialise class members.

Definition at line 1009 of file GTestSuite.cpp.

References GLog::buffer_size(), GLog::clear(), cout(), m_errors, m_failures, m_functions, m_index, m_log, m_name, m_names, m_stack_try, m_tests, and m_timestamp.

Referenced by clear(), GTestSuite(), and operator=().

const std::string & GTestSuite::name ( void  ) const
inline

Return test suite name.

Definition at line 158 of file GTestSuite.hpp.

References m_name.

Referenced by format_name(), GTestSuite(), name(), run(), test_try_failure(), and GTestSuites::write().

void GTestSuite::name ( const std::string &  name)
inline

Set Test Suite name.

Parameters
[in]nameTest suite name.

Definition at line 170 of file GTestSuite.hpp.

References m_name, and name().

GTestSuite & GTestSuite::operator= ( const GTestSuite suite)

Assignment operator.

Parameters
[in]suiteTest suite.
Returns
Test suite.

Definition at line 139 of file GTestSuite.cpp.

References copy_members(), free_members(), and init_members().

GTestCase & GTestSuite::operator[] ( const int &  index)

Returns reference to test case.

Parameters
[in]indexTest case index [0,...,size()-1].
Exceptions
GException::out_of_rangeTest case index is out of range.

Definition at line 168 of file GTestSuite.cpp.

References G_OP_ACCESS, m_tests, and size().

const GTestCase & GTestSuite::operator[] ( const int &  index) const

Returns reference to test case.

Parameters
[in]indexTest case index [0,...,size()-1].
Exceptions
GException::out_of_rangeTest case index is out of range.

Definition at line 189 of file GTestSuite.cpp.

References G_OP_ACCESS, m_tests, and size().

std::string GTestSuite::print ( const GChatter chatter = NORMAL) const
virtual

Print test suite information.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing test suite information.

Implements GBase.

Definition at line 971 of file GTestSuite.cpp.

References errors(), failures(), m_name, m_names, gammalib::parformat(), SILENT, size(), and gammalib::str().

bool GTestSuite::run ( void  )

Run all tests in test suite.

Returns
True if all tests were successful, false otherwise.

Executes all test functions that have been appended to the test suite. For each test function a test case is added to the test suite.

Definition at line 253 of file GTestSuite.cpp.

References GTestCase::duration(), GTestCase::ERROR_TEST, errors(), failures(), GTestCase::has_passed(), m_errors, m_failures, m_functions, m_index, m_names, m_tests, GTestCase::message(), name(), GTestCase::print(), set(), success(), and GTestCase::type().

virtual void GTestSuite::set ( void  )
pure virtual

Referenced by run().

int GTestSuite::size ( void  ) const
inline

Return number of tests in test suite.

Definition at line 148 of file GTestSuite.hpp.

References m_tests.

Referenced by operator[](), print(), success(), and GTestSuites::write().

int GTestSuite::success ( void  ) const

Return the number of successful tests.

Definition at line 938 of file GTestSuite.cpp.

References m_errors, m_failures, and size().

Referenced by run().

void GTestSuite::test_assert ( const bool &  assert,
const std::string &  name,
const std::string &  message = "" 
)

Test an assert.

Parameters
[in]assertAssert (true/false).
[in]nameTest case name.
[in]messageTest case name (defaults to "").

Tests if a condition is true or false. This method adds a test case of type "failure" to the test suite.

Examples: test_assert(x>3, "Test if x > 3"); test_assert(x>3 && x<10, "Test if 3 < x < 10 ");

Definition at line 368 of file GTestSuite.cpp.

References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), and GTestCase::print().

void GTestSuite::test_try ( const std::string &  name)

Test an try block.

Parameters
[in]nameTest case name (defaults to "").
See Also
test_try_sucess()
test_try_failure(const std::string& message,const std::string& type)
test_try_failure(const std::exception& e)

Call before testing a try/catch block.

Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }

Definition at line 727 of file GTestSuite.cpp.

References GTestCase::ERROR_TEST, format_name(), and m_stack_try.

void GTestSuite::test_try_failure ( const std::string &  message = "",
const std::string &  type = "" 
)

Notice when a try block failed.

Parameters
[in]messageMessage to explain why test failed (defaults to "").
[in]typeType of message (defaults to "").
Exceptions
GException::test_nested_try_errorTest case index is out of range.
See Also
test_try_sucess()
test_try(const std::string& name)
test_try_failure(const std::exception& e)

Call this method in the catch block.

Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }

Definition at line 809 of file GTestSuite.cpp.

References GTestCase::ERROR_TEST, m_errors, m_stack_try, and m_tests.

Referenced by test_try_failure().

void GTestSuite::test_try_failure ( const std::exception &  e)

Notice when a try block failed.

Parameters
[in]eException.
Exceptions
GException::test_nested_try_errorTest case index is out of range.
See Also
test_try_sucess()
test_try(const std::string& name)
test_try_failure(const std::string& message, const std::string& type)

Call this method in a catch block.

Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(exception& e) { test_try_failure(e); }

Definition at line 871 of file GTestSuite.cpp.

References name(), and test_try_failure().

void GTestSuite::test_try_success ( void  )

Notice when a try block succeeded.

Exceptions
GException::test_nested_try_errorTest case index is out of range.
See Also
test_try(const std::string& name)
test_try_failure(const std::string& message, const std::string& type)
test_try_failure(const std::exception& e)

Call this method at the last line of a try

Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }

Definition at line 762 of file GTestSuite.cpp.

References G_TRY_SUCCESS, m_stack_try, and m_tests.

void GTestSuite::test_value ( const int &  value,
const int &  expected,
const std::string &  name = "",
const std::string &  message = "" 
)

Test an integer value.

Parameters
[in]valueInteger value to test.
[in]expectedExpected integer value.
[in]nameTest case name.
[in]messageTest case message.

Test if integer value is the expected value.

Definition at line 406 of file GTestSuite.cpp.

References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), GTestCase::print(), and gammalib::str().

Referenced by test_value().

void GTestSuite::test_value ( const double &  value,
const double &  expected,
const std::string &  name = "",
const std::string &  message = "" 
)

Test a double precision value.

Parameters
[in]valueDouble precision value to test.
[in]expectedExpected double precision value.
[in]nameTest case name.
[in]messageTest case message.

Test if the value is equal to the expected value within a relative precision of 1.0e-7.

Definition at line 467 of file GTestSuite.cpp.

References abs(), and test_value().

void GTestSuite::test_value ( const double &  value,
const double &  expected,
const double &  eps,
const std::string &  name = "",
const std::string &  message = "" 
)

Test a double precision value.

Parameters
[in]valueDouble precision value to test.
[in]expectedExpected double precision value.
[in]epsPrecision of the test.
[in]nameTest case name.
[in]messageTest case message.

Test if the value is comprised in the interval [expected-eps, expected+eps].

Definition at line 495 of file GTestSuite.cpp.

References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), GTestCase::print(), and gammalib::str().

void GTestSuite::test_value ( const std::complex< double > &  value,
const std::complex< double > &  expected,
const std::string &  name = "",
const std::string &  message = "" 
)

Test a complex value.

Parameters
[in]valueComplex value to test.
[in]expectedExpected complex value.
[in]nameTest case name.
[in]messageTest case message.

Test if the value is equal to the expected value within a relative precision of 1.0e-7.

Definition at line 561 of file GTestSuite.cpp.

References abs(), and test_value().

void GTestSuite::test_value ( const std::complex< double > &  value,
const std::complex< double > &  expected,
const double &  eps,
const std::string &  name = "",
const std::string &  message = "" 
)

Test a complex value.

Parameters
[in]valueComplex value to test.
[in]expectedExpected complex value.
[in]epsPrecision of the test.
[in]nameTest case name.
[in]messageTest case message.

Test if the value is comprised in the interval [expected-eps, expected+eps].

Definition at line 589 of file GTestSuite.cpp.

References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), GTestCase::print(), and gammalib::str().

void GTestSuite::test_value ( const std::string &  value,
const std::string &  expected,
const std::string &  name = "",
const std::string &  message = "" 
)

Test a string value.

Parameters
[in]valueString value to test.
[in]expectedExpected string value.
[in]nameTest case name (defaults to "").
[in]messageTest case message (defaults to "").

Test if the string value corresponds to the expected value.

Definition at line 657 of file GTestSuite.cpp.

References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), and GTestCase::print().

const time_t & GTestSuite::timestamp ( void  ) const
inline

Return the timestamp.

Returns
Timestamp.

The timestamp is set at the construction of the object.

Definition at line 224 of file GTestSuite.hpp.

References m_timestamp.

Referenced by GTestSuites::write().

Member Data Documentation

int GTestSuite::m_errors
protected

Number of errors.

Definition at line 138 of file GTestSuite.hpp.

Referenced by copy_members(), errors(), init_members(), run(), success(), and test_try_failure().

int GTestSuite::m_failures
protected

Number of failures.

Definition at line 137 of file GTestSuite.hpp.

Referenced by copy_members(), failures(), init_members(), run(), success(), test_assert(), and test_value().

std::vector<pfunction> GTestSuite::m_functions
protected

Test functions of this suite.

Definition at line 133 of file GTestSuite.hpp.

Referenced by append(), copy_members(), init_members(), and run().

int GTestSuite::m_index
protected

Index of actual test function.

Definition at line 136 of file GTestSuite.hpp.

Referenced by copy_members(), format_name(), init_members(), and run().

GLog GTestSuite::m_log
protected

Log.

Definition at line 139 of file GTestSuite.hpp.

Referenced by copy_members(), cout(), free_members(), and init_members().

std::string GTestSuite::m_name
protected

Name of the test suite.

Definition at line 131 of file GTestSuite.hpp.

Referenced by copy_members(), GTestSuite(), init_members(), name(), and print().

std::vector<std::string> GTestSuite::m_names
protected

Names of test functions.

Definition at line 132 of file GTestSuite.hpp.

Referenced by append(), copy_members(), format_name(), init_members(), print(), and run().

std::vector<GTestCase*> GTestSuite::m_stack_try
protected
std::vector<GTestCase*> GTestSuite::m_tests
protected
time_t GTestSuite::m_timestamp
protected

Timestamp.

Definition at line 140 of file GTestSuite.hpp.

Referenced by copy_members(), init_members(), and timestamp().


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