forked from jart/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.h
401 lines (361 loc) · 13.5 KB
/
test.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#ifndef TEST_TEST_H_
#define TEST_TEST_H_
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "blink/machine.h"
#define TEST(GROUP, NAME) \
void GROUP##_##NAME(void); \
__attribute__((__constructor__)) void GROUP##_##NAME##_init(void) { \
static struct Test test; \
test.func = GROUP##_##NAME; \
test.next = g_testing.tests; \
g_testing.tests = &test; \
} \
void GROUP##_##NAME(void)
#define ASSERT_EQ(WANT, GOT, ...) \
AssertionInt64(AssertEq, true, "ASSERT_EQ", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_EQ(WANT, GOT, ...) \
AssertionInt64(AssertEq, false, "EXPECT_EQ", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_NE(WANT, GOT, ...) \
AssertionInt64(AssertNe, true, "ASSERT_NE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_NE(WANT, GOT, ...) \
AssertionInt64(AssertNe, false, "EXPECT_NE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_LE(WANT, GOT, ...) \
AssertionInt64(AssertLe, true, "ASSERT_LE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_LE(WANT, GOT, ...) \
AssertionInt64(AssertLe, false, "EXPECT_LE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_GE(WANT, GOT, ...) \
AssertionInt64(AssertGe, true, "ASSERT_GE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_GE(WANT, GOT, ...) \
AssertionInt64(AssertGe, false, "EXPECT_GE", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_LT(WANT, GOT, ...) \
AssertionInt64(AssertLt, true, "ASSERT_LT", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_LT(WANT, GOT, ...) \
AssertionInt64(AssertLt, false, "EXPECT_LT", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_GT(WANT, GOT, ...) \
AssertionInt64(AssertGt, true, "ASSERT_GT", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_GT(WANT, GOT, ...) \
AssertionInt64(AssertGt, false, "EXPECT_GT", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_STREQ(WANT, GOT, ...) \
AssertionStr(AssertStreq, true, "ASSERT_STREQ", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define EXPECT_STREQ(WANT, GOT, ...) \
AssertionStr(AssertStreq, false, "EXPECT_STREQ", __FILE__, __LINE__, \
__FUNCTION__, WANT, #WANT, GOT, #GOT, " " __VA_ARGS__)
#define ASSERT_NOTNULL(GOT, ...) \
AssertionInt64(AssertNe, true, "ASSERT_NOTNULL", __FILE__, __LINE__, \
__FUNCTION__, 0, "NULL", (i64)(intptr_t)(GOT), #GOT, \
" " __VA_ARGS__)
#define EXPECT_NOTNULL(GOT, ...) \
AssertionInt64(AssertNe, false, "EXPECT_NOTNULL", __FILE__, __LINE__, \
__FUNCTION__, 0, "NULL", (i64)(intptr_t)(GOT), #GOT, \
" " __VA_ARGS__)
#define ASSERT_TRUE(GOT, ...) \
AssertionBool(true, true, __FILE__, __LINE__, __FUNCTION__, GOT, #GOT, \
" " __VA_ARGS__)
#define EXPECT_TRUE(GOT, ...) \
AssertionBool(true, false, __FILE__, __LINE__, __FUNCTION__, GOT, #GOT, \
" " __VA_ARGS__)
#define ASSERT_FALSE(GOT, ...) \
AssertionBool(false, true, __FILE__, __LINE__, __FUNCTION__, GOT, #GOT, \
" " __VA_ARGS__)
#define EXPECT_FALSE(GOT, ...) \
AssertionBool(false, false, __FILE__, __LINE__, __FUNCTION__, GOT, #GOT, \
" " __VA_ARGS__)
#define ASSERT_LDBL_EQ(WANT, GOT, ...) \
do { \
long double Got, Want; \
Got = GOT; \
Want = WANT; \
if (isnan(Got) || isnan(Want) || fabsl(Got - Want) > 0.00000001) { \
fprintf(stderr, "error:%s:%d: %s() ASSERT_LDBL_EQ failed:", __FILE__, \
__LINE__, __FUNCTION__); \
fprintf(stderr, " " __VA_ARGS__); \
fprintf(stderr, \
"\n\twant %Lg (%s)\n" \
"\tgot %Lg (%s)\n", \
Want, #WANT, Got, #GOT); \
exit(1); \
} \
} while (0)
#define SPAWN(METHOD) \
{ \
int child, _failed = g_testing.fails; \
ASSERT_NE(-1, (child = METHOD())); \
if (!child) {
#define EXITS(CODE) \
PARENT() \
WAIT(Exit, CODE)
#define TERMS(SIG) \
PARENT() \
WAIT(Term, SIG)
#define PARENT() \
_Exit(MAX(0, MIN(255, g_testing.fails - _failed))); \
}
#define WAIT(KIND, CODE) \
WaitFor##KIND(__FILE__, __LINE__, #CODE, CODE, child); \
}
struct Test {
struct Test *next;
void (*func)(void);
};
struct Tests {
int fails;
struct Test *tests;
} g_testing;
struct Garbage {
struct Garbage *next;
void *ptr;
} * g_garbage;
void SetUp(void);
void TearDown(void);
void *Gc(void *ptr) {
struct Garbage *g;
g = (struct Garbage *)malloc(sizeof(struct Garbage));
g->ptr = ptr;
g->next = g_garbage;
g_garbage = g;
return ptr;
}
void Collect(struct Garbage *g) {
if (!g) return;
Collect(g->next);
free(g->ptr);
free(g);
}
char *Format(const char *Format, ...) {
char *s;
va_list va;
s = (char *)Gc(malloc(64));
va_start(va, Format);
vsnprintf(s, 64, Format, va);
va_end(va);
return s;
}
static void AssertionInt64(bool pred(int64_t, int64_t), bool isfatal,
const char *test, const char *file, int line,
const char *func, int64_t want, const char *wantstr,
int64_t got, const char *gotstr, const char *fmt,
...) {
va_list va;
if (!pred(want, got)) {
int err = errno;
fprintf(stderr, "error:%s:%d: %s() %s failed:", file, line, func, test);
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
fprintf(stderr,
"\n\twant %" PRId64 " (%#" PRIx64 ") %s\n"
"\tgot %" PRId64 " (%#" PRIx64 ") %s\n"
"\terrno = %d (%s)\n",
want, want, wantstr, got, got, gotstr, err, strerror(err));
if (isfatal) {
exit(1);
} else {
++g_testing.fails;
}
}
}
static void AssertionStr(bool pred(const char *, const char *), bool isfatal,
const char *test, const char *file, int line,
const char *func, const char *want,
const char *wantstr, const char *got,
const char *gotstr, const char *fmt, ...) {
va_list va;
char *gotcopy;
char *wantcopy;
if (!pred(want, got)) {
fprintf(stderr, "error:%s:%d: %s() %s failed:", file, line, func, test);
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
// we should ideally display an escaped version, but this should be
// sufficient to not lose one's mind, when the strings are the same
// but one of them contains invisible ansi escape sequences.
gotcopy = strdup(got);
wantcopy = strdup(want);
for (long i = 0; gotcopy[i]; ++i) {
if (!isprint(gotcopy[i])) {
gotcopy[i] = '?';
}
}
for (long i = 0; wantcopy[i]; ++i) {
if (!isprint(wantcopy[i])) {
wantcopy[i] = '?';
}
}
fprintf(stderr,
"\n"
"\twant %s (%s)\n"
"\tgot %s (%s)\n",
wantcopy, wantstr, gotcopy, gotstr);
free(gotcopy);
free(wantcopy);
if (isfatal) {
exit(1);
} else {
++g_testing.fails;
}
}
}
static void AssertionBool(bool need, bool isfatal, const char *file, int line,
const char *func, bool got, const char *gotstr,
const char *fmt, ...) {
va_list va;
if (got != need) {
fprintf(stderr, "error:%s:%d: %s() ASSERT_%s failed:", file, line, func,
need ? "TRUE" : "FALSE");
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
fprintf(stderr, "\n\t%s\n", gotstr);
if (isfatal) {
exit(1);
} else {
++g_testing.fails;
}
}
}
static bool AssertStreq(const char *want, const char *got) {
return !strcmp(want, got);
}
static bool AssertEq(int64_t want, int64_t got) {
return want == got;
}
static bool AssertNe(int64_t want, int64_t got) {
return want != got;
}
static bool AssertLe(int64_t want, int64_t got) {
return want <= got;
}
static bool AssertGe(int64_t want, int64_t got) {
return want >= got;
}
static bool AssertLt(int64_t want, int64_t got) {
return want < got;
}
static bool AssertGt(int64_t want, int64_t got) {
return want > got;
}
static void WaitForExit(const char *file, int line, const char *code, int rc,
int pid) {
int ws;
char host[64];
ASSERT_NE(-1, wait(&ws));
if (WIFEXITED(ws)) {
if (WEXITSTATUS(ws) == rc) {
return;
}
fprintf(stderr,
"%s:%d: test failed\n"
"\tEXITS(%s)\n"
"\t want WEXITSTATUS(%d)\n"
"\t got WEXITSTATUS(%d)\n",
file, line, code, rc, WEXITSTATUS(ws));
} else if (WIFSIGNALED(ws)) {
fprintf(stderr,
"%s:%d: test failed\n"
"\tEXITS(%s)\n"
"\t want _Exit(%d)\n"
"\t got WTERMSIG(%s)\n",
file, line, code, rc, strsignal(WTERMSIG(ws)));
} else if (WIFSTOPPED(ws)) {
fprintf(stderr,
"%s:%d: test failed\n"
"\tEXITS(%s)\n"
"\t want _Exit(%d)\n"
"\t got WSTOPSIG(%s)\n",
file, line, code, rc, strsignal(WSTOPSIG(ws)));
} else {
fprintf(stderr,
"%s:%d: test failed\n"
"\tEXITS(%s)\n"
"\t want _Exit(%d)\n"
"\t got ws=%#x\n",
file, line, code, rc, ws);
}
if (gethostname(host, sizeof(host))) {
strcpy(host, "unknown");
}
fprintf(stderr, "\t%s\n", host);
exit(1);
}
static void WaitForTerm(const char *file, int line, const char *code, int sig,
int pid) {
int ws;
char host[64];
ASSERT_NE(-1, waitpid(pid, &ws, 0));
if (WIFSIGNALED(ws)) {
if (WTERMSIG(ws) == sig) {
return;
}
fprintf(stderr,
"%s:%d: test failed\n"
"\tTERMS(%s)\n"
"\t want WTERMSIG(%s)\n"
"\t got WTERMSIG(%s)\n",
file, line, code, strsignal(sig), strsignal(WTERMSIG(ws)));
} else if (WIFEXITED(ws)) {
fprintf(stderr,
"%s:%d: test failed\n"
"\tTERMS(%s)\n"
"\t want WTERMSIG(%s)\n"
"\t got _Exit(%d)\n",
file, line, code, strsignal(sig), WEXITSTATUS(ws));
} else if (WIFSTOPPED(ws)) {
fprintf(stderr,
"%s:%d: test failed\n"
"\tTERMS(%s)\n"
"\t want WTERMSIG(%s)\n"
"\t got WSTOPSIG(%s)\n",
file, line, code, strsignal(sig), strsignal(WSTOPSIG(ws)));
} else {
fprintf(stderr,
"%s:%d: test failed\n"
"\tTERMS(%s)\n"
"\t want WTERMSIG(%s)\n"
"\t got ws=%#x\n",
file, line, code, strsignal(sig), ws);
}
if (gethostname(host, sizeof(host))) {
strcpy(host, "unknown");
}
fprintf(stderr, "\t%s\n", host);
exit(1);
}
_Noreturn void TerminateSignal(struct Machine *m, int sig) {
abort();
}
int main(int argc, char *argv[]) {
struct Test *test;
for (test = g_testing.tests; test; test = test->next) {
SetUp();
test->func();
TearDown();
Collect(g_garbage);
}
return g_testing.fails;
}
#endif /* TEST_TEST_H_ */