Version: SMASH-2.0
Unit Test Macros and Functions

Namespaces

 vir
 

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_TYPES(T, test_name, typelist)
 Define a test function template, with type parameter T, which is specialized for all types in the typelist. 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...
 

Typedefs

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

Functions

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...
 
MultiParticleReactionsBitSet smash::Test::no_multiparticle_reactions ()
 returns BitSet for multi-particle reactions, where everything is off More...
 
ExperimentParameters smash::Test::default_parameters (int testparticles=1, double dt=0.1, CollisionCriterion crit=CollisionCriterion::Geometric)
 Creates a standard ExperimentParameters object which works for almost all testing purposes. More...
 
EventInfo smash::Test::default_event_info (double impact_parameter=0.0, bool empty_event=false)
 Creates default EventInfo object for 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 229 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 238 of file unittest.h.

◆ TEST_TYPES

#define TEST_TYPES (   T,
  test_name,
  typelist 
)

Define a test function template, with type parameter T, which is specialized for all types in the typelist.

Definition at line 244 of file unittest.h.

◆ VERIFY

#define VERIFY (   condition)

Verifies that condition is true.

Definition at line 249 of file unittest.h.

◆ COMPARE

#define COMPARE (   test_value,
  reference 
)

Verifies that test_value is equal to reference.

Definition at line 254 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 264 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 283 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:

vir::test::setFuzzyness<float>(4);
vir::test::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 332 of file unittest.h.

◆ FAIL

#define FAIL ( )

Call this to fail a test.

Definition at line 337 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 203 of file setup.h.

Function Documentation

◆ create_actual_particletypes()

void smash::Test::create_actual_particletypes ( )
inline

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

Definition at line 36 of file setup.h.

36  {
37 #ifndef DOXYGEN
38 #include <particles.txt.h>
40 #endif
41  ParticleType::create_type_list(data);
42 }
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 48 of file setup.h.

48  {
49 #ifndef DOXYGEN
50 #include <decaymodes.txt.h>
52 #endif
53  DecayModes::load_decaymodes(data);
54 }
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 66 of file setup.h.

66  {
67  ParticleType::create_type_list(
68  "# NAME MASS[GEV] WIDTH[GEV] PARITY PDG\n"
69  "σ " +
70  std::to_string(smashon_mass) + " " + std::to_string(smashon_width) +
71  " + 661\n");
72 }
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 87 of file setup.h.

87  {
88  ParticleData p{ParticleType::find(0x661), id};
89  return p;
90 }
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 95 of file setup.h.

95  {
96  ParticleData p{ParticleType::find(0x661), id};
97  p.set_4position(position);
98  return p;
99 }
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 104 of file setup.h.

104  {
105  ParticleData p{ParticleType::find(0x661), id};
106  p.set_4momentum(momentum);
107  return p;
108 }
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 113 of file setup.h.

114  {
115  ParticleData p{ParticleType::find(0x661), id};
116  p.set_4position(position);
117  p.set_4momentum(momentum);
118  return p;
119 }
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 127 of file setup.h.

128  {
129  ParticleData p{ParticleType::find(0x661), id};
130  p.set_4position(position);
131  p.set_4momentum(momentum);
132  return p;
133 }
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 138 of file setup.h.

138  {
139  auto random_value = random::make_uniform_distribution(-15.0, +15.0);
140  ParticleData p{ParticleType::find(0x661), id};
141  p.set_4position(
142  {random_value(), random_value(), random_value(), random_value()});
143  p.set_4momentum(smashon_mass,
144  {random_value(), random_value(), random_value()});
145  return p;
146 }
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 162 of file setup.h.

162  {}) {
163  Configuration c{bf::path{TEST_CONFIG_PATH} / "input"};
164  if (!overrides.empty()) {
165  c.merge_yaml(overrides);
166  }
167  return c;
168 }
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 176 of file setup.h.

177  {
178  return ExperimentBase::create(c, ".");
179 }
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 185 of file setup.h.

185  {
186  return ExperimentBase::create(configuration(configOverrides), ".");
187 }
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 193 of file setup.h.

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

◆ 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 210 of file setup.h.

210  {
211  ParticlesPtr p = make_unique<Particles>();
212  for (auto i = n; i; --i) {
213  p->insert(generator());
214  }
215  return p;
216 }

