From b4856ea15c424a72c6d44ca666bff57a68ed0a0a Mon Sep 17 00:00:00 2001 From: Frank Gaede <frank.gaede@desy.de> Date: Thu, 9 Jan 2014 12:10:59 +0000 Subject: [PATCH] - renamed DD4hepTest to DDTest and put it in namespace DD4hep --- DDTest/CMakeLists.txt | 7 +- DDTest/include/DD4hepTest.h | 165 ---------------------------------- DDTest/include/DDTest.h | 170 ++++++++++++++++++++++++++++++++++++ DDTest/src/example_test.cc | 5 +- 4 files changed, 178 insertions(+), 169 deletions(-) delete mode 100644 DDTest/include/DD4hepTest.h create mode 100644 DDTest/include/DDTest.h diff --git a/DDTest/CMakeLists.txt b/DDTest/CMakeLists.txt index a1c6c51d0..0c9bab6f6 100644 --- a/DDTest/CMakeLists.txt +++ b/DDTest/CMakeLists.txt @@ -1,6 +1,4 @@ - - #-------------------------------------------------- include_directories( ./include ) @@ -16,3 +14,8 @@ SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "TEST_PA SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED" ) #-------------------------------------------------- + + +install(FILES ./include/DDTest.h + DESTINATION include/DD4hep + ) diff --git a/DDTest/include/DD4hepTest.h b/DDTest/include/DD4hepTest.h deleted file mode 100644 index cb0eba942..000000000 --- a/DDTest/include/DD4hepTest.h +++ /dev/null @@ -1,165 +0,0 @@ -#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/include/DDTest.h b/DDTest/include/DDTest.h new file mode 100644 index 000000000..de968c3a8 --- /dev/null +++ b/DDTest/include/DDTest.h @@ -0,0 +1,170 @@ +#include <iostream> +#include <sstream> +#include <stdlib.h> + +namespace DD4hep{ + + /** Simple class for defining unit tests. + * Use in main program that is added as a test to ctest: + * + * DDTest test = DDTest( "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 DDTest{ + + DDTest() : _out(std::cout) {} + + public: + + + /** Only constructor + */ + DDTest( 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 + */ + ~DDTest(){ + + 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 + }; + + +} // end namespace diff --git a/DDTest/src/example_test.cc b/DDTest/src/example_test.cc index f17ac76e9..686a88d5b 100644 --- a/DDTest/src/example_test.cc +++ b/DDTest/src/example_test.cc @@ -1,13 +1,14 @@ -#include "DD4hepTest.h" +#include "DDTest.h" #include <exception> #include <iostream> #include <assert.h> #include <cmath> using namespace std ; +using namespace DD4hep ; // this should be the first line in your test -DD4hepTest test = DD4hepTest( "example" ) ; +DDTest test = DDTest( "example" ) ; //============================================================================= -- GitLab