-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdrive.h
351 lines (313 loc) · 10.3 KB
/
testdrive.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
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
#pragma once
/*
Copyright (c) 2021 Martin Evald
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TD_VERSION 1.1.1
#ifndef TD_MAX_SECTIONS
#define TD_MAX_SECTIONS 128
#endif
#ifndef TD_MAX_NESTING
#define TD_MAX_NESTING 32
#endif
#ifndef TD_MAX_ASSERTS
#define TD_MAX_ASSERTS 256
#endif
#define TD_TEST_INFO(NAME) td__test_ ## NAME
#define TD_TEST_FUNCTION(NAME) td__test_fn_ ## NAME
#define TD_ALLOC_SECTIONS(CONTEXT)\
if (!CONTEXT->sections) {\
CONTEXT->sections = calloc(\
1,\
sizeof(struct td_test_context) * TD_MAX_SECTIONS\
);\
}
#define TD_EVENT(EVENT_, TEST_)\
if (!td__retracing) {\
td_listener(EVENT_, TEST_, td__path[td__level], __FILE__, __LINE__);\
}
// Note: In order to be able to assert using the REQUIRE() macro, and proceed
// to run the next test section on failure, a combination of setjmp() and
// longjmp() are employed. A failing assertion will longjmp() back to the
// location referenced by the td__continue pointer, from where the conditional
// executed when returning from setjmp() will advance to the next sequence (if
// there were any unexecuted sections preceeding the failing assertion), or
// break out from the current scope.
#define TD_BREAK_HERE(CONTINUE_)\
if (setjmp(CONTINUE_) == 1) {\
if (++td__path[td__level] > td__test_ptr->section_idx) {\
break;\
}\
}
#define TD_FIXTURE(NAME, DESCRIPTION)\
struct td_test_context TD_TEST_INFO(NAME) = {\
NULL,\
#NAME,\
DESCRIPTION\
};\
bool TD_TEST_FUNCTION(NAME)(\
volatile struct td_test_context* td__test_ptr\
) {\
TD_ALLOC_SECTIONS(td__test_ptr);\
/* Local variables must be volatile if accessed after setjmp(). */\
volatile size_t td__path[TD_MAX_NESTING] = { 0 };\
volatile size_t td__visited[TD_MAX_NESTING] = { 0 };\
volatile size_t td__level = 0;\
volatile struct td_test_context* td__root = td__test_ptr;\
volatile bool td__retracing = false;\
volatile size_t td__target_level = 0;\
volatile size_t td__assert_count = 0;\
TD_EVENT(TD_TEST_START, td__test_ptr);\
jmp_buf td__begin;\
jmp_buf* td__continue = &td__begin;\
do {\
TD_BREAK_HERE(td__begin)\
td__test_ptr->section_idx = 0;\
#define TD_SECTION(DESCRIPTION)\
TD_EVENT(TD_SECTION_PRE, td__test_ptr);\
assert(td__test_ptr->section_idx < TD_MAX_SECTIONS);\
if (\
td__test_ptr->sections[++(td__test_ptr->section_idx)].name\
== NULL\
) {\
td__test_ptr->sections[td__test_ptr->section_idx]\
= (struct td_test_context) {\
td__test_ptr,\
"",\
DESCRIPTION\
};\
}\
if (\
td__path[td__level] != td__test_ptr->section_idx) {\
TD_EVENT(\
TD_SECTION_SKIP,\
&td__test_ptr->sections[td__test_ptr->section_idx]\
);\
} else {\
{\
if (!td__retracing) {\
td__retracing = true;\
td__target_level = td__level + 1;\
td__level = 0;\
td__test_ptr = td__root;\
longjmp(td__begin, 2);\
}\
jmp_buf* td__parent_continue = td__continue;\
td__test_ptr = &td__test_ptr->sections[td__path[td__level++]];\
TD_ALLOC_SECTIONS(td__test_ptr);\
if (td__retracing && td__level == td__target_level) {\
td__retracing = false;\
}\
TD_EVENT(TD_SECTION_START, td__test_ptr);\
jmp_buf td__section_begin;\
td__continue = &td__section_begin;\
do {\
TD_BREAK_HERE(td__section_begin)\
td__test_ptr->section_idx = 0;\
#define TD_END_SECTION\
} while (td__path[td__level]++ < td__test_ptr->section_idx);\
TD_EVENT(TD_SECTION_END, td__test_ptr);\
td__test_ptr = td__test_ptr->parent;\
td__continue = td__parent_continue;\
td__path[td__level] = 0;\
td__level--;\
}\
}
#define TD_END_FIXTURE\
} while (td__test_ptr->section_idx > td__path[td__level]++);\
TD_EVENT(TD_TEST_END, td__test_ptr);\
{\
return td__root->num_failed;\
}\
}
#define TD_EXTERN_FIXTURE(NAME)\
extern struct td_test_context TD_TEST_INFO(NAME);\
void TD_TEST_FUNCTION(NAME)(struct td_test*, const size_t)
#define TD_ASSERT_LOGIC(CONDITION, SUCCESS, FAILURE)\
if (td__path[td__level] == 0) {\
td__test_ptr->assertions[td__test_ptr->assert_count++] = #CONDITION;\
TD_EVENT(TD_ASSERT_PRE, td__test_ptr);\
if (!(CONDITION)) {\
FAILURE;\
} else {\
SUCCESS;\
}\
} else {\
CONDITION;\
}
#define TD_REQUIRE(CONDITION) TD_ASSERT_LOGIC(\
CONDITION, ({\
td__test_ptr->assert_success[td__test_ptr->assert_count - 1]\
= true;\
TD_EVENT(TD_ASSERT_SUCCESS, td__test_ptr)\
}), ({\
td__test_ptr->assert_success[td__test_ptr->assert_count - 1]\
= false;\
td__root->num_failed++;\
TD_EVENT(TD_ASSERT_FAILURE, td__test_ptr);\
longjmp(*td__continue, 1);\
})\
)
#define TD_REQUIRE_FAIL(CONDITION) TD_ASSERT_LOGIC(\
CONDITION, ({\
td__test_ptr->assert_success[td__test_ptr->assert_count - 1]\
= false;\
td__root->num_failed++;\
TD_EVENT(TD_ASSERT_FAILURE, td__test_ptr);\
}), ({\
td__test_ptr->assert_success[td__test_ptr->assert_count - 1]\
= true;\
TD_EVENT(TD_ASSERT_SUCCESS, td__test_ptr);\
})\
);\
longjmp(*td__continue, 1);
#define TD_SET_LISTENER(LISTENER_FUNC) td_listener = LISTENER_FUNC
#define TD_RUN_TEST(NAME) TD_TEST_FUNCTION(NAME)(&TD_TEST_INFO(NAME))
#ifndef TD_DEFAULT_LISTENER
#define TD_DEFAULT_LISTENER td_console_listener
#endif
#ifndef TD_ONLY_PREFIXED_MACROS
#define FIXTURE(...) TD_FIXTURE(__VA_ARGS__)
#define SECTION(...) TD_SECTION(__VA_ARGS__)
#define END_SECTION TD_END_SECTION
#define END_FIXTURE TD_END_FIXTURE
#define EXTERN_FIXTURE(...) TD_EXTERN_FIXTURE(__VA_ARGS__)
#define REQUIRE(...) TD_REQUIRE(__VA_ARGS__)
#define REQUIRE_FAIL(...) TD_REQUIRE_FAIL(__VA_ARGS__)
#define RUN_TEST(...) TD_RUN_TEST(__VA_ARGS__)
#endif
enum td_event {
TD_TEST_START,
TD_SECTION_PRE,
TD_SECTION_SKIP,
TD_SECTION_START,
TD_SECTION_END,
TD_ASSERT_PRE,
TD_ASSERT_FAILURE,
TD_ASSERT_SUCCESS,
TD_TEST_END
};
struct td_test_context {
volatile struct td_test_context* parent;
const char* name;
const char* description;
size_t section_idx;
size_t assert_count;
bool assert_success[TD_MAX_ASSERTS];
const char* assertions[TD_MAX_ASSERTS];
size_t num_failed;
volatile struct td_test_context* sections;
};
static void td_console_listener(
enum td_event event,
volatile struct td_test_context* test,
size_t sequence,
const char* file,
size_t line
);
static void(*td_listener)(
enum td_event event,
volatile struct td_test_context* test,
size_t sequence,
const char* file,
size_t line
) = TD_DEFAULT_LISTENER;
// Default console listener implementation below this line.
static const char* td__indent(size_t indent) {
static char buffer[256] = { 0 };
for (size_t i = 0; i < indent; ++i) {
memcpy(&buffer[i * 3], "| ", 3);
}
buffer[indent * 3] = '\0';
return buffer;
}
static void td__print_ratio(
size_t indent,
size_t successful,
size_t total
) {
fprintf(
stdout,
"%s+- %ld/%ld assertions succeded.\n",
td__indent(indent),
successful,
total
);
}
static size_t td__count_success(volatile struct td_test_context* test) {
size_t count = 0;
for (size_t i = 0; i < test->assert_count; ++i) {
count += test->assert_success[i];
}
return count;
}
static void td_console_listener(
enum td_event event,
volatile struct td_test_context* test,
size_t sequence,
const char* file,
size_t line
) {
static size_t indent = 0;
switch (event) {
case TD_TEST_START:
fprintf(stdout, "Running test: \"%s\"\n", test->description);
indent++;
break;
case TD_SECTION_PRE:
break;
case TD_SECTION_SKIP:
break;
case TD_SECTION_START:
fprintf(
stdout,
"%sRunning section: \"%s\"\n",
td__indent(indent),
test->description
);
indent++;
break;
case TD_SECTION_END:
indent--;
td__print_ratio(indent, td__count_success(test), test->assert_count);
break;
case TD_ASSERT_PRE:
break;
case TD_ASSERT_FAILURE:
fprintf(
stdout,
"%sFailed assertion: %s (%s:%ld)\n",
td__indent(indent),
test->assertions[test->assert_count - 1],
file,
line
);
break;
case TD_ASSERT_SUCCESS:
break;
case TD_TEST_END:
indent--;
td__print_ratio(indent, td__count_success(test), test->assert_count);
break;
}
}