3 * $Id: testrig.c,v 1.4 1999/05/19 19:02:17 mdw Exp $
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/05/19 19:02:17 mdw
34 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
35 * constants; abandon old double-underscore convention for internal
36 * functions and variables.
38 * Revision 1.3 1999/05/06 19:51:35 mdw
39 * Reformatted the LGPL notice a little bit.
41 * Revision 1.2 1999/05/05 18:50:31 mdw
42 * Change licensing conditions to LGPL.
44 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
45 * Initial version of mLib
49 /*----- Header files ------------------------------------------------------*/
62 /*----- Static variables --------------------------------------------------*/
71 /*----- Main code ---------------------------------------------------------*/
75 * Arguments: @int tok@ = token type to decode
77 * Returns: Pointer to a textual representation of the token.
79 * Use: Produces a readable representation of a token.
82 static const char *decode(int t)
96 return ("<buggy-program>");
101 * Arguments: @FILE *fp@ = file handle to read from
103 * Returns: Type of token read.
105 * Use: Reads a token from the input stream.
108 static int gettok(FILE *fp)
112 /* --- Clear the token accumulator --- */
116 /* --- Prime the lookahead character --- */
121 /* --- Skip leading whitespace --- */
123 while (isspace((unsigned char)ch))
126 /* --- Trap some special characters --- */
130 /* --- Comments --- */
133 do ch = getc(fp); while (ch != EOF && ch != '\n');
136 /* --- End of file --- */
141 /* --- Quote characters --- */
151 if (ch == EOF || ch == quote)
164 /* --- Anything else is either a word or self-delimiting --- */
167 if (isalnum((unsigned char)ch)) {
173 ch == '\"' || ch == '\'' || ch == '`' ||
174 isspace((unsigned char)ch))
190 /* --- @type_hex@ --- */
192 static void cvt_hex(const char *s, dstr *d)
194 while (s[0] && s[1]) {
195 int x = s[0], y = s[1];
196 if ('0' <= x && x <= '9') x -= '0';
197 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
198 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
200 if ('0' <= y && y <= '9') y -= '0';
201 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
202 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
204 DPUTC(d, (x << 4) + y);
209 static void dump_hex(dstr *d, FILE *fp)
212 for (p = d->buf, q = p + d->len; p < q; p++)
213 fprintf(fp, "%02x", *(unsigned char *)p);
216 test_type type_hex = { cvt_hex, dump_hex };
218 /* --- @type_string@ --- */
220 static void cvt_string(const char *s, dstr *d)
225 static void dump_string(dstr *d, FILE *fp)
230 test_type type_string = { cvt_string, dump_string };
232 /* --- @type_int@ --- */
234 static void cvt_int(const char *s, dstr *d)
236 DENSURE(d, sizeof(long));
237 sscanf(s, "%i", (int *)d->buf + d->len);
240 static void dump_int(dstr *d, FILE *fp)
242 fprintf(fp, "%i", *(int *)(d->buf));
245 test_type type_int = { cvt_int, dump_int };
247 /* --- @test_run@ --- *
249 * Arguments: @int argc@ = number of command line arguments
250 * @char *argv[]@ = pointer to command line arguments
251 * @const test_chunk chunk[]@ = pointer to chunk definitions
252 * @const char *vec@ = name of default test vector file
256 * Use: Runs a set of test vectors to ensure that a component is
260 void test_run(int argc, char *argv[],
261 const test_chunk chunk[],
266 const test_chunk *cch;
267 dstr dv[TEST_FIELDMAX];
268 int fail = 0, ok = 1;
271 /* --- Silly bits of initialization --- */
275 for (i = 0; i < TEST_FIELDMAX; i++)
278 /* --- Parse command line arguments --- */
289 if (strcmp(p, "--") == 0) {
293 if (p[0] != '-' || p[1] == 0)
299 printf("%s test driver\n"
300 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
305 die(1, "option `-f' expects an argument");
312 die(1, "option `-%c' unknown", p[-1]);
318 /* --- Start parsing from the file --- */
320 if ((fp = fopen(vec, "r")) == 0)
321 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
326 /* --- This is a reasonable place to stop --- */
331 /* --- Pick out the chunk name --- */
334 die(1, "expected <word>; found `%s'", decode(t));
336 /* --- Find the right chunk block --- */
338 for (cch = chunk; ; cch++) {
341 if (strcmp(tok.buf, cch->name) == 0)
345 /* --- Past the open brace to the first chunk --- */
347 if ((t = gettok(fp)) != '{')
348 die(1, "expected '{'; found `%s'", decode(t));
350 /* --- Start on the test data now --- */
352 printf("%s: ", cch->name);
360 /* --- Accept a close brace --- */
365 /* --- Otherwise I expect a list of words --- */
367 for (i = 0; cch->f[i]; i++) {
370 die(1, "expected <word>; found `%s'", decode(t));
371 cch->f[i]->cvt(tok.buf, &dv[i]);
375 /* --- And a terminating semicolon --- */
378 die(1, "expected `;'; found `%s'", decode(t));
380 /* --- Run the test code --- */
382 if (!cch->test(dv)) {
383 printf("%s: ", cch->name);
384 for (i = 0; i < sofar; i++) putchar('.');
392 puts(ok ? " ok" : " failed");
397 if ((t = gettok(fp)) != '{')
398 die(1, "expected '{'; found `%s'", decode(t));
403 while (t == TOK_WORD)
406 die(1, "expected `;'; found `%s'", decode(t));
413 /*----- That's all, folks -------------------------------------------------*/