Belle II Software  release-05-02-19
TestHelpers.h
1 #pragma once
2 
3 #include <framework/logging/LogSystem.h>
4 
5 #include <gtest/gtest.h>
6 
7 #include <string>
8 
9 namespace Belle2 {
21  namespace TestHelpers {
22 
35  class TestWithGearbox : public ::testing::Test {
36 
37  public:
39  static void SetUpTestCase();
40 
42  static void TearDownTestCase();
43  };
44 
58  public:
59  TempDirCreator();
60  ~TempDirCreator();
65  std::string getTempDir() const;
66  private:
67  std::string m_oldpwd;
68  std::string m_tmpdir;
69  };
70  }
72 }
73 
81 #define EXPECT_LOGMESSAGE(x, loglevel) \
82  do { \
83  int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
84  { \
85  x; \
86  } \
87  int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
88  EXPECT_TRUE(nmessages_after > nmessages_before) << "Message of level " << #loglevel << " expected, but not found."; \
89  } while(0)
90 
96 #define EXPECT_B2FATAL(x) EXPECT_EXIT(x, ::testing::ExitedWithCode(1),"")
97 
102 #define EXPECT_B2ERROR(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
103 
108 #define EXPECT_B2WARNING(x) EXPECT_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
109 
117 #define EXPECT_NO_LOGMESSAGE(x, loglevel) \
118  do { \
119  int nmessages_before = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
120  { \
121  x; \
122  } \
123  int nmessages_after = Belle2::LogSystem::Instance().getMessageCounter(loglevel); \
124  EXPECT_TRUE(nmessages_after == nmessages_before) << "Message of level " << #loglevel << " found, but non expected."; \
125  } while(0)
126 
131 #define EXPECT_NO_B2FATAL(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Fatal)
132 
137 #define EXPECT_NO_B2ERROR(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Error)
138 
143 #define EXPECT_NO_B2WARNING(x) EXPECT_NO_LOGMESSAGE(x, Belle2::LogConfig::c_Warning)
144 
156 #define TEST_CONTEXT(message) SCOPED_TRACE([&](){std::ostringstream messageStream; messageStream << message; return messageStream.str();}())
157 
158 
162 #define EXPECT_ANGLE_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
163 
167 #define ASSERT_ANGLE_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::angleNear, expected, actual, tolerance)
168 
172 #define EXPECT_SAME_SIGN(expected, actual) EXPECT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
173 
177 #define ASSERT_SAME_SIGN(expected, actual) ASSERT_PRED2(::Belle2::TestHelpers::sameSign, expected, actual)
178 
179 
183 #define EXPECT_POSITIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isPositive, expected)
184 
188 #define ASSERT_POSITIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isPositive, expected)
189 
193 #define EXPECT_NEGATIVE(expected) EXPECT_PRED1(::Belle2::TestHelpers::isNegative, expected)
194 
198 #define ASSERT_NEGATIVE(expected) ASSERT_PRED1(::Belle2::TestHelpers::isNegative, expected)
199 
216 #define EXPECT_ALL_NEAR(expected, actual, tolerance) EXPECT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
217 
234 #define ASSERT_ALL_NEAR(expected, actual, tolerance) ASSERT_PRED3(::Belle2::TestHelpers::allNear<decltype(expected)>, expected, actual, tolerance)
235 
236 class TVector3;
237 namespace Belle2 {
242  namespace TestHelpers {
244  bool angleNear(double expected, double actual, double tolerance);
245 
247  bool sameSign(double expected, double actual);
248 
250  bool isPositive(double expected);
251 
253  bool isNegative(double expected);
254 
258  template<class T>
259  bool allNear(const T& expected, const T& actual, double tolerance)
260  {
261  using std::fabs;
262  return fabs(expected - actual) < tolerance;
263  }
264 
266  template<>
267  bool allNear<TVector3>(const TVector3& expected, const TVector3& actual, double tolerance);
268 
270  void PrintTo(const TVector3& tVector3, ::std::ostream& output);
271  }
273 }
274 
275 
276 
Belle2::TestHelpers::TempDirCreator::TempDirCreator
TempDirCreator()
ctor.
Definition: TestHelpers.cc:38
Belle2::TestHelpers::isNegative
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:77
Belle2::TestHelpers::allNear< TVector3 >
bool allNear< TVector3 >(const TVector3 &expected, const TVector3 &actual, double tolerance)
Predicate checking that all three components of TVector3 are close by a maximal error of tolerance.
Definition: TestHelpers.cc:83
Belle2::TestHelpers::TestWithGearbox::TearDownTestCase
static void TearDownTestCase()
Closes the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:32
Belle2::TestHelpers::angleNear
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:58
Belle2::TestHelpers::TempDirCreator
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:57
Belle2::TestHelpers::TempDirCreator::m_tmpdir
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:68
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TestHelpers::TempDirCreator::m_oldpwd
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:67
Belle2::TestHelpers::TestWithGearbox::SetUpTestCase
static void SetUpTestCase()
Sets up the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:18
Belle2::TestHelpers::allNear
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:259
Belle2::TestHelpers::isPositive
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:72
Belle2::TestHelpers::TestWithGearbox
This class provides a test fixture managing the opening and closing of the Gearbox with the default g...
Definition: TestHelpers.h:35
Belle2::TestHelpers::sameSign
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:63
Belle2::TestHelpers::TempDirCreator::~TempDirCreator
~TempDirCreator()
dtor.
Definition: TestHelpers.cc:47
Belle2::TestHelpers::TempDirCreator::getTempDir
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:53
Belle2::TestHelpers::PrintTo
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:93