|
Chebyshev
Unit testing for scientific software
|
A C++ testing framework designed for scientific software
Chebyshev is a header-only C++ testing framework specifically designed for scientific computing and numerical software. The framework is made up of different modules which handle specific types of testing: the prec module estimates the precision of numerical methods, benchmark module benchmarks code performance, the err module controls proper error handling. In addition, the output module controls output of test results in a fully customizable way, and random provides simple, thread-safe random numbers and distribution sampling.
The three modules prec, benchmark and err are used through contexts, which are simply objects that handle requests and settings:
Tests are run through the context and are automatically executed, either sequentially or in parallel, according to the set policy (ctx.settings.multithreading). Once a call to ctx.terminate() is reached or the context is destroyed, it waits for test results and outputs the results according to settings (ctx.settings and ctx.output->settings).
The prec module uses deterministic and stochastic methods to estimate the numerical errors of the functions you want to test. This is done by calling ctx.equals() to check a simple equivalence (e.g. sqrt(4) == 2) or by calling ctx.estimate() to use an estimator to get a more detailed error estimation over a discrete or continuous domain (e.g. checking that sqrt(x) works correctly over the positive real numbers):
This ctx.estimate() call will estimate relevant error integrals such as the mean and max error:
$$ \varepsilon_{\text{avg}} = \frac{1}{\mu(\Omega)} \int_\Omega |\tilde f(x) - f(x)| dx, \ \ \ \ \ \ \ \ \ \ \ \varepsilon_{\text{max}} = \max_\Omega |\tilde f(x) - f(x)| $$
These results are stored and written to file, according to the settings. You can also implement fully customized estimators for your own use cases. Other than comparing to a more precise implementation, you can check for properties of your method (e.g. homogeneity, involution, idempotence).
The benchmark module provides the ctx.benchmark() method which allows to estimate the average runtime and other statistics of any given function. The function under benchmark is run multiple times over many runs and iterations, where different runs use the same set of inputs and iterations run over different inputs. The input sets can be fully customized, allowing for deterministic or randomized samples over a function's domain, using the random module (e.g. what is the average runtime of my sqrt(x) implementation on the real numbers?).
To use Chebyshev, simply download the code and include the relevant header file for the module you need (e.g. prec.h) and start writing tests! You can also include chebyshev.h to include all modules and functionalities. The framework is designed to be easy to use, with a minimal learning curve and many customization options.
Here is a simple snippet:
The output of this code is something like this:
Chebyshev has no dependencies, and only a C++14 compiler is needed to use it. Simply include the module headers in your code and start writing tests.