Simulating the Schrödinger wave equation using the Crank-Nicolson method in 2+1 dimensions Simulating the Schrödinger wave equation using the Crank-Nicolson method in 2+1 dimensions

Loading...
Searching...
No Matches
testlib.hpp
Go to the documentation of this file.
1
16#ifndef __TESTLIB__
17#define __TESTLIB__
18
19#include "utils.hpp"
20
21#include <armadillo>
22#include <string>
23#include <type_traits>
24
31#define ASSERT(expr, msg) \
32 details::m_assert(expr, #expr, __METHOD_NAME__, __FILE__, __LINE__, msg)
33
34 namespace details {
35
48 void m_assert(bool expr, std::string expr_str, std::string func,
49 std::string file, int line, std::string msg);
50} // namespace details
51
52 namespace testlib {
53
65 template <class T,
66 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
67 static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
68{
69 if (a.n_elem != b.n_elem) {
70 return false;
71 }
72
73 for (size_t i = 0; i < a.n_elem; i++) {
74 if (!close_to(a(i), b(i))) {
75 return false;
76 }
77 }
78 return true;
79}
80
92 template <class T,
93 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
94 static bool close_to(T a, T b, double tol = 1e-8)
95{
96 return std::fabs(a - b) < tol;
97}
98
109 template <class T,
110 class = typename std::enable_if<std::is_integral<T>::value>::type>
111 static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
112{
113 for (size_t i = 0; i < a.n_elem; i++) {
114 if (!(a(i) == b(i))) {
115 return false;
116 }
117 }
118 return true;
119}
120
128 template <class T,
129 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
130 static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
131{
132 for (size_t i = 0; i < M.n_elem; i++) {
133 if (!expr(M(i))) {
134 return false;
135 }
136 }
137 return true;
138}
139} // namespace testlib
140#endif
testlib::close_to
static bool close_to(arma::Mat< T > &a, arma::Mat< T > &b, double tol=1e-8)
Test if two armadillo matrices/vectors are close to each other.
Definition: testlib.hpp:67
details::m_assert
void m_assert(bool expr, std::string expr_str, std::string func, std::string file, int line, std::string msg)
Test an expression, confirm that test is ok, or abort execution.
testlib::is_equal
static bool is_equal(arma::Mat< T > &a, arma::Mat< T > &b)
Test if two armadillo matrices/vectors are equal.
Definition: testlib.hpp:111
testlib::assert_each
static bool assert_each(std::function< bool(T)> expr, arma::Mat< T > &M)
Test that all elements fulfill the condition.
Definition: testlib.hpp:130
utils.hpp
Function prototypes and macros that are useful.