Loading...
Searching...
No Matches
testlib.hpp
Go to the documentation of this file.
#ifndef __TESTLIB__
#define __TESTLIB__
#include "utils.hpp"
#include <armadillo>
#include <string>
#include <type_traits>
#define ASSERT(expr, msg) \
details::m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
namespace details {
void m_assert(bool expr, std::string expr_str, std::string func,
std::string file, int line, std::string msg);
} // namespace details
namespace testlib {
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
{
if (a.n_elem != b.n_elem) {
return false;
}
for (size_t i = 0; i < a.n_elem; i++) {
if (!close_to(a(i), b(i))) {
return false;
}
}
return true;
}
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool close_to(T a, T b, double tol = 1e-8)
{
return std::fabs(a - b) < tol;
}
template <class T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
{
for (size_t i = 0; i < a.n_elem; i++) {
if (!(a(i) == b(i))) {
return false;
}
}
return true;
}
template <class T,
class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
{
for (size_t i = 0; i < M.n_elem; i++) {
if (!expr(M(i))) {
return false;
}
}
return true;
}
} // namespace testlib
#endif
utils.hpp
Function prototypes and macros that are useful.