forked from toptensoftware/simplelib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunittesting.h
More file actions
360 lines (285 loc) · 7.51 KB
/
unittesting.h
File metadata and controls
360 lines (285 loc) · 7.51 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef _test_core_h_
#define _test_core_h_
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "stringpool.h"
#include "stringbuilder.h"
/*
eg:
void mytest_with(int a, int b, int c)
{
assert_equal(a + b, c);
}
void mytest()
{
run(mytest_with(5, 5, 10));
run(mytest_with(10, 10, 20));
run(mytest_with(100, 100, 0));
}
int main()
{
run(mytest());
test_summary();
}
-------- output --------
Test mytest()
Test mytest_with(5, 5, 10) - PASS
Test mytest_with(10, 10, 20) - PASS
Test mytest_with(100, 100, 0)
failed: a + b should equal c
a + b: 200
c: 0
at: C:\Users\Brad\source\repos\simplelibsandbox\main.cpp(7)
Failed 1 of 4 tests!
*/
#define run(x) \
SimpleLib::CUnitTesting::EnterTest(#x); \
x; \
SimpleLib::CUnitTesting::LeaveTest()
#undef assert
#define assert_condition(a, compare, msg) { auto _a = a; if (!(compare)) { SimpleLib::CUnitTesting::FailTest(__FILE__, __LINE__, #a " " msg, #a, test_format(_a)); return; } }
#define assert_compare(a, b, compare, msg) { auto _a = a; auto _b = b; if (!(compare)) { SimpleLib::CUnitTesting::FailTest(__FILE__, __LINE__, #a " " msg " " #b, #a, test_format(_a), #b, test_format(_b) ); return; } }
#define assert_true(a) assert_condition(a, !!_a, "should be true")
#define assert_false(a) assert_condition(a, !_a, "should be false")
#define assert_null(a) assert_condition(a, _a == nullptr, "should be null")
#define assert_not_null(a) assert_condition(a, _a != nullptr, "should not be null")
#define assert_error(a) assert_condition(a, _a != 0, "should have failed with error")
#define assert_not_error(a) { auto _a = a; if (_a) { SimpleLib::CUnitTesting::FailTest(__FILE__, __LINE__, #a " should not have failed with error", #a, test_format(_a), "errno", test_format(errno)); return; } }
#define assert_equal(a, b) assert_compare(a, b, _a == _b, "should equal")
#define assert_not_equal(a, b) assert_compare(a, b, _a != _b, "should not equal")
#define assert_gt(a, b) assert_compare(a, b, _a > _b, "should be greater than")
#define assert_ge(a, b) assert_compare(a, b, _a >= _b, "should be greater or equal to")
#define assert_lt(a, b) assert_compare(a, b, _a < _b, "should be less than")
#define assert_le(a, b) assert_compare(a, b, _a <= _b, "should be less than or equal to")
#define assert_string_equal(a, b) assert_compare(a, b, SimpleLib::CUnitTesting::StringCompare(_a,_b)==0, "should equal")
#define assert_string_not_equal(a, b) assert_compare(a, b, SimpleLib::CUnitTesting::StringCompare(_a,_b)!=0, "should not equal")
#define test_summary() SimpleLib::CUnitTesting::WriteSummary()
namespace SimpleLib
{
class CUnitTesting
{
public:
static void EnterTest(const char* name)
{
ActiveTest() = new CTest(ActiveTest(), name);
}
static void LeaveTest()
{
ActiveTest()->Finish();
ActiveTest() = ActiveTest()->GetParent();
StringPool().FreeAll();
}
static void FailTest(const char* file, int lineNumber,
const char* message,
const char* exprA = nullptr, const char* valA = nullptr,
const char* exprB = nullptr, const char* valB = nullptr
)
{
if (ActiveTest())
ActiveTest()->Fail(file, lineNumber, message, exprA, valA, exprB, valB);
}
static void WriteSummary()
{
if (FailedTests() == 0)
{
printf("\nPassed all %i tests!\n\n", TotalTests());
}
else
{
printf("\nFailed %i of %i tests!\n\n", FailedTests(), TotalTests());
}
}
static const char* vsprintf(const char* format, va_list args)
{
CStringBuilder<char> buf;
buf.FormatV(format, args);
return StringPool().Alloc(buf);
}
static const char* FormatString(const char* value, char chDelim = '\"')
{
if (value == nullptr)
return "nullptr";
CStringBuilder<char> buf;
buf.Append(chDelim);
const char* p = value;
while (*p)
{
switch (*p)
{
case '\0': buf.Append("\\0", 2); break;
case '\a': buf.Append("\\a", 2); break;
case '\b': buf.Append("\\b", 2); break;
case '\f': buf.Append("\\f", 2); break;
case '\n': buf.Append("\\n", 2); break;
case '\r': buf.Append("\\r", 2); break;
case '\t': buf.Append("\\t", 2); break;
case '\v': buf.Append("\\v", 2); break;
case '\\': buf.Append("\\\\", 2); break;
case '\'': buf.Append("\\\'", 2); break;
case '\"': buf.Append("\\\"", 2); break;
default:
buf.Append(*p);
break;
}
p++;
}
buf.Append(chDelim);
return StringPool().Alloc(buf);
}
static int StringCompare(const char* a, const char* b)
{
if (a == nullptr && b == nullptr)
return 0;
if (a == nullptr)
return -1;
if (b == nullptr)
return 1;
return strcmp(a, b);
}
static CStringPool<char>& StringPool() { static CStringPool<char> val; return val; }
private:
class CTest;
static int& TotalTests() { static int val = 0; return val; }
static int& FailedTests() { static int val = 0; return val; }
static CTest*& ActiveTest() { static CTest* val = nullptr; return val; }
class CTest
{
public:
CTest(CTest* pParent, const char* name)
{
TotalTests()++;
if (pParent)
{
pParent->CloseHeader();
m_iDepth = pParent->m_iDepth + 1;
}
else
{
m_iDepth = 0;
}
m_pParent = pParent;
m_bHeaderClosed = false;
WriteIndent();
printf("Test %s", name);
}
void WriteIndent()
{
for (int i = 0; i < 2 * m_iDepth; i++)
{
printf(" ");
}
}
void CloseHeader()
{
if (!m_bHeaderClosed)
{
printf("\n");
m_bHeaderClosed = true;
}
}
void Finish()
{
if (!m_bHeaderClosed)
{
printf(" - ");
printf("PASS");
CloseHeader();
}
}
void Fail(
const char* file, int lineNumber,
const char* message,
const char* exprA, const char* valA,
const char* exprB, const char* valB
)
{
if (m_pParent)
m_pParent->ChildFailed();
CloseHeader();
printf("\n");
m_iDepth++;
WriteIndent();
printf("%20s: %s\n", "failed", message);
if (exprA && strcmp(exprA, valA))
{
WriteIndent();
printf("%20s: %s\n", exprA, valA);
}
if (exprB && strcmp(exprB, valB))
{
WriteIndent();
printf("%20s: %s\n", exprB, valB);
}
WriteIndent();
printf("%20s: %s(%i)\n", "at", file, lineNumber);
m_iDepth--;
printf("\n");
FailedTests()++;
}
void ChildFailed()
{
CloseHeader();
}
CTest* GetParent() { return m_pParent; }
CTest* m_pParent;
int m_iDepth;
bool m_bHeaderClosed;
};
};
} // namespace
inline const char* test_sprintf(const char* format, ...)
{
va_list args;
va_start(args, format);
const char* result = SimpleLib::CUnitTesting::vsprintf(format, args);
va_end(args);
return result;
}
inline const char* test_format(bool value)
{
return value ? "true" : "false";
}
inline const char* test_format(int value)
{
return test_sprintf("%i", value);
}
inline const char* test_format(unsigned int value)
{
return test_sprintf("%uu", value);
}
inline const char* test_format(long value)
{
return test_sprintf("%lil", value);
}
inline const char* test_format(unsigned long value)
{
return test_sprintf("%luul", value);
}
inline const char* test_format(long long value)
{
return test_sprintf("%llill", value);
}
inline const char* test_format(unsigned long long value)
{
return test_sprintf("%lluull", value);
}
inline const char* test_format(void* value)
{
return test_sprintf("0x%p", value);
}
inline const char* test_format(char value)
{
char sz[2] = "x";
sz[0] = value;
return SimpleLib::CUnitTesting::FormatString(sz, '\'');
}
inline const char* test_format(double value)
{
return test_sprintf("%f", value);
}
inline const char* test_format(const char* value)
{
return SimpleLib::CUnitTesting::FormatString(value);
}
#endif // _test_core_h_