3 * $Id: testrig.c,v 1.8 1999/12/10 23:41:37 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.8 1999/12/10 23:41:37 mdw
34 * Support for different sizes and types of integers.
36 * Revision 1.7 1999/11/21 13:01:39 mdw
37 * Allow more characters to start words in test vector files.
39 * Revision 1.6 1999/11/16 15:03:23 mdw
40 * Mark test types as constant.
42 * Revision 1.5 1999/05/21 22:14:30 mdw
43 * Take advantage of the new dynamic string macros.
45 * Revision 1.4 1999/05/19 19:02:17 mdw
46 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
47 * constants; abandon old double-underscore convention for internal
48 * functions and variables.
50 * Revision 1.3 1999/05/06 19:51:35 mdw
51 * Reformatted the LGPL notice a little bit.
53 * Revision 1.2 1999/05/05 18:50:31 mdw
54 * Change licensing conditions to LGPL.
56 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
57 * Initial version of mLib
61 /*----- Header files ------------------------------------------------------*/
74 /*----- Static variables --------------------------------------------------*/
83 /*----- Main code ---------------------------------------------------------*/
87 * Arguments: @int tok@ = token type to decode
89 * Returns: Pointer to a textual representation of the token.
91 * Use: Produces a readable representation of a token.
94 static const char *decode(int t)
108 return ("<buggy-program>");
111 /* --- @gettok@ --- *
113 * Arguments: @FILE *fp@ = file handle to read from
115 * Returns: Type of token read.
117 * Use: Reads a token from the input stream.
120 static int gettok(FILE *fp)
124 /* --- Clear the token accumulator --- */
128 /* --- Prime the lookahead character --- */
133 /* --- Skip leading whitespace --- */
135 while (isspace((unsigned char)ch))
138 /* --- Trap some special characters --- */
142 /* --- Comments --- */
145 do ch = getc(fp); while (ch != EOF && ch != '\n');
148 /* --- End of file --- */
153 /* --- Quote characters --- */
163 if (ch == EOF || ch == quote)
176 /* --- Self-delimiting things --- */
183 /* --- Anything else is a word --- */
199 if (isspace((unsigned char)ch))
215 /* --- @type_hex@ --- */
217 static void cvt_hex(const char *s, dstr *d)
219 while (s[0] && s[1]) {
220 int x = s[0], y = s[1];
221 if ('0' <= x && x <= '9') x -= '0';
222 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
223 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
225 if ('0' <= y && y <= '9') y -= '0';
226 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
227 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
229 DPUTC(d, (x << 4) + y);
234 static void dump_hex(dstr *d, FILE *fp)
237 for (p = d->buf, q = p + d->len; p < q; p++)
238 fprintf(fp, "%02x", *(unsigned char *)p);
241 const test_type type_hex = { cvt_hex, dump_hex };
243 /* --- @type_string@ --- */
245 static void cvt_string(const char *s, dstr *d)
250 static void dump_string(dstr *d, FILE *fp)
255 const test_type type_string = { cvt_string, dump_string };
257 /* --- @type_int@ --- */
259 static void cvt_int(const char *s, dstr *d)
261 DENSURE(d, sizeof(int));
262 sscanf(s, "%i", (int *)d->buf);
265 static void dump_int(dstr *d, FILE *fp)
267 fprintf(fp, "%i", *(int *)d->buf);
270 const test_type type_int = { cvt_int, dump_int };
272 /* --- @type_long@ --- */
274 static void cvt_long(const char *s, dstr *d)
276 DENSURE(d, sizeof(long));
277 *(long *)d->buf = strtol(s, 0, 0);
280 static void dump_long(dstr *d, FILE *fp)
282 fprintf(fp, "%li", *(long *)d->buf);
285 const test_type type_long = { cvt_long, dump_long };
287 /* --- @type_ulong@ --- */
289 static void cvt_ulong(const char *s, dstr *d)
291 DENSURE(d, sizeof(unsigned long));
292 *(unsigned long *)d->buf = strtoul(s, 0, 0);
295 static void dump_ulong(dstr *d, FILE *fp)
297 fprintf(fp, "%lu", *(unsigned long *)d->buf);
300 const test_type type_ulong = { cvt_ulong, dump_ulong };
302 /* --- @type_uint32@ --- */
304 static void cvt_uint32(const char *buf, dstr *d)
306 DENSURE(d, sizeof(uint32));
307 *(uint32 *)d->buf = strtoul(buf, 0, 0);
310 static void dump_uint32(dstr *d, FILE *fp)
312 fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
315 const test_type type_uint32 = { cvt_uint32, dump_uint32 };
317 /* --- @test_run@ --- *
319 * Arguments: @int argc@ = number of command line arguments
320 * @char *argv[]@ = pointer to command line arguments
321 * @const test_chunk chunk[]@ = pointer to chunk definitions
322 * @const char *vec@ = name of default test vector file
326 * Use: Runs a set of test vectors to ensure that a component is
330 void test_run(int argc, char *argv[],
331 const test_chunk chunk[],
336 const test_chunk *cch;
337 dstr dv[TEST_FIELDMAX];
338 int fail = 0, ok = 1;
341 /* --- Silly bits of initialization --- */
345 for (i = 0; i < TEST_FIELDMAX; i++)
348 /* --- Parse command line arguments --- */
359 if (strcmp(p, "--") == 0) {
363 if (p[0] != '-' || p[1] == 0)
369 printf("%s test driver\n"
370 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
375 die(1, "option `-f' expects an argument");
382 die(1, "option `-%c' unknown", p[-1]);
388 /* --- Start parsing from the file --- */
390 if ((fp = fopen(vec, "r")) == 0)
391 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
396 /* --- This is a reasonable place to stop --- */
401 /* --- Pick out the chunk name --- */
404 die(1, "expected <word>; found `%s'", decode(t));
406 /* --- Find the right chunk block --- */
408 for (cch = chunk; ; cch++) {
411 if (strcmp(tok.buf, cch->name) == 0)
415 /* --- Past the open brace to the first chunk --- */
417 if ((t = gettok(fp)) != '{')
418 die(1, "expected `{'; found `%s'", decode(t));
420 /* --- Start on the test data now --- */
422 printf("%s: ", cch->name);
430 /* --- Accept a close brace --- */
435 /* --- Otherwise I expect a list of words --- */
437 for (i = 0; cch->f[i]; i++) {
440 die(1, "expected <word>; found `%s'", decode(t));
441 cch->f[i]->cvt(tok.buf, &dv[i]);
445 /* --- And a terminating semicolon --- */
448 die(1, "expected `;'; found `%s'", decode(t));
450 /* --- Run the test code --- */
452 if (!cch->test(dv)) {
453 printf("%s: ", cch->name);
454 for (i = 0; i < sofar; i++) putchar('.');
462 puts(ok ? " ok" : " failed");
467 if ((t = gettok(fp)) != '{')
468 die(1, "expected '{'; found `%s'", decode(t));
473 while (t == TOK_WORD)
476 die(1, "expected `;'; found `%s'", decode(t));
483 /*----- That's all, folks -------------------------------------------------*/