]>
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); | |
35 | 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 |
36 | static inline u64 bt_random(void) |
37 | { return ((u64) random() & 0xffffffff) | ((u64) random() << 32); } | |
9b0a0ba9 OZ |
38 | |
39 | void bt_log_suite_result(int result, const char *fmt, ...); | |
40 | void bt_log_suite_case_result(int result, const char *fmt, ...); | |
41 | ||
9b0a0ba9 OZ |
42 | #define BT_TIMEOUT 5 /* Default timeout in seconds */ |
43 | #define BT_FORKING 1 /* Forking is enabled in default */ | |
44 | ||
bb001af0 | 45 | #define BT_RANDOM_SEED 0x5097d2bb |
9b0a0ba9 OZ |
46 | |
47 | #define BT_BUFFER_SIZE 10000 | |
48 | ||
49 | #define BT_PROMPT_GREEN "\e[1;32m" | |
50 | #define BT_PROMPT_RED "\e[1;31m" | |
51 | #define BT_PROMPT_NORMAL "\e[0m" | |
52 | #define BT_PROMPT_OK " [" BT_PROMPT_GREEN " OK " BT_PROMPT_NORMAL "] " | |
53 | #define BT_PROMPT_OK_NO_COLOR " [" " OK " "] " | |
54 | #define BT_PROMPT_FAIL " [" BT_PROMPT_RED "FAIL" BT_PROMPT_NORMAL "] " | |
55 | #define BT_PROMPT_FAIL_NO_COLOR " [" "FAIL" "] " | |
56 | #define BT_PROMPT_OK_FAIL_STRLEN 8 /* strlen ' [FAIL] ' */ | |
57 | ||
58 | #define bt_test_suite(fn, dsc, ...) \ | |
59 | bt_test_suite_extra(fn, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__) | |
60 | ||
61 | #define bt_test_suite_extra(fn, f, t, dsc, ...) \ | |
62 | bt_test_suite_base((int (*)(const void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__) | |
63 | ||
64 | #define bt_test_suite_arg(fn, arg, dsc, ...) \ | |
65 | bt_test_suite_arg_extra(fn, arg, BT_FORKING, BT_TIMEOUT, dsc, ##__VA_ARGS__) | |
66 | ||
67 | #define bt_test_suite_arg_extra(fn, arg, f, t, dsc, ...) \ | |
68 | bt_test_suite_base(fn, #fn, arg, f, t, dsc, ##__VA_ARGS__) | |
69 | ||
70 | #define bt_abort() \ | |
71 | bt_abort_msg("Aborted at %s:%d", __FILE__, __LINE__) | |
72 | ||
73 | #define bt_abort_msg(format, ...) \ | |
74 | do \ | |
75 | { \ | |
76 | bt_log(format, ##__VA_ARGS__); \ | |
77 | abort(); \ | |
78 | } while (0) | |
79 | ||
80 | #define bt_log(format, ...) \ | |
81 | do \ | |
82 | { \ | |
83 | if (bt_test_id) \ | |
84 | printf("%s: %s: " format "\n", bt_filename, bt_test_id, ##__VA_ARGS__); \ | |
85 | else \ | |
86 | printf("%s: " format "\n", bt_filename, ##__VA_ARGS__); \ | |
87 | } while(0) | |
88 | ||
89 | #define bt_debug(format, ...) \ | |
90 | do \ | |
91 | { \ | |
92 | if (bt_verbose >= BT_VERBOSE_ABSOLUTELY_ALL) \ | |
93 | printf(format, ##__VA_ARGS__); \ | |
94 | } while (0) | |
95 | ||
96 | #define bt_assert(test) \ | |
97 | bt_assert_msg(test, "Assertion (%s) at %s:%d", #test, __FILE__, __LINE__) | |
98 | ||
99 | #define bt_assert_msg(test, format, ...) \ | |
100 | do \ | |
101 | { \ | |
5e3cd0e5 | 102 | int bt_suit_case_result = 1; \ |
9b0a0ba9 OZ |
103 | if ((test) == 0) \ |
104 | { \ | |
5e3cd0e5 PT |
105 | bt_result = 0; \ |
106 | bt_suite_result = 0; \ | |
107 | bt_suit_case_result = 0; \ | |
9b0a0ba9 OZ |
108 | } \ |
109 | bt_log_suite_case_result(bt_suit_case_result, format, ##__VA_ARGS__); \ | |
110 | } while (0) | |
111 | ||
112 | #define bt_syscall(test, format, ...) \ | |
113 | do \ | |
114 | { \ | |
115 | if (test) \ | |
116 | { \ | |
117 | bt_log(format ": %s", ##__VA_ARGS__, strerror(errno)); \ | |
118 | exit(3); \ | |
119 | } \ | |
120 | } while (0) | |
121 | ||
122 | #define bt_sprintf_concat(s, format, ...) \ | |
123 | snprintf(s + strlen(s), sizeof(s) - strlen(s), format, ##__VA_ARGS__) | |
124 | ||
125 | struct bt_pair { | |
126 | const void *in; | |
127 | const void *out; | |
128 | }; | |
129 | ||
130 | /* Data structure used by bt_assert_batch() function */ | |
131 | struct bt_batch { | |
132 | /* in_fmt / out_fmt - formating data | |
133 | * @buf: buffer for write stringified @data | |
134 | * @size: empty size in @buf | |
135 | * @data: data for stringify | |
136 | * | |
137 | * There are some build-in functions, see bt_fmt_* functions */ | |
138 | void (*in_fmt)(char *buf, size_t size, const void *data); | |
139 | void (*out_fmt)(char *buf, size_t size, const void *data); | |
140 | ||
141 | /* Temporary output buffer */ | |
142 | void *out_buf; | |
143 | ||
144 | /* test_fn - testing function | |
145 | * @out: output data from tested function | |
146 | * @in: data for input | |
147 | * @expected_out: expected data from tested function | |
148 | * | |
149 | * Input arguments should not be stringified using in_fmt() or out_fmt() | |
5e3cd0e5 | 150 | * function already. This function should return only 0 or 1 */ |
9b0a0ba9 OZ |
151 | int (*test_fn)(void *out, const void *in, const void *expected_out); |
152 | ||
153 | /* Name of testing function @test_fn */ | |
154 | const char *test_fn_name; | |
155 | ||
5e3cd0e5 | 156 | /* Number of items in data */ |
9b0a0ba9 OZ |
157 | int ndata; |
158 | ||
159 | /* Array of input and expected output pairs */ | |
160 | struct bt_pair *data; | |
161 | }; | |
162 | ||
163 | void bt_fmt_str(char *buf, size_t size, const void *data); | |
164 | void bt_fmt_unsigned(char *buf, size_t size, const void *data); | |
165 | void bt_fmt_ipa(char *buf, size_t size, const void *data); | |
166 | int bt_assert_batch__(struct bt_batch *opts); | |
167 | int bt_is_char(byte c); | |
168 | ||
169 | #define bt_assert_batch(data__, fn__, in_fmt__, out_fmt__) \ | |
170 | bt_assert_batch__(& (struct bt_batch) { \ | |
171 | .data = data__, \ | |
172 | .ndata = ARRAY_SIZE(data__), \ | |
173 | .test_fn = fn__, \ | |
174 | .test_fn_name = #fn__, \ | |
175 | .in_fmt = in_fmt__, \ | |
176 | .out_fmt = out_fmt__, \ | |
177 | .out_buf = bt_out_fmt_buf, /* Global memory for this usage */ \ | |
178 | }) | |
179 | ||
180 | #endif /* _BIRDTEST_H_ */ |