◆ 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 222 of file setup.h.

223  {
224  ParticlesPtr p = make_unique<Particles>();
225  for (const auto &data : init) {
226  p->insert(data);
227  }
228  return p;
229 }

◆ all_reactions_included()

ReactionsBitSet smash::Test::all_reactions_included ( )
inline

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

Definition at line 232 of file setup.h.

232  {
233  return ReactionsBitSet().set();
234 }
Here is the caller graph for this function:

◆ no_multiparticle_reactions()

MultiParticleReactionsBitSet smash::Test::no_multiparticle_reactions ( )
inline

returns BitSet for multi-particle reactions, where everything is off

Definition at line 237 of file setup.h.

237  {
238  return MultiParticleReactionsBitSet().reset();
239 }
Here is the caller graph for this function:

◆ default_parameters()

ExperimentParameters smash::Test::default_parameters ( int  testparticles = 1,
double  dt = 0.1,
CollisionCriterion  crit = CollisionCriterion::Geometric 
)
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 248 of file setup.h.

250  {
251  return ExperimentParameters{
252  make_unique<UniformClock>(0., dt), // labclock
253  make_unique<UniformClock>(0., 1.), // outputclock
254  testparticles, // testparticles
255  1.0, // Gaussian smearing width
256  4.0, // Gaussian smearing cut-off
257  crit,
258  true, // two_to_one
261  false, // strings switch
262  false, // use_AQM
263  1.0,
264  false, // string_with_probability
266  0., // low energy sigma_NN cut-off
267  false, // potential_affect_threshold
268  -1.0, // box_length
269  200.0, // max. cross section
270  1.0, // cross section scaling
271  0.0 // additional elastic cross section
272  };
273 }
Here is the call graph for this function:

◆ default_event_info()

EventInfo smash::Test::default_event_info ( double  impact_parameter = 0.0,
bool  empty_event = false 
)
inline

Creates default EventInfo object for testing purposes.

Definition at line 276 of file setup.h.

277  {
278  return EventInfo{impact_parameter, 0.0, 0.0, 0.0, 0.0, 0.0, 1, empty_event};
279 }

Variable Documentation

◆ smashon_mass

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

The mass of the smashon particle.

Definition at line 57 of file setup.h.

◆ smashon_width

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

The decay width of the smashon particle.

Definition at line 59 of file setup.h.

◆ smashon_pdg_string

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

The PDG code of the smashon particle.

Definition at line 61 of file setup.h.

smash::Test::configuration
Configuration configuration(std::string overrides={})
Return a configuration object filled with data from src/config.yaml.
Definition: setup.h:162
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.
Definition: unittest.h:264
smash::Test::ParticlesPtr
std::unique_ptr< Particles > ParticlesPtr
A type alias for a unique_ptr of Particles.
Definition: setup.h:203
ReactionsBitSet
std::bitset< 10 > ReactionsBitSet
Container for the 2 to 2 reactions in the code.
Definition: forwarddeclarations.h:231
smash::Test::no_multiparticle_reactions
MultiParticleReactionsBitSet no_multiparticle_reactions()
returns BitSet for multi-particle reactions, where everything is off
Definition: setup.h:237
MultiParticleReactionsBitSet
std::bitset< 2 > MultiParticleReactionsBitSet
Container for the 2 to 2 reactions in the code.
Definition: forwarddeclarations.h:240
smash::Test::smashon_width
static constexpr double smashon_width
The decay width of the smashon particle.
Definition: setup.h:59
smash::random::make_uniform_distribution
uniform_dist< T > make_uniform_distribution(T min, T max)
Definition: random.h:135
smash::Test::smashon_mass
static constexpr double smashon_mass
The mass of the smashon particle.
Definition: setup.h:57
smash::pdg::p
constexpr int p
Proton.
Definition: pdgcode_constants.h:28
smash::pdg::n
constexpr int n
Neutron.
Definition: pdgcode_constants.h:30
NNbarTreatment::NoAnnihilation
No Annihilation.
smash::Test::all_reactions_included
ReactionsBitSet all_reactions_included()
returns BitSet of 2->2 reactions, where everything is on
Definition: setup.h:232