-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test.h
72 lines (61 loc) · 1.25 KB
/
unit_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
#ifndef __UNIT_TEST_H__
#define __UNIT_TEST_H__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ERR_BUF_SIZE 1024
#define UNIT_START \
int main() { \
unsigned int tests_passed = 0; \
unsigned int tests_total; \
unsigned int fail = 0; \
char err_buf[ERR_BUF_SIZE];
#define UNIT_END \
end: \
tests_total = __COUNTER__; \
if (fail) { \
printf("\tfail\n%s\n", err_buf); \
fsync(STDOUT_FILENO); \
} \
printf("\nPassed %u tests from %u\n", tests_passed, tests_total); \
if (fail) { \
abort(); \
} else { \
return 0;\
} \
}
#define TEST_START(test_name) \
do { \
printf("%d %s",__COUNTER__ + 1, test_name); \
fsync(STDOUT_FILENO); \
} while(0)
#define TEST_END \
do { \
++tests_passed; \
puts("\tok"); \
fsync(STDOUT_FILENO); \
} while(0)
#define _STR(x) #x
#define STR(x) _STR(x)
#define LINE_STR STR(__LINE__)
#define UNIT_TEST_ERR(fmt, ...) \
do { \
snprintf(err_buf, ERR_BUF_SIZE, "Error: " fmt " on " \
__FILE__ ":" LINE_STR, ##__VA_ARGS__); \
fsync(STDOUT_FILENO); \
fail = 1; \
goto end; \
} while(0)
#define CHECK_IF(exp) \
do { \
if (!(exp)) { \
UNIT_TEST_ERR(#exp); \
}\
} while(0)
#define CHECK_EQUAL(a, b) \
do { \
if ((a) != (b)) { \
UNIT_TEST_ERR(#a " != " #b); \
} \
} while(0)
#endif /* __UNIT_TEST_H__ */