forked from qicosmos/ormpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.hpp
224 lines (196 loc) · 6.7 KB
/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#include <functional>
#include <vector>
#include <string>
#include <iostream>
#include <csignal>
struct BaseCase
{
virtual void run() = 0;
virtual void abort() = 0;
virtual bool isAborted() = 0;
virtual ~BaseCase() = default;
};
struct AbortThisCase {};
class UnitTest
{
private:
UnitTest() : last_checked_line_{ 0 }, failure_num_{ 0 }, current_case_{ nullptr } {}
std::vector<BaseCase*> test_cases_;
std::string last_checked_file_;
size_t last_checked_line_;
size_t failure_num_;
BaseCase* current_case_;
public:
static UnitTest& getInstance()
{
static UnitTest instance;
return instance;
}
void runAll()
{
std::cout << ">>> running " << test_cases_.size() << " tests..." << std::endl;
for (BaseCase* test : test_cases_)
{
current_case_ = test;
current_case_->run();
}
}
BaseCase* currentCase()
{
return current_case_;
}
void registerTestCase(BaseCase *test)
{
test_cases_.push_back(test);
}
void printLastCheckedPoint()
{
std::cout << ">>> ";
std::cout << last_checked_file_ << "(" << last_checked_line_ << ")" << ": last checkpoint" << std::endl;
}
void checkFile(const std::string& file)
{
last_checked_file_ = file;
}
void checkLine(size_t line)
{
last_checked_line_ = line;
}
void incFailure()
{
++failure_num_;
}
size_t getFailureNum()
{
return failure_num_;
}
};
template <bool should_be_included = true>
struct TestCase : BaseCase
{
public:
TestCase(std::function<void()> method, const std::string& name, const std::string& file, size_t line)
: method_{ method }, case_name_{ name }, defined_file_{ file }, defined_line_{ line }, is_aborted_{ false }
{
UnitTest::getInstance().registerTestCase(this);
}
void run() override
{
try
{
UnitTest::getInstance().checkFile(defined_file_);
UnitTest::getInstance().checkLine(defined_line_);
size_t old_failure_num = UnitTest::getInstance().getFailureNum();
method_();
auto failures = UnitTest::getInstance().getFailureNum() - old_failure_num;
if (failures)
{
std::cout << ">>> ";
std::cout << failures << " failures are detected in the test case \"" << case_name_ << "\"" << std::endl;
}
}
catch (AbortThisCase&)
{
std::cout << ">>> " << case_name_ << " aborted." << std::endl;
UnitTest::getInstance().printLastCheckedPoint();
}
catch (std::exception& e)
{
UnitTest::getInstance().incFailure();
std::cout << ">>> fatal error: in \"" << case_name_ << "\": " << typeid(e).name() << ": " << e.what() << std::endl;
UnitTest::getInstance().printLastCheckedPoint();
}
catch (...)
{
UnitTest::getInstance().incFailure();
std::cout << ">>> fatal error: in \"" << case_name_ << "\": unknown type exception" << std::endl;
UnitTest::getInstance().printLastCheckedPoint();
}
}
void abort() override
{
is_aborted_ = true;
}
bool isAborted() override
{
return is_aborted_;
}
~TestCase() override = default;
private:
std::function<void()> method_;
std::string case_name_;
std::string defined_file_;
size_t defined_line_;
bool is_aborted_;
};
template <>
struct TestCase<false>
{
TestCase(std::function<void()>, const std::string&, const std::string&, size_t)
{
}
};
#ifdef TEST_MAIN
[[noreturn]]
static void report_and_exit()
{
std::cout << "\n**** ";
std::cout << UnitTest::getInstance().getFailureNum() << " failures are detected." << std::endl;
exit((int)UnitTest::getInstance().getFailureNum());
}
int main()
{
signal(SIGSEGV, [](int)
{
UnitTest::getInstance().incFailure();
std::cout << ">>> fatal error: received SIGSEGV." << std::endl;
UnitTest::getInstance().printLastCheckedPoint();
report_and_exit();
});
UnitTest::getInstance().runAll();
report_and_exit();
}
#endif
template <typename F, typename... Args, typename = decltype(std::declval<F>()(std::declval<Args>()...))>
void do_check_failed(F&& f, Args&&... args)
{
f(std::forward<Args>(args)...);
}
template <typename... Msgs>
void do_check_failed(Msgs&&... msgs)
{
(void)std::initializer_list<int>{(std::cout << ">>> " << msgs << std::endl, 0)...};
}
#define TEST_CASE(test_name, ...) \
static void test_name(); \
static TestCase<__VA_ARGS__> test_name##_case{test_name, #test_name, __FILE__, __LINE__};\
static void test_name()
#define G_CHECK(cond, strict, ...) \
do { \
BaseCase* cur_case = UnitTest::getInstance().currentCase(); \
if(cur_case->isAborted()) \
throw AbortThisCase{}; \
UnitTest::getInstance().checkFile(__FILE__); \
UnitTest::getInstance().checkLine(__LINE__); \
if(!(cond)) { \
UnitTest::getInstance().incFailure(); \
if(strict) { \
std::cout << ">>> check \"" << #cond << "\" failed." << std::endl; \
std::cout << ">>> critical error at " __FILE__ "(" << __LINE__ << ")." << std::endl;\
do_check_failed(__VA_ARGS__); \
cur_case->abort(); \
throw AbortThisCase{}; \
} else { \
std::cout << ">>> check \"" << #cond << "\" failed." << "at " \
<< __FILE__ << "(" << __LINE__ << ")" << std::endl; \
do_check_failed(__VA_ARGS__); \
} \
} \
} while(0)
#define TEST_CHECK(cond, ...) \
G_CHECK(cond, false, __VA_ARGS__)
#define TEST_REQUIRE(cond, ...) \
G_CHECK(cond, true, __VA_ARGS__)
#define TEST_REQUIRE_GUARD(...) \
TEST_CHECK(true, __VA_ARGS__)