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 <filesystem>
15
16#include <boost/math/special_functions/sign.hpp>
17
18#include <cmath>
19#include <vector>
20
21using namespace Belle2::TestHelpers;
22using namespace std::filesystem;
23
25{
26 //Setup the gearbox
27 Gearbox& gearbox = Gearbox::getInstance();
28
29 std::vector<std::string> backends;
30 backends.emplace_back("file:");
31 gearbox.setBackends(backends);
32
33 B2INFO("Start open gearbox.");
34 gearbox.open("geometry/Belle2.xml");
35 B2INFO("Finished open gearbox.");
36}
37
39{
40 Gearbox& gearbox = Gearbox::getInstance();
41 gearbox.close();
42}
43
45 m_oldpwd(current_path().string())
46{
47 char* temporaryDirName = strdup((temp_directory_path() / "basf2_XXXXXX").c_str());
48 auto directory = mkdtemp(temporaryDirName);
49 if (!directory) {
50 B2ERROR("Cannot create temporary directory: " << strerror(errno));
51 free(temporaryDirName);
52 return;
53 }
54 path tmpdir = directory;
55 current_path(tmpdir);
56 m_tmpdir = tmpdir.string();
57 free(temporaryDirName);
58}
59
61{
62 current_path(m_oldpwd);
63 remove_all(m_tmpdir);
64}
65
66std::string TempDirCreator::getTempDir() const
67{
68 return m_tmpdir;
69}
70
71bool Belle2::TestHelpers::angleNear(double expected, double actual, double absError)
72{
73 return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
74}
75
76bool Belle2::TestHelpers::sameSign(double expected, double actual)
77{
78 if (std::isnan(expected) or std::isnan(actual)) return false;
79 using boost::math::sign;
80 int expectedSign = sign(expected);
81 int actualSign = sign(actual);
82 return expectedSign == actualSign;
83}
84
86{
87 return expected > 0;
88}
89
91{
92 return expected < 0;
93}
94
95template<>
96bool Belle2::TestHelpers::allNear<ROOT::Math::XYZVector>(const ROOT::Math::XYZVector& expected,
97 const ROOT::Math::XYZVector& actual,
98 double tolerance)
99{
100 bool xNear = std::fabs(expected.X() - actual.X()) < tolerance;
101 bool yNear = std::fabs(expected.Y() - actual.Y()) < tolerance;
102 bool zNear = std::fabs(expected.Z() - actual.Z()) < tolerance;
103 return xNear and yNear and zNear;
104}
105
106void Belle2::TestHelpers::PrintTo(const ROOT::Math::XYZVector& vector3, ::std::ostream& output)
107{
108 output
109 << "ROOT::Math::XYZVector("
110 << vector3.X() << ", "
111 << vector3.Y() << ", "
112 << vector3.Z() << ")";
113}
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:66
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:24
static void TearDownTestCase()
Closes the Gearbox once for all test in this TestCase.
Definition: TestHelpers.cc:38
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:81
Some utilities to help with writing unit tests.
Definition: Helix.cc:34
bool isPositive(double expected)
Predicate checking that a value is bigger than zero.
Definition: TestHelpers.cc:85
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:71
bool isNegative(double expected)
Predicate checking that a value is smaller than zero.
Definition: TestHelpers.cc:90
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:76
void PrintTo(const ROOT::Math::XYZVector &vector3, ::std::ostream &output)
Print function for the google test framework to print a ROOT::Math::XYZVector to an output stream.
Definition: TestHelpers.cc:106