Belle II Software  release-05-02-19
TestHelpers.cc
1 #include <framework/logging/Logger.h>
2 
3 #include <framework/utilities/TestHelpers.h>
4 
5 #include <framework/gearbox/Gearbox.h>
6 
7 #include <TVector3.h>
8 #include <cmath>
9 
10 #include <boost/filesystem.hpp>
11 #include <boost/math/special_functions/sign.hpp>
12 
13 #include <vector>
14 
15 using namespace Belle2::TestHelpers;
16 using namespace boost::filesystem;
17 
19 {
20  //Setup the gearbox
21  Gearbox& gearbox = Gearbox::getInstance();
22 
23  std::vector<std::string> backends;
24  backends.emplace_back("file:");
25  gearbox.setBackends(backends);
26 
27  B2INFO("Start open gearbox.");
28  gearbox.open("geometry/Belle2.xml");
29  B2INFO("Finished open gearbox.");
30 }
31 
33 {
34  Gearbox& gearbox = Gearbox::getInstance();
35  gearbox.close();
36 }
37 
39  m_oldpwd(current_path().string())
40 {
41  path tmpdir = temp_directory_path() / unique_path();
42  create_directories(tmpdir);
43  current_path(tmpdir);
44  m_tmpdir = tmpdir.string();
45 }
46 
48 {
49  current_path(m_oldpwd);
50  remove_all(m_tmpdir);
51 }
52 
53 std::string TempDirCreator::getTempDir() const
54 {
55  return m_tmpdir;
56 }
57 
58 bool Belle2::TestHelpers::angleNear(double expected, double actual, double absError)
59 {
60  return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
61 }
62 
63 bool Belle2::TestHelpers::sameSign(double expected, double actual)
64 {
65  if (std::isnan(expected) or std::isnan(actual)) return false;
66  using boost::math::sign;
67  int expectedSign = sign(expected);
68  int actualSign = sign(actual);
69  return expectedSign == actualSign;
70 }
71 
72 bool Belle2::TestHelpers::isPositive(double expected)
73 {
74  return expected > 0;
75 }
76 
77 bool Belle2::TestHelpers::isNegative(double expected)
78 {
79  return expected < 0;
80 }
81 
82 template<>
83 bool Belle2::TestHelpers::allNear<TVector3>(const TVector3& expected,
84  const TVector3& actual,
85  double tolerance)
86 {
87  bool xNear = std::fabs(expected.X() - actual.X()) < tolerance;
88  bool yNear = std::fabs(expected.Y() - actual.Y()) < tolerance;
89  bool zNear = std::fabs(expected.Z() - actual.Z()) < tolerance;
90  return xNear and yNear and zNear;
91 }
92 
93 void Belle2::TestHelpers::PrintTo(const TVector3& tVector3, ::std::ostream& output)
94 {
95  output
96  << "TVector3("
97  << tVector3.X() << ", "
98  << tVector3.Y() << ", "
99  << tVector3.Z() << ")";
100 }
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::Gearbox::getInstance
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:74
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::m_tmpdir
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:68
Belle2::TestHelpers
Some utilities to help with writing unit tests.
Definition: Helix.cc:24
Belle2::Gearbox
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:44
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::isPositive
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:72
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