ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
AtlasTest
TestTools
TestTools
expect.h
Go to the documentation of this file.
1
/*
2
Copyright (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>
16
namespace
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>
48
class
TestedValue
{
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: {}"
,
59
m_file
,
m_line
,
val_form
(e),
val_form
(
m_value
));
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: {}"
,
67
m_file
,
m_line
,
val_form
(e),
val_form
(
m_value
));
68
assert( not
cmp_eq
(e,
m_value
) );
69
}
70
}
71
private
:
72
T
m_value
;
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
a
static Double_t a
Definition
LArPhysWaveHECTool.cxx:38
x
#define x
Athena_test::TestedValue::TestedValue
TestedValue(const T &v, std::string &&f, int l)
Definition
expect.h:51
Athena_test::TestedValue::EXPECTED
void EXPECTED(const T &e)
Definition
expect.h:55
Athena_test::TestedValue::m_line
int m_line
Definition
expect.h:74
Athena_test::TestedValue::m_file
std::string m_file
Definition
expect.h:73
Athena_test::TestedValue::m_value
T m_value
Definition
expect.h:72
Athena_test::TestedValue::NOT_EXPECTED
void NOT_EXPECTED(const T &e)
Definition
expect.h:63
Athena_test
functions & macros to test the difference between floats
Definition
InitGaudiGoogleTest.h:30
Athena_test::cmp_eq< float >
bool cmp_eq< float >(float a, float b)
Definition
expect.h:24
Athena_test::val_form
const U & val_form(const U &x)
Helpers for error message formatting.
Definition
expect.h:31
Athena_test::cmp_eq< double >
bool cmp_eq< double >(double a, double b)
Definition
expect.h:26
Athena_test::cmp_eq
bool cmp_eq(T a, T b)
Definition
expect.h:22
std
STL namespace.
Generated on
for ATLAS Offline Software by
1.17.0