ATLAS Offline Software
expect.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef TestTools_expect_h
6 #define TestTools_expect_h
7 
8 #undef NDEBUG
9 #include <cassert>
10 #include <iostream>
11 #include <cmath>
12 namespace Athena_test {
13 
14  /*
15  * Helpers for float/double comarisons with the precision and every other type exactly
16  */
17  template <typename T>
18  bool cmp_eq( T a, T b ) { return a == b; }
19  template<>
20  bool cmp_eq<float>( float a, float b ) { return std::abs(a - b) < 1.e-4; }
21  template<>
22  bool cmp_eq<double>( double a, double b ) { return std::abs(a - b) < 1.e-6; }
23 
24  /*
25  * Helper class, offering method to compare for equality to the value captured during construction.
26  * In case of a difference the message of what is the value captured and what was expected
27  * is printed. In addition the assertion macro is used to make the test failing in this case.
28  *
29  * There is also a symmetric method for checking for inequality.
30  */
31  template <typename T>
32  class TestedValue {
33  public:
34  TestedValue( T v, std::string&& f, int l)
35  : m_value(v),
36  m_file(std::move(f)),
37  m_line(l) {}
38  void EXPECTED( const T& e ) {
39  if ( not cmp_eq(e, m_value) ) {
40  std::cerr << m_file << ":" << m_line << ": error: Test failed, "
41  << "expected: " << e << " obtained: " << m_value << "\n";
42  assert( cmp_eq(e, m_value) );
43  }
44  }
45  void NOT_EXPECTED( const T& e ) {
46  if ( cmp_eq(e, m_value) ) {
47  std::cerr << m_file << ":" << m_line << ": error: Test failed, "
48  << "NOT expected: " << e << " obtained: " << m_value << "\n";
49  assert( not cmp_eq(e, m_value) );
50  }
51  }
52  private:
54  std::string m_file;
55  int m_line;
56  };
57 }
58 
59 #define VALUE( TESTED ) Athena_test::TestedValue<decltype(TESTED)>(TESTED, __FILE__, __LINE__).
60 
61 /*
62  * @brief macros (& simple class) for human readable stating assertions in unit tests
63  * The syntax will be:
64  * VALUE ( x ) EXPECTED ( true ); // exact comparisons
65  * VALUE ( y ) EXPECTED ( "something");
66  * VALUE ( z ) EXPECTED ( 3.1415 ); // this would compare with precision 1e-4 for floats and 1e-6 for doubles
67  * VALUE ( t ) NOT_EXPECTED ( 0 ); // the inverted check is also possible
68  */
69 
70 #endif // TestTools_expect_h
Athena_test::cmp_eq< double >
bool cmp_eq< double >(double a, double b)
Definition: expect.h:22
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Athena_test::TestedValue::TestedValue
TestedValue(T v, std::string &&f, int l)
Definition: expect.h:34
Athena_test::cmp_eq< float >
bool cmp_eq< float >(float a, float b)
Definition: expect.h:20
Athena_test::cmp_eq
bool cmp_eq(T a, T b)
Definition: expect.h:18
Athena_test::TestedValue::m_file
std::string m_file
Definition: expect.h:54
Athena_test::TestedValue
Definition: expect.h:32
Athena_test::TestedValue::m_value
T m_value
Definition: expect.h:53
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
Athena_test::TestedValue::EXPECTED
void EXPECTED(const T &e)
Definition: expect.h:38
Athena_test::TestedValue::NOT_EXPECTED
void NOT_EXPECTED(const T &e)
Definition: expect.h:45
Athena_test::TestedValue::m_line
int m_line
Definition: expect.h:55
Athena_test
functions & macros to test the difference between floats
Definition: InitGaudiGoogleTest.h:30
hist_file_dump.f
f
Definition: hist_file_dump.py:135
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
python.PyAthena.v
v
Definition: PyAthena.py:154
a
TList * a
Definition: liststreamerinfos.cxx:10