Belle II Software development
TestHelpers.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/logging/LogSystem.h>
11
12#include <gtest/gtest.h>
13
14#include <Math/Vector3D.h>
15
16#include <string>
17
18namespace Belle2 {
30 namespace TestHelpers {
31
44 class TestWithGearbox : public ::testing::Test {
45
46 public:
48 static void SetUpTestCase();
49
51 static void TearDownTestCase();
52 };
53
67 public:
74 std::string getTempDir() const;
75 private:
76 std::string m_oldpwd;
77 std::string m_tmpdir;
78 };
79 }
81}
82
90#define EXPECT_LOGMESSAGE(x, loglevel) \
91 do { \
92 int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
93 { \
94 x; \
95 } \
96 int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
97 EXPECT_TRUE(nmessages_after > nmessages_before) << "Message of level " << #loglevel << " expected, but not found."; \
98 } while(0)
99
105#define EXPECT_B2FATAL(x) EXPECT_EXIT(x, ::testing::ExitedWithCode(1),"")
106
111#define EXPECT_B2ERROR(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
112
117#define EXPECT_B2WARNING(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
118
126#define EXPECT_NO_LOGMESSAGE(x, loglevel) \
127 do { \
128 int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
129 { \
130 x; \
131 } \
132 int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
133 EXPECT_TRUE(nmessages_after == nmessages_before) << "Message of level " << #loglevel << " found, but non expected."; \
134 } while(0)
135
140#define EXPECT_NO_B2FATAL(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Fatal)
141
146#define EXPECT_NO_B2ERROR(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
147
152#define EXPECT_NO_B2WARNING(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
153
165#define TEST_CONTEXT(message) SCOPED_TRACE([&](){std::ostringstream messageStream; messageStream << message; return messageStream.str();}())
166
167
171#define EXPECT_ANGLE_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
172
176#define ASSERT_ANGLE_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
177
181#define EXPECT_SAME_SIGN(expected, actual) EXPECT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
182
186#define ASSERT_SAME_SIGN(expected, actual) ASSERT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
187
188
192#define EXPECT_POSITIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isPositive, expected)
193
197#define ASSERT_POSITIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isPositive, expected)
198
202#define EXPECT_NEGATIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isNegative, expected)
203
207#define ASSERT_NEGATIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isNegative, expected)
208
225#define EXPECT_ALL_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
226
243#define ASSERT_ALL_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
244
245namespace Belle2 {
250 namespace TestHelpers {
252 bool angleNear(double expected, double actual, double tolerance);
253
255 bool sameSign(double expected, double actual);
256
258 bool isPositive(double expected);
259
261 bool isNegative(double expected);
262
266 template<class T>
267 bool allNear(const T& expected, const T& actual, double tolerance)
268 {
269 using std::fabs;
270 return fabs(expected - actual) < tolerance;
271 }
272
274 template<>
275 bool allNear<ROOT::Math::XYZVector>(const ROOT::Math::XYZVector& expected, const ROOT::Math::XYZVector& actual, double tolerance);
276
278 void PrintTo(const ROOT::Math::XYZVector& vector3, ::std::ostream& output);
279 }
281}
282
283
284
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:66
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:66
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:77
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:76
This class provides a test fixture managing the opening and closing of the Gearbox with the default g...
Definition: TestHelpers.h:44
static void SetUpTestCase()
Sets up the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:24
static void TearDownTestCase()
Closes the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:38
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:85
bool allNear(const T &expected, const T &actual, double tolerance)
Templated version of predicate checking if two combound object containing some floating point are nea...
Definition: TestHelpers.h:267
bool angleNear(double expected, double actual, double tolerance)
Predicate checking that two angular values are close to each other modulus a 2 * PI difference.
Definition: TestHelpers.cc:71
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:90
bool allNear< ROOT::Math::XYZVector >(const ROOT::Math::XYZVector &expected, const ROOT::Math::XYZVector &actual, double tolerance)
Predicate checking that all three components of XYZVector are close by a maximal error of tolerance.
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:76
void PrintTo(const ROOT::Math::XYZVector &vector3, ::std::ostream &output)
Print function for the google test framework to print a ROOT::Math::XYZVector to an output stream.
Definition: TestHelpers.cc:106
Abstract base class for different kinds of events.