-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.h
58 lines (45 loc) · 1.19 KB
/
tests.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
// Copyright 2019–2022, Taeber Rapczak <[email protected]>
// MIT License https://opensource.org/licenses/MIT
#ifndef TESTS_H
#define TESTS_H
typedef _Bool (*TestCase)(void);
#ifndef MAX_TESTS
#define MAX_TESTS 256
#endif
char *names[MAX_TESTS];
TestCase cases[MAX_TESTS];
int numCases;
#define MACROSTRING(X) #X
#define STRING(X) MACROSTRING(X)
#define test(name) \
_Bool test_ ## name(void); \
__attribute__((constructor)) \
void test_ ## name ## _init(void) { \
names[numCases] = __FILE__ "@" STRING(__LINE__) ":" #name; \
cases[numCases] = test_##name; \
numCases++; \
} \
_Bool test_##name(void)
#ifdef TESTS_H_IMPLEMENTATION
#include <stdio.h>
int main() {
int passed = 0,
failed = 0;
for (int i = 0; i < numCases; i++) {
if (cases[i]()) {
passed++;
continue;
}
fprintf(stderr, "FAIL: %s\n", names[i]);
failed++;
}
if (failed > 0) {
putc('\n', stderr);
}
fprintf(stderr, "%3d passed\n", passed);
fprintf(stderr, "%3d failed\n", failed);
fprintf(stderr, "%3d total\n", numCases);
return failed;
}
#endif//TESTS_H_IMPLEMENTATION
#endif//TESTS_H