scran_test_utils
Test utilities for libscran projects
Loading...
Searching...
No Matches
scran_tests Namespace Reference

Test utilites for libscran. More...

Classes

struct  CompareAlmostEqualParameters
 Parameters for compare_almost_equal(). More...
 
struct  SimulateCompressedSparseMatrixParameters
 Parameters for simulate_compressed_sparse_matrix(). More...
 
struct  SimulatedCompressedSparseMatrix
 Results of simulate_compressed_sparse_matrix(). More...
 
struct  SimulateVectorParameters
 Parameters for simulate_vector(). More...
 

Typedefs

typedef std::mt19937_64 RngEngine
 

Functions

bool compare_almost_equal (double left, double right, const CompareAlmostEqualParameters &params)
 
template<class LeftContainer_ , class RightContainer_ >
void compare_almost_equal_containers (const LeftContainer_ &left, const RightContainer_ &right, CompareAlmostEqualParameters params)
 
template<typename Function_ >
void expect_error (Function_ fun, std::string match)
 
int initial_value ()
 
template<typename Data_ = double, typename Index_ = int, typename Pointer_ = std::size_t>
SimulatedCompressedSparseMatrix< Data_, Index_, Pointer_ > simulate_compressed_sparse_matrix (Index_ primary, Index_ secondary, const SimulateCompressedSparseMatrixParameters< Data_ > &params)
 
template<typename Type_ >
constexpr Type_ default_simulation_min ()
 
template<typename Type_ = double>
std::vector< Type_ > simulate_vector (const typename std::vector< Type_ >::size_type length, const SimulateVectorParameters< Type_ > &params)
 
template<typename Type_ >
std::vector< Type_ > vector_n (const Type_ *ptr, std::size_t n)
 

Detailed Description

Test utilites for libscran.

Typedef Documentation

◆ RngEngine

typedef std::mt19937_64 scran_tests::RngEngine

Type of the random number generator for simulating data.

Function Documentation

◆ compare_almost_equal()

bool scran_tests::compare_almost_equal ( double left,
double right,
const CompareAlmostEqualParameters & params )
inline

Check if two floating-point numbers are equal, accounting for some relative difference.

Two numbers are considered equal if their difference is less than the product of CompareAlmostEqualParameters::relative_tolerance and their mean. If both numbers are close to zero, they are considered equal if the difference is below CompareAlmostEqualParameters::absolute_tolerance; this avoids being overly stringent for numbers that should be zero, at the cost of potentially missing large relative differences.

If both numbers are NaN, they are reported to be equal unless CompareAlmostEqualParameters::nan_equal = false. Obviously, NaNs are considered to be non-equal to any non-NaN number.

Parameters
leftOne of the numbers.
rightThe other number.
paramsFurther parameters.
Returns
Whether the two numbers are equal.

◆ compare_almost_equal_containers()

template<class LeftContainer_ , class RightContainer_ >
void scran_tests::compare_almost_equal_containers ( const LeftContainer_ & left,
const RightContainer_ & right,
CompareAlmostEqualParameters params )

Check if two vectors contain an equal number of almost-equal floats. This compares the corresponding elements from each vector using compare_almost_equal(). Any test failure is reported via GoogleTest.

Template Parameters
LeftVector_Some vector-like container of floating-point values.
RightVector_Another vector-like container of floating-point values.
Parameters
leftOne of the vectors.
rightThe other vector.
paramsFurther parameters. Note that CompareAlmostEqualParameters::report is ignored, any mismatching value will always be reported.

◆ default_simulation_min()

template<typename Type_ >
Type_ scran_tests::default_simulation_min ( )
constexpr
Template Parameters
Type_Numeric type of the simulated value.
Returns
-10 if Type is signed, otherwise 0.

◆ expect_error()

template<typename Function_ >
void scran_tests::expect_error ( Function_ fun,
std::string match )

Expect an error to be raised with the given message. If no error is raised, or if the error message does not match match, an error is reported via GoogleTest.

Template Parameters
Function_Function to call with no arguments.
Parameters
funFunction that should raise an error.
matchString to be matched against the error message.

◆ initial_value()

int scran_tests::initial_value ( )
inline

Some functions store their output in a user-supplied pointer to a pre-allocated array. Such functions should not assume that the array has been zeroed. Incorrectly making this assumption can lead to subtle bugs when the functions are called with non-zeroed buffers. To test that this assumption is not present in function X(), we can initialize an output buffer with a non-zero initial value from the initial_value() function below. After calling X() with output, we can compare the contents of output against the expected output to verify that the function ignores the non-zero initial value. If the expected output is not readily available, we could instead initialize a separate vector with another call to initial_value() and call X() on it. Any inconsistency in the results would indicate that the variable initial_value() is affecting the results.

Returns
An integer in [1, 255] that is incremented on every call (or rolls back to 1).

◆ simulate_compressed_sparse_matrix()

template<typename Data_ = double, typename Index_ = int, typename Pointer_ = std::size_t>
SimulatedCompressedSparseMatrix< Data_, Index_, Pointer_ > scran_tests::simulate_compressed_sparse_matrix ( Index_ primary,
Index_ secondary,
const SimulateCompressedSparseMatrixParameters< Data_ > & params )
Template Parameters
Data_Numeric type of the data.
Index_Integer type of the indices.
Pointer_Integer type of the compressed sparse pointers.
Parameters
primaryExtent of the primary dimension, i.e., along which the structural non-zero elements are compressed. For example, in a compressed sparse row matrix, the rows would be the primary dimension.
secondaryExtent of the secondary dimension, i.e., not the primary dimension.
paramsSimulation parameters.
Returns
Contents of a simulated compressed sparse matrix.

◆ simulate_vector()

template<typename Type_ = double>
std::vector< Type_ > scran_tests::simulate_vector ( const typename std::vector< Type_ >::size_type length,
const SimulateVectorParameters< Type_ > & params )

Simulate a vector of random values. If SimulationParameters::density < 1, this will be a sparse vector with structural zeros.

Template Parameters
Type_Numeric type of the simulated value.
Parameters
lengthLength of the vector.
paramsSimulation parameters.
Returns
Vector of simulated values.

◆ vector_n()

template<typename Type_ >
std::vector< Type_ > scran_tests::vector_n ( const Type_ * ptr,
std::size_t n )

Quickly create a vector from an array, primarily for convenient use in EXPECT_EQ with another vector.

Parameters
[in]ptrPointer to the start of an array.
nLength of the array.
Returns
Vector of the array's values.