-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.h
72 lines (62 loc) · 1.7 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
#ifndef _test_
#define _test_
#include <signal.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define unit_begin(name, ln, ...) do { \
if (!unit_has_init) unit_init(); \
unit_set_lineno(ln); \
unit_##name##_init(__VA_ARGS__); \
alarm(3); \
} while (0)
#define ok(TEST) do { \
unit_begin(ok,__LINE__,#TEST); \
int unit_result = (TEST); \
alarm(0); \
if (unit_result) break; \
unit_longjmp(); \
} while (0)
#define try(TEST) do { \
unit_begin(try,__LINE__,#TEST); \
TEST; \
alarm(0); \
} while (0)
#define expect(VAL, TEST) expectm(VAL, TEST, #TEST)
#define expectf(VAL, TEST, ...) do { \
char buf[256]; \
snprintf(buf, 256, __VA_ARGS__);\
expectm(VAL, TEST, buf); \
} while (0)
#define expectm(VAL, TEST, MSG) do { \
unit_begin(expect, __LINE__, VAL, MSG); \
int unit_result = (TEST); \
alarm(0); \
if (unit_expected(unit_result)) break; \
else unit_longjmp(); \
} while (0)
struct test {
char *msg;
void (*setup)();
void (*test)();
void (*cleanup)();
void *ctx;
};
// must be defined
extern struct test unit_tests[];
extern char unit_filename[];
// provided
extern bool unit_has_init;
extern size_t unit_total_failures;
void unit_longjmp(void);
void unit_fail(void);
void unit_ok_init(char *);
void unit_expect_init(int, char *);
bool unit_expected(int);
void unit_set_lineno(int);
void unit_try_init(char *);
void unit_init(void);
#endif