-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_unit_test.hpp
83 lines (68 loc) · 3.89 KB
/
simple_unit_test.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
// std
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <typeinfo>
namespace util::test {
/// default equality check, proxies to operator==
inline bool equal(const auto& lhs, const auto& rhs)
{
return lhs == rhs;
}
static int total_tests = 0;
static int passed_tests = 0;
static int failed_tests = 0;
void report()
{
std::cout << "total tests: " << total_tests << ", passed: " << passed_tests << ", failed: " << failed_tests << "\n";
}
static int _atexit = std::atexit(report);
}
#define ASSERT_EQUAL(a, b) { \
auto __a = (a); \
auto __b = (b); \
std::stringstream ss; ss << "failure at line " << __LINE__ << " (a: actual, e: expected)\na: [" << __a << "]\ne: [" << __b << "]"; \
++util::test::total_tests; \
if (util::test::equal(__a, __b)) \
{ \
++util::test::passed_tests; \
} else { \
++util::test::failed_tests; \
std::cout << ss.str() << "\n"; \
} \
}
#define ASSERT_NEQUAL(a, b) { \
auto __a = (a); \
auto __b = (b); \
std::stringstream ss; ss << "failure at line " << __LINE__ << " (a: actual, e: expected)\na: [" << __a << "]\ne: [" << __b << "]"; \
++util::test::total_tests; \
if (!util::test::equal(__a, __b)) \
{ \
++util::test::passed_tests; \
} else { \
++util::test::failed_tests; \
std::cout << ss.str() << "\n"; \
} \
}
#define ASSERT_TRUE(a) ASSERT_EQUAL(a, true)
#define ASSERT_FALSE(a) ASSERT_EQUAL(a, false)
#define ASSERT_EXCEPTION(f, e) { \
std::stringstream ss; ss << "failure at line " << __LINE__ << ": expected exception of type: " << typeid(e).name() << "\n"; \
++util::test::total_tests; \
bool passed = false; \
try { \
f(); \
} \
catch (const e& exception) \
{ \
passed = true; \
} \
if (!passed) \
{ \
++util::test::failed_tests; \
std::cout << ss.str() << "\n"; \
} else { \
++util::test::passed_tests; \
} \
}