3 * $Id: testrig.c,v 1.9 2000/06/17 10:35:07 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.9 2000/06/17 10:35:07 mdw
34 * Initialize the token buffer properly.
36 * Revision 1.8 1999/12/10 23:41:37 mdw
37 * Support for different sizes and types of integers.
39 * Revision 1.7 1999/11/21 13:01:39 mdw
40 * Allow more characters to start words in test vector files.
42 * Revision 1.6 1999/11/16 15:03:23 mdw
43 * Mark test types as constant.
45 * Revision 1.5 1999/05/21 22:14:30 mdw
46 * Take advantage of the new dynamic string macros.
48 * Revision 1.4 1999/05/19 19:02:17 mdw
49 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
50 * constants; abandon old double-underscore convention for internal
51 * functions and variables.
53 * Revision 1.3 1999/05/06 19:51:35 mdw
54 * Reformatted the LGPL notice a little bit.
56 * Revision 1.2 1999/05/05 18:50:31 mdw
57 * Change licensing conditions to LGPL.
59 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
60 * Initial version of mLib
64 /*----- Header files ------------------------------------------------------*/
77 /*----- Static variables --------------------------------------------------*/
79 static dstr tok = DSTR_INIT;
86 /*----- Main code ---------------------------------------------------------*/
90 * Arguments: @int tok@ = token type to decode
92 * Returns: Pointer to a textual representation of the token.
94 * Use: Produces a readable representation of a token.
97 static const char *decode(int t)
111 return ("<buggy-program>");
114 /* --- @gettok@ --- *
116 * Arguments: @FILE *fp@ = file handle to read from
118 * Returns: Type of token read.
120 * Use: Reads a token from the input stream.
123 static int gettok(FILE *fp)
127 /* --- Clear the token accumulator --- */
131 /* --- Prime the lookahead character --- */
136 /* --- Skip leading whitespace --- */
138 while (isspace((unsigned char)ch))
141 /* --- Trap some special characters --- */
145 /* --- Comments --- */
148 do ch = getc(fp); while (ch != EOF && ch != '\n');
151 /* --- End of file --- */
156 /* --- Quote characters --- */
166 if (ch == EOF || ch == quote)
179 /* --- Self-delimiting things --- */
186 /* --- Anything else is a word --- */
202 if (isspace((unsigned char)ch))
218 /* --- @type_hex@ --- */
220 static void cvt_hex(const char *s, dstr *d)
222 while (s[0] && s[1]) {
223 int x = s[0], y = s[1];
224 if ('0' <= x && x <= '9') x -= '0';
225 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
226 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
228 if ('0' <= y && y <= '9') y -= '0';
229 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
230 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
232 DPUTC(d, (x << 4) + y);
237 static void dump_hex(dstr *d, FILE *fp)
240 for (p = d->buf, q = p + d->len; p < q; p++)
241 fprintf(fp, "%02x", *(unsigned char *)p);
244 const test_type type_hex = { cvt_hex, dump_hex };
246 /* --- @type_string@ --- */
248 static void cvt_string(const char *s, dstr *d)
253 static void dump_string(dstr *d, FILE *fp)
258 const test_type type_string = { cvt_string, dump_string };
260 /* --- @type_int@ --- */
262 static void cvt_int(const char *s, dstr *d)
264 DENSURE(d, sizeof(int));
265 sscanf(s, "%i", (int *)d->buf);
268 static void dump_int(dstr *d, FILE *fp)
270 fprintf(fp, "%i", *(int *)d->buf);
273 const test_type type_int = { cvt_int, dump_int };
275 /* --- @type_long@ --- */
277 static void cvt_long(const char *s, dstr *d)
279 DENSURE(d, sizeof(long));
280 *(long *)d->buf = strtol(s, 0, 0);
283 static void dump_long(dstr *d, FILE *fp)
285 fprintf(fp, "%li", *(long *)d->buf);
288 const test_type type_long = { cvt_long, dump_long };
290 /* --- @type_ulong@ --- */
292 static void cvt_ulong(const char *s, dstr *d)
294 DENSURE(d, sizeof(unsigned long));
295 *(unsigned long *)d->buf = strtoul(s, 0, 0);
298 static void dump_ulong(dstr *d, FILE *fp)
300 fprintf(fp, "%lu", *(unsigned long *)d->buf);
303 const test_type type_ulong = { cvt_ulong, dump_ulong };
305 /* --- @type_uint32@ --- */
307 static void cvt_uint32(const char *buf, dstr *d)
309 DENSURE(d, sizeof(uint32));
310 *(uint32 *)d->buf = strtoul(buf, 0, 0);
313 static void dump_uint32(dstr *d, FILE *fp)
315 fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
318 const test_type type_uint32 = { cvt_uint32, dump_uint32 };
320 /* --- @test_run@ --- *
322 * Arguments: @int argc@ = number of command line arguments
323 * @char *argv[]@ = pointer to command line arguments
324 * @const test_chunk chunk[]@ = pointer to chunk definitions
325 * @const char *vec@ = name of default test vector file
329 * Use: Runs a set of test vectors to ensure that a component is
333 void test_run(int argc, char *argv[],
334 const test_chunk chunk[],
339 const test_chunk *cch;
340 dstr dv[TEST_FIELDMAX];
341 int fail = 0, ok = 1;
344 /* --- Silly bits of initialization --- */
348 for (i = 0; i < TEST_FIELDMAX; i++)
351 /* --- Parse command line arguments --- */
362 if (strcmp(p, "--") == 0) {
366 if (p[0] != '-' || p[1] == 0)
372 printf("%s test driver\n"
373 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
378 die(1, "option `-f' expects an argument");
385 die(1, "option `-%c' unknown", p[-1]);
391 /* --- Start parsing from the file --- */
393 if ((fp = fopen(vec, "r")) == 0)
394 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
399 /* --- This is a reasonable place to stop --- */
404 /* --- Pick out the chunk name --- */
407 die(1, "expected <word>; found `%s'", decode(t));
409 /* --- Find the right chunk block --- */
411 for (cch = chunk; ; cch++) {
414 if (strcmp(tok.buf, cch->name) == 0)
418 /* --- Past the open brace to the first chunk --- */
420 if ((t = gettok(fp)) != '{')
421 die(1, "expected `{'; found `%s'", decode(t));
423 /* --- Start on the test data now --- */
425 printf("%s: ", cch->name);
433 /* --- Accept a close brace --- */
438 /* --- Otherwise I expect a list of words --- */
440 for (i = 0; cch->f[i]; i++) {
443 die(1, "expected <word>; found `%s'", decode(t));
444 cch->f[i]->cvt(tok.buf, &dv[i]);
448 /* --- And a terminating semicolon --- */
451 die(1, "expected `;'; found `%s'", decode(t));
453 /* --- Run the test code --- */
455 if (!cch->test(dv)) {
456 printf("%s: ", cch->name);
457 for (i = 0; i < sofar; i++) putchar('.');
465 puts(ok ? " ok" : " failed");
470 if ((t = gettok(fp)) != '{')
471 die(1, "expected '{'; found `%s'", decode(t));
476 while (t == TOK_WORD)
479 die(1, "expected `;'; found `%s'", decode(t));
486 /*----- That's all, folks -------------------------------------------------*/