ATLAS Offline Software
Loading...
Searching...
No Matches
expect.h
Go to the documentation of this file.
1/*
2Copyright (C) 2002-2025 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>
12namespace 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>
33 public:
34 TestedValue( const 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
static Double_t a
TestedValue(const T &v, std::string &&f, int l)
Definition expect.h:34
void EXPECTED(const T &e)
Definition expect.h:38
std::string m_file
Definition expect.h:54
void NOT_EXPECTED(const T &e)
Definition expect.h:45
functions & macros to test the difference between floats
bool cmp_eq< float >(float a, float b)
Definition expect.h:20
bool cmp_eq< double >(double a, double b)
Definition expect.h:22
bool cmp_eq(T a, T b)
Definition expect.h:18
STL namespace.