10 #ifndef META_UNIT_TEST_H_
11 #define META_UNIT_TEST_H_
23 #define ASSERT(expr) \
27 FAIL("Assertion failed: " #expr); \
34 #define ASSERT_EQUAL(exp1, exp2) \
37 std::string msg = testing::assert_equal(exp1, exp2, #exp1, #exp2); \
45 #define ASSERT_APPROX_EQUAL(exp1, exp2) \
49 = testing::assert_approx_equal(exp1, exp2, #exp1, #exp2); \
57 #define ASSERT_BINOP(exp1, exp2, binop) \
60 std::string msg = testing::assert(exp1, exp2, #exp1, #exp2, binop); \
72 std::string fail_msg = "[ " + printing::make_red("FAIL") + " ] " \
73 + (why) + " (" + testing::filename(__FILE__) \
74 + ":" + std::to_string(__LINE__) + ")"; \
75 throw testing::unit_test_exception{fail_msg}; \
81 #define ASSERT_LESS(exp1, exp2) \
84 std::string msg = testing::assert_less(exp1, exp2, #exp1, #exp2); \
92 #define ASSERT_GREATER(exp1, exp2) \
95 std::string msg = testing::assert_greater(exp1, exp2, #exp1, #exp2); \
109 static double epsilon = 0.0000001;
114 inline std::string
filename(
const std::string& path)
116 size_t slash = path.find_last_of(
"/\\");
117 if(slash == std::string::npos)
119 return path.substr(slash + 1);
131 template <
class T,
class K,
class BinOp>
133 const char* expstr,
const char* actstr,
136 if (!binop(expected, actual))
138 std::stringstream ss;
139 ss <<
"[" << expstr <<
" == " << actstr <<
"] => [" << expected
140 <<
" == " << actual <<
"]";
152 template <
class T,
class K>
154 const char* expstr,
const char* actstr)
157 [](
const T& a,
const K& b)
169 template <
class T,
class K>
171 const char* expstr,
const char* actstr)
173 if (!(std::abs(expected - actual) < epsilon))
175 std::stringstream ss;
176 ss <<
"[abs(" << expstr <<
" - " << actstr <<
") < epsilon] => [abs("
177 << expected <<
" - " << actual <<
") < " << epsilon <<
"]";
189 template <
class T,
class K>
190 inline std::string
assert_less(
const T& expected,
const K& actual,
191 const char* expstr,
const char* actstr)
193 if (!(expected < actual))
195 std::stringstream ss;
196 ss <<
"[" << expstr <<
" < " << actstr <<
"] => [" << expected <<
" < "
209 template <
class T,
class K>
211 const char* expstr,
const char* actstr)
213 if (!(expected > actual))
215 std::stringstream ss;
216 ss <<
"[" << expstr <<
" > " << actstr <<
"] => [" << expected <<
" > "
229 using std::runtime_error::runtime_error;
238 template <
class Func>
239 int run_test(
const std::string& test_name, Func&& func)
245 catch (std::exception& ex)
247 std::cerr <<
" " << std::setw(40) << std::left
248 << (test_name +
": ") << ex.what() << std::endl;