Implementations of different problems

This commit is contained in:
2023-11-08 17:31:14 +01:00
parent 5ac838a266
commit 090510175b
13 changed files with 704 additions and 98 deletions

View File

@@ -5,9 +5,10 @@
*
* @version 1.0
*
* @brief Function prototypes and macros that are useful.
* @brief A small test library.
*
* @details This a small testing library that is tailored for the needs of the project.
* @details This a small testing library that is tailored for the needs of the
* project.
*
* @bug No known bugs
* */
@@ -27,8 +28,9 @@
* assertion function than the regular assert function from cassert.
* */
#define ASSERT(expr, msg) \
m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
details::m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
namespace details {
/** @brief Test an expression, confirm that test is ok, or abort execution.
*
* @details This function takes in an expression and prints an OK message if
@@ -43,7 +45,9 @@
* */
void m_assert(bool expr, std::string expr_str, std::string func,
std::string file, int line, std::string msg);
} // namespace details
namespace testlib {
/** @brief Test if two armadillo matrices/vectors are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
@@ -64,12 +68,29 @@ static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
}
for (size_t i = 0; i < a.n_elem; i++) {
if (std::abs(a(i) - b(i)) >= tol) {
if (!close_to(a(i), b(i))) {
return false;
}
}
return true;
}
/** @brief Test if two numbers are close to each other.
*
* @details This function takes in 2 matrices/vectors and checks if they are
* approximately equal to each other given a tolerance.
*
* @param a Matrix/vector a
* @param b Matrix/vector b
* @param tol The tolerance
*
* @return bool
* */
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::abs(a - b) < tol;
}
/** @brief Test if two armadillo matrices/vectors are equal.
@@ -93,10 +114,11 @@ static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
}
return true;
}
/** @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
* @param M The matrix/vector to iterate over
*
* @return bool
* */
@@ -111,5 +133,5 @@ static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
}
return true;
}
} // namespace testlib
#endif