2 Dimensional Ising Model
Simulate the change in energy and magnetization in a ferro magnet
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
34namespace details {
47void m_assert(bool expr, std::string expr_str, std::string func,
48 std::string file, int line, std::string msg);
49} // namespace details
50
51namespace testlib {
63template <class T,
64 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
65static bool close_to(arma::Mat<T> &a, arma::Mat<T> &b, double tol = 1e-8)
66{
67 if (a.n_elem != b.n_elem) {
68 return false;
69 }
70
71 for (size_t i = 0; i < a.n_elem; i++) {
72 if (!close_to(a(i), b(i))) {
73 return false;
74 }
75 }
76 return true;
77}
78
90template <class T,
91 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
92static bool close_to(T a, T b, double tol = 1e-8)
93{
94 return std::fabs(a - b) < tol;
95}
96
107template <class T,
108 class = typename std::enable_if<std::is_integral<T>::value>::type>
109static bool is_equal(arma::Mat<T> &a, arma::Mat<T> &b)
110{
111 for (size_t i = 0; i < a.n_elem; i++) {
112 if (!(a(i) == b(i))) {
113 return false;
114 }
115 }
116 return true;
117}
118
126template <class T,
127 class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
128static bool assert_each(std::function<bool(T)> expr, arma::Mat<T> &M)
129{
130 for (size_t i = 0; i < M.n_elem; i++) {
131 if (!expr(M(i))) {
132 return false;
133 }
134 }
135 return true;
136}
137} // namespace testlib
138#endif
Function prototypes and macros that are useful.