3 * $Id: testrig.c,v 1.5 1999/05/21 22:14:30 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.5 1999/05/21 22:14:30 mdw
34 * Take advantage of the new dynamic string macros.
36 * Revision 1.4 1999/05/19 19:02:17 mdw
37 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
38 * constants; abandon old double-underscore convention for internal
39 * functions and variables.
41 * Revision 1.3 1999/05/06 19:51:35 mdw
42 * Reformatted the LGPL notice a little bit.
44 * Revision 1.2 1999/05/05 18:50:31 mdw
45 * Change licensing conditions to LGPL.
47 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
48 * Initial version of mLib
52 /*----- Header files ------------------------------------------------------*/
65 /*----- Static variables --------------------------------------------------*/
74 /*----- Main code ---------------------------------------------------------*/
78 * Arguments: @int tok@ = token type to decode
80 * Returns: Pointer to a textual representation of the token.
82 * Use: Produces a readable representation of a token.
85 static const char *decode(int t)
99 return ("<buggy-program>");
102 /* --- @gettok@ --- *
104 * Arguments: @FILE *fp@ = file handle to read from
106 * Returns: Type of token read.
108 * Use: Reads a token from the input stream.
111 static int gettok(FILE *fp)
115 /* --- Clear the token accumulator --- */
119 /* --- Prime the lookahead character --- */
124 /* --- Skip leading whitespace --- */
126 while (isspace((unsigned char)ch))
129 /* --- Trap some special characters --- */
133 /* --- Comments --- */
136 do ch = getc(fp); while (ch != EOF && ch != '\n');
139 /* --- End of file --- */
144 /* --- Quote characters --- */
154 if (ch == EOF || ch == quote)
167 /* --- Anything else is either a word or self-delimiting --- */
170 if (isalnum((unsigned char)ch)) {
176 ch == '\"' || ch == '\'' || ch == '`' ||
177 isspace((unsigned char)ch))
193 /* --- @type_hex@ --- */
195 static void cvt_hex(const char *s, dstr *d)
197 while (s[0] && s[1]) {
198 int x = s[0], y = s[1];
199 if ('0' <= x && x <= '9') x -= '0';
200 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
201 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
203 if ('0' <= y && y <= '9') y -= '0';
204 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
205 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
207 DPUTC(d, (x << 4) + y);
212 static void dump_hex(dstr *d, FILE *fp)
215 for (p = d->buf, q = p + d->len; p < q; p++)
216 fprintf(fp, "%02x", *(unsigned char *)p);
219 test_type type_hex = { cvt_hex, dump_hex };
221 /* --- @type_string@ --- */
223 static void cvt_string(const char *s, dstr *d)
228 static void dump_string(dstr *d, FILE *fp)
233 test_type type_string = { cvt_string, dump_string };
235 /* --- @type_int@ --- */
237 static void cvt_int(const char *s, dstr *d)
239 DENSURE(d, sizeof(long));
240 sscanf(s, "%i", (int *)d->buf + d->len);
243 static void dump_int(dstr *d, FILE *fp)
245 fprintf(fp, "%i", *(int *)(d->buf));
248 test_type type_int = { cvt_int, dump_int };
250 /* --- @test_run@ --- *
252 * Arguments: @int argc@ = number of command line arguments
253 * @char *argv[]@ = pointer to command line arguments
254 * @const test_chunk chunk[]@ = pointer to chunk definitions
255 * @const char *vec@ = name of default test vector file
259 * Use: Runs a set of test vectors to ensure that a component is
263 void test_run(int argc, char *argv[],
264 const test_chunk chunk[],
269 const test_chunk *cch;
270 dstr dv[TEST_FIELDMAX];
271 int fail = 0, ok = 1;
274 /* --- Silly bits of initialization --- */
278 for (i = 0; i < TEST_FIELDMAX; i++)
281 /* --- Parse command line arguments --- */
292 if (strcmp(p, "--") == 0) {
296 if (p[0] != '-' || p[1] == 0)
302 printf("%s test driver\n"
303 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
308 die(1, "option `-f' expects an argument");
315 die(1, "option `-%c' unknown", p[-1]);
321 /* --- Start parsing from the file --- */
323 if ((fp = fopen(vec, "r")) == 0)
324 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
329 /* --- This is a reasonable place to stop --- */
334 /* --- Pick out the chunk name --- */
337 die(1, "expected <word>; found `%s'", decode(t));
339 /* --- Find the right chunk block --- */
341 for (cch = chunk; ; cch++) {
344 if (strcmp(tok.buf, cch->name) == 0)
348 /* --- Past the open brace to the first chunk --- */
350 if ((t = gettok(fp)) != '{')
351 die(1, "expected '{'; found `%s'", decode(t));
353 /* --- Start on the test data now --- */
355 printf("%s: ", cch->name);
363 /* --- Accept a close brace --- */
368 /* --- Otherwise I expect a list of words --- */
370 for (i = 0; cch->f[i]; i++) {
373 die(1, "expected <word>; found `%s'", decode(t));
374 cch->f[i]->cvt(tok.buf, &dv[i]);
378 /* --- And a terminating semicolon --- */
381 die(1, "expected `;'; found `%s'", decode(t));
383 /* --- Run the test code --- */
385 if (!cch->test(dv)) {
386 printf("%s: ", cch->name);
387 for (i = 0; i < sofar; i++) putchar('.');
395 puts(ok ? " ok" : " failed");
400 if ((t = gettok(fp)) != '{')
401 die(1, "expected '{'; found `%s'", decode(t));
406 while (t == TOK_WORD)
409 die(1, "expected `;'; found `%s'", decode(t));
416 /*----- That's all, folks -------------------------------------------------*/