cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Test.h
Go to the documentation of this file.
1#ifndef CepGen_Utils_Test_h
2#define CepGen_Utils_Test_h
3
4#include <cmath>
5
8
9namespace cepgen {
10 namespace test {
11 bool debug = false;
12 double failure_tolerance = 0.;
13 const double base_precision = 1.e-3;
15 size_t num_total = 0;
16 size_t num_passed = 0;
17 } // namespace test
18} // namespace cepgen
19
20#define CG_FAILED(name) \
21 CG_LOG << cepgen::utils::colourise("FAILED ", cepgen::utils::Colour::red, cepgen::utils::Modifier::bold) << name \
22 << "!"
23#define CG_PASSED(name) CG_LOG << cepgen::utils::colourise("Passed ", cepgen::utils::Colour::green) << name << "."
24
25#define CG_TEST_DEBUG(debugging) cepgen::test::debug = debugging
26#define CG_TEST_SET_FAILURE_TOLERANCE_RATE(tolerance) cepgen::test::failure_tolerance = tolerance
27#define CG_TEST_SET_PRECISION(precis) cepgen::test::precision = precis
28#define CG_TEST_RESET_PRECISION() cepgen::test::precision = cepgen::test::base_precision
29
30#define CG_TEST(test_cond, name) \
31 { \
32 if (cepgen::test::debug) \
33 CG_LOG << cepgen::utils::colourise("TEST INFO", cepgen::utils::Colour::magenta, cepgen::utils::Modifier::bold) \
34 << " " << cepgen::utils::colourise(name, cepgen::utils::Colour::magenta) << "\n" \
35 << "\tcondition: " << cepgen::utils::boldify(#test_cond) << "."; \
36 if (!(test_cond)) \
37 CG_FAILED(name); \
38 else { \
39 CG_PASSED(name); \
40 cepgen::test::num_passed++; \
41 } \
42 cepgen::test::num_total++; \
43 }
44
45#define CG_TEST_EQUAL(var1, var2, name) \
46 { \
47 if (cepgen::test::debug) \
48 CG_LOG << cepgen::utils::colourise("TEST INFO", cepgen::utils::Colour::magenta, cepgen::utils::Modifier::bold) \
49 << " " << cepgen::utils::colourise(name, cepgen::utils::Colour::magenta) << "\n" \
50 << "\tvariable 1(" << cepgen::utils::boldify(#var1) << "): " << var1 << "\n" \
51 << "\tvariable 2(" << cepgen::utils::boldify(#var2) << "): " << var2 << "."; \
52 if ((var1) != (var2)) \
53 CG_FAILED(name) << " " << var1 << " != " << var2 << "."; \
54 else { \
55 CG_PASSED(name); \
56 cepgen::test::num_passed++; \
57 } \
58 cepgen::test::num_total++; \
59 }
60
61#define CG_TEST_EQUIV(var1, var2, name) \
62 { \
63 if (std::fabs((var1) - (var2)) > cepgen::test::precision) \
64 CG_FAILED(name) << " " << var1 << " is not within " << cepgen::test::precision << " of " << var2 << "."; \
65 else { \
66 CG_PASSED(name); \
67 cepgen::test::num_passed++; \
68 } \
69 cepgen::test::num_total++; \
70 }
71
72#define CG_TEST_UNCERT(diff, unc, nsigma, name) \
73 { \
74 if (cepgen::test::debug) \
75 CG_LOG << cepgen::utils::colourise("TEST INFO", cepgen::utils::Colour::magenta, cepgen::utils::Modifier::bold) \
76 << " " << cepgen::utils::colourise(name, cepgen::utils::Colour::magenta) << "\n" \
77 << "\tdifference: " << diff << ", sigma: " << unc << " = " << diff / unc << " * sigma " \
78 << ((diff > nsigma * unc) ? ">" : "<") << " " << nsigma << " * sigma."; \
79 if (unc > 0 && diff > nsigma * unc) \
80 CG_FAILED(name) << " difference " << diff << " is not within " << nsigma << " sigmas=" << unc << "."; \
81 else { \
82 CG_PASSED(name); \
83 cepgen::test::num_passed++; \
84 } \
85 cepgen::test::num_total++; \
86 }
87
88#define CG_TEST_VALUES(val1, val2, nsigma, name) \
89 { \
90 const auto diff = cepgen::Value(val1) - cepgen::Value(val2); \
91 if (cepgen::test::debug) \
92 CG_LOG << cepgen::utils::colourise("TEST INFO", cepgen::utils::Colour::magenta, cepgen::utils::Modifier::bold) \
93 << " " << cepgen::utils::colourise(name, cepgen::utils::Colour::magenta) << "\n" \
94 << "\tvals: " << val1 << ", " << val2 << ", difference: " << (val1 - val2) \
95 << ", sigma: " << diff.uncertainty() << " = " << diff.relativeUncertainty() << " * sigma " \
96 << ((std::fabs(1. / diff.relativeUncertainty()) > nsigma) ? ">" : "<") << " " << nsigma << " * sigma."; \
97 if (diff.uncertainty() > 0 && diff > nsigma * diff.uncertainty()) \
98 CG_FAILED(name) << " difference " << diff << " is not within " << nsigma << " sigmas."; \
99 else { \
100 CG_PASSED(name); \
101 cepgen::test::num_passed++; \
102 } \
103 cepgen::test::num_total++; \
104 }
105
106#define CG_TEST_EXCEPT(sequence, name) \
107 if (cepgen::test::debug) \
108 CG_LOG << cepgen::utils::colourise("TEST INFO", cepgen::utils::Colour::magenta, cepgen::utils::Modifier::bold) \
109 << " " << cepgen::utils::colourise(name, cepgen::utils::Colour::magenta) << "\n" \
110 << "\tsequence: " << cepgen::utils::boldify(#sequence) << "."; \
111 try { \
112 sequence(); \
113 throw cepgen::Exception("", "this_test"); \
114 } catch (const cepgen::Exception& exc) { \
115 if (exc.from() == "this_test") \
116 CG_FAILED(name); \
117 else { \
118 CG_PASSED(name) << " Resulting exception:\n" \
119 << cepgen::utils::colourise(exc.message(), \
120 cepgen::utils::Colour::none, \
121 cepgen::utils::Modifier::dimmed | cepgen::utils::Modifier::italic); \
122 cepgen::test::num_passed++; \
123 } \
124 cepgen::test::num_total++; \
125 }
126
127#define CG_TEST_SUMMARY \
128 { \
129 { \
130 auto col = cepgen::utils::Colour::yellow; \
131 if (cepgen::test::num_passed == cepgen::test::num_total) \
132 col = cepgen::utils::Colour::green; \
133 else if ((cepgen::test::failure_tolerance > 0. && \
134 cepgen::test::num_total - cepgen::test::num_passed > \
135 cepgen::test::failure_tolerance * cepgen::test::num_total) || \
136 cepgen::test::num_passed < 0.1 * cepgen::test::num_total) \
137 col = cepgen::utils::Colour::red; \
138 CG_LOG << cepgen::utils::colourise(std::to_string(cepgen::test::num_passed) + " out of " + \
139 cepgen::utils::s("test", cepgen::test::num_total, true) + " passed.", \
140 col); \
141 } \
142 return (cepgen::test::num_total - cepgen::test::num_passed <= \
143 cepgen::test::failure_tolerance * cepgen::test::num_total) \
144 ? 0 \
145 : cepgen::test::num_total - cepgen::test::num_passed; \
146 }
147
148#endif
size_t num_passed
Definition Test.h:16
bool debug
Definition Test.h:11
double failure_tolerance
Definition Test.h:12
const double base_precision
Definition Test.h:13
double precision
Definition Test.h:14
size_t num_total
Definition Test.h:15
Common namespace for this Monte Carlo generator.