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

Test utilites for libscran. More...

Classes

struct  SimulationParameters
 Parameters for simulate_vector(). More...
 

Functions

bool compare_almost_equal (double left, double right, double tol=1e-8, bool report=true)
 
template<class Vector_ >
void compare_almost_equal (const Vector_ &left, const Vector_ &right, double tol=1e-8)
 
template<typename Function_ >
void expect_error (Function_ fun, std::string match)
 
int initial_value ()
 
template<typename Type_ = double>
std::vector< Type_ > simulate_vector (size_t length, const SimulationParameters< Type_ > &params)
 
template<typename Type_ >
std::vector< Type_ > vector_n (const Type_ *ptr, size_t n)
 

Detailed Description

Test utilites for libscran.

Function Documentation

◆ compare_almost_equal() [1/2]

template<class Vector_ >
void scran_tests::compare_almost_equal ( const Vector_ &  left,
const Vector_ &  right,
double  tol = 1e-8 
)

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
Vector_Some vector-like container of floating-point values.
Parameters
leftOne of the vectors.
rightThe other vector.
tolThe tolerance for the comparison, passed to the other compare_almost_equal() function.

◆ compare_almost_equal() [2/2]

bool scran_tests::compare_almost_equal ( double  left,
double  right,
double  tol = 1e-8,
bool  report = true 
)
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 tolerance and their mean. If both numbers are close to zero, they are considered equal if the difference is below 1e-15; this avoids being overly stringent for numbers that should be zero, at the cost of potentially missing large relative differences.

Parameters
leftOne of the numbers.
rightThe other number.
tolThe tolerance for the comparison.
reportWhether to report the error via GoogleTest.
Returns
Whether the two numbers are equal.

◆ 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_vector()

template<typename Type_ = double>
std::vector< Type_ > scran_tests::simulate_vector ( size_t  length,
const SimulationParameters< 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 values.

◆ vector_n()

template<typename Type_ >
std::vector< Type_ > scran_tests::vector_n ( const Type_ *  ptr,
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.