utils/macros.h: Add <ctype.h> and `foocmp' helper macros.
[mLib] / test / testrig.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
0875b58f 3 * Generic test driver
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
0875b58f 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
c846879c 13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
0875b58f 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c846879c 20 * GNU Library General Public License for more details.
d4efbcd9 21 *
c846879c 22 * You should have received a copy of the GNU Library General Public
0bd98442 23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
0875b58f 26 */
27
0875b58f 28/*----- Header files ------------------------------------------------------*/
29
30#include <ctype.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "dstr.h"
36188114 37#include "macros.h"
0875b58f 38#include "report.h"
39#include "quis.h"
40#include "testrig.h"
41
42/*----- Static variables --------------------------------------------------*/
43
a249f27e 44static dstr tok = DSTR_INIT;
0875b58f 45
46enum {
083cdbed 47 TOK_EOF = 0x100,
48 TOK_WORD
0875b58f 49};
50
51/*----- Main code ---------------------------------------------------------*/
52
083cdbed 53/* --- @decode@ --- *
0875b58f 54 *
55 * Arguments: @int tok@ = token type to decode
56 *
57 * Returns: Pointer to a textual representation of the token.
58 *
59 * Use: Produces a readable representation of a token.
60 */
61
083cdbed 62static const char *decode(int t)
0875b58f 63{
64 static char buf[4];
65
083cdbed 66 switch (t) {
67 case TOK_EOF:
0875b58f 68 return ("<eof>");
083cdbed 69 case TOK_WORD:
70 return (tok.buf);
0875b58f 71 default:
083cdbed 72 buf[0] = t;
0875b58f 73 buf[1] = 0;
74 return (buf);
75 }
76 return ("<buggy-program>");
77}
78
083cdbed 79/* --- @gettok@ --- *
0875b58f 80 *
81 * Arguments: @FILE *fp@ = file handle to read from
82 *
83 * Returns: Type of token read.
84 *
85 * Use: Reads a token from the input stream.
86 */
87
083cdbed 88static int gettok(FILE *fp)
0875b58f 89{
90 int ch;
91
92 /* --- Clear the token accumulator --- */
93
0bfb1431 94 DRESET(&tok);
0875b58f 95
96 /* --- Prime the lookahead character --- */
97
98again:
99 ch = getc(fp);
100
101 /* --- Skip leading whitespace --- */
102
36188114 103 while (ISSPACE(ch))
0875b58f 104 ch = getc(fp);
105
106 /* --- Trap some special characters --- */
107
108 switch (ch) {
109
110 /* --- Comments --- */
111
112 case '#':
113 do ch = getc(fp); while (ch != EOF && ch != '\n');
114 goto again;
115
116 /* --- End of file --- */
d4efbcd9 117
0875b58f 118 case EOF:
083cdbed 119 return (TOK_EOF);
0875b58f 120
121 /* --- Quote characters --- */
122
123 case '`':
124 ch = '\'';
125 case '\'':
126 case '\"': {
127 int quote = ch;
128
129 for (;;) {
130 ch = getc(fp);
131 if (ch == EOF || ch == quote)
132 break;
133 if (ch == '\\') {
134 ch = getc(fp);
135 if (ch == EOF)
136 ch = '\\';
137 }
083cdbed 138 DPUTC(&tok, ch);
0875b58f 139 }
083cdbed 140 DPUTZ(&tok);
141 return (TOK_WORD);
0875b58f 142 }
143
12080081 144 /* --- Self-delimiting things --- */
145
146 case ';':
147 case '{':
148 case '}':
149 return (ch);
150
151 /* --- Anything else is a word --- */
0875b58f 152
153 default:
12080081 154 for (;;) {
155 DPUTC(&tok, ch);
156 ch = getc(fp);
157 switch (ch) {
158 case EOF:
159 case ';':
160 case '{':
161 case '}':
162 case '\"':
163 case '\'':
164 case '`':
165 goto done;
166 default:
36188114 167 if (ISSPACE(ch))
12080081 168 goto done;
169 }
170 if (ch == '\\') {
0875b58f 171 ch = getc(fp);
12080081 172 if (ch == EOF)
173 ch = '\\';
0875b58f 174 }
12080081 175 }
176 done:
177 ungetc(ch, fp);
178 DPUTZ(&tok);
179 return (TOK_WORD);
0875b58f 180 }
181}
182
183/* --- @type_hex@ --- */
184
185static void cvt_hex(const char *s, dstr *d)
186{
187 while (s[0] && s[1]) {
188 int x = s[0], y = s[1];
189 if ('0' <= x && x <= '9') x -= '0';
190 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
191 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
192 else x = 0;
193 if ('0' <= y && y <= '9') y -= '0';
194 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
195 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
196 else y = 0;
197 DPUTC(d, (x << 4) + y);
198 s += 2;
199 }
200}
201
202static void dump_hex(dstr *d, FILE *fp)
203{
204 const char *p, *q;
205 for (p = d->buf, q = p + d->len; p < q; p++)
206 fprintf(fp, "%02x", *(unsigned char *)p);
207}
208
d66299dc 209const test_type type_hex = { cvt_hex, dump_hex };
0875b58f 210
211/* --- @type_string@ --- */
212
213static void cvt_string(const char *s, dstr *d)
214{
215 DPUTS(d, s);
216}
217
218static void dump_string(dstr *d, FILE *fp)
219{
0bfb1431 220 DWRITE(d, fp);
0875b58f 221}
222
d66299dc 223const test_type type_string = { cvt_string, dump_string };
0875b58f 224
225/* --- @type_int@ --- */
226
227static void cvt_int(const char *s, dstr *d)
228{
dadc1afb 229 DENSURE(d, sizeof(int));
230 sscanf(s, "%i", (int *)d->buf);
0875b58f 231}
232
233static void dump_int(dstr *d, FILE *fp)
234{
dadc1afb 235 fprintf(fp, "%i", *(int *)d->buf);
0875b58f 236}
237
d66299dc 238const test_type type_int = { cvt_int, dump_int };
0875b58f 239
dadc1afb 240/* --- @type_long@ --- */
241
242static void cvt_long(const char *s, dstr *d)
243{
244 DENSURE(d, sizeof(long));
245 *(long *)d->buf = strtol(s, 0, 0);
246}
247
248static void dump_long(dstr *d, FILE *fp)
249{
250 fprintf(fp, "%li", *(long *)d->buf);
251}
252
253const test_type type_long = { cvt_long, dump_long };
254
255/* --- @type_ulong@ --- */
256
257static void cvt_ulong(const char *s, dstr *d)
258{
259 DENSURE(d, sizeof(unsigned long));
260 *(unsigned long *)d->buf = strtoul(s, 0, 0);
261}
262
263static void dump_ulong(dstr *d, FILE *fp)
264{
265 fprintf(fp, "%lu", *(unsigned long *)d->buf);
266}
267
268const test_type type_ulong = { cvt_ulong, dump_ulong };
269
270/* --- @type_uint32@ --- */
271
272static void cvt_uint32(const char *buf, dstr *d)
273{
274 DENSURE(d, sizeof(uint32));
275 *(uint32 *)d->buf = strtoul(buf, 0, 0);
276}
277
278static void dump_uint32(dstr *d, FILE *fp)
279{
280 fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
281}
282
283const test_type type_uint32 = { cvt_uint32, dump_uint32 };
284
9fcce036 285/* --- @test_do@ --- *
0875b58f 286 *
9fcce036
MW
287 * Arguments: @const test_suite suites[]@ = pointer to suite definitions
288 * @FILE *fp@ = test vector file, ready opened
289 * @test_results *results@ = where to put results
0875b58f 290 *
9fcce036
MW
291 * Returns: Negative if something bad happened, or the number of
292 * failures.
0875b58f 293 *
9fcce036
MW
294 * Use: Runs a collection of tests against a file of test vectors and
295 * reports the results.
0875b58f 296 */
297
9fcce036 298int test_do(const test_suite suites[], FILE *fp, test_results *results)
0875b58f 299{
9fcce036 300 test_results dummy;
0875b58f 301 dstr dv[TEST_FIELDMAX];
9fcce036
MW
302 const test_suite *ss;
303 const test_chunk *chunks = suites[0].chunks;
304 const test_chunk *cch;
305 int rc = -1;
306 int ok;
307 int i;
0875b58f 308
309 for (i = 0; i < TEST_FIELDMAX; i++)
0bfb1431 310 DCREATE(&dv[i]);
0875b58f 311
9fcce036
MW
312 if (!results)
313 results = &dummy;
314 results->tests = 0;
315 results->failed = 0;
0875b58f 316
317 for (;;) {
083cdbed 318 int t = gettok(fp);
0875b58f 319
320 /* --- This is a reasonable place to stop --- */
321
083cdbed 322 if (t == TOK_EOF)
0875b58f 323 break;
324
325 /* --- Pick out the chunk name --- */
326
9fcce036
MW
327 if (t != TOK_WORD) {
328 moan("expected <word>; found `%s'", decode(t));
329 goto done;
330 }
331
36188114 332 if (STRCMP(tok.buf, ==, "SUITE")) {
9fcce036
MW
333 t = gettok(fp);
334 if (t != TOK_WORD) {
335 moan("expected <word>; found `%s'", decode(t));
336 goto done;
337 }
338 for (ss = suites; ; ss++) {
339 if (!ss->name) {
340 chunks = 0;
341 break;
342 }
36188114 343 if (STRCMP(tok.buf, ==, ss->name)) {
9fcce036
MW
344 chunks = ss->chunks;
345 break;
346 }
347 }
348 continue;
349 }
0875b58f 350
351 /* --- Find the right chunk block --- */
352
9fcce036
MW
353 if (!chunks)
354 goto skip_chunk;
355 for (cch = chunks; ; cch++) {
0875b58f 356 if (!cch->name)
357 goto skip_chunk;
36188114 358 if (STRCMP(tok.buf, ==, cch->name))
0875b58f 359 break;
360 }
361
362 /* --- Past the open brace to the first chunk --- */
363
9fcce036
MW
364 if ((t = gettok(fp)) != '{') {
365 moan("expected `{'; found `%s'", decode(t));
366 goto done;
367 }
0875b58f 368
369 /* --- Start on the test data now --- */
370
371 printf("%s: ", cch->name);
372 fflush(stdout);
0875b58f 373 ok = 1;
374
375 for (;;) {
083cdbed 376 t = gettok(fp);
0875b58f 377
378 /* --- Accept a close brace --- */
379
083cdbed 380 if (t == '}')
0875b58f 381 break;
382
383 /* --- Otherwise I expect a list of words --- */
384
385 for (i = 0; cch->f[i]; i++) {
0bfb1431 386 DRESET(&dv[i]);
9fcce036
MW
387 if (t != TOK_WORD) {
388 moan("expected <word>; found `%s'", decode(t));
389 goto done;
390 }
083cdbed 391 cch->f[i]->cvt(tok.buf, &dv[i]);
392 t = gettok(fp);
0875b58f 393 }
394
395 /* --- And a terminating semicolon --- */
396
9fcce036
MW
397 if (t != ';') {
398 moan("expected `;'; found `%s'", decode(t));
399 goto done;
400 }
0875b58f 401
402 /* --- Run the test code --- */
403
404 if (!cch->test(dv)) {
9fcce036 405 ok = 0;
0875b58f 406 printf("%s: ", cch->name);
9fcce036
MW
407 for (i = 0; i < results->tests; i++) putchar('.');
408 results->failed++;
0875b58f 409 }
0875b58f 410 putchar('.');
9fcce036 411 results->tests++;
0875b58f 412 fflush(stdout);
413 }
414
415 puts(ok ? " ok" : " failed");
416 fflush(stdout);
417 continue;
418
419 skip_chunk:
9fcce036
MW
420 if ((t = gettok(fp)) != '{') {
421 moan("expected '{'; found `%s'", decode(t));
422 goto done;
423 }
0875b58f 424 for (;;) {
083cdbed 425 t = gettok(fp);
426 if (t == '}')
0875b58f 427 break;
083cdbed 428 while (t == TOK_WORD)
429 t = gettok(fp);
9fcce036
MW
430 if (t != ';') {
431 moan("expected `;'; found `%s'", decode(t));
432 goto done;
433 }
434 }
435 }
436 rc = results->failed;
437
438 /* --- All done --- */
439
440done:
441 for (i = 0; i < TEST_FIELDMAX; i++)
442 dstr_destroy(&dv[i]);
443 return (rc);
444}
445
446/* --- @test_run@ --- *
447 *
448 * Arguments: @int argc@ = number of command line arguments
449 * @char *argv[]@ = pointer to command line arguments
450 * @const test_chunk chunk[]@ = pointer to chunk definitions
451 * @const char *vec@ = name of default test vector file
452 *
453 * Returns: Doesn't.
454 *
455 * Use: Runs a set of test vectors to ensure that a component is
456 * working properly.
457 */
458
459void test_run(int argc, char *argv[],
460 const test_chunk chunk[],
461 const char *vec)
462{
463 FILE *fp;
464 test_results res;
465 int rc;
466 test_suite suite[2];
467
468 /* --- Silly bits of initialization --- */
469
470 ego(argv[0]);
471
472 /* --- Parse command line arguments --- */
473
474 {
475 const char *p = 0;
476 int i = 0;
477
478 for (;;) {
479 if (!p || !*p) {
480 if (i >= argc - 1)
481 break;
482 p = argv[++i];
36188114 483 if (STRCMP(p, ==, "--")) {
9fcce036
MW
484 i++;
485 break;
486 }
487 if (p[0] != '-' || p[1] == 0)
488 break;
489 p++;
490 }
491 switch (*p++) {
492 case 'h':
493 printf("%s test driver\n"
494 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
495 exit(0);
496 case 'f':
497 if (!*p) {
498 if (i >= argc - 1)
499 die(1, "option `-f' expects an argument");
500 p = argv[++i];
501 }
502 vec = p;
503 p = 0;
504 break;
505 default:
506 die(1, "option `-%c' unknown", p[-1]);
507 break;
508 }
d4efbcd9 509 }
0875b58f 510 }
511
9fcce036
MW
512 /* --- Start parsing from the file --- */
513
514 if ((fp = fopen(vec, "r")) == 0)
515 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
516 suite[0].name = "simple";
517 suite[0].chunks = chunk;
518 suite[1].name = 0;
519 rc = test_do(suite, fp, &res);
520 if (rc < 0)
521 exit(127);
522 if (res.failed) {
523 fprintf(stderr, "FAILED %u of %u test%s\n",
524 res.failed, res.tests, res.tests == 1 ? "" : "s");
525 } else {
526 fprintf(stderr, "PASSED all %u test%s\n",
527 res.tests, res.tests == 1 ? "" : "s");
528 }
529 exit(!!res.failed);
0875b58f 530}
531
532/*----- That's all, folks -------------------------------------------------*/