diff --git a/DDTest/CMakeLists.txt b/DDTest/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a1c6c51d07621695ea2bd0a3353c69b634c07bb9 --- /dev/null +++ b/DDTest/CMakeLists.txt @@ -0,0 +1,18 @@ + + + +#-------------------------------------------------- + +include_directories( ./include ) + +#-------------------------------------------------- + +SET( test_name "example_test" ) + +ADD_EXECUTABLE( ${test_name} ./src/${test_name}.cc ) + +ADD_TEST( t_${test_name} "${EXECUTABLE_OUTPUT_PATH}/${test_name}" ) +SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "TEST_PASSED" ) +SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED" ) + +#-------------------------------------------------- diff --git a/DDTest/include/DD4hepTest.h b/DDTest/include/DD4hepTest.h new file mode 100644 index 0000000000000000000000000000000000000000..cb0eba942e317eab4299effd978314b0481fa8c5 --- /dev/null +++ b/DDTest/include/DD4hepTest.h @@ -0,0 +1,165 @@ +#include <iostream> +#include <sstream> +#include <stdlib.h> + +/** Simple class for defining unit tests. + * Use in main program that is added as a test to ctest: + * + * DD4hepTest test = DD4hepTest( "example" ) ; + * test.log( "example test" ); + * test( "Example", "Example", "example test - string comparison " ); // this test will pass + * test( "Example", "BadExample", "example test - string comparison " ); // this test will fail + * + * @author F.Gaede, DESY, 2014 + * based on original version from J.Engels + */ +class DD4hepTest{ + + DD4hepTest() : _out(std::cout) {} + +public: + + + /** Only constructor + */ + DD4hepTest( const std::string& testname, std::ostream& stream=std::cout ) : + _testname(testname), + _out(stream), + _failed(0), + _passed(0), + _last_test_status(false) { + + _out << std::endl << "[" << _testname << "] "; + + _out << "****************************** TEST_BEGIN ******************************" << std::endl << std::endl; + } + + + + /** Destructor - print summary of tests passed and failed + */ + ~DD4hepTest(){ + + std::stringstream sstr ; + + sstr << std::endl; + sstr << "[" << _testname << "] number of tests PASSED : " << _passed << std::endl ; + sstr << "[" << _testname << "] number of tests FAILED : " << _failed << std::endl ; + sstr << std::endl; + + sstr << "[" << _testname << "] " ; + sstr << "****************************** " ; + sstr << ( _failed == 0 ? "TEST_PASSED" : "TEST_FAILED" ) ; + sstr << " ******************************" ; + sstr << std::endl << std::endl ; + + _out << sstr.str() ; + + if( _failed != 0 ) exit(1) ; + + } + + + /** Operator for calling a test - test is passed if v1 == v2 + */ + template <class V1, class V2 > + void operator()(const V1& v1, const V2& v2, const std::string& name ) { + + if ( ! (v1 == v2) ) { + + std::stringstream sstr ; + sstr << " " << name<< " : [" << v1 << "] != [" << v2 <<"]" ; + + error( sstr.str() ) ; + + } else { + + pass( name ) ; + } + + return ; + } + + /** Operator for calling a test - test is passed if (!c)==false + */ + template <class Cond > + void operator()(const Cond& c, const std::string& name ) { + + if ( ! (c) ) { + + std::stringstream sstr ; + sstr << " " << name<< " : [" << c << "] " ; + + error( sstr.str() ) ; + + } else { + + pass( name ) ; + } + return ; + } + + + /** Simple log message */ + void log( const std::string& msg ){ + _out << "[" << _testname << "] " << msg << std::endl; + } + + + /** print message when test passed */ + void pass( const std::string& msg ){ + + _passed++; + _last_test_status = true ; + + _out << "[" << _testname << "] test " << last_test_status() << " : " << msg << std::endl; + } + + + + /** print message when test failed */ + void error( const std::string& msg ){ + + _failed++; + _last_test_status = false ; + + std::stringstream errmsg; + // errmsg << std::endl; + errmsg << "[" << _testname << "] ##################### TEST_FAILED ######################" << std::endl; + errmsg << "[" << _testname << "] ### ERROR: " << msg << std::endl; + errmsg << "[" << _testname << "] ########################################################" << std::endl; + // errmsg << std::endl; + + _out << errmsg.str(); + + // also send error to stderr + //std::cerr << errmsg.str(); + } + + + + /** Fatal error ...*/ + void fatal_error( const std::string& msg ){ + error( msg ); + _out << "FATAL ERROR OCCURRED, program will exit now !!" << std::endl ; + exit(1); + } + + + + /** Return the status from the last test - either PASSED or FAILED */ + const char* last_test_status(){ + return ( _last_test_status ? "PASSED" : "FAILED" ) ; + } + + + +private: + + std::string _testname ; + std::ostream& _out ; + + unsigned int _failed ; // number of failed tests + unsigned int _passed ; // number of passed tests + bool _last_test_status ; // true if last test succeeded, false otherwise +}; diff --git a/DDTest/src/example_test.cc b/DDTest/src/example_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..f17ac76e94822ff4bbbec44c37ac614e6dc46881 --- /dev/null +++ b/DDTest/src/example_test.cc @@ -0,0 +1,55 @@ +#include "DD4hepTest.h" +#include <exception> +#include <iostream> +#include <assert.h> +#include <cmath> + +using namespace std ; + +// this should be the first line in your test +DD4hepTest test = DD4hepTest( "example" ) ; + +//============================================================================= + +int main(int argc, char** argv ){ + + try{ + + // ----- write your tests in here ------------------------------------- + + test.log( "example test" ); + + // ----- example test for testing two expressions for equality: + + test( "Example", "Example", "example test - string comparison " ); // this test will pass + + //test( "Example", "BadExample", "example test - string comparison " ); // this test will fail + + + + double pi_d = M_PI ; + float pi_f = M_PI ; + + // ----- example for test of an expresssion - not using equality + + test( 1.1 * pi_f > pi_d , " example comparison: 1.1* float(M_PI) > double( M_PI) " ) ; + + // ... guess which of these will pass ... + // test( double(pi_f) == pi_d , " example comparison: float(M_PI) == double( M_PI) " ) ; + // test( double(pi_f) < pi_d , " example comparison: float(M_PI) < double( M_PI) " ) ; + // test( double(pi_f) > pi_d , " example comparison: float(M_PI) > double( M_PI) " ) ; + + // -------------------------------------------------------------------- + + + } catch( exception &e ){ + //} catch( ... ){ + + test.log( e.what() ); + test.error( "exception occurred" ); + } + + return 0; +} + +//=============================================================================