JayBeams
0.1
Another project to have fun coding.
|
Helper functions and classes to simplify unit tests. More...
Namespaces | |
defaults | |
Define the default values for a microbenchmark config. | |
detail | |
Classes | |
struct | create_random_sample |
Wrap a random number generator so it can be used for both real and complex numbers. More... | |
struct | create_random_sample< std::complex< T > > |
Specialize create_random_sample for complex numbers. More... | |
struct | extrapolate_periodic |
A functor to extrapolate a periodic timseries. More... | |
struct | extrapolate_with_zeroes |
A functor to extrapolate with zeroes. More... | |
class | has_resize |
Type trait to evaluate if a collection of numbers (e.g. More... | |
class | microbenchmark |
Run a micro-benchmark on a given class. More... | |
class | microbenchmark_base |
Refactor non-template parts of the microbenchmark template class. More... | |
class | microbenchmark_config |
Configure a micro-benchmark. More... | |
Typedefs | |
template<typename config > | |
using | microbenchmark_group = std::map< std::string, std::function< void(config const &cfg)> > |
Define a representation for a group of microbenchmarks. More... | |
Functions | |
template<typename value_t , typename std::enable_if< std::is_integral< value_t >::value >::type * = nullptr> | |
bool | check_close_enough (value_t num_a, value_t num_b, int tol) |
Given two numbers of the same integer type check if the difference is within a given tolerance. More... | |
template<typename value_t > | |
bool | check_close_enough (std::complex< value_t > com_a, std::complex< value_t > com_b, int tol=1) |
Given two complex numbers of the same value type check if the difference is within a given tolerance. More... | |
template<typename value_t > | |
bool | check_close_enough (value_t com_a[2], value_t com_b[2], int tol=1) |
Compare complex number (as arrays of size 2) values with some tolerance. More... | |
template<typename floating > | |
void | check_small (floating t, double small) |
Verify that a floating point value is "close enough" to a small number. More... | |
template<typename real > | |
void | check_small (std::complex< real > t, double small) |
Verify that a complex number is "close enough" to a small number. More... | |
template<typename precision_t > | |
std::complex< precision_t > | format (precision_t v[2]) |
Wrap FFTW-style complex numbers in std::complex for iostreaming. More... | |
template<typename value_type > | |
value_type | format (value_type t) |
Allow generic treatment of FFTW-style complex numbers and other types. More... | |
template<typename real , typename std::enable_if< std::is_floating_point< real >::value >::type * = nullptr> | |
real | relative_error (real actual, real expected) |
Calculate the relative error between two float point numbers. More... | |
template<typename integral , typename std::enable_if< std::is_integral< integral >::value >::type * = nullptr> | |
integral | relative_error (integral actual, integral expected) |
Adapt relative_error() for integral numbers. More... | |
template<typename real > | |
real | relative_error (std::complex< real > actual, std::complex< real > expected) |
Calculate the relative error between two complex numbers. More... | |
template<typename real > | |
real | relative_error (real actual[2], real expected[2]) |
Calculate the relative error between two complex numbers in FFTW representation. More... | |
template<typename collection_t > | |
bool | check_collection_close_enough (collection_t const &a, collection_t const &b, int tol=1, int max_differences_printed=JB_TESTING_MAX_DIFFERENCES_PRINTED) |
Given two collections of numbers of the same value type, find the differences that are out of a given tolerance and report them via Boost.Test functions. More... | |
template<typename T , std::size_t K> | |
bool | check_collection_close_enough (boost::multi_array< T, K > const &a, boost::multi_array< T, K > const &b, int tol=1, int max_differences_printed=JB_TESTING_MAX_DIFFERENCES_PRINTED) |
Specialization for boost::multi_array type. More... | |
template<typename value_t > | |
bool | check_collection_close_enough (std::size_t size, value_t const *a, value_t const *b, int tol=1, int max_differences_printed=JB_TESTING_MAX_DIFFERENCES_PRINTED) |
Given two collections of integers, floating point or complex numbers find the differences that are out of a given tolerance and report them via Boost.Test functions. More... | |
template<typename timeseries , typename generator > | |
void | create_random_timeseries (generator &gen, int nsamples, timeseries &ts) |
Create a simple timeseries where the values look like a random. More... | |
template<typename timeseries > | |
void | create_square_timeseries (int nsamples, timeseries &ts) |
Create a simple timeseries where the values look like a square. More... | |
template<typename timeseries > | |
void | create_triangle_timeseries (int nsamples, timeseries &ts) |
Create a simple timeseries where the values look like a triangle. More... | |
template<typename T , std::size_t K, typename A > | |
void | create_triangle_timeseries (int nsamples, boost::multi_array< T, K, A > &ts) |
Create a family of timeseries where the values look like parallel triangles shifted by a constant value. More... | |
template<typename timeseries_t , typename duration_t , typename extrapolation_functor > | |
jb::detail::array_traits< timeseries_t >::element_type | extrapolate_timeseries (timeseries_t const &ts, duration_t t, duration_t sampling_period, extrapolation_functor const &extrapolation) |
A function to get the extrapolated value of a timeseries. More... | |
template<typename timeseries_t , typename duration_t , typename extrapolation_functor > | |
timeseries_t | delay_timeseries (timeseries_t const &ts, duration_t delay, duration_t sampling_period, extrapolation_functor const &extrapolation) |
Delay a timeseries using a user-provided extrapolation policy. More... | |
template<typename timeseries_t , typename duration_t > | |
timeseries_t | delay_timeseries_periodic (timeseries_t const &ts, duration_t delay, duration_t sampling_period) |
Delay a timeseries using a periodic extension for early values. More... | |
template<typename timeseries_t , typename duration_t > | |
timeseries_t | delay_timeseries_zeroes (timeseries_t const &ts, duration_t delay, duration_t sampling_period) |
Delay a timeseries using zeroes for early values. More... | |
template<typename Generator > | |
Generator | initialize_mersenne_twister (int seed, std::string const &token) |
Initialize a Mersenne-Twister based generator. More... | |
std::ostream & | operator<< (std::ostream &os, microbenchmark_base::summary const &x) |
Stream the summary of a microbenchmark results in microseconds. More... | |
int | microbenchmark_group_main (int argc, char *argv[], microbenchmark_group< microbenchmark_config > const &testcases) |
Overload microbenchmark_group_main for jb::testing::microbenchmark_config. More... | |
template<typename config > | |
int | microbenchmark_group_main (int argc, char *argv[], microbenchmark_group< config > const &testcases) |
Implement the main() function for a multi-testcase benchmark. More... | |
template<typename timeseries > | |
void | resize_if_applicable (timeseries &ts, std::size_t newsize, std::true_type) |
Resize a timeseries to a newsize. More... | |
template<typename timeseries > | |
void | resize_if_applicable (timeseries &ts, std::size_t newsize, std::false_type) |
Dummy specialization for non-resizable timeseries collections. More... | |
template<typename timeseries > | |
void | resize_if_applicable (timeseries &ts, std::size_t newsize) |
Resize a timeseries to a newsize if it is resizable. More... | |
template<typename container_t > | |
auto | sum_square (container_t const &ts) |
Compute the sum square of a family of timeseries. More... | |
Variables | |
char const | uname_a [] |
The kernel version, release, machine hardware, etc. More... | |
char const | compiler [] |
The compiler version. More... | |
char const | compiler_flags [] |
The compiler flags. More... | |
char const | linker [] |
The linker version. More... | |
char const | gitrev [] |
The git revision at compile time. More... | |
char const | default_initialization_marker [] = "__default__" |
Helper functions and classes to simplify unit tests.
using jb::testing::microbenchmark_group = typedef std::map<std::string, std::function<void(config const& cfg)> > |
Define a representation for a group of microbenchmarks.
Each microbenchmark is encapsulated in a (type erased) std::function, which typically creates an instance of jb::testing::microbenchmark and runs it. The keys into the group are the names of the testcases.
config | the configuration class for the group of microbenchmarks. |
Definition at line 33 of file microbenchmark_group_main.hpp.
bool jb::testing::check_close_enough | ( | value_t | num_a, |
value_t | num_b, | ||
int | tol | ||
) |
Given two numbers of the same integer type check if the difference is within a given tolerance.
Given two numbers of the same floating-point type check if the difference is within a given tolerance.
Tolerance is expressed as the difference between any pair of integer numbers.
num_a | the first number to compare |
num_b | the second number to compare |
tol | tolerance that the pair of numbers have to be within |
value_t | the type of the integral argument numbers |
Tolerance is expressed in "number of epsilons" (i.e., the function will tolerate errors up to tolerance * std::numeric_limit<real>::epsilon()
num_a | the first number to compare |
num_b | the second number to compare |
tol | tolerance that the pair of numbers have to be within |
value_t | the type of the floating-point argument numbers |
Definition at line 34 of file check_close_enough.hpp.
Referenced by BOOST_AUTO_TEST_CASE(), check_close_enough(), and check_collection_close_enough().
bool jb::testing::check_close_enough | ( | std::complex< value_t > | com_a, |
std::complex< value_t > | com_b, | ||
int | tol = 1 |
||
) |
Given two complex numbers of the same value type check if the difference is within a given tolerance.
If value types are floating-point numbers, tolerance is expressed in "number of epsilons" (i.e., the function will tolerate errors up to tolerance * std::numeric_limit<real>::epsilon()
If value types are integer numbers, tolerance is expressed as the difference between any pair of integer numbers.
com_a | the first complex number to compare |
com_b | the second complex number to compare |
tol | tolerance that the pair of numbers have to be within |
value_type | the type of std::complex number |
Definition at line 95 of file check_close_enough.hpp.
References check_close_enough().
bool jb::testing::check_close_enough | ( | value_t | com_a[2], |
value_t | com_b[2], | ||
int | tol = 1 |
||
) |
Compare complex number (as arrays of size 2) values with some tolerance.
com_a | the first complex number to compare |
com_b | the second complex number to compare |
tol | tolerance that the pair of numbers have to be within |
value_type | the type of std::complex number |
Definition at line 115 of file check_close_enough.hpp.
References check_close_enough().
bool jb::testing::check_collection_close_enough | ( | collection_t const & | a, |
collection_t const & | b, | ||
int | tol = 1 , |
||
int | max_differences_printed = JB_TESTING_MAX_DIFFERENCES_PRINTED |
||
) |
Given two collections of numbers of the same value type, find the differences that are out of a given tolerance and report them via Boost.Test functions.
Collections of numbers are any container representation (e.g. vector, deque), boost::multi_array, c-like array, etc.
Value types are integers, floating-point, and std::complex (integers or floating-point).
If value types are floating-point numbers, tolerance is expressed in "number of epsilons" (i.e., the function will tolerate errors up to tolerance * std::numeric_limit<real>::epsilon()
If value types are integer numbers, tolerance is expressed as the difference between any pair of integer numbers.
a | the first collection of numbers to compare |
b | the second collection of numbers to compare |
tol | tolerance that each pair of numbers have to be within |
max_differences_printed | how many differences will be printed out in full detail, some of the collections are large and printing all the differences can be overwhelming |
collection_t | the type of the collection containing the numbers (e.g. std::vector<>, boost::multi_array) |
Definition at line 259 of file check_close_enough.hpp.
References check_close_enough(), jb::error, format(), and relative_error().
Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES().
bool jb::testing::check_collection_close_enough | ( | boost::multi_array< T, K > const & | a, |
boost::multi_array< T, K > const & | b, | ||
int | tol = 1 , |
||
int | max_differences_printed = JB_TESTING_MAX_DIFFERENCES_PRINTED |
||
) |
Specialization for boost::multi_array type.
a | the first multi array of numbers to compare |
b | the second multi array of numbers to compare |
tol | tolerance that each pair of numbers have to be within |
max_differences_printed | how many differences will be printed out in full detail, some of the collections are large and printing all the differences can be overwhelming |
T | type of the value stored on the timeseries |
K | timeseries dimensionality |
A | an Allocator type for type T allocator storage |
Definition at line 299 of file check_close_enough.hpp.
References check_close_enough(), jb::error, format(), and relative_error().
bool jb::testing::check_collection_close_enough | ( | std::size_t | size, |
value_t const * | a, | ||
value_t const * | b, | ||
int | tol = 1 , |
||
int | max_differences_printed = JB_TESTING_MAX_DIFFERENCES_PRINTED |
||
) |
Given two collections of integers, floating point or complex numbers find the differences that are out of a given tolerance and report them via Boost.Test functions.
size | the size of collection |
a | the first multi array of numbers to compare |
b | the second multi array of numbers to compare |
tol | tolerance that each pair of numbers have to be within |
max_differences_printed | how many differences will be printed out in full detail, some of the collections are large and printing all the differences can be overwhelming |
value_t | value type of the number stored on collections |
Definition at line 342 of file check_close_enough.hpp.
References check_close_enough(), jb::error, format(), relative_error(), and jb::testing::defaults::size.
void jb::testing::check_small | ( | floating | t, |
double | small | ||
) |
Verify that a floating point value is "close enough" to a small number.
Definition at line 125 of file check_close_enough.hpp.
void jb::testing::check_small | ( | std::complex< real > | t, |
double | small | ||
) |
Verify that a complex number is "close enough" to a small number.
Definition at line 133 of file check_close_enough.hpp.
void jb::testing::create_random_timeseries | ( | generator & | gen, |
int | nsamples, | ||
timeseries & | ts | ||
) |
Create a simple timeseries where the values look like a random.
Definition at line 13 of file create_random_timeseries.hpp.
References jb::detail::nsamples().
Referenced by BOOST_AUTO_TEST_CASE().
void jb::testing::create_square_timeseries | ( | int | nsamples, |
timeseries & | ts | ||
) |
Create a simple timeseries where the values look like a square.
Definition at line 13 of file create_square_timeseries.hpp.
References jb::detail::nsamples(), and resize_if_applicable().
Referenced by BOOST_AUTO_TEST_CASE(), and main().
void jb::testing::create_triangle_timeseries | ( | int | nsamples, |
timeseries & | ts | ||
) |
Create a simple timeseries where the values look like a triangle.
Definition at line 16 of file create_triangle_timeseries.hpp.
References resize_if_applicable().
Referenced by BOOST_AUTO_TEST_CASE(), and main().
void jb::testing::create_triangle_timeseries | ( | int | nsamples, |
boost::multi_array< T, K, A > & | ts | ||
) |
Create a family of timeseries where the values look like parallel triangles shifted by a constant value.
nsamples | samples on each timeseries |
ts | multi array containing timeseries to be filled as triangle |
T | type of the timeseries field |
K | timeseries dimensionality |
A | an Allocator type for type T allocator storage |
The values stored in the input timeseries
Definition at line 38 of file create_triangle_timeseries.hpp.
References jb::detail::element_count(), and jb::detail::nsamples().
timeseries_t jb::testing::delay_timeseries | ( | timeseries_t const & | ts, |
duration_t | delay, | ||
duration_t | sampling_period, | ||
extrapolation_functor const & | extrapolation | ||
) |
Delay a timeseries using a user-provided extrapolation policy.
The values stored in the input timeseries ts
Definition at line 78 of file delay_timeseries.hpp.
References jb::detail::array_shape(), jb::detail::element_count(), and extrapolate_timeseries().
Referenced by delay_timeseries_periodic(), and delay_timeseries_zeroes().
timeseries_t jb::testing::delay_timeseries_periodic | ( | timeseries_t const & | ts, |
duration_t | delay, | ||
duration_t | sampling_period | ||
) |
Delay a timeseries using a periodic extension for early values.
The values stored in the input timeseries ts
Definition at line 99 of file delay_timeseries.hpp.
References delay_timeseries().
Referenced by BOOST_AUTO_TEST_CASE(), and main().
timeseries_t jb::testing::delay_timeseries_zeroes | ( | timeseries_t const & | ts, |
duration_t | delay, | ||
duration_t | sampling_period | ||
) |
Delay a timeseries using zeroes for early values.
The values stored in the input timeseries ts
Definition at line 112 of file delay_timeseries.hpp.
References delay_timeseries().
Referenced by BOOST_AUTO_TEST_CASE().
jb::detail::array_traits<timeseries_t>::element_type jb::testing::extrapolate_timeseries | ( | timeseries_t const & | ts, |
duration_t | t, | ||
duration_t | sampling_period, | ||
extrapolation_functor const & | extrapolation | ||
) |
A function to get the extrapolated value of a timeseries.
ts | a timeseries, typically a std::vector-like data structure |
t | the timestamp at which we want to extrapolate the timeseries |
sampling_period | the sampling period of the timeseries |
extrapolation | implement the extrapolation |
timeseries_t | the type of the timeseries |
duration_t | the type used to represent timestamps, typically a std::chrono::duration<> instantiation |
extrapolation_functor | the type of the extrapolation |
Definition at line 62 of file delay_timeseries.hpp.
References jb::detail::element_count().
Referenced by delay_timeseries().
std::complex<precision_t> jb::testing::format | ( | precision_t | v[2] | ) |
Wrap FFTW-style complex numbers in std::complex for iostreaming.
Definition at line 140 of file check_close_enough.hpp.
Referenced by check_collection_close_enough().
value_type jb::testing::format | ( | value_type | t | ) |
Allow generic treatment of FFTW-style complex numbers and other types.
Definition at line 146 of file check_close_enough.hpp.
Generator jb::testing::initialize_mersenne_twister | ( | int | seed, |
std::string const & | token | ||
) |
Initialize a Mersenne-Twister based generator.
Often tests need to initialize a PRNG based on either a command-line argument (to make tests reproduceable), or from the std::random_device (to have good distribution properties in the PRGN). This is a fairly tedious initialization, so we have refactored it to a common function.
seed | the command-line argument seed, 0 means initialize from std::random_device. |
token | the parameter to initialize std::random_device, if default then the random device is default initialized. The semantics of token are platform specific, but on Linux it is usually the device to read random numbers from (/dev/urandom or /dev/random). It is very rare to need /dev/random, specially in tests, where this function is most commonly used. |
std::exception | if the token is invalid on the platform. |
Generator | an instantiation of std::mersenne_twister_generator<>, most likely std::mt19937 or std::mt19937_64. |
Definition at line 38 of file initialize_mersenne_twister.hpp.
int jb::testing::microbenchmark_group_main | ( | int | argc, |
char * | argv[], | ||
microbenchmark_group< microbenchmark_config > const & | testcases | ||
) |
Overload microbenchmark_group_main for jb::testing::microbenchmark_config.
A common idiom in complex benchmarks is to create multiple testcases that exercise different implementations, different template instantiations, or otherwise vary the test in a non-trivial way.
In such cases the main() function follows a well understood pattern:
argc | the number of arguments in argv |
argv | the command-line arguments |
testcases | a group of microbenchmarks, executes the one selected via the –microbenchmark.test-case command-line argument. |
nothing,all | exceptions are captured and printed to stderr before the program exits. |
Definition at line 6 of file microbenchmark_group_main.cpp.
References jb::info, JB_LOG, jb::config_object::process_cmdline(), jb::testing::detail::report_exception_at_exit(), jb::testing::microbenchmark_config::test_case, and jb::testing::microbenchmark_config::verbose.
Referenced by main(), and microbenchmark_group_main().
int jb::testing::microbenchmark_group_main | ( | int | argc, |
char * | argv[], | ||
microbenchmark_group< config > const & | testcases | ||
) |
Implement the main() function for a multi-testcase benchmark.
A common idiom in complex benchmarks is to create multiple testcases that exercise different implementations, different template instantiations, or otherwise vary the test in a non-trivial way.
In such cases the main() function follows a well understood pattern:
This class refactors that pattern into a static function.
config | the configuration object used by the benchmark. We expect that it has at least two configuration attributes:
|
argc | the number of arguments in argv |
argv | the command-line arguments |
testcases | a group of microbenchmarks, executes the one selected via the –microbenchmark.test-case command-line argument. |
nothing,all | exceptions are captured and printed to stderr before the program exits. |
Definition at line 66 of file microbenchmark_group_main.hpp.
References jb::info, jb::log::init(), JB_LOG, microbenchmark_group_main(), and jb::testing::detail::report_exception_at_exit().
std::ostream & jb::testing::operator<< | ( | std::ostream & | os, |
microbenchmark_base::summary const & | x | ||
) |
Stream the summary of a microbenchmark results in microseconds.
Definition at line 65 of file microbenchmark_base.cpp.
References jb::testing::microbenchmark_base::summary::max, jb::testing::microbenchmark_base::summary::min, jb::testing::microbenchmark_base::summary::n, jb::testing::microbenchmark_base::summary::p25, jb::testing::microbenchmark_base::summary::p50, jb::testing::microbenchmark_base::summary::p75, jb::testing::microbenchmark_base::summary::p90, jb::testing::microbenchmark_base::summary::p99, and jb::testing::microbenchmark_base::summary::p99_9.
real jb::testing::relative_error | ( | real | actual, |
real | expected | ||
) |
Calculate the relative error between two float point numbers.
actual | the number the test got |
expected | the number the test expected |
real | the type of floating point number (float, double, long double) |
Definition at line 163 of file check_close_enough.hpp.
Referenced by check_collection_close_enough(), and relative_error().
integral jb::testing::relative_error | ( | integral | actual, |
integral | expected | ||
) |
Adapt relative_error() for integral numbers.
actual | the number the test got |
expected | the number the test expected |
integral | the type of integral number |
Definition at line 186 of file check_close_enough.hpp.
real jb::testing::relative_error | ( | std::complex< real > | actual, |
std::complex< real > | expected | ||
) |
Calculate the relative error between two complex numbers.
actual | the number the test got |
expected | the number the test expected |
real | the type of floating point number (float, double, long double) |
Definition at line 205 of file check_close_enough.hpp.
References relative_error().
real jb::testing::relative_error | ( | real | actual[2], |
real | expected[2] | ||
) |
Calculate the relative error between two complex numbers in FFTW representation.
actual | the number the test got |
expected | the number the test expected |
real | the type of floating point number (float, double, long double) |
Definition at line 224 of file check_close_enough.hpp.
References relative_error().
void jb::testing::resize_if_applicable | ( | timeseries & | ts, |
std::size_t | newsize, | ||
std::true_type | |||
) |
Resize a timeseries to a newsize.
ts | timeseries are any container representation (e.g. vector, deque), boost::multi_array, c-like array, etc |
newsize | size to resize the timeseries if possible |
timeseries | type of the timeseries collection representation |
Definition at line 46 of file resize_if_applicable.hpp.
Referenced by create_square_timeseries(), create_triangle_timeseries(), and resize_if_applicable().
void jb::testing::resize_if_applicable | ( | timeseries & | ts, |
std::size_t | newsize, | ||
std::false_type | |||
) |
Dummy specialization for non-resizable timeseries collections.
ts | timeseries are any container representation (e.g. vector, deque), boost::multi_array, c-like array, etc |
newsize | size to resize the timeseries if possible |
timeseries | type of the timeseries collection representation |
Definition at line 59 of file resize_if_applicable.hpp.
void jb::testing::resize_if_applicable | ( | timeseries & | ts, |
std::size_t | newsize | ||
) |
Resize a timeseries to a newsize if it is resizable.
ts | timeseries are any container representation (e.g. vector, deque), boost::multi_array, c-like array, etc |
newsize | size to resize the timeseries if possible |
timeseries | type of the timeseries collection representation |
Definition at line 72 of file resize_if_applicable.hpp.
References resize_if_applicable().
auto jb::testing::sum_square | ( | container_t const & | ts | ) |
Compute the sum square of a family of timeseries.
container_t | type of the collection of timeseries |
Extract T out of std::complex<T>, otherwise simply T.
Definition at line 17 of file sum_square.hpp.
References jb::detail::element_count(), and jb::detail::nsamples().
Referenced by BOOST_AUTO_TEST_CASE(), and main().
char const jb::testing::compiler[] |
The compiler version.
Referenced by main().
char const jb::testing::compiler_flags[] |
The compiler flags.
Referenced by main().
char const jb::testing::default_initialization_marker[] = "__default__" |
Definition at line 11 of file initialize_mersenne_twister.hpp.
Referenced by main().
char const jb::testing::gitrev[] |
The git revision at compile time.
Referenced by main().
char const jb::testing::linker[] |
The linker version.
Referenced by main().
char const jb::testing::uname_a[] |
The kernel version, release, machine hardware, etc.
Referenced by main().