Version: SMASH-1.5
Unit Test Macros and Functions

Classes

struct  smash::Test::Position
 A FourVector that is marked as a position vector. More...
 
struct  smash::Test::Momentum
 A FourVector that is marked as a momentum vector. More...
 

Macros

#define TEST(function_name)
 Defines a test function. More...
 
#define TEST_CATCH(function_name, ExceptionType)
 Same as above, but expects the code to throw an exception of type ExceptionType. More...
 
#define TEST_BEGIN(T, function_name, typelist)
 Tests that should be tested with several types as template parameter can use this macro. More...
 
#define TEST_END
 Test functions created with TEST_BEGIN need to end with TEST_END. More...
 
#define VERIFY(condition)
 Verifies that condition is true. More...
 
#define COMPARE(test_value, reference)
 Verifies that test_value is equal to reference. More...
 
#define COMPARE_ABSOLUTE_ERROR(test_value, reference, allowed_difference)
 Verifies that the difference between test_value and reference is smaller than allowed_difference. More...
 
#define COMPARE_RELATIVE_ERROR(test_value, reference, allowed_relative_difference)
 Verifies that the difference between test_value and reference is smaller than allowed_relative_difference * reference. More...
 
#define FUZZY_COMPARE(test_value, reference)
 Verifies that test_value is equal to reference within a pre-defined distance in units of least precision (ulp). More...
 
#define FAIL()
 Call this to fail a test. More...
 
#define EXPECT_ASSERT_FAILURE(code)
 Wrap code that should fail an assertion with this macro. More...
 

Typedefs

using smash::Test::ParticlesPtr = std::unique_ptr< Particles >
 A type alias for a unique_ptr of Particles. More...
 

Functions

static void EXPECT_FAILURE ()
 Use this to mark that the failure of a following test is expected. More...
 
void smash::Test::create_actual_particletypes ()
 Creates the ParticleType list containing the actual particles that SMASH uses. More...
 
void smash::Test::create_actual_decaymodes ()
 Creates the DecayModes list containing the actual decay modes that SMASH uses. More...
 
void smash::Test::create_smashon_particletypes ()
 Creates a ParticleType list containing only the smashon test particle. More...
 
ParticleData smash::Test::smashon (int id=-1)
 Create a particle with 0 position and momentum vectors and optionally a given id. More...
 
ParticleData smash::Test::smashon (const Position &position, int id=-1)
 Create a particle with 0 momentum vector, the given position, and optionally a given id. More...
 
ParticleData smash::Test::smashon (const Momentum &momentum, int id=-1)
 Create a particle with 0 position vector, the given momentum, and optionally a given id. More...
 
ParticleData smash::Test::smashon (const Position &position, const Momentum &momentum, int id=-1)
 Create a particle with the given position and momentum vectors, and optionally a given id. More...
 
ParticleData smash::Test::smashon (const Momentum &momentum, const Position &position, int id=-1)
 Create a particle with the given position and momentum vectors, and optionally a given id. More...
 
ParticleData smash::Test::smashon_random (int id=-1)
 Create a particle with random position and momentum vectors and optionally a given id. More...
 
Configuration smash::Test::configuration (std::string overrides={})
 Return a configuration object filled with data from src/config.yaml. More...
 
std::unique_ptr< ExperimentBasesmash::Test::experiment (const Configuration &c=configuration())
 Create an experiment. More...
 
std::unique_ptr< ExperimentBasesmash::Test::experiment (const char *configOverrides)
 Creates an experiment using the default config and the specified configOverrides. More...
 
template<typename G >
ParticleList smash::Test::create_particle_list (std::size_t n, G &&generator)
 Generate a list of particles from the given generator function. More...
 
template<typename G >
ParticlesPtr smash::Test::create_particles (int n, G &&generator)
 Creates a Particles object and fills it with n particles generated by the generator function. More...
 
ParticlesPtr smash::Test::create_particles (const std::initializer_list< ParticleData > &init)
 Creates a Particles object and fills it with the particles passed as initializer_list to this function. More...
 
ReactionsBitSet smash::Test::all_reactions_included ()
 returns BitSet of 2->2 reactions, where everything is on More...
 
ExperimentParameters smash::Test::default_parameters (int testparticles=1, double dt=0.1)
 Creates a standard ExperimentParameters object which works for almost all testing purposes. More...
 

Variables

static constexpr double smash::Test::smashon_mass = 0.123
 The mass of the smashon particle. More...
 
static constexpr double smash::Test::smashon_width = 1.2
 The decay width of the smashon particle. More...
 
static constexpr const char smash::Test::smashon_pdg_string [] = "661"
 The PDG code of the smashon particle. More...
 

Macro Definition Documentation

◆ TEST

#define TEST (   function_name)

Defines a test function.

