-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchibi.c
424 lines (378 loc) · 13.7 KB
/
chibi.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifndef AMIGA
#include <sys/stat.h>
#include <sys/time.h>
#endif
#include "chibi.h"
#define MAX_DIGITS_INT 10
chibi_suite *chibi_suite_new_fixture(const char *name,
chibi_fixfunc setup,
chibi_fixfunc teardown,
void *userdata)
{
chibi_suite *result = calloc(1, sizeof(chibi_suite));
result->name = name;
result->head = NULL;
result->setup = setup;
result->teardown = teardown;
result->userdata = userdata;
result->first_child = NULL;
result->next = NULL;
return result;
}
chibi_suite *chibi_suite_new(const char *name)
{
return chibi_suite_new_fixture(name, NULL, NULL, NULL);
}
void chibi_suite_delete(chibi_suite *suite)
{
if (suite) {
struct _chibi_testcase *tc = suite->head, *tmp;
while (tc) {
tmp = tc->next;
if (tc->error_msg) free(tc->error_msg);
free(tc);
tc = tmp;
}
/* recursively free the children and siblings */
if (suite->first_child) chibi_suite_delete(suite->first_child);
if (suite->next) chibi_suite_delete(suite->next);
free(suite);
}
}
void chibi_suite_add_suite(chibi_suite *suite, chibi_suite *toadd)
{
if (!suite->first_child) suite->first_child = toadd;
else {
chibi_suite *cur = suite->first_child;
while (cur->next) cur = cur->next;
cur->next = toadd;
}
}
void _chibi_suite_add_test(chibi_suite *suite, chibi_testfunc fun, const char *fname)
{
struct _chibi_testcase *tc, *newtc;
newtc = calloc(1, sizeof(struct _chibi_testcase));
newtc->fun = fun;
newtc->fname = fname;
newtc->next = NULL;
newtc->success = 1;
newtc->error_msg = NULL;
newtc->userdata = suite->userdata;
if (!suite->head) suite->head = newtc;
else {
tc = suite->head;
while (tc->next) tc = tc->next;
tc->next = newtc;
}
}
static void _chibi_suite_summary_data(chibi_suite *suite, chibi_summary_data *summary, int level)
{
if (suite && summary) {
chibi_testcase *tc = suite->head;
if (!level) {
summary->num_runs = 0;
summary->num_failures = 0;
summary->num_pass = 0;
}
while (tc) {
summary->num_runs++;
if (!tc->success) {
summary->num_failures++;
}
tc = tc->next;
}
if (suite->first_child) _chibi_suite_summary_data(suite->first_child, summary, level + 1);
if (suite->next) _chibi_suite_summary_data(suite->next, summary, level + 1);
if (!level) summary->num_pass = summary->num_runs - summary->num_failures;
}
}
static int _print_messages(chibi_suite *suite, int testnum)
{
chibi_testcase *tc = suite->head;
while (tc) {
if (!tc->success) {
fprintf(stderr, "%d. %s\n", testnum, tc->error_msg);
testnum++;
}
tc = tc->next;
}
if (suite->first_child) testnum = _print_messages(suite->first_child, testnum);
if (suite->next) testnum = _print_messages(suite->next, testnum);
return testnum;
}
static void chibi_suite_print_summary(chibi_suite *suite)
{
chibi_summary_data summary;
_chibi_suite_summary_data(suite, &summary, 0);
fprintf(stderr, "\n\nSummary (chibitest %s)\n\n", CHIBI_TEST_GIT_SHA);
if (summary.num_failures > 0) {
fprintf(stderr, "# of failures: %d\n\n", summary.num_failures);
_print_messages(suite, 0);
fprintf(stderr, "\n");
}
fprintf(stderr, "Runs: %d Pass: %d Fail: %d\n\n", summary.num_runs,
summary.num_runs - summary.num_failures, summary.num_failures);
}
/*
* Reused by assertions to generate a standard format error message.
*/
static char *assemble_message(const char *msg, const char *srcfile,
const char *funname, int line)
{
char *msgbuffer = calloc(strlen(msg) + strlen(srcfile) + strlen(funname) + MAX_DIGITS_INT + 8,
sizeof(char));
sprintf(msgbuffer, "%s:%d - %s() - %s", srcfile, line, funname, msg);
return msgbuffer;
}
static char *assemble_message2(const char *msg1, const char *msg2,
const char *srcfile, const char *funname,
int line)
{
char *msgbuffer = calloc(strlen(msg1) + strlen(msg2) + strlen(srcfile) + strlen(funname)
+ MAX_DIGITS_INT + 10, sizeof(char));
sprintf(msgbuffer, "%s:%d - %s() - %s %s", srcfile, line, funname, msg1, msg2);
return msgbuffer;
}
/**********************************************************************
*
* ASSERTIONS
*
* TODO: test exit after first fail: make a test case with 2 assertions and ensure
* that the second is not executed when the first fails
* Note: setjmp()/longjmp() seem to be broken on Amiga/VBCC
*
**********************************************************************/
void _exit_on_fail(chibi_testcase *tc)
{
#ifndef AMIGA
longjmp(tc->env, 0);
#endif
}
void _chibi_assert_not_null(chibi_testcase *tc, void *ptr, const char *msg, const char *srcfile,
int line)
{
if (ptr == NULL) {
tc->error_msg = assemble_message(msg, srcfile, tc->fname, line);
tc->success = 0;
_exit_on_fail(tc);
}
}
void _chibi_fail(chibi_testcase *tc, const char *msg, const char *srcfile, int line)
{
tc->error_msg = assemble_message(msg, srcfile, tc->fname, line);
tc->success = 0;
_exit_on_fail(tc);
}
void _chibi_assert(chibi_testcase *tc, int cond, const char *cond_str, const char *msg,
const char *srcfile, int line)
{
if (!cond) {
tc->error_msg = assemble_message2(msg, cond_str, srcfile, tc->fname, line);
tc->success = 0;
_exit_on_fail(tc);
}
}
void _chibi_assert_eq_int(chibi_testcase *tc, int expected, int value,
const char *srcfile, int line)
{
if (value != expected) {
char *fmt = "%s:%d - %s() - expected:<%d> but was:<%d>";
char *msgbuffer = calloc(strlen(fmt) + strlen(srcfile) + strlen(tc->fname)
+ MAX_DIGITS_INT * 3 + 10, sizeof(char));
sprintf(msgbuffer, fmt, srcfile, line, tc->fname, expected, value);
tc->error_msg = msgbuffer;
tc->success = 0;
_exit_on_fail(tc);
}
}
void _chibi_assert_eq_cstr(chibi_testcase *tc, const char *expected, const char *value,
const char *srcfile, int line)
{
if (value == expected) return;
if (!value || !expected || strcmp(value, expected)) {
char *fmt, *msgbuffer;
if (!expected) expected = "(null)";
if (!value) value = "(null)";
fmt = "%s:%d - %s() - expected:<%s> but was:<%s>";
msgbuffer = calloc(strlen(fmt) + strlen(srcfile) + strlen(tc->fname)
+ strlen(expected) + strlen(value)
+ MAX_DIGITS_INT + 10, sizeof(char));
sprintf(msgbuffer, fmt, srcfile, line, tc->fname, expected, value);
tc->error_msg = msgbuffer;
tc->success = 0;
_exit_on_fail(tc);
}
}
/**********************************************************************
*
* TEST RUNNERS
*
**********************************************************************/
/*
* Generic runner. Supports fixtures and report function customization.
* If the suite was defined with setup and/or teardown functions, those
* are run on the optional userdata object.
* The report_num_tests(int), report_success(int, chibi_testcase *) and
* report_fail(int, chibi_testcase *) functions are used to support
* different output protocols (e.g. for reporting the success/failure of
* tests while they are run).
*/
static int _count_tests(chibi_suite *suite) {
chibi_testcase *testcase = suite->head;
int result = 0;
if (suite->first_child) result += _count_tests(suite->first_child);
if (suite->next) result += _count_tests(suite->next);
while (testcase) {
result++;
testcase = testcase->next;
}
return result;
}
static int _chibi_suite_run(chibi_suite *suite, void (*report_num_tests)(int),
void (*report_success)(int, chibi_testcase *),
void (*report_fail)(int, chibi_testcase *),
int tcnum, int level)
{
if (suite) {
chibi_testcase *testcase;
#ifndef AMIGA
struct timeval start_time, end_time;
#endif
if (suite->first_child) {
tcnum = _chibi_suite_run(suite->first_child, report_num_tests,
report_success, report_fail, tcnum, level + 1);
}
if (suite->next) {
tcnum = _chibi_suite_run(suite->next, report_num_tests,
report_success, report_fail, tcnum, level + 1);
}
/* only report the number of tests at the top level */
if (level == 0) report_num_tests(_count_tests(suite));
/* run this level's tests */
testcase = suite->head;
while (testcase) {
#ifndef AMIGA
gettimeofday(&start_time, NULL);
if (!setjmp(testcase->env)) {
#endif
if (suite->setup) suite->setup(suite->userdata);
testcase->fun(testcase);
if (suite->teardown) suite->teardown(suite->userdata);
#ifndef AMIGA
}
gettimeofday(&end_time, NULL);
testcase->elapsed_millis = (end_time.tv_sec - start_time.tv_sec) * 1000 +
(end_time.tv_usec - start_time.tv_usec) / 1000.0;
#endif
if (testcase->success) report_success(tcnum, testcase);
else report_fail(tcnum, testcase);
testcase = testcase->next;
tcnum++;
}
}
return tcnum;
}
/*
* Standard Runner
*/
static void report_num_tests_silent(int num_tests) { }
static void report_success_silent(int testnum, chibi_testcase *testcase) { }
static void report_fail_silent(int testnum, chibi_testcase *testcase) { }
static void report_success_std(int testnum, chibi_testcase *testcase) { fprintf(stderr, "."); }
static void report_fail_std(int testnum, chibi_testcase *testcase) { fprintf(stderr, "F"); }
void chibi_suite_run(chibi_suite *suite, chibi_summary_data *summary)
{
_chibi_suite_run(suite, report_num_tests_silent, report_success_std, report_fail_std, 0, 0);
if (summary) _chibi_suite_summary_data(suite, summary, 0);
chibi_suite_print_summary(suite);
}
void chibi_suite_run_silently(chibi_suite *suite, chibi_summary_data *summary)
{
_chibi_suite_run(suite, report_num_tests_silent, report_success_silent, report_fail_silent, 0, 0);
if (summary) _chibi_suite_summary_data(suite, summary, 0);
}
/*
* TAP Runner
*/
static void report_num_tests_tap(int num_tests) { fprintf(stdout, "1..%d\n", num_tests); }
static void report_success_tap(int testnum, chibi_testcase *testcase)
{
fprintf(stdout, "ok %d - %s\n", testnum + 1, testcase->fname);
}
static void report_fail_tap(int testnum, chibi_testcase *testcase)
{
fprintf(stdout, "not ok %d - %s\n", testnum + 1, testcase->fname);
}
void chibi_suite_run_tap(chibi_suite *suite, chibi_summary_data *summary)
{
_chibi_suite_run(suite, report_num_tests_tap, report_success_tap, report_fail_tap, 0, 0);
if (summary) _chibi_suite_summary_data(suite, summary, 0);
}
static int create_dir_if_needed(const char *dir)
{
#ifndef AMIGA
if (mkdir(dir, S_IRWXU | S_IRGRP | S_IROTH) == -1) {
if (errno == EEXIST) return 1;
else {
fprintf(stderr, "can't create output directory\n");
return 0;
}
}
#else
// TODO: AmigaDOS equivalent
#endif
return 1;
}
#ifndef AMIGA
void chibi_suite_run_xml(chibi_suite *suite, chibi_summary_data *summary, const char *outdir)
{
if (!create_dir_if_needed(outdir)) return;
// take time stamp
time_t now = time(NULL);
int datetime_len = strlen("YYYYMMDDHHMMSS");
char *datetime_buffer = calloc(datetime_len + 1, sizeof(char));
strftime(datetime_buffer, datetime_len, "%Y%m%d%H%M%S", localtime(&now));
// reserve enough space for full path
int buffer_len = strlen(outdir) + strlen(PATH_SEPARATOR) + strlen("TEST-") +
strlen(suite->name) + strlen("-YYYYMMDDHHMMSS") + strlen(".xml") + 1;
char *path_buffer = calloc(buffer_len, sizeof(char));
if (!path_buffer) {
free(datetime_buffer);
return;
}
snprintf(path_buffer, buffer_len, "%s%sTEST-%s-%s.xml",
outdir, PATH_SEPARATOR, suite->name, datetime_buffer);
FILE *fp = fopen(path_buffer, "w");
if (!fp) {
free(datetime_buffer);
free(path_buffer);
return;
}
struct _chibi_testcase *cur = suite->head;
_chibi_suite_run(suite, report_num_tests_silent, report_success_silent, report_fail_silent, 0, 0);
if (summary) _chibi_suite_summary_data(suite, summary, 0);
fputs("<?xml version=\"1.0\" ?>", fp);
int num_tests = summary->num_failures + summary->num_pass;
fprintf(fp, "<testsuite errors=\"%d\" failures=\"%d\" name=\"%s\" tests=\"%d\" time=\"%.03f\">\n",
0, summary->num_failures, suite->name, num_tests, 0.123);
while (cur != NULL) {
fprintf(fp, " <testcase classname=\"%s\" name=\"%s\" time=\"%.03f\">\n",
suite->name, cur->fname, cur->elapsed_millis / 1000.0f);
if (!cur->success) {
fprintf(fp, " <failure type=\"%s\" message=\"%s\" />",
"error", cur->error_msg);
}
fputs(" </testcase>\n", fp);
cur = cur->next;
}
fputs("</testsuite>\n", fp);
fclose(fp);
free(datetime_buffer);
free(path_buffer);
}
#endif