Belle II Software development
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
22using namespace Belle2::TestHelpers;
23using 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 = strdup((temp_directory_path() / "basf2_XXXXXX").c_str());
49 auto directory = mkdtemp(temporaryDirName);
50 if (!directory) {
51 B2ERROR("Cannot create temporary directory: " << strerror(errno));
52 free(temporaryDirName);
53 return;
54 }
55 path tmpdir = directory;
56 current_path(tmpdir);
57 m_tmpdir = tmpdir.string();
58 free(temporaryDirName);
59}
60
62{
63 current_path(m_oldpwd);
64 remove_all(m_tmpdir);
65}
66
67std::string TempDirCreator::getTempDir() const
68{
69 return m_tmpdir;
70}
71
72bool Belle2::TestHelpers::angleNear(double expected, double actual, double absError)
73{
74 return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
75}
76
77bool Belle2::TestHelpers::sameSign(double expected, double actual)
78{
79 if (std::isnan(expected) or std::isnan(actual)) return false;
80 using boost::math::sign;
81 int expectedSign = sign(expected);
82 int actualSign = sign(actual);
83 return expectedSign == actualSign;
84}
85
87{
88 return expected > 0;
89}
90
92{
93 return expected < 0;
94}
95
96template<>
97bool Belle2::TestHelpers::allNear<ROOT::Math::XYZVector>(const ROOT::Math::XYZVector& expected,
98 const ROOT::Math::XYZVector& actual,
99 double tolerance)
100{
101 bool xNear = std::fabs(expected.X() - actual.X()) < tolerance;
102 bool yNear = std::fabs(expected.Y() - actual.Y()) < tolerance;
103 bool zNear = std::fabs(expected.Z() - actual.Z()) < tolerance;
104 return xNear and yNear and zNear;
105}
106
107void Belle2::TestHelpers::PrintTo(const TVector3& tVector3, ::std::ostream& output)
108{
109 output
110 << "TVector3("
111 << tVector3.X() << ", "
112 << tVector3.Y() << ", "
113 << tVector3.Z() << ")";
114}
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:67
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: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< ROOT::Math::XYZVector >(const ROOT::Math::XYZVector &expected, const ROOT::Math::XYZVector &actual, double tolerance)
Predicate checking that all three components of XYZVector are close by a maximal error of tolerance.
bool sameSign(double expected, double actual)
Predicate checking that two values have the same sign.
Definition: TestHelpers.cc:77