-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool.h
More file actions
86 lines (73 loc) · 2.21 KB
/
test_tool.h
File metadata and controls
86 lines (73 loc) · 2.21 KB
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
#ifndef TEST_TOOL_H
#define TEST_TOOL_H
#include <exception>
#include <string>
#include <vector>
#include <functional>
#include <iostream>
/*****************************************************
* private interface
* - AssertFail
* - RegisterTest
*****************************************************/
class AssertFail final: public std::exception {
public:
AssertFail(const std::string& msg, const std::string& filename,
const std::string& funcname, int line)
: m_msg{msg}, m_filename{filename}, m_funcname{funcname}, m_line{line} {}
const char* what() const noexcept {
static std::string format_msg;
format_msg = "[" + m_filename + "]"
+ "[" + m_funcname + "]"
+ "[" + std::to_string(m_line) + "]: "
+ m_msg;
return format_msg.c_str();
}
private:
const std::string m_msg;
const std::string m_filename;
const std::string m_funcname;
const int m_line;
};
class RegisterTest final {
public:
explicit RegisterTest(std::function<void(void)> fun) {
m_functionList.push_back(fun);
}
static const std::vector<std::function<void(void)>>& getFunList() {
return m_functionList;
}
private:
static std::vector<std::function<void(void)>> m_functionList;
};
/*****************************************************
* public interface
* - runTest
* - AssertEqual
* - AssertEqualDefaultMsg
* - AssertTrue
* - AssertTrueDefaultMsg
* - ADD_TEST
*****************************************************/
void runTest();
#define AssertEqual(actual, expect, msg) \
do { \
if (!((actual) == (expect))) { \
throw AssertFail(msg, __FILE__, __FUNCTION__, __LINE__); \
} \
} while (false)
#define AssertEqualDefaultMsg(actual, expect) \
AssertEqual(actual, expect, \
"Expect<" + std::to_string(expect) + ">, " \
+ "Actual<" + std::to_string(actual) + ">")
#define AssertTrue(cond, msg) \
do { \
if (!(cond)) { \
throw AssertFail(msg, __FILE__, __FUNCTION__, __LINE__); \
} \
} while (false)
#define AssertTrueDefaultMsg(cond) \
AssertTrue(cond, "Expect<true>, Actual<false>")
#define ADD_TEST(fun) \
static const RegisterTest RegisterTest##fun{fun};
#endif // TEST_TOOL_H