Belle II Software development
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

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 ROOT::Math::XYZVector &vector3, ::std::ostream &output)
 Print function for the google test framework to print a ROOT::Math::XYZVector to an output stream.
 
template<>
bool allNear< TrackFindingCDC::Vector3D > (const TrackFindingCDC::Vector3D &expected, const TrackFindingCDC::Vector3D &actual, double tolerance)
 Predicate checking that all three components of Vector3D are close by a maximal error of tolerance.
 

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:128

Function Documentation

◆ allNear()

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.

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

Definition at line 267 of file TestHelpers.h.

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

◆ 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 71 of file TestHelpers.cc.

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

◆ isNegative()

bool isNegative ( double expected)

Predicate checking that a value is smaller than zero.

Definition at line 90 of file TestHelpers.cc.

91{
92 return expected < 0;
93}

◆ isPositive()

bool isPositive ( double expected)

Predicate checking that a value is bigger than zero.

Definition at line 85 of file TestHelpers.cc.

86{
87 return expected > 0;
88}

◆ PrintTo()

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

107{
108 output
109 << "ROOT::Math::XYZVector("
110 << vector3.X() << ", "
111 << vector3.Y() << ", "
112 << vector3.Z() << ")";
113}

◆ 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 76 of file TestHelpers.cc.

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}