Belle II Software  light-2309-munchkin
TestHelpers.cc
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 #include <framework/logging/Logger.h>
9 
10 #include <framework/utilities/TestHelpers.h>
11 
12 #include <framework/gearbox/Gearbox.h>
13 
14 #include <TVector3.h>
15 #include <filesystem>
16 
17 #include <boost/math/special_functions/sign.hpp>
18 
19 #include <cmath>
20 #include <vector>
21 
22 using namespace Belle2::TestHelpers;
23 using namespace std::filesystem;
24 
26 {
27  //Setup the gearbox
28  Gearbox& gearbox = Gearbox::getInstance();
29 
30  std::vector<std::string> backends;
31  backends.emplace_back("file:");
32  gearbox.setBackends(backends);
33 
34  B2INFO("Start open gearbox.");
35  gearbox.open("geometry/Belle2.xml");
36  B2INFO("Finished open gearbox.");
37 }
38 
40 {
41  Gearbox& gearbox = Gearbox::getInstance();
42  gearbox.close();
43 }
44 
46  m_oldpwd(current_path().string())
47 {
48  char temporaryDirName[] = "/tmp/basf2_XXXXXX";
49  if (mkdtemp(temporaryDirName) == nullptr) {
50  B2ERROR("Cannot create temporary directory: " << strerror(errno));
51  return;
52  }
53  path tmpdir = temporaryDirName;
54  current_path(tmpdir);
55  m_tmpdir = tmpdir.string();
56 }
57 
59 {
60  current_path(m_oldpwd);
61  remove_all(m_tmpdir);
62 }
63 
64 std::string TempDirCreator::getTempDir() const
65 {
66  return m_tmpdir;
67 }
68 
69 bool Belle2::TestHelpers::angleNear(double expected, double actual, double absError)
70 {
71  return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
72 }
73 
74 bool Belle2::TestHelpers::sameSign(double expected, double actual)
75 {
76  if (std::isnan(expected) or std::isnan(actual)) return false;
77  using boost::math::sign;
78  int expectedSign = sign(expected);
79  int actualSign = sign(actual);
80  return expectedSign == actualSign;
81 }
82 
83 bool Belle2::TestHelpers::isPositive(double expected)
84 {
85  return expected > 0;
86 }
87 
88 bool Belle2::TestHelpers::isNegative(double expected)
89 {
90  return expected < 0;
91 }
92 
93 template<>
94 bool Belle2::TestHelpers::allNear<ROOT::Math::XYZVector>(const ROOT::Math::XYZVector& expected,
95  const ROOT::Math::XYZVector& actual,
96  double tolerance)
97 {
98  bool xNear = std::fabs(expected.X() - actual.X()) < tolerance;
99  bool yNear = std::fabs(expected.Y() - actual.Y()) < tolerance;
100  bool zNear = std::fabs(expected.Z() - actual.Z()) < tolerance;
101  return xNear and yNear and zNear;
102 }
103 
104 void Belle2::TestHelpers::PrintTo(const TVector3& tVector3, ::std::ostream& output)
105 {
106  output
107  << "TVector3("
108  << tVector3.X() << ", "
109  << tVector3.Y() << ", "
110  << tVector3.Z() << ")";
111 }
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:34
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:64
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:77
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:76
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
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:81
Some utilities to help with writing unit tests.
Definition: Helix.cc:33
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:104
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:83
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:69
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:88
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:74