]>
Commit | Line | Data |
---|---|---|
9b0a0ba9 OZ |
1 | /* |
2 | * BIRD -- Unit Test Framework (BIRD Test) | |
3 | * | |
4 | * Can be freely distributed and used under the terms of the GNU GPL. | |
5 | */ | |
6 | ||
7 | #ifndef _BIRDTEST_H_ | |
8 | #define _BIRDTEST_H_ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <stdint.h> | |
13 | #include <string.h> | |
14 | #include <errno.h> | |
15 | #include <sys/types.h> | |
16 | ||
17 | #include "nest/bird.h" | |
18 | ||
19 | ||
20 | extern int bt_result; | |
21 | extern int bt_suite_result; | |
22 | extern char bt_out_fmt_buf[1024]; | |
23 | ||
24 | extern uint bt_verbose; | |
25 | #define BT_VERBOSE_NO 0 | |
26 | #define BT_VERBOSE_SUITE 1 | |
27 | #define BT_VERBOSE_SUITE_CASE 2 | |
28 | #define BT_VERBOSE_ABSOLUTELY_ALL 3 | |
29 | ||
30 | extern const char *bt_filename; | |
31 | extern const char *bt_test_id; | |
32 | ||
33 | void bt_init(int argc, char *argv[]); | |
34 | int bt_exit_value(void); | |
067f69a5 | 35 | void bt_reset_suite_case_timer(void); |
9b0a0ba9 | 36 | int bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...); |
bb001af0 MM |
37 | static inline u64 bt_random(void) |
38 | { return ((u64) random() & 0xffffffff) | ((u64) random() << 32); } | |
9b0a0ba9 | 39 | |
3cf91fb9 OZ |
40 | static inline u32 bt_random_n(u32 max) |
41 | { return random() % max; } | |
42 | ||
43 | ||
9b0a0ba9 OZ |
44 | void bt_log_suite_result(int result, const char *fmt, ...); |
45 | void bt_log_suite_case_result(int result, const char *fmt, ...); | |
46 | ||
dc139fb6 | 47 | #define BT_TIMEOUT 60 /* Default timeout in seconds */ |
9b0a0ba9 OZ |
48 | #define BT_FORKING 1 /* Forking is enabled in default */ |
49 | ||
bb001af0 | 50 | #define BT_RANDOM_SEED 0x5097d2bb |
9b0a0ba9 OZ |
51 | |
52 | #define BT_BUFFER_SIZE 10000 | |
53 | ||
54 | #define BT_PROMPT_GREEN "\e[1;32m" | |
55 | #define BT_PROMPT_RED "\e[1;31m" | |
56 | #define BT_PROMPT_NORMAL "\e[0m" | |
57 | #define BT_PROMPT_OK " [" BT_PROMPT_GREEN " OK " BT_PROMPT_NORMAL "] " | |
58 | #define BT_PROMPT_OK_NO_COLOR " [" " OK " "] " | |
59 | #define BT_PROMPT_FAIL " [" BT_PROMPT_RED "FAIL" BT_PROMPT_NORMAL "] " | |
60 | #define BT_PROMPT_FAIL_NO_COLOR " [" "FAIL" "] " | |
61 | #define BT_PROMPT_OK_FAIL_STRLEN 8 /* strlen ' [FAIL] ' */ | |
62 | ||
1322e205 MM |
63 | static inline int bt_test_fn_noarg(const void *cp) { return ((int (*)(void)) cp)(); } |
64 | ||
9b0a0ba9 OZ |
65 | #define bt_test_suite(fn, dsc, ...) \ |
66 | bt_test_suite_extra(fn, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__) | |
67 | ||
68 | #define bt_test_suite_extra(fn, f, t, dsc, ...) \ | |
1322e205 | 69 | bt_test_suite_base(bt_test_fn_noarg, #fn, fn, f, t, dsc, ##__VA_ARGS__) |
9b0a0ba9 OZ |
70 | |
71 | #define bt_test_suite_arg(fn, arg, dsc, ...) \ | |
72 | bt_test_suite_arg_extra(fn, arg, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__) | |
73 | ||
74 | #define bt_test_suite_arg_extra(fn, arg, f, t, dsc, ...) \ | |
75 | bt_test_suite_base(fn, #fn, arg, f, t, dsc, ##__VA_ARGS__) | |
76 | ||
77 | #define bt_abort() \ | |
78 | bt_abort_msg("Aborted at %s:%d", __FILE__, __LINE__) | |
79 | ||
80 | #define bt_abort_msg(format, ...) \ | |
81 | do \ | |
82 | { \ | |
83 | bt_log(format, ##__VA_ARGS__); \ | |
84 | abort(); \ | |
85 | } while (0) | |
86 | ||
87 | #define bt_log(format, ...) \ | |
88 | do \ | |
89 | { \ | |
90 | if (bt_test_id) \ | |
91 | printf("%s: %s: " format "\n", bt_filename, bt_test_id, ##__VA_ARGS__); \ | |
92 | else \ | |
93 | printf("%s: " format "\n", bt_filename, ##__VA_ARGS__); \ | |
94 | } while(0) | |
95 | ||
96 | #define bt_debug(format, ...) \ | |
97 | do \ | |
98 | { \ | |
99 | if (bt_verbose >= BT_VERBOSE_ABSOLUTELY_ALL) \ | |
100 | printf(format, ##__VA_ARGS__); \ | |
101 | } while (0) | |
102 | ||
103 | #define bt_assert(test) \ | |
104 | bt_assert_msg(test, "Assertion (%s) at %s:%d", #test, __FILE__, __LINE__) | |
105 | ||
106 | #define bt_assert_msg(test, format, ...) \ | |
107 | do \ | |
108 | { \ | |
3dabf7b8 | 109 | int bt_suit_case_result = 1; \ |
9b0a0ba9 OZ |
110 | if ((test) == 0) \ |
111 | { \ | |
3dabf7b8 OZ |
112 | bt_result = 0; \ |
113 | bt_suite_result = 0; \ | |
114 | bt_suit_case_result = 0; \ | |
9b0a0ba9 OZ |
115 | } \ |
116 | bt_log_suite_case_result(bt_suit_case_result, format, ##__VA_ARGS__); \ | |
117 | } while (0) | |
118 | ||
119 | #define bt_syscall(test, format, ...) \ | |
120 | do \ | |
121 | { \ | |
122 | if (test) \ | |
123 | { \ | |
124 | bt_log(format ": %s", ##__VA_ARGS__, strerror(errno)); \ | |
125 | exit(3); \ | |
126 | } \ | |
127 | } while (0) | |
128 | ||
129 | #define bt_sprintf_concat(s, format, ...) \ | |
130 | snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__) | |
131 | ||
132 | struct bt_pair { | |
133 | const void *in; | |
134 | const void *out; | |
135 | }; | |
136 | ||
137 | /* Data structure used by bt_assert_batch() function */ | |
138 | struct bt_batch { | |
139 | /* in_fmt / out_fmt - formating data | |
140 | * @buf: buffer for write stringified @data | |
141 | * @size: empty size in @buf | |
142 | * @data: data for stringify | |
143 | * | |
144 | * There are some build-in functions, see bt_fmt_* functions */ | |
145 | void (*in_fmt)(char *buf, size_t size, const void *data); | |
146 | void (*out_fmt)(char *buf, size_t size, const void *data); | |
147 | ||
148 | /* Temporary output buffer */ | |
149 | void *out_buf; | |
150 | ||
151 | /* test_fn - testing function | |
152 | * @out: output data from tested function | |
153 | * @in: data for input | |
154 | * @expected_out: expected data from tested function | |
155 | * | |
156 | * Input arguments should not be stringified using in_fmt() or out_fmt() | |
5e3cd0e5 | 157 | * function already. This function should return only 0 or 1 */ |
9b0a0ba9 OZ |
158 | int (*test_fn)(void *out, const void *in, const void *expected_out); |
159 | ||
160 | /* Name of testing function @test_fn */ | |
161 | const char *test_fn_name; | |
162 | ||
5e3cd0e5 | 163 | /* Number of items in data */ |
9b0a0ba9 OZ |
164 | int ndata; |
165 | ||
166 | /* Array of input and expected output pairs */ | |
167 | struct bt_pair *data; | |
168 | }; | |
169 | ||
170 | void bt_fmt_str(char *buf, size_t size, const void *data); | |
171 | void bt_fmt_unsigned(char *buf, size_t size, const void *data); | |
172 | void bt_fmt_ipa(char *buf, size_t size, const void *data); | |
e709dc09 OZ |
173 | void bt_format_net(char *buf, size_t size, const void *data); |
174 | ||
9b0a0ba9 OZ |
175 | int bt_assert_batch__(struct bt_batch *opts); |
176 | int bt_is_char(byte c); | |
177 | ||
178 | #define bt_assert_batch(data__, fn__, in_fmt__, out_fmt__) \ | |
179 | bt_assert_batch__(& (struct bt_batch) { \ | |
180 | .data = data__, \ | |
181 | .ndata = ARRAY_SIZE(data__), \ | |
182 | .test_fn = fn__, \ | |
183 | .test_fn_name = #fn__, \ | |
184 | .in_fmt = in_fmt__, \ | |
185 | .out_fmt = out_fmt__, \ | |
186 | .out_buf = bt_out_fmt_buf, /* Global memory for this usage */ \ | |
187 | }) | |
188 | ||
189 | #endif /* _BIRDTEST_H_ */ |