Consider this to expand to void function_name(). The function_name will also be the name that appears in the output after PASS/FAIL.

Definition at line 282 of file unittest.h.

◆ TEST_CATCH

#define TEST_CATCH (   function_name,
  ExceptionType 
)

Same as above, but expects the code to throw an exception of type ExceptionType.

If the code does not throw (or throws a different exception), the test is considered failed.

Definition at line 291 of file unittest.h.

◆ TEST_BEGIN

#define TEST_BEGIN (   T,
  function_name,
  typelist 
)

Tests that should be tested with several types as template parameter can use this macro.

Your test function then has this signature: template <typename T> void function_name().

Definition at line 300 of file unittest.h.

◆ TEST_END

#define TEST_END

Test functions created with TEST_BEGIN need to end with TEST_END.

Definition at line 305 of file unittest.h.

◆ VERIFY

#define VERIFY (   condition)

Verifies that condition is true.

Definition at line 310 of file unittest.h.

◆ COMPARE

#define COMPARE (   test_value,
  reference 
)

Verifies that test_value is equal to reference.

Definition at line 315 of file unittest.h.

◆ COMPARE_ABSOLUTE_ERROR

#define COMPARE_ABSOLUTE_ERROR (   test_value,
  reference,
  allowed_difference 
)

Verifies that the difference between test_value and reference is smaller than allowed_difference.

If the test fails the output will show the actual difference between test_value and reference. If this value is positive test_value is too large. If it is negative test_value is too small.

Definition at line 325 of file unittest.h.

◆ COMPARE_RELATIVE_ERROR

#define COMPARE_RELATIVE_ERROR (   test_value,
  reference,
  allowed_relative_difference 
)

Verifies that the difference between test_value and reference is smaller than allowed_relative_difference * reference.

If the test fails the output will show the actual difference between test_value and reference. If this value is positive test_value is too large. If it is negative test_value is too small.

The following example tests that a is no more than 1% different from b:

Note
This test macro still works even if reference is set to 0. It will then compare the difference against allowed_relative_difference * <smallest positive normalized value of reference type>.

Definition at line 344 of file unittest.h.

◆ FUZZY_COMPARE

#define FUZZY_COMPARE (   test_value,
  reference 
)

Verifies that test_value is equal to reference within a pre-defined distance in units of least precision (ulp).

If the test fails it will print the distance in ulp between test_value and reference as well as the maximum allowed distance. Often this difference is not visible in the value because the conversion of a double/float to a string needs to round the value to a sensible length.

The allowed distance can be modified by calling:

UnitTest::setFuzzyness<float>(4);
UnitTest::setFuzzyness<double>(7);

ulp

Unit of least precision is a unit that is derived from the the least significant bit in the mantissa of a floating-point value. Consider a single-precision number (23 mantissa bits) with exponent \(e\). Then 1 ulp is \(2^{e-23}\). Thus, \(\log_2(u)\) signifies the the number incorrect mantissa bits (with \(u\) the distance in ulp).

If test_value and reference have a different exponent the meaning of ulp depends on the variable you look at. The FUZZY_COMPARE code always uses reference to determine the magnitude of 1 ulp.

Example: The value 1. is 0x3f800000 in binary. The value 1.00000011920928955078125 with binary representation 0x3f800001 therefore has a distance of 1 ulp. A positive distance means the test_value is larger than the reference. A negative distance means the test_value is smaller than the reference.

  • FUZZY_COMPARE(1.00000011920928955078125, 1.) will show a distance of 1
  • FUZZY_COMPARE(1., 1.00000011920928955078125) will show a distance of -1

The value 0.999999940395355224609375 with binary representation 0x3f7fffff has a smaller exponent than 1.:

  • FUZZY_COMPARE(0.999999940395355224609375, 1.) will show a distance of -0.5
  • FUZZY_COMPARE(1., 0.999999940395355224609375) will show a distance of 1

Comparing to 0

Distance to 0 is implemented as comparing to std::numeric_limits<T>::min() instead and adding 1 to the resulting distance.

Definition at line 393 of file unittest.h.

◆ FAIL

#define FAIL ( )

Call this to fail a test.

Definition at line 398 of file unittest.h.

◆ EXPECT_ASSERT_FAILURE

#define EXPECT_ASSERT_FAILURE (   code)

Wrap code that should fail an assertion with this macro.

Definition at line 403 of file unittest.h.

Typedef Documentation

◆ ParticlesPtr

using smash::Test::ParticlesPtr = typedef std::unique_ptr<Particles>

A type alias for a unique_ptr of Particles.

Definition at line 202 of file setup.h.

Function Documentation

◆ EXPECT_FAILURE()

static void EXPECT_FAILURE ( )
inlinestatic

Use this to mark that the failure of a following test is expected.

