3 * $Id: testrig.c,v 1.6 1999/11/16 15:03:23 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.6 1999/11/16 15:03:23 mdw
34 * Mark test types as constant.
36 * Revision 1.5 1999/05/21 22:14:30 mdw
37 * Take advantage of the new dynamic string macros.
39 * Revision 1.4 1999/05/19 19:02:17 mdw
40 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
41 * constants; abandon old double-underscore convention for internal
42 * functions and variables.
44 * Revision 1.3 1999/05/06 19:51:35 mdw
45 * Reformatted the LGPL notice a little bit.
47 * Revision 1.2 1999/05/05 18:50:31 mdw
48 * Change licensing conditions to LGPL.
50 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
51 * Initial version of mLib
55 /*----- Header files ------------------------------------------------------*/
68 /*----- Static variables --------------------------------------------------*/
77 /*----- Main code ---------------------------------------------------------*/
81 * Arguments: @int tok@ = token type to decode
83 * Returns: Pointer to a textual representation of the token.
85 * Use: Produces a readable representation of a token.
88 static const char *decode(int t)
102 return ("<buggy-program>");
105 /* --- @gettok@ --- *
107 * Arguments: @FILE *fp@ = file handle to read from
109 * Returns: Type of token read.
111 * Use: Reads a token from the input stream.
114 static int gettok(FILE *fp)
118 /* --- Clear the token accumulator --- */
122 /* --- Prime the lookahead character --- */
127 /* --- Skip leading whitespace --- */
129 while (isspace((unsigned char)ch))
132 /* --- Trap some special characters --- */
136 /* --- Comments --- */
139 do ch = getc(fp); while (ch != EOF && ch != '\n');
142 /* --- End of file --- */
147 /* --- Quote characters --- */
157 if (ch == EOF || ch == quote)
170 /* --- Anything else is either a word or self-delimiting --- */
173 if (isalnum((unsigned char)ch)) {
179 ch == '\"' || ch == '\'' || ch == '`' ||
180 isspace((unsigned char)ch))
196 /* --- @type_hex@ --- */
198 static void cvt_hex(const char *s, dstr *d)
200 while (s[0] && s[1]) {
201 int x = s[0], y = s[1];
202 if ('0' <= x && x <= '9') x -= '0';
203 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
204 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
206 if ('0' <= y && y <= '9') y -= '0';
207 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
208 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
210 DPUTC(d, (x << 4) + y);
215 static void dump_hex(dstr *d, FILE *fp)
218 for (p = d->buf, q = p + d->len; p < q; p++)
219 fprintf(fp, "%02x", *(unsigned char *)p);
222 const test_type type_hex = { cvt_hex, dump_hex };
224 /* --- @type_string@ --- */
226 static void cvt_string(const char *s, dstr *d)
231 static void dump_string(dstr *d, FILE *fp)
236 const test_type type_string = { cvt_string, dump_string };
238 /* --- @type_int@ --- */
240 static void cvt_int(const char *s, dstr *d)
242 DENSURE(d, sizeof(long));
243 sscanf(s, "%i", (int *)d->buf + d->len);
246 static void dump_int(dstr *d, FILE *fp)
248 fprintf(fp, "%i", *(int *)(d->buf));
251 const test_type type_int = { cvt_int, dump_int };
253 /* --- @test_run@ --- *
255 * Arguments: @int argc@ = number of command line arguments
256 * @char *argv[]@ = pointer to command line arguments
257 * @const test_chunk chunk[]@ = pointer to chunk definitions
258 * @const char *vec@ = name of default test vector file
262 * Use: Runs a set of test vectors to ensure that a component is
266 void test_run(int argc, char *argv[],
267 const test_chunk chunk[],
272 const test_chunk *cch;
273 dstr dv[TEST_FIELDMAX];
274 int fail = 0, ok = 1;
277 /* --- Silly bits of initialization --- */
281 for (i = 0; i < TEST_FIELDMAX; i++)
284 /* --- Parse command line arguments --- */
295 if (strcmp(p, "--") == 0) {
299 if (p[0] != '-' || p[1] == 0)
305 printf("%s test driver\n"
306 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
311 die(1, "option `-f' expects an argument");
318 die(1, "option `-%c' unknown", p[-1]);
324 /* --- Start parsing from the file --- */
326 if ((fp = fopen(vec, "r")) == 0)
327 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
332 /* --- This is a reasonable place to stop --- */
337 /* --- Pick out the chunk name --- */
340 die(1, "expected <word>; found `%s'", decode(t));
342 /* --- Find the right chunk block --- */
344 for (cch = chunk; ; cch++) {
347 if (strcmp(tok.buf, cch->name) == 0)
351 /* --- Past the open brace to the first chunk --- */
353 if ((t = gettok(fp)) != '{')
354 die(1, "expected '{'; found `%s'", decode(t));
356 /* --- Start on the test data now --- */
358 printf("%s: ", cch->name);
366 /* --- Accept a close brace --- */
371 /* --- Otherwise I expect a list of words --- */
373 for (i = 0; cch->f[i]; i++) {
376 die(1, "expected <word>; found `%s'", decode(t));
377 cch->f[i]->cvt(tok.buf, &dv[i]);
381 /* --- And a terminating semicolon --- */
384 die(1, "expected `;'; found `%s'", decode(t));
386 /* --- Run the test code --- */
388 if (!cch->test(dv)) {
389 printf("%s: ", cch->name);
390 for (i = 0; i < sofar; i++) putchar('.');
398 puts(ok ? " ok" : " failed");
403 if ((t = gettok(fp)) != '{')
404 die(1, "expected '{'; found `%s'", decode(t));
409 while (t == TOK_WORD)
412 die(1, "expected `;'; found `%s'", decode(t));
419 /*----- That's all, folks -------------------------------------------------*/