Belle II Software  release-08-01-10
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 
18 namespace 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:
68  TempDirCreator();
69  ~TempDirCreator();
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 
245 class TVector3;
246 namespace Belle2 {
251  namespace TestHelpers {
253  bool angleNear(double expected, double actual, double tolerance);
254 
256  bool sameSign(double expected, double actual);
257 
259  bool isPositive(double expected);
260 
262  bool isNegative(double expected);
263 
267  template<class T>
268  bool allNear(const T& expected, const T& actual, double tolerance)
269  {
270  using std::fabs;
271  return fabs(expected - actual) < tolerance;
272  }
273 
275  template<>
276  bool allNear<ROOT::Math::XYZVector>(const ROOT::Math::XYZVector& expected, const ROOT::Math::XYZVector& actual, double tolerance);
277 
279  void PrintTo(const TVector3& tVector3, ::std::ostream& output);
280  }
282 }
283 
284 
285 
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:67
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:25
static void TearDownTestCase()
Closes the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:39
void PrintTo(const TVector3 &tVector3, ::std::ostream &output)
Print function for the google test framework to print a TVector3 to an output stream.
Definition: TestHelpers.cc:107
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:86
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:72
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:91
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:268
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:77
Abstract base class for different kinds of events.