ATLAS Offline Software
Loading...
Searching...
No Matches
expect.h
Go to the documentation of this file.
1/*
2Copyright (C) 2002-2026 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#ifndef XAOD_STANDALONE
10# include "GaudiKernel/StatusCode.h"
11#endif
12#include <cassert>
13#include <iostream>
14#include <print>
15#include <cmath>
16namespace Athena_test {
17
18 /*
19 * Helpers for float/double comarisons with the precision and every other type exactly
20 */
21 template <typename T>
22 bool cmp_eq( T a, T b ) { return a == b; }
23 template<>
24 bool cmp_eq<float>( float a, float b ) { return std::abs(a - b) < 1.e-4; }
25 template<>
26 bool cmp_eq<double>( double a, double b ) { return std::abs(a - b) < 1.e-6; }
27
30 template <class U>
31 const U& val_form (const U& x) { return x; }
32 template <class U>
33 const void* val_form (const U* x) { return x; }
34 template <class U>
35 const void* val_form (U* x) { return x; }
36#ifndef XAOD_STANDALONE
37 std::string val_form (const StatusCode& x) { return x.message(); }
38#endif
39
40 /*
41 * Helper class, offering method to compare for equality to the value captured during construction.
42 * In case of a difference the message of what is the value captured and what was expected
43 * is printed. In addition the assertion macro is used to make the test failing in this case.
44 *
45 * There is also a symmetric method for checking for inequality.
46 */
47 template <typename T>
49 public:
50
51 TestedValue( const T & v, std::string&& f, int l)
52 : m_value(v),
53 m_file(std::move(f)),
54 m_line(l) {}
55 void EXPECTED( const T& e ) {
56 if ( not cmp_eq(e, m_value) ) {
57 std::println (std::cerr, "{}:{}: error: Test failed, "
58 "expected: {} obtained: {}",
60 assert( cmp_eq(e, m_value) );
61 }
62 }
63 void NOT_EXPECTED( const T& e ) {
64 if ( cmp_eq(e, m_value) ) {
65 std::println (std::cerr, "{}:{}: error: Test failed, "
66 "NOT expected: {} obtained: {}",
68 assert( not cmp_eq(e, m_value) );
69 }
70 }
71 private:
73 std::string m_file;
74 int m_line;
75 };
76}
77
78#define VALUE( TESTED ) Athena_test::TestedValue<decltype(TESTED)>(TESTED, __FILE__, __LINE__).
79
80/*
81 * @brief macros (& simple class) for human readable stating assertions in unit tests
82 * The syntax will be:
83 * VALUE ( x ) EXPECTED ( true ); // exact comparisons
84 * VALUE ( y ) EXPECTED ( "something");
85 * VALUE ( z ) EXPECTED ( 3.1415 ); // this would compare with precision 1e-4 for floats and 1e-6 for doubles
86 * VALUE ( t ) NOT_EXPECTED ( 0 ); // the inverted check is also possible
87 */
88
89#endif // TestTools_expect_h
static Double_t a
#define x
TestedValue(const T &v, std::string &&f, int l)
Definition expect.h:51
void EXPECTED(const T &e)
Definition expect.h:55
std::string m_file
Definition expect.h:73
void NOT_EXPECTED(const T &e)
Definition expect.h:63
functions & macros to test the difference between floats
bool cmp_eq< float >(float a, float b)
Definition expect.h:24
const U & val_form(const U &x)
Helpers for error message formatting.
Definition expect.h:31
bool cmp_eq< double >(double a, double b)
Definition expect.h:26
bool cmp_eq(T a, T b)
Definition expect.h:22
STL namespace.