Belle II Software light-2406-ragdoll
Belle2::TestHelpers Namespace Reference

Some utilities to help with writing unit tests. More...

Classes

class  LogMessageTest
 Test fixture to be able to check the contents and types of emitted log messages in detail. More...
 
class  TempDirCreator
 changes working directory into a newly created directory, and removes it (and contents) on destruction. More...
 
class  TestWithGearbox
 This class provides a test fixture managing the opening and closing of the Gearbox with the default geometry file. More...
 

Functions

template<>
bool allNear< Belle2::Helix > (const Belle2::Helix &expected, const Belle2::Helix &actual, double tolerance)
 Predicate checking that all five components of the Helix are close by a maximal error of absError.
 
bool angleNear (double expected, double actual, double tolerance)
 Predicate checking that two angular values are close to each other modulus a 2 * PI difference.
 
bool sameSign (double expected, double actual)
 Predicate checking that two values have the same sign.
 
bool isPositive (double expected)
 Predicate checking that a value is bigger than zero.
 
bool isNegative (double expected)
 Predicate checking that a value is smaller than zero.
 
template<class T >
bool allNear (const T &expected, const T &actual, double tolerance)
 Templated version of predicate checking if two combound object containing some floating point are near each other by maximum deviation.
 
template<>
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.
 
void PrintTo (const TVector3 &tVector3, ::std::ostream &output)
 Print function for the google test framework to print a TVector3 to an output stream.
 

Detailed Description

Some utilities to help with writing unit tests.

It is possible to nest these macros with others to test two things, e.g.:

EXPECT_B2ERROR(EXPECT_DOUBLE_EQ(5e3, Unit::convertValue(5e3, "nonexistingunit")));
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129

Function Documentation

◆ allNear()

bool allNear ( const T &  expected,
const T &  actual,
double  tolerance 
)

Templated version of predicate checking if two combound object containing some floating point are near each other by maximum deviation.

Concrete implementations can be given as simple overloads of the allNear function.

Definition at line 268 of file TestHelpers.h.

269 {
270 using std::fabs;
271 return fabs(expected - actual) < tolerance;
272 }

◆ allNear< Belle2::Helix >()

bool allNear< Belle2::Helix > ( const Belle2::Helix expected,
const Belle2::Helix actual,
double  tolerance 
)

Predicate checking that all five components of the Helix are close by a maximal error of absError.

Extends the EXPECT_ALL_NEAR/ASSERT_ALL_NEAR macro in the framework testhelpers to work for the helix class

Definition at line 40 of file Helix.cc.

43 {
44 bool d0Near = fabs(expected.getD0() - actual.getD0()) < tolerance;
45 bool phi0Near = angleNear(expected.getPhi0(), actual.getPhi0(), tolerance);
46 bool omegaNear = fabs(expected.getOmega() - actual.getOmega()) < tolerance;
47 bool z0Near = fabs(expected.getZ0() - actual.getZ0()) < tolerance;
48 bool tanLambdaNear = fabs(expected.getTanLambda() - actual.getTanLambda()) < tolerance;
49
50 return d0Near and phi0Near and omegaNear and z0Near and tanLambdaNear;
51 }
double getOmega() const
Getter for omega, which is a signed curvature measure of the track.
Definition: Helix.h:387
double getD0() const
Getter for d0, which is the signed distance to the perigee in the r-phi plane.
Definition: Helix.h:372
double getTanLambda() const
Getter for tan lambda, which is the z over two dimensional arc length slope of the track.
Definition: Helix.h:393
double getZ0() const
Getter for z0, which is the z coordinate of the perigee.
Definition: Helix.h:390
double getPhi0() const
Getter for phi0, which is the azimuth angle of the transverse momentum at the perigee.
Definition: Helix.h:378
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

◆ 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 at line 72 of file TestHelpers.cc.

73{
74 return fabs(remainder(expected - actual, 2 * M_PI)) < absError;
75}

◆ isNegative()

bool isNegative ( double  expected)

Predicate checking that a value is smaller than zero.

Definition at line 91 of file TestHelpers.cc.

92{
93 return expected < 0;
94}

◆ isPositive()

bool isPositive ( double  expected)

Predicate checking that a value is bigger than zero.

Definition at line 86 of file TestHelpers.cc.

87{
88 return expected > 0;
89}

◆ 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 at line 107 of file TestHelpers.cc.

108{
109 output
110 << "TVector3("
111 << tVector3.X() << ", "
112 << tVector3.Y() << ", "
113 << tVector3.Z() << ")";
114}

◆ sameSign()

bool sameSign ( double  expected,
double  actual 
)

Predicate checking that two values have the same sign.

Returns nan if any of the values is nan.

Definition at line 77 of file TestHelpers.cc.

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}