TEST(something) {
// this needs to pass
VERIFY(true);
// if this fails, the test will be marked "XFAIL" and the failure
// will not be counted
VERIFY(false);
// this will not be checked anymore, because the TEST stops at the
// (expectedly) failing VERIFY.
VERIFY(true);
}

Definition at line 524 of file unittest.h.

524  { // {{{1
525  global_unit_test_object_.expect_failure = true;
526 }

◆ create_actual_particletypes()

void smash::Test::create_actual_particletypes ( )
inline

Creates the ParticleType list containing the actual particles that SMASH uses.

Definition at line 35 of file setup.h.

35  {
36 #ifndef DOXYGEN
37 #include <particles.txt.h>
39 #endif
40  ParticleType::create_type_list(data);
41 }
Here is the call graph for this function:

◆ create_actual_decaymodes()

void smash::Test::create_actual_decaymodes ( )
inline

Creates the DecayModes list containing the actual decay modes that SMASH uses.

Definition at line 47 of file setup.h.

47  {
48 #ifndef DOXYGEN
49 #include <decaymodes.txt.h>
51 #endif
52  DecayModes::load_decaymodes(data);
53 }
Here is the call graph for this function:

◆ create_smashon_particletypes()

void smash::Test::create_smashon_particletypes ( )
inline

Creates a ParticleType list containing only the smashon test particle.

Definition at line 65 of file setup.h.

65  {
66  ParticleType::create_type_list(
67  "# NAME MASS[GEV] WIDTH[GEV] PARITY PDG\n"
68  "σ " +
69  std::to_string(smashon_mass) + " " + std::to_string(smashon_width) +
70  " + 661\n");
71 }
static constexpr double smashon_width
The decay width of the smashon particle.
Definition: setup.h:58
static constexpr double smashon_mass
The mass of the smashon particle.
Definition: setup.h:56
Here is the call graph for this function:

◆ smashon() [1/5]

ParticleData smash::Test::smashon ( int  id = -1)
inline

Create a particle with 0 position and momentum vectors and optionally a given id.

Definition at line 86 of file setup.h.

86  {
87  ParticleData p{ParticleType::find(0x661), id};
88  return p;
89 }
constexpr int p
Proton.
Here is the call graph for this function:

◆ smashon() [2/5]

ParticleData smash::Test::smashon ( const Position position,
int  id = -1 
)
inline

Create a particle with 0 momentum vector, the given position, and optionally a given id.

Definition at line 94 of file setup.h.

94  {
95  ParticleData p{ParticleType::find(0x661), id};
96  p.set_4position(position);
97  return p;
98 }
constexpr int p
Proton.
Here is the call graph for this function:

◆ smashon() [3/5]

ParticleData smash::Test::smashon ( const Momentum momentum,
int  id = -1 
)
inline

Create a particle with 0 position vector, the given momentum, and optionally a given id.

Definition at line 103 of file setup.h.

103  {
104  ParticleData p{ParticleType::find(0x661), id};
105  p.set_4momentum(momentum);
106  return p;
107 }
constexpr int p
Proton.
Here is the call graph for this function:

◆ smashon() [4/5]

ParticleData smash::Test::smashon ( const Position position,
const Momentum momentum,
int  id = -1 
)
inline

Create a particle with the given position and momentum vectors, and optionally a given id.

Definition at line 112 of file setup.h.

113  {
114  ParticleData p{ParticleType::find(0x661), id};
115  p.set_4position(position);
116  p.set_4momentum(momentum);
117  return p;
118 }
constexpr int p
Proton.
Here is the call graph for this function:

◆ smashon() [5/5]

ParticleData smash::Test::smashon ( const Momentum momentum,
const Position position,
int  id = -1 
)
inline

Create a particle with the given position and momentum vectors, and optionally a given id.

Convenience overload of the above to allow arbitrary order of momentum and position.

Definition at line 126 of file setup.h.

127  {
128  ParticleData p{ParticleType::find(0x661), id};
129  p.set_4position(position);
130  p.set_4momentum(momentum);
131  return p;
132 }
constexpr int p
Proton.
Here is the call graph for this function:

◆ smashon_random()

ParticleData smash::Test::smashon_random ( int  id = -1)
inline

Create a particle with random position and momentum vectors and optionally a given id.

Definition at line 137 of file setup.h.

137  {
138  auto random_value = random::make_uniform_distribution(-15.0, +15.0);
139  ParticleData p{ParticleType::find(0x661), id};
140  p.set_4position(
141  {random_value(), random_value(), random_value(), random_value()});
142  p.set_4momentum(smashon_mass,
143  {random_value(), random_value(), random_value()});
144  return p;
145 }
static constexpr double smashon_mass
The mass of the smashon particle.
Definition: setup.h:56
uniform_dist< T > make_uniform_distribution(T min, T max)
Definition: random.h:132
constexpr int p
Proton.
Here is the call graph for this function:

◆ configuration()

Configuration smash::Test::configuration ( std::string  overrides = {})
inline

Return a configuration object filled with data from src/config.yaml.

Note that a change to that file may affect test results if you use it.

If you want specific values in the config for testing simply overwrite the relevant settings e.g. with:

auto config = Test::configuration(
"General:\n"
" Modus: Box\n"
" Testparticles: 100\n"
);

Definition at line 161 of file setup.h.

161  {}) {
162  Configuration c{bf::path{TEST_CONFIG_PATH} / "input"};
163  if (!overrides.empty()) {
164  c.merge_yaml(overrides);
165  }
166  return c;
167 }
Here is the caller graph for this function:

◆ experiment() [1/2]

std::unique_ptr<ExperimentBase> smash::Test::experiment ( const Configuration c = configuration())
inline

Create an experiment.

If you want a specific configuration you can pass it as parameter, otherwise it will use the result from configuration above.

Definition at line 175 of file setup.h.

176  {
177  return ExperimentBase::create(c, ".");
178 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ experiment() [2/2]

std::unique_ptr<ExperimentBase> smash::Test::experiment ( const char *  configOverrides)
inline

Creates an experiment using the default config and the specified configOverrides.

Definition at line 184 of file setup.h.

184  {
185  return ExperimentBase::create(configuration(configOverrides), ".");
186 }
Configuration configuration(std::string overrides={})
Return a configuration object filled with data from src/config.yaml.
Definition: setup.h:161
Here is the call graph for this function:

◆ create_particle_list()

template<typename G >
ParticleList smash::Test::create_particle_list ( std::size_t  n,
G &&  generator 
)
inline

Generate a list of particles from the given generator function.

Definition at line 192 of file setup.h.

192  {
193  ParticleList list;
194  list.reserve(n);
195  for (auto i = n; i; --i) {
196  list.emplace_back(generator());
197  }
198  return list;
199 }
constexpr int n
Neutron.

◆ create_particles() [1/2]

template<typename G >
ParticlesPtr smash::Test::create_particles ( int  n,
G &&  generator 
)
inline

Creates a Particles object and fills it with n particles generated by the generator function.

Definition at line 209 of file setup.h.

209  {
210  ParticlesPtr p = make_unique<Particles>();
211  for (auto i = n; i; --i) {
212  p->insert(generator());
213  }
214  return p;
215 }
std::unique_ptr< Particles > ParticlesPtr
A type alias for a unique_ptr of Particles.
Definition: setup.h:202
constexpr int p
Proton.
constexpr int n
Neutron.

◆ create_particles() [2/2]

ParticlesPtr smash::Test::create_particles ( const std::initializer_list< ParticleData > &  init)
inline

Creates a Particles object and fills it with the particles passed as initializer_list to this function.

Definition at line 221 of file setup.h.

222  {
223  ParticlesPtr p = make_unique<Particles>();
224  for (const auto &data : init) {
225  p->insert(data);
226  }
227  return p;
228 }
std::unique_ptr< Particles > ParticlesPtr
A type alias for a unique_ptr of Particles.
Definition: setup.h:202
constexpr int p
Proton.

◆ all_reactions_included()

ReactionsBitSet smash::Test::all_reactions_included ( )
inline

returns BitSet of 2->2 reactions, where everything is on

Definition at line 231 of file setup.h.

231  {
232  return ReactionsBitSet().set();
233 }
std::bitset< 6 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
Here is the caller graph for this function:

◆ default_parameters()

ExperimentParameters smash::Test::default_parameters ( int  testparticles = 1,
double  dt = 0.1 
)
inline

Creates a standard ExperimentParameters object which works for almost all testing purposes.

If needed you can set the testparticles parameter to a different value than 1.

Definition at line 242 of file setup.h.

243  {
244  return ExperimentParameters{{0., dt},
245  {0., 1.},
246  testparticles,
247  1.0,
248  4.0,
249  true,
251  false,
252  false,
253  false,
255  false,
256  0.,
257  false};
258 }
ReactionsBitSet all_reactions_included()
returns BitSet of 2->2 reactions, where everything is on
Definition: setup.h:231
Here is the call graph for this function:

Variable Documentation

◆ smashon_mass

constexpr double smash::Test::smashon_mass = 0.123
static

The mass of the smashon particle.

Definition at line 56 of file setup.h.

◆ smashon_width

constexpr double smash::Test::smashon_width = 1.2
static

The decay width of the smashon particle.

Definition at line 58 of file setup.h.

◆ smashon_pdg_string

constexpr const char smash::Test::smashon_pdg_string[] = "661"
static

The PDG code of the smashon particle.

Definition at line 60 of file setup.h.