Update testlib

This commit is contained in:
2023-10-31 10:49:03 +01:00
parent 033947b97d
commit 8e1e5ae024
2 changed files with 29 additions and 4 deletions

View File

@@ -14,10 +14,11 @@
#ifndef __TESTLIB__
#define __TESTLIB__
#include "utils.hpp"
#include <armadillo>
#include <string>
#include "utils.hpp"
#include <type_traits>
/** @def ASSERT(expr)
* @brief A prettier assertion function.
@@ -55,4 +56,24 @@ void m_assert(bool expr, std::string expr_str, std::string func,
* @return bool
* */
bool close_to(arma::vec &a, arma::vec &b, double tol = 1e-8);
/** @brief Test that all elements fulfill the condition.
*
* @param expr The boolean expression to apply to each element
* @param M The matrix/vector to iterate over
*
* @return bool
* */
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;
}
#endif