Belle II Software  release-06-02-00
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 <cmath>
16 
17 #include <boost/filesystem.hpp>
18 #include <boost/math/special_functions/sign.hpp>
19 
20 #include <vector>
21 
22 using namespace Belle2::TestHelpers;
23 using namespace boost::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  path tmpdir = temp_directory_path() / unique_path();
49  create_directories(tmpdir);
50  current_path(tmpdir);
51  m_tmpdir = tmpdir.string();
52 }
53 
55 {
56  current_path(m_oldpwd);
57  remove_all(m_tmpdir);
58 }
59 
60 std::string TempDirCreator::getTempDir() const
61 {
62  return m_tmpdir;
63 }
64 
65 bool Belle2::TestHelpers::angleNear(double expected, double actual, double absError)
66 {
67  return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
68 }
69 
70 bool Belle2::TestHelpers::sameSign(double expected, double actual)
71 {
72  if (std::isnan(expected) or std::isnan(actual)) return false;
73  using boost::math::sign;
74  int expectedSign = sign(expected);
75  int actualSign = sign(actual);
76  return expectedSign == actualSign;
77 }
78 
79 bool Belle2::TestHelpers::isPositive(double expected)
80 {
81  return expected > 0;
82 }
83 
84 bool Belle2::TestHelpers::isNegative(double expected)
85 {
86  return expected < 0;
87 }
88 
89 template<>
90 bool Belle2::TestHelpers::allNear<TVector3>(const TVector3& expected,
91  const TVector3& actual,
92  double tolerance)
93 {
94  bool xNear = std::fabs(expected.X() - actual.X()) < tolerance;
95  bool yNear = std::fabs(expected.Y() - actual.Y()) < tolerance;
96  bool zNear = std::fabs(expected.Z() - actual.Z()) < tolerance;
97  return xNear and yNear and zNear;
98 }
99 
100 void Belle2::TestHelpers::PrintTo(const TVector3& tVector3, ::std::ostream& output)
101 {
102  output
103  << "TVector3("
104  << tVector3.X() << ", "
105  << tVector3.Y() << ", "
106  << tVector3.Z() << ")";
107 }
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:60
std::string m_tmpdir
path of temporary director.
Definition: TestHelpers.h:75
std::string m_oldpwd
previous working directory.
Definition: TestHelpers.h:74
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:31
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:100
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:79
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:65
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:84
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:70
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:90