File indexing completed on 2025-02-21 09:58:00
0001 #ifndef DD4HEP_DDTEST_H
0002 #define DD4HEP_DDTEST_H
0003
0004 #include <iostream>
0005 #include <sstream>
0006 #include <stdlib.h>
0007
0008 namespace dd4hep{
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 class DDTest{
0022
0023 public:
0024
0025 DDTest() = delete;
0026
0027
0028 DDTest(const DDTest& copy) = delete;
0029
0030
0031 DDTest& operator=(const DDTest& copy) = delete;
0032
0033
0034
0035
0036 DDTest( const std::string& testname, std::ostream& stream=std::cout ) :
0037 _testname(testname),
0038 _out(stream),
0039 _failed(0),
0040 _passed(0),
0041 _last_test_status(false) {
0042
0043 _out << std::endl << "[" << _testname << "] ";
0044
0045 _out << "****************************** TEST_BEGIN ******************************" << std::endl << std::endl;
0046 }
0047
0048
0049
0050
0051
0052 ~DDTest(){
0053
0054 std::stringstream sstr ;
0055
0056 sstr << std::endl;
0057 sstr << "[" << _testname << "] number of tests PASSED : " << _passed << std::endl ;
0058 sstr << "[" << _testname << "] number of tests FAILED : " << _failed << std::endl ;
0059 sstr << std::endl;
0060
0061 sstr << "[" << _testname << "] " ;
0062 sstr << "****************************** " ;
0063 sstr << ( _failed == 0 ? "TEST_PASSED" : "TEST_FAILED" ) ;
0064 sstr << " ******************************" ;
0065 sstr << std::endl << std::endl ;
0066
0067 _out << sstr.str() ;
0068
0069 if( _failed != 0 ) exit(1) ;
0070
0071 }
0072
0073
0074
0075
0076 template <class V1, class V2 >
0077 void operator()(const V1& v1, const V2& v2, const std::string& name ) {
0078
0079 if ( ! (v1 == v2) ) {
0080
0081 std::stringstream sstr ;
0082 sstr << " " << name<< " : [" << v1 << "] != [" << v2 <<"]" ;
0083
0084 error( sstr.str() ) ;
0085
0086 } else {
0087
0088 std::stringstream sstr ;
0089 sstr << " " << name<< " : [" << v1 << "] == [" << v2 <<"]" ;
0090
0091 pass( sstr.str() ) ;
0092 }
0093
0094 return ;
0095 }
0096
0097
0098
0099 template <class Cond >
0100 void operator()(const Cond& c, const std::string& name ) {
0101
0102 if ( ! (c) ) {
0103
0104 std::stringstream sstr ;
0105 sstr << " " << name<< " : [" << c << "] " ;
0106
0107 error( sstr.str() ) ;
0108
0109 } else {
0110
0111 std::stringstream sstr ;
0112 sstr << " " << name<< " : [" << c << "] " ;
0113
0114 pass( sstr.str() ) ;
0115 }
0116 return ;
0117 }
0118
0119
0120
0121 void log( const std::string& msg ){
0122 _out << "[" << _testname << "] " << msg << std::endl;
0123 }
0124
0125
0126
0127 void pass( const std::string& msg ){
0128
0129 _passed++;
0130 _last_test_status = true ;
0131
0132 _out << "[" << _testname << "] test " << last_test_status() << ": " << msg << std::endl;
0133 }
0134
0135
0136
0137
0138 void error( const std::string& msg ){
0139
0140 _failed++;
0141 _last_test_status = false ;
0142
0143 std::stringstream errmsg;
0144
0145 errmsg << "[" << _testname << "] ##################### TEST_FAILED ######################" << std::endl;
0146 errmsg << "[" << _testname << "] ### ERROR: " << msg << std::endl;
0147 errmsg << "[" << _testname << "] ########################################################" << std::endl;
0148
0149
0150 _out << errmsg.str();
0151
0152
0153
0154 }
0155
0156
0157 void fatal_error( const std::string& msg ){
0158 error( msg );
0159 _out << "FATAL ERROR OCCURRED, program will exit now !!" << std::endl ;
0160 exit(1);
0161 }
0162
0163
0164 const char* last_test_status(){
0165 return ( _last_test_status ? "PASSED" : "FAILED" ) ;
0166 }
0167
0168 private:
0169
0170 std::string _testname ;
0171 std::ostream& _out = std::cout;
0172
0173 unsigned int _failed = 0;
0174 unsigned int _passed = 0;
0175 bool _last_test_status = false;
0176 };
0177
0178
0179 }
0180
0181 #endif