-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathunit_test.c
306 lines (249 loc) · 5.82 KB
/
unit_test.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "unit_test.h"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX_APP_NAME_SZ 256
#define UT_DISABLED(tests, t) ((tests)->is_disabled && \
(tests)->is_disabled((t)->disabled))
/* io functionality */
int vscanf(const char *format, va_list ap);
static int first_comment;
int ask_user;
int vio_colour(vio_t vfunc, char *colour, char *fmt, va_list va)
{
int ret;
if (!colour)
colour = C_NORMAL;
ret = printf("%s", colour);
ret += vfunc(fmt, va);
ret += printf("%s", C_NORMAL);
fflush(stdout);
return ret;
}
int p_colour(char *colour, char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = vio_colour(vprintf, colour, fmt, va);
va_end(va);
return ret;
}
static int io_init(void)
{
int ret = 0;
if (first_comment) {
ret = printf("\n");
first_comment = 0;
}
return ret + p_colour(C_GREY, "> ");
}
static int _p_comment(char *fmt, va_list va, int is_newline)
{
int ret = io_init();
ret += vio_colour(vprintf, C_GREY, fmt, va);
if (is_newline)
ret += p_colour(C_NORMAL, "\n");
return ret;
}
int p_comment(char *comment, ...)
{
int ret;
va_list va;
va_start(va, comment);
ret = _p_comment(comment, va, 0);
va_end(va);
return ret;
}
int p_comment_nl(char *comment, ...)
{
int ret;
va_list va;
va_start(va, comment);
ret = _p_comment(comment, va, 1);
va_end(va);
return ret;
}
static int to_vscanf(char *fmt, va_list va)
{
fd_set fdr;
struct timeval tv;
int ret = 0, i, timeout = 10;
for (i = 0; i < timeout; i++) {
ret += vio_colour(vprintf, C_GREY, ".", NULL);
tv.tv_sec = 0;
tv.tv_usec = 500000;
FD_ZERO(&fdr);
FD_SET(0, &fdr);
if (select(1, &fdr, NULL, NULL, &tv) || FD_ISSET(0, &fdr))
break;
}
return (i == timeout) ? 0 : ret + vio_colour(vscanf, C_GREY, fmt, va);
}
int s_comment(char *comment, char *fmt, ...)
{
int ret, scn;
va_list va;
ret = io_init();
va_start(va, fmt);
ret += vio_colour(vprintf, C_GREY, comment, NULL);
ret += (scn = to_vscanf(fmt, va)) ? scn : printf("\n");
va_end(va);
return ret;
}
static void p_test_summery(int total, int passed, int failed, int known_issues,
int disabled, char *summery_comment)
{
printf("\ntest summery%s%s%s\n", summery_comment ? " (" : "",
summery_comment ? summery_comment : "", summery_comment ?
")" : "");
printf("------------\n");
printf("%stotal: %i%s\n", C_HIGHLIGHT, total, C_NORMAL);
printf("passed: %i\n", passed);
printf("failed: %i\n", failed);
printf("known issues: %i\n", known_issues);
printf("disabled: %i\n", disabled);
}
static char *app_name(char *argv0)
{
char *name, *ptr;
static char path[MAX_APP_NAME_SZ];
snprintf(path, MAX_APP_NAME_SZ, "%s", argv0);
for (name = ptr = path; *ptr; ptr++) {
if (*ptr != '/')
continue;
name = ptr + 1;
}
return name;
}
static void test_usage(char *path)
{
char *app = app_name(path);
printf("usage:\n"
"%s - run all tests\n"
" or\n"
"%s <test> - run a specific test\n"
" or\n"
"%s <from> <to> - run a range of tests\n"
" or\n"
"%s list - list all tests\n",
app, app, app, app);
}
static int test_getarg(char *arg, int *arg_ival, int min, int max)
{
char *err;
*arg_ival = strtol(arg, &err, 10);
if (*err)
return -1;
if (*arg_ival < min || *arg_ival > max) {
printf("test number out of range: %i\n", *arg_ival);
return -1;
}
return 0;
}
static int test_getargs(int argc, char *argv[], int *from, int *to, int max)
{
if (argc > 3) {
test_usage(argv[0]);
return -1;
}
if (argc == 1) {
*from = 0;
*to = max;
ask_user = 1;
return 0;
}
/* 2 <= argc <= 3*/
if (test_getarg(argv[1], from, 1, max)) {
test_usage(argv[0]);
return -1;
}
if (argc == 2) {
*to = *from;
}
else { /* argc == 3 */
if (test_getarg(argv[2], to, *from, max)) {
test_usage(argv[0]);
return -1;
}
}
(*from)--; /* map test number to table index */
return 0;
}
static int is_list_tests(int argc, char *argv[], unit_test_t *tests)
{
int i, size = tests->size;
char *list_comment = tests->list_comment;
test_t *arr = tests->arr;
if (argc != 2 || strcmp(argv[1], "list"))
return 0;
p_colour(C_HIGHLIGHT, "%s unit tests%s%s%s\n", app_name(argv[0]),
list_comment ? " (" : "", list_comment ? list_comment : "",
list_comment ? ")" : "");
for (i = 0; i < size - 1; i++) {
test_t *t = &arr[i];
int is_disabled = UT_DISABLED(tests, t);
printf("%i. ", i + 1);
p_colour(is_disabled ? C_GREY : C_NORMAL, "%s",
t->description);
if (is_disabled) {
p_colour(C_CYAN, " (disabled)");
}
else if (t->known_issue) {
p_colour(C_BLUE, " (known issue: ");
p_colour(C_GREY, t->known_issue);
p_colour(C_BLUE, ")");
}
printf("\n");
}
return 1;
}
int unit_test(int argc, char *argv[], unit_test_t *tests)
{
test_t *t;
int from, to, max = tests->size, ret;
int total = 0, disabled = 0, passed = 0, failed = 0, known_issues = 0;
if (tests->tests_init)
tests->tests_init(argc, argv);
if (is_list_tests(argc, argv, tests))
return 0;
if (test_getargs(argc, argv, &from, &to, max - 1))
return -1;
for (t = &tests->arr[from]; t < tests->arr + MIN(to, max); t++) {
first_comment = 1;
total++;
printf("%i. %s: ", from + total, t->description);
if (UT_DISABLED(tests, t)) {
disabled++;
p_colour(C_CYAN, "disabled\n");
continue;
}
if (t->known_issue) {
p_colour(C_BLUE, "known issue: ");
p_colour(C_NORMAL, "%s\n", t->known_issue);
known_issues++;
continue;
}
if (!t->func) {
p_colour(C_CYAN, "function does not exist\n");
return -1;
}
fflush(stdout);
if (tests->pre_test)
tests->pre_test();
if ((ret = t->func())) {
p_colour(C_RED, "Failed");
failed++;
}
else {
p_colour(C_GREEN, "OK");
passed++;
}
printf("\n");
}
p_test_summery(total, passed, failed, known_issues, disabled,
tests->summery_comment);
return 0;
}