-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtl.h
139 lines (113 loc) · 3.27 KB
/
mtl.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
/**
* MTL: Miles's Test Library
*/
#ifndef MTL_H
#define MTL_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <getopt.h>
#define MTL_MAX_MESSAGE_SIZE 4096
/* MTL_STR() stringifies the given macro parameter, and MTL_STR_() is a helper
* for MTL_STR() */
#define MTL_STR_(X) #X
#define MTL_STR(X) MTL_STR_(X)
#define MTL_COLOR_RED "\033[91m"
#define MTL_COLOR_GREEN "\033[92m"
#define MTL_COLOR_RESET "\033[0m"
#define MTL_ASSERT(CONDITION) \
do { \
mtl_successful = (CONDITION); \
if (mtl_successful) { \
snprintf(mtl_message, MTL_MAX_MESSAGE_SIZE, MTL_COLOR_GREEN "%s" MTL_COLOR_RESET ": ok" "\n", __func__); \
} else { \
snprintf(mtl_message, MTL_MAX_MESSAGE_SIZE, MTL_COLOR_RED "%s" MTL_COLOR_RESET ": not ok (%s)\n", __func__, MTL_STR(CONDITION)); \
return; \
} \
} while (0)
#define MTL_FAIL(MESSAGE) \
do { \
mtl_successful = false; \
snprintf(mtl_message, MTL_MAX_MESSAGE_SIZE, MTL_COLOR_RED "%s" MTL_COLOR_RESET ": not ok (%s)\n", __func__, MESSAGE); \
return; \
} while (0)
#define MTL_MAIN(TESTS...) \
int main(int argc, char **argv) { \
mtl_function tests[] = {TESTS, NULL}; \
return mtl_main(argc, argv, tests); \
}
// Declare a new test:
#define MTL_TEST(NAME) static void NAME(void)
typedef void (*mtl_function)(void);
// Global variables:
static bool mtl_successful = true;
static unsigned mtl_number_of_failures = 0;
static unsigned mtl_number_of_tests = 0;
static char mtl_message[MTL_MAX_MESSAGE_SIZE] = {0};
static bool mtl_verbose = false;
static bool mtl_fail_fast = false;
static void mtl_run_test(mtl_function test) {
// Reset globals before test, because a test might not change them if
// `MTL_ASSERT` is never called inside the test.
mtl_successful = true;
mtl_message[0] = '\0';
test();
if (mtl_successful) {
fprintf(stderr, "%s", mtl_message);
} else {
mtl_number_of_failures += 1;
fprintf(stderr, "%s", mtl_message);
}
}
static void mtl_run_tests(mtl_function tests[]) {
mtl_number_of_failures = 0;
mtl_number_of_tests = 0;
for (unsigned i = 0; tests[i] != NULL; i += 1) {
mtl_run_test(tests[i]);
mtl_number_of_tests += 1;
if (mtl_fail_fast && mtl_number_of_failures > 0) {
break;
}
}
}
static int mtl_main(int argc, char **argv, mtl_function tests[]) {
static const char short_options[] = "hvf";
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"fail-fast", no_argument, 0, 'f'},
{0, 0, 0, 0},
};
static const char help[] = \
"Options:\n" \
" --help (-h): show this message\n" \
" --verbose (-v): more chatter\n" \
" --fail-fast (-f): exit upon first failure\n";
int option = 0;
while ((option = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (option) {
case 'h':
fprintf(stderr, "%s", help);
return 0;
case 'v':
mtl_verbose = true;
break;
case 'f':
mtl_fail_fast = true;
break;
default:
fprintf(stderr, "%s", help);
return 2;
}
}
mtl_run_tests(tests);
if (mtl_verbose) {
fprintf(stderr, "\n%u of %u (%.1lf%%) ok\n",
mtl_number_of_tests - mtl_number_of_failures,
mtl_number_of_tests,
100.0 * (mtl_number_of_tests - mtl_number_of_failures) / mtl_number_of_tests
);
}
return mtl_number_of_failures == 0 ? 0 : 1;
}
#endif