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
|
#include "test.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <setjmp.h>
#include <signal.h>
kit_tests_list_t kit_tests_list = { 0 };
static void report(int i, int line, int ok) {
int const n = kit_tests_list.tests[i].assertions++;
if (n >= KIT_TEST_ASSERTIONS_LIMIT)
return;
kit_tests_list.tests[i].line[n] = line;
kit_tests_list.tests[i].status[n] = ok;
}
static long long ns_to_ms(long long ns) {
return (ns + 500000) / 1000000;
}
static long long sec_to_ms(long long sec) {
return 1000 * sec;
}
enum { white, blue, light, yellow, red, green };
char const *const color_codes[] = {
[white] = "\x1b[38m", [blue] = "\x1b[34m", [light] = "\x1b[37m",
[yellow] = "\x1b[33m", [red] = "\x1b[31m", [green] = "\x1b[32m"
};
static int print_color(int c) {
return printf("%s", color_codes[c]);
}
void kit_test_register(char const *name, char const *file,
kit_test_run_fn fn) {
int n = kit_tests_list.size++;
if (n < KIT_TESTS_SIZE_LIMIT) {
kit_tests_list.tests[n].test_fn = fn;
kit_tests_list.tests[n].test_name = name;
kit_tests_list.tests[n].test_file = file;
kit_tests_list.tests[n].assertions = 0;
}
}
static jmp_buf kit_test_restore_execution;
static int const signums[] = { SIGINT, SIGILL, SIGABRT,
SIGFPE, SIGSEGV, SIGTERM };
static char const *const signames[] = {
[SIGINT] = "Interactive attention signal",
[SIGILL] = "Illegal instruction",
[SIGABRT] = "Abnormal termination",
[SIGFPE] = "Erroneous arithmetic operation",
[SIGSEGV] = "Invalid access to storage",
[SIGTERM] = "Termination request"
};
static void handle_signal(int signum) {
longjmp(kit_test_restore_execution, signum);
}
static void setup_signals() {
for (int i = 0; i < sizeof signums / sizeof *signums; i++) {
#if defined(_WIN32) && !(defined __CYGWIN__)
signal(signums[i], handle_signal);
#else
struct sigaction action;
memset(&action, 0, sizeof action);
action.sa_handler = handle_signal;
sigaction(signums[i], &action, NULL);
#endif
}
}
static int run_test(volatile int i) {
int signum = setjmp(kit_test_restore_execution);
if (signum != 0) {
kit_tests_list.tests[i].signal = signum;
return 0;
}
kit_tests_list.tests[i].test_fn(i, report);
return 1;
}
int kit_run_tests(int argc, char **argv) {
setup_signals();
int success_count = 0;
int fail_assertion_count = 0;
int total_assertion_count = 0;
int status = 0;
int quiet = 0;
int no_color = 0;
int line_width = 20;
int carriage_return = 1;
for (int i = 0; i < argc; i++)
if (strcmp("--no-term-color", argv[i]) == 0)
no_color = 1;
else if (strcmp("--no-carriage-return", argv[i]) == 0)
carriage_return = 0;
else if (strcmp("--quiet", argv[i]) == 0)
quiet = 1;
if (quiet)
no_color = 1;
char const *file = NULL;
ptrdiff_t file_root = -1;
for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT;
i++) {
int const l = 2 + (int) strlen(kit_tests_list.tests[i].test_name);
if (line_width < l)
line_width = l;
}
if (kit_tests_list.size > 0) {
char const *const s = kit_tests_list.tests[0].test_file;
for (int j = 1;
j < kit_tests_list.size && j < KIT_TESTS_SIZE_LIMIT; j++) {
if (strcmp(s, kit_tests_list.tests[j].test_file) == 0)
continue;
int k = 0;
for (; s[k] != '\0' &&
kit_tests_list.tests[j].test_file[k] != '\0' &&
s[k] == kit_tests_list.tests[j].test_file[k];
k++) { }
if (file_root == -1 || file_root > k)
file_root = k;
}
if (file_root == -1) {
for (int i = 0; s[i] != '\0'; i++)
if (s[i] == '/' || s[i] == '\\')
file_root = i + 1;
}
}
for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT;
i++) {
if (file == NULL ||
strcmp(file, kit_tests_list.tests[i].test_file) != 0) {
if (file != NULL)
quiet || printf("\n");
file = kit_tests_list.tests[i].test_file;
no_color || print_color(blue);
quiet || printf("*");
no_color || print_color(white);
quiet || printf(" %s\n", file + file_root);
}
!carriage_return || no_color || print_color(yellow);
carriage_return || no_color || print_color(light);
quiet || printf("` %s ", kit_tests_list.tests[i].test_name);
quiet || fflush(stdout);
struct timespec begin, end;
timespec_get(&begin, TIME_UTC);
int test_status = run_test(i);
timespec_get(&end, TIME_UTC);
int duration = (int) (ns_to_ms(end.tv_nsec - begin.tv_nsec) +
sec_to_ms(end.tv_sec - begin.tv_sec));
for (int j = 0; j < kit_tests_list.tests[i].assertions; j++)
if (kit_tests_list.tests[i].status[j] == 0) {
fail_assertion_count++;
test_status = 0;
}
if (kit_tests_list.tests[i].assertions >
KIT_TEST_ASSERTIONS_LIMIT)
test_status = 0;
total_assertion_count += kit_tests_list.tests[i].assertions;
if (carriage_return) {
no_color || print_color(light);
quiet || printf("\r` %s ", kit_tests_list.tests[i].test_name);
}
int const l = (int) strlen(kit_tests_list.tests[i].test_name);
quiet || printf("%*c", line_width - l, ' ');
if (test_status == 0) {
no_color || print_color(red);
quiet || printf("FAIL");
no_color || print_color(light);
duration == 0 || quiet || printf(" %d ms", duration);
quiet || printf("\n");
status = 1;
} else {
no_color || print_color(green);
quiet || printf("OK");
no_color || print_color(light);
duration == 0 || quiet || printf(" %d ms", duration);
quiet || printf("\n");
success_count++;
}
}
no_color || print_color(white);
quiet || printf("\n%d of %d tests passed.\n", success_count,
kit_tests_list.size);
quiet || printf("%d of %d assertions passed.\n\n",
total_assertion_count - fail_assertion_count,
total_assertion_count);
no_color || print_color(light);
if (status != 0) {
for (int i = 0;
i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) {
if (kit_tests_list.tests[i].signal != 0) {
int signum = kit_tests_list.tests[i].signal;
if (signum >= 0 &&
signum < sizeof signames / sizeof *signames &&
signames[signum] != NULL)
quiet ||
printf("Signal \"%s\" (%d) for \"%s\" in \"%s\"!\n",
signames[signum], signum,
kit_tests_list.tests[i].test_name,
kit_tests_list.tests[i].test_file);
else
quiet ||
printf("Unknown signal (%d) for \"%s\" in \"%s\"!\n",
signum, kit_tests_list.tests[i].test_name,
kit_tests_list.tests[i].test_file);
}
if (kit_tests_list.tests[i].assertions >
KIT_TEST_ASSERTIONS_LIMIT)
quiet || printf("Too many assertions for \"%s\" in \"%s\"!\n",
kit_tests_list.tests[i].test_name,
kit_tests_list.tests[i].test_file);
else
for (int j = 0; j < kit_tests_list.tests[i].assertions; j++)
if (!kit_tests_list.tests[i].status[j])
quiet || printf("Assertion on line %d in \"%s\" failed\n",
kit_tests_list.tests[i].line[j],
kit_tests_list.tests[i].test_file);
}
quiet || printf("\n");
}
if (kit_tests_list.size > KIT_TESTS_SIZE_LIMIT) {
quiet || printf("Too many tests!\n\n");
status = 1;
}
if (status == 0) {
no_color || print_color(green);
quiet || printf("OK\n");
} else {
no_color || print_color(red);
quiet || printf("FAILED\n");
}
no_color || print_color(white);
return status;
}
|