DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
DDTest.h
Go to the documentation of this file.
1 #ifndef DD4HEP_DDTEST_H
2 #define DD4HEP_DDTEST_H
3 
4 #include <iostream>
5 #include <sstream>
6 #include <stdlib.h>
7 
8 namespace dd4hep{
9 
11 
21  class DDTest{
22 
23  public:
25  DDTest() = delete;
26 
28  DDTest(const DDTest& copy) = delete;
29 
31  DDTest& operator=(const DDTest& copy) = delete;
32 
33 
36  DDTest( const std::string& testname, std::ostream& stream=std::cout ) :
37  _testname(testname),
38  _out(stream),
39  _failed(0),
40  _passed(0),
41  _last_test_status(false) {
42 
43  _out << std::endl << "[" << _testname << "] ";
44 
45  _out << "****************************** TEST_BEGIN ******************************" << std::endl << std::endl;
46  }
47 
48 
49 
53 
54  std::stringstream sstr ;
55 
56  sstr << std::endl;
57  sstr << "[" << _testname << "] number of tests PASSED : " << _passed << std::endl ;
58  sstr << "[" << _testname << "] number of tests FAILED : " << _failed << std::endl ;
59  sstr << std::endl;
60 
61  sstr << "[" << _testname << "] " ;
62  sstr << "****************************** " ;
63  sstr << ( _failed == 0 ? "TEST_PASSED" : "TEST_FAILED" ) ;
64  sstr << " ******************************" ;
65  sstr << std::endl << std::endl ;
66 
67  _out << sstr.str() ;
68 
69  if( _failed != 0 ) exit(1) ;
70 
71  }
72 
73 
76  template <class V1, class V2 >
77  void operator()(const V1& v1, const V2& v2, const std::string& name ) {
78 
79  if ( ! (v1 == v2) ) {
80 
81  std::stringstream sstr ;
82  sstr << " " << name<< " : [" << v1 << "] != [" << v2 <<"]" ;
83 
84  error( sstr.str() ) ;
85 
86  } else {
87 
88  std::stringstream sstr ;
89  sstr << " " << name<< " : [" << v1 << "] == [" << v2 <<"]" ;
90 
91  pass( sstr.str() ) ;
92  }
93 
94  return ;
95  }
96 
99  template <class Cond >
100  void operator()(const Cond& c, const std::string& name ) {
101 
102  if ( ! (c) ) {
103 
104  std::stringstream sstr ;
105  sstr << " " << name<< " : [" << c << "] " ;
106 
107  error( sstr.str() ) ;
108 
109  } else {
110 
111  std::stringstream sstr ;
112  sstr << " " << name<< " : [" << c << "] " ;
113 
114  pass( sstr.str() ) ;
115  }
116  return ;
117  }
118 
119 
121  void log( const std::string& msg ){
122  _out << "[" << _testname << "] " << msg << std::endl;
123  }
124 
125 
127  void pass( const std::string& msg ){
128 
129  _passed++;
130  _last_test_status = true ;
131 
132  _out << "[" << _testname << "] test " << last_test_status() << ": " << msg << std::endl;
133  }
134 
135 
136 
138  void error( const std::string& msg ){
139 
140  _failed++;
141  _last_test_status = false ;
142 
143  std::stringstream errmsg;
144  // errmsg << std::endl;
145  errmsg << "[" << _testname << "] ##################### TEST_FAILED ######################" << std::endl;
146  errmsg << "[" << _testname << "] ### ERROR: " << msg << std::endl;
147  errmsg << "[" << _testname << "] ########################################################" << std::endl;
148  // errmsg << std::endl;
149 
150  _out << errmsg.str();
151 
152  // also send error to stderr
153  //std::cerr << errmsg.str();
154  }
155 
157  void fatal_error( const std::string& msg ){
158  error( msg );
159  _out << "FATAL ERROR OCCURRED, program will exit now !!" << std::endl ;
160  exit(1);
161  }
162 
164  const char* last_test_status(){
165  return ( _last_test_status ? "PASSED" : "FAILED" ) ;
166  }
167 
168  private:
169 
170  std::string _testname ;
171  std::ostream& _out = std::cout;
172 
173  unsigned int _failed = 0; // number of failed tests
174  unsigned int _passed = 0; // number of passed tests
175  bool _last_test_status = false; // true if last test succeeded, false otherwise
176  };
177 
178 
179 } // end namespace
180 
181 #endif
dd4hep::DDTest::_passed
unsigned int _passed
Definition: DDTest.h:174
dd4hep::DDTest::last_test_status
const char * last_test_status()
Definition: DDTest.h:164
dd4hep::DDTest::operator()
void operator()(const V1 &v1, const V2 &v2, const std::string &name)
Definition: DDTest.h:77
dd4hep::DDTest::_testname
std::string _testname
Definition: DDTest.h:170
dd4hep::DDTest::~DDTest
~DDTest()
Definition: DDTest.h:52
dd4hep::DDTest::operator=
DDTest & operator=(const DDTest &copy)=delete
Assignment operator.
dd4hep::DDTest::pass
void pass(const std::string &msg)
Definition: DDTest.h:127
dd4hep::DDTest
Simple class for defining unit tests.
Definition: DDTest.h:21
dd4hep::DDTest::DDTest
DDTest()=delete
Default constructor.
dd4hep::DDTest::DDTest
DDTest(const DDTest &copy)=delete
Copy constructor.
dd4hep::DDTest::fatal_error
void fatal_error(const std::string &msg)
Definition: DDTest.h:157
dd4hep::DDTest::operator()
void operator()(const Cond &c, const std::string &name)
Definition: DDTest.h:100
dd4hep::DDTest::_out
std::ostream & _out
Definition: DDTest.h:171
dd4hep::DDTest::DDTest
DDTest(const std::string &testname, std::ostream &stream=std::cout)
Definition: DDTest.h:36
dd4hep::DDTest::error
void error(const std::string &msg)
Definition: DDTest.h:138
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DDTest::_failed
unsigned int _failed
Definition: DDTest.h:173
dd4hep::DDTest::_last_test_status
bool _last_test_status
Definition: DDTest.h:175
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::DDTest::log
void log(const std::string &msg)
Definition: DDTest.h:121