-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_framework_main.h
149 lines (120 loc) · 3.9 KB
/
test_framework_main.h
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
#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <tuple>
#include <functional>
#include <sstream>
template <typename F, typename S>
std::ostream& operator << (std::ostream&, const std::pair<F, S>&);
template <typename F>
std::ostream& operator << (std::ostream&, const std::vector<F>&);
template <typename T>
std::ostream& operator << (std::ostream&, const std::set<T>&);
template <typename F, typename S>
std::ostream& operator << (std::ostream& os, const std::pair<F, S>& p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class T>
std::ostream& operator << (std::ostream& os, const std::vector<T>& s) {
os << "{";
bool first = true;
for (const auto& x : s) {
if (!first) {
os << ", ";
}
first = false;
os << x;
}
return os << "}";
}
template <class T>
std::ostream& operator << (std::ostream& os, const std::set<T>& s) {
os << "{";
bool first = true;
for (const auto& x : s) {
if (!first) {
os << ", ";
}
first = false;
os << x;
}
return os << "}";
}
template<class T, class U>
void AssertEqual(const T& t, const U& u, const std::string& hint = {}) {
if (t != u) {
std::ostringstream os;
os << "Assertion failed: " << t << " != " << u;
if (!hint.empty()) {
os << std::endl << "hint: " << hint;
}
throw std::runtime_error(os.str());
}
}
void Assert(bool b, const std::string& hint);
class TestRunner {
private:
struct TestCase {
std::function<void()> Test;
std::string Name;
TestCase(const std::function<void()>& test, const std::string& name)
: Test(test)
, Name(name)
{
}
};
public:
template <class TestFunc>
void RunTest(TestFunc func, const std::string& testName) {
try {
func();
std::cerr << testName << " OK" << std::endl;
} catch (std::exception& e) {
++FailCount;
std::cerr << testName << " fail: " << e.what() << std::endl;
std::cerr << std::endl;
} catch (...) {
++FailCount;
std::cerr << "Unknown exception caught" << std::endl;
}
}
void AddTest(const std::function<void()>& func, const std::string& testName) {
TestCases.emplace_back(func, testName);
}
bool HasFails() {
return FailCount > 0;
}
uint32_t Fails() {
return FailCount;
}
void RunAllTests() {
for (const auto& testCase : TestCases) {
RunTest(testCase.Test, testCase.Name);
}
}
private:
uint32_t FailCount = 0;
std::vector<TestCase> TestCases;
};
#define TEST_SUITE(NAME) \
namespace TestSuite ## NAME { \
std::string TestSuiteName = "TestSuite_" #NAME "__"; \
} \
namespace TestSuite ## NAME
#define TEST(NAME) \
class TestCase ## NAME { \
public: \
std::string Name = TestSuiteName + "TestCase_" #NAME; \
TestCase ## NAME () { \
Runner.AddTest(RunTest, Name); \
} \
static void RunTest(); \
} testCase ## NAME; \
void TestCase ## NAME ::RunTest()
#define ASSERT_THAT_C(A, B) Assert((A), (B));
#define ASSERT_THAT(A) Assert((A), #A " is false");
#define ASSERT_EQUAL(A, B) AssertEqual((A), (B), "[" __FILE__ ", " + std::to_string(__LINE__) + "] " #A " must be equal to " #B)
#define ASSERT_THAT_NOT(A) ASSERT_THAT(!(A))