forked from kudaba/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletest.cpp
More file actions
272 lines (240 loc) · 7.56 KB
/
simpletest.cpp
File metadata and controls
272 lines (240 loc) · 7.56 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
#include "simpletest.h"
#ifdef __cplusplus
extern "C" {
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
} ;
#endif // __cplusplus
extern "C" void __cxa_pure_virtual() { while (1); } // added to support setting virtual functions as pure (by adding ' = 0 ' at the end of their declaration.
//---------------------------------------------------------------------------------
// statics
//---------------------------------------------------------------------------------
void DefaultPrint(char const* string) { printf("%s", string); } // switch printf with any other printing solution you want.
TestFixture* TestFixture::ourFirstTest;
TestFixture* TestFixture::ourLastTest;
//thread_local TestFixture* TestFixture::ourCurrentTest;
TestFixture* TestFixture::ourCurrentTest;
void (*TestFixture::Print)(char const* string) = DefaultPrint;
//---------------------------------------------------------------------------------
// helper to get the number of decimal places to print for floats and doubles
//---------------------------------------------------------------------------------
template <typename T>
static int locDecimals(T value)
{
const T eps = 0.00001f;
T remainder = value - (int)value;
if (remainder == 0)
return 0;
int decimals = 0;
// add decimals until hitting the first non-zero number that shouldn't be rounded (close to 0 or 1)
bool hitsomething = int(remainder * 10) != 0;
while (!hitsomething ||
((remainder > eps && remainder < (1 - eps)) ||
(remainder < -eps && remainder >(-1 + eps))))
{
remainder = remainder * 10;
remainder = remainder - (int)remainder;
hitsomething |= int(remainder * 10) != 0;
++decimals;
}
return decimals;
}
//---------------------------------------------------------------------------------
// Standard type printers
//---------------------------------------------------------------------------------
TempString::TempString(const TempString& other)
{
if (other.myTextPointer == other.myTextBuffer)
{
strcpy(myTextBuffer, other.myTextBuffer);
myTextPointer = myTextBuffer;
}
else
{
myTextPointer = other.myTextPointer;
}
}
TempString TypeToString(int value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%d", value);
return tempString;
}
TempString TypeToString(int64 value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%lld", value);
return tempString;
}
TempString TypeToString(uint value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%u", value);
return tempString;
}
TempString TypeToString(uint64 value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%llu", value);
return tempString;
}
TempString TypeToString(float value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%0.*f", locDecimals(value), value);
return tempString;
}
TempString TypeToString(double value)
{
TempString tempString;
sprintf(tempString.myTextBuffer, "%0.*f", locDecimals(value), value);
return tempString;
}
TempString TypeToString(bool value)
{
return TempString(value ? "true" : "false");
}
TempString TypeToString(char const* value)
{
return TempString(value ? value : "(nullptr)");
}
TempString TypeToString(void const* value)
{
if (value == nullptr)
return TempString("(nullptr)");
TempString tempString;
sprintf(tempString.myTextBuffer, "0x%p", value);
return tempString;
}
//---------------------------------------------------------------------------------
// Fixture implementation
//---------------------------------------------------------------------------------
TestFixture::TestFixture()
: myNextTest(nullptr)
, myNumTestsChecked(0)
, myNumErrors(0)
, myNextError(nullptr)
{
// global link list registration, add in order of discovery
if (ourFirstTest == nullptr)
{
ourFirstTest = this;
ourLastTest = this;
}
else
{
ourLastTest->myNextTest = this;
ourLastTest = this;
}
}
//---------------------------------------------------------------------------------
// Write error into current error object and advance pointer if there's still enough space
//---------------------------------------------------------------------------------
void TestFixture::LogError(char const* string, ...)
{
++myNumErrors;
uintptr_t spaceLeft = (myMessageSpace + MESSAGE_SPACE) - (char*)myNextError;
if (spaceLeft == 0)
{
return;
}
spaceLeft -= sizeof(TestError);
va_list args;
va_start(args, string);
int printedChars = vsnprintf(myNextError->message, spaceLeft, string, args);
va_end(args);
// if there isn't a reasonable amount of space left then just advance to end and stop printing errors
if (printedChars < (int)(spaceLeft - sizeof(TestError) - 64))
{
uintptr_t nextOffset = (uintptr_t(myNextError->message) + printedChars + sizeof(TestError) * 2 - 1);
nextOffset -= nextOffset % alignof(TestError);
TestError* next = (TestError*)(nextOffset);
myNextError->next = next;
}
else
{
myNextError->next = (TestError*)(myMessageSpace + MESSAGE_SPACE);
}
myNextError = myNextError->next;
}
TestFixture const* TestFixture::LinkTest(TestFixture* test)
{
test->myNextTest = TestFixture::ourFirstTest;
TestFixture::ourFirstTest = test;
return test;
}
//---------------------------------------------------------------------------------
// Standard / Example runners
//---------------------------------------------------------------------------------
static bool locExecuteTest(TestFixture* test, TestFixture::OutputMode output)
{
if (output == TestFixture::Verbose)
TestFixture::Printf("Running [%s/%s]", test->TestGroup(), test->TestName());
if (test->ExecuteTest())
{
if (output == TestFixture::Verbose)
TestFixture::Printf(": Passed %d out of %d tests\n", test->NumTests(), test->NumTests());
return true;
}
if (output != TestFixture::Silent)
{
if (output != TestFixture::Verbose)
TestFixture::Printf("[%s/%s]", test->TestGroup(), test->TestName());
TestFixture::Printf(": Failed %d out of %d tests\n", test->NumErrors(), test->NumTests());
for (TestError const* err = test->GetFirstError(), *e = test->GetLastError(); err != e; err = err->next)
{
TestFixture::Printf("%s\n", err->message);
}
}
return false;
}
void TestFixture::Printf(char const* string, ...)
{
char tempSpace[100];
va_list args;
va_start(args, string);
vsnprintf(tempSpace, sizeof(tempSpace), string, args);
va_end(args);
TestFixture::Print(tempSpace);
}
bool TestFixture::ExecuteAllTests(char const* groupFilter, char const* nameFilter, OutputMode output)
{
if (output != Silent)
{
if (groupFilter == nullptr && nameFilter == nullptr)
Printf("Running all tests.\n");
else if (groupFilter != nullptr && nameFilter == nullptr)
Printf("Running all tests in groups [%s].\n", groupFilter);
else if (groupFilter == nullptr && nameFilter != nullptr)
Printf("Running all tests named [%s].\n", nameFilter);
else
Printf("Running all tests named [%s/%s].\n", groupFilter, nameFilter);
}
int count = 0;
int passes = 0;
int fails = 0;
bool passed = true;
for (auto i = TestFixture::GetFirstTest(); i; i = i->GetNextTest())
{
bool matchGroup = groupFilter == nullptr || strcmp(groupFilter, i->TestGroup()) == 0;
bool matchName = nameFilter == nullptr || strcmp(nameFilter, i->TestName()) == 0;
if (matchGroup && matchName)
{
++count;
passed &= locExecuteTest(i, output);
passes += i->NumTests();
fails += i->NumErrors();
}
}
if (output != Silent)
{
if (count == 0)
Printf("Failed to find any tests.\n");
else if (passed)
Printf("%d Tests finished. All %d assertions are passing.\n", count, passes);
else
Printf("%d Tests finished, %d of %d assertions failed. Some tests are reporting errors.\n", count, fails, passes);
}
return passed;
}