Assert — An overview of the Assert toolbox.
The goal of this toolbox is to provide functions to be used in unit tests. We emphasize the use of consistent tools for testing numerical issues, with the goal of testing numerical algorithms more easily. In particular, we provide a comparison function for two floating point numbers, which allows to check that two numbers are "numerically almost equal", i.e. that the relative error is small.
Some functions under test may produce an unsorted matrix of complex
values.
For example, the output of the "root" function may
produce a given set of roots on one machine and another set of
roots on another machines. The most common reasons for these
discrepancies are the change of operating system, the use of
various compiling options (e.g. debug or optimized), or the
use of various libraries (e.g. ATLAS, Intel MKL).
This makes the testing of such a function difficult so that
the assert_checkalmostequal
function, by itself, is
only a part of the solution.
This is why we provide a numerically stable sorting algorithm in the
assert_sortcomplex
.
First, the comparison is done on the real part first, and on the
imaginary part if there is a tie on the real part.
Second, we use a comparison function which takes into account
for numerical issues and uses a mixed relative-absolute
criteria.
This allows to produce a robust sorted matrix of complex doubles, which
can be consistently given to the assert_checkalmostequal
function.
The toolbox is based on macros.
The assert_true
function allows to
check that a matrix of booleans is true.
The following assertion fails and generate an error.
assert_true ( [%t %F] );
The assert_checkequal
function allows to
check that two variables are equal.
The following assertion is a success and runs silently.
assert_checkequal ( %nan , %nan );
The assert_checkalmostequal
function allows to
check that a computed result is close to an expected result.
In the following script, we check that computed=1.23456
is close to expected=1.23457
, but that
11 digits have been lost with respect to the maximum
achievable accuracy.
assert_checkalmostequal ( 1.23456 , 1.23457 , 1.e11*%eps );
The assert_sortcomplex
provides a numerically
stable sorting algorithm for matrices of complex doubles.
In the following example, we sort the matrix of complex doubles
presented in the bug report number 415.
x = [ -1.9914145 -1.895889 -1.6923826 -1.4815461 -1.1302576 -0.5652256 - 0.0655080 * %i -0.5652256 + 0.0655080 * %i 0.3354023 - 0.1602902 * %i 0.3354023 + 0.1602902 * %i 1.3468911 1.5040136 1.846668 1.9736772 1.9798866 ]; // Consider less than 4 significant digits tol = [1.e-5 0]; y = assert_sortcomplex(x,tol)