-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickTestCPP.h
288 lines (270 loc) · 6.83 KB
/
QuickTestCPP.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* QuickTestCPP.h
* Author: Christopher Bate
*
* This is a header-only framework, no need for libraries!
* The basic idea is simply add several tests, and let the singleton take care of keeping track
* of managing the tests. Use the assertion methods within QuickTest class within testing code.
*/
#ifndef CTB_TEST
#define CTB_TEST
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <unordered_map>
#include <iomanip>
/**
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47
reset 0 (everything back to normal)
bold/bright 1 (often a brighter shade of the same colour)
underline 4
inverse 7 (swap foreground and background colours)
bold/bright off 21
underline off 24
inverse off 27
**/
const std::string RED = "\033[1;31m";
const std::string GRN = "\033[32m";
const std::string RST = "\033[0m";
#define QTEqual(val1, val2) QuickTest::Equal(val1, val2, __FILE__, __LINE__)
#define QTNotEqual(val1, val2) QuickTest::NotEqual(val1, val2, __FILE__, __LINE__)
#define QTAlmostEqual(val1, val2, eps) QuickTest::AlmostEqual(val1, val2, eps, __FILE__, __LINE__)
/**
* Test Exception class
*/
class QuickTestError : public std::exception
{
public:
QuickTestError(const std::string &errorMsg)
: m_errorMsg(errorMsg) {}
const char *what()
{
return m_errorMsg.c_str();
}
private:
std::string m_errorMsg;
};
/**
* QuickTest Assert Statements
*/
class QuickTest
{
public:
template <typename T1, typename T2>
static void Equal(T1 a, T2 b)
{
T1 c = static_cast<T1>(b);
if (a != c)
{
std::stringstream ss;
ss << "NotEqual: " << a << " != " << c;
throw QuickTestError(ss.str());
}
}
template <typename T1, typename T2>
static void Equal(T1 a, T2 b, const char *file, const int line)
{
std::cout.precision(3);
T1 c = static_cast<T1>(b);
if (a != c)
{
std::stringstream ss;
ss << "Test Failure at " << file << ":" << line
<< " NotEqual: " << a << " != " << c;
throw QuickTestError(ss.str());
}
}
static void NotEqual(void *a, void *b)
{
if (a == b)
{
std::string msg = "Equal: " + std::to_string(uint64_t(a)) + " != " + std::to_string(uint64_t(b));
throw QuickTestError(msg);
}
}
template <typename T1, typename T2>
static void NotEqual(T1 a, T2 b, const char *file, const int line)
{
T1 c = static_cast<T1>(b);
if (a == c)
{
std::stringstream ss;
ss << "Test Failure at " << file << ":" << line
<< " Should not equal: " << a << " == " << c;
throw QuickTestError(ss.str());
}
}
static void AlmostEqual(float a, float b, float eps, const char *file, const int line)
{
if (std::abs(a - b) > eps)
{
std::stringstream ss;
ss << "Test Failure at " << file << ":" << line
<< ": " << a << " != " << b << " "
<< " (AlmostEqual)" << (a-b) << " > " << eps;
throw QuickTestError(ss.str());
}
}
};
/**
* Represents an individual test.
*/
class Test
{
public:
Test(std::string n, void (*f)())
{
name = n;
func = f;
fail = false;
}
void Run()
{
std::cout << name << ": " << std::endl;
try
{
func();
std::cout << GRN << "Test Passed" << RST << std::endl;
fail = false;
}
catch (QuickTestError &e)
{
std::cout << RED << "Test Failed \n";
std::cout << e.what() << RST << std::endl;
fail = true;
}
catch (std::exception &e)
{
std::cout << RED << "Test Failed, Fatal Exception \n";
std::cout << e.what() << RST << std::endl;
fail = true;
}
}
bool fail;
void (*func)();
std::string name;
};
/**
* Represents a set of tests.
*/
class TestCase
{
public:
TestCase(std::string name)
: m_name(name), failCount(0) {}
~TestCase()
{
for (unsigned int i = 0; i < tests.size(); i++)
{
delete tests[i];
}
}
void Run()
{
for (auto it = tests.begin(); it != tests.end(); it++)
{
(*it)->Run();
std::cout << std::endl;
if ((*it)->fail)
{
failCount++;
}
}
}
void PrintFailures()
{
for (auto it = tests.begin(); it != tests.end(); it++)
{
std::cout << RED;
if ((*it)->fail)
{
std::cout << (*it)->name << std::endl;
}
std::cout << RST;
}
}
std::vector<Test *> tests;
std::string m_name;
int failCount;
};
/**
* TestRunner
* Runs all tests
*/
class TestRunner
{
public:
/** Returns singleton instance */
static TestRunner *GetRunner()
{
static TestRunner runner;
return &runner;
}
~TestRunner()
{
auto it = m_cases.begin();
while (it != m_cases.end())
{
delete it->second;
it++;
}
}
void Run()
{
auto it = m_cases.begin();
while (it != m_cases.end())
{
std::cout << "--------------------\n";
std::cout << it->first << "\n";
std::cout << "--------------------\n"
<< std::endl;
it->second->Run();
it++;
}
}
void RunOne(std::string test)
{
auto testCase = m_cases.find(test);
if (testCase != m_cases.end())
{
testCase->second->Run();
}
}
void PrintSummary()
{
std::cout << "======SUMMARY======" << std::endl;
for (auto it = m_cases.begin(); it != m_cases.end(); it++)
{
std::cout << it->first << ":" << it->second->tests.size() - it->second->failCount << "/" << it->second->tests.size() << std::endl;
it->second->PrintFailures();
}
}
int GetRetCode()
{
return m_retCode;
}
void AddTest(std::string caseName, std::string testName, void (*f)())
{
auto testCase = m_cases.find(caseName);
if (testCase == m_cases.end())
{
m_cases[caseName] = new TestCase(caseName);
}
m_cases[caseName]->tests.push_back(new Test(testName, f));
}
/** The vector of test casts (sets of tests) */
std::unordered_map<std::string, TestCase *> m_cases;
int m_retCode;
TestRunner()
{
}
};
#endif