-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.h
93 lines (77 loc) · 2.53 KB
/
unit.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
#ifndef _edna_unit_
#define _edna_unit_
#include <setjmp.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <util.h>
#define unit_catch() sigsetjmp(unit_checkpoint, 1)
#define unit_error_fmt(...) do { \
char buf[256]; \
snprintf(buf, 256, __VA_ARGS__); \
unit_error(buf); \
} while (0)
#define unit_fail_fmt(...) do { \
char buf[256]; \
snprintf(buf, 256, __VA_ARGS__); \
unit_fail(buf); \
} while (0)
#define unit_list(...) ((void (*[])()){__VA_ARGS__, 0x0})
#define try(EXPR) do { \
unit_set_expr(#EXPR,__LINE__); \
EXPR; \
unit_unset_expr(); \
} while (0)
#define ok(EXPR) okf(EXPR, "assertion false: \"%s\" ", #EXPR);
#define okm(EXPR, MSG) okf(EXPR, MSG "%s", "")
#define okf(EXPR, FMT, ...) do { \
char *unit_msg = asprintf(FMT " (line %d)", \
__VA_ARGS__, __LINE__); \
_unit_ok(EXPR, unit_msg); \
} while (0)
#define _unit_ok(EXPR, MSG) do { \
bool unit_res; \
unit_set_expr(#EXPR, __LINE__); \
unit_res = !!(EXPR); \
if (!unit_res) { \
unit_fail(MSG); \
unit_yield(); \
} \
unit_unset_expr(); \
} while (0)
#define expect(VAL, EXPR) do { \
unit_set_expr(#EXPR, __LINE__); \
long unit_res=(EXPR); \
long unit_val=(VAL); \
if (unit_res != unit_val) { \
raise(SIGTRAP); \
unit_fail_fmt("expected %s, got %ld" \
" (expr \"%s\", line %d)", \
#VAL, unit_res, \
#EXPR, __LINE__); \
unit_yield(); \
} \
unit_unset_expr(); \
} while (0)
struct unit_test {
char *msg;
void (**fun)();
void *ctx;
};
extern int unit_opt_error_fd;
extern unsigned unit_opt_timeout;
extern unsigned unit_opt_flakiness;
extern unsigned unit_opt_test_num;
extern bool unit_failed;
extern bool unit_has_init;
extern jmp_buf unit_checkpoint;
int unit_init();
void unit_set_expr(char *, int);
void unit_unset_expr(void);
void unit_perror(char *);
void unit_error(char *);
void unit_fail(char *);
int unit_parse_argv(size_t argc, char **argv);
int unit_run_tests(struct unit_test *, size_t);
void unit_yield(void) __attribute__((noreturn));
#endif