3 * $Id: testrig.c,v 1.7 1999/11/21 13:01:39 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.7 1999/11/21 13:01:39 mdw
34 * Allow more characters to start words in test vector files.
36 * Revision 1.6 1999/11/16 15:03:23 mdw
37 * Mark test types as constant.
39 * Revision 1.5 1999/05/21 22:14:30 mdw
40 * Take advantage of the new dynamic string macros.
42 * Revision 1.4 1999/05/19 19:02:17 mdw
43 * Aesthetic changes: fix spelling of `initialize'; use uppercase token
44 * constants; abandon old double-underscore convention for internal
45 * functions and variables.
47 * Revision 1.3 1999/05/06 19:51:35 mdw
48 * Reformatted the LGPL notice a little bit.
50 * Revision 1.2 1999/05/05 18:50:31 mdw
51 * Change licensing conditions to LGPL.
53 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
54 * Initial version of mLib
58 /*----- Header files ------------------------------------------------------*/
71 /*----- Static variables --------------------------------------------------*/
80 /*----- Main code ---------------------------------------------------------*/
84 * Arguments: @int tok@ = token type to decode
86 * Returns: Pointer to a textual representation of the token.
88 * Use: Produces a readable representation of a token.
91 static const char *decode(int t
)
105 return ("<buggy-program>");
108 /* --- @gettok@ --- *
110 * Arguments: @FILE *fp@ = file handle to read from
112 * Returns: Type of token read.
114 * Use: Reads a token from the input stream.
117 static int gettok(FILE *fp
)
121 /* --- Clear the token accumulator --- */
125 /* --- Prime the lookahead character --- */
130 /* --- Skip leading whitespace --- */
132 while (isspace((unsigned char)ch
))
135 /* --- Trap some special characters --- */
139 /* --- Comments --- */
142 do ch
= getc(fp
); while (ch
!= EOF
&& ch
!= '\n');
145 /* --- End of file --- */
150 /* --- Quote characters --- */
160 if (ch
== EOF
|| ch
== quote
)
173 /* --- Self-delimiting things --- */
180 /* --- Anything else is a word --- */
196 if (isspace((unsigned char)ch
))
212 /* --- @type_hex@ --- */
214 static void cvt_hex(const char *s
, dstr
*d
)
216 while (s
[0] && s
[1]) {
217 int x
= s
[0], y
= s
[1];
218 if ('0' <= x
&& x
<= '9') x
-= '0';
219 else if ('A' <= x
&& x
<= 'F') x
-= 'A' - 10;
220 else if ('a' <= x
&& x
<= 'f') x
-= 'a' - 10;
222 if ('0' <= y
&& y
<= '9') y
-= '0';
223 else if ('A' <= y
&& y
<= 'F') y
-= 'A' - 10;
224 else if ('a' <= y
&& y
<= 'f') y
-= 'a' - 10;
226 DPUTC(d
, (x
<< 4) + y
);
231 static void dump_hex(dstr
*d
, FILE *fp
)
234 for (p
= d
->buf
, q
= p
+ d
->len
; p
< q
; p
++)
235 fprintf(fp
, "%02x", *(unsigned char *)p
);
238 const test_type type_hex
= { cvt_hex
, dump_hex
};
240 /* --- @type_string@ --- */
242 static void cvt_string(const char *s
, dstr
*d
)
247 static void dump_string(dstr
*d
, FILE *fp
)
252 const test_type type_string
= { cvt_string
, dump_string
};
254 /* --- @type_int@ --- */
256 static void cvt_int(const char *s
, dstr
*d
)
258 DENSURE(d
, sizeof(long));
259 sscanf(s
, "%i", (int *)d
->buf
+ d
->len
);
262 static void dump_int(dstr
*d
, FILE *fp
)
264 fprintf(fp
, "%i", *(int *)(d
->buf
));
267 const test_type type_int
= { cvt_int
, dump_int
};
269 /* --- @test_run@ --- *
271 * Arguments: @int argc@ = number of command line arguments
272 * @char *argv[]@ = pointer to command line arguments
273 * @const test_chunk chunk[]@ = pointer to chunk definitions
274 * @const char *vec@ = name of default test vector file
278 * Use: Runs a set of test vectors to ensure that a component is
282 void test_run(int argc
, char *argv
[],
283 const test_chunk chunk
[],
288 const test_chunk
*cch
;
289 dstr dv
[TEST_FIELDMAX
];
290 int fail
= 0, ok
= 1;
293 /* --- Silly bits of initialization --- */
297 for (i
= 0; i
< TEST_FIELDMAX
; i
++)
300 /* --- Parse command line arguments --- */
311 if (strcmp(p
, "--") == 0) {
315 if (p
[0] != '-' || p
[1] == 0)
321 printf("%s test driver\n"
322 "Usage: %s [-f FILENAME]\n", QUIS
, QUIS
);
327 die(1, "option `-f' expects an argument");
334 die(1, "option `-%c' unknown", p
[-1]);
340 /* --- Start parsing from the file --- */
342 if ((fp
= fopen(vec
, "r")) == 0)
343 die(1, "couldn't open test vector file `%s': %s", vec
, strerror(errno
));
348 /* --- This is a reasonable place to stop --- */
353 /* --- Pick out the chunk name --- */
356 die(1, "expected <word>; found `%s'", decode(t
));
358 /* --- Find the right chunk block --- */
360 for (cch
= chunk
; ; cch
++) {
363 if (strcmp(tok
.buf
, cch
->name
) == 0)
367 /* --- Past the open brace to the first chunk --- */
369 if ((t
= gettok(fp
)) != '{')
370 die(1, "expected `{'; found `%s'", decode(t
));
372 /* --- Start on the test data now --- */
374 printf("%s: ", cch
->name
);
382 /* --- Accept a close brace --- */
387 /* --- Otherwise I expect a list of words --- */
389 for (i
= 0; cch
->f
[i
]; i
++) {
392 die(1, "expected <word>; found `%s'", decode(t
));
393 cch
->f
[i
]->cvt(tok
.buf
, &dv
[i
]);
397 /* --- And a terminating semicolon --- */
400 die(1, "expected `;'; found `%s'", decode(t
));
402 /* --- Run the test code --- */
404 if (!cch
->test(dv
)) {
405 printf("%s: ", cch
->name
);
406 for (i
= 0; i
< sofar
; i
++) putchar('.');
414 puts(ok ?
" ok" : " failed");
419 if ((t
= gettok(fp
)) != '{')
420 die(1, "expected '{'; found `%s'", decode(t
));
425 while (t
== TOK_WORD
)
428 die(1, "expected `;'; found `%s'", decode(t
));
435 /*----- That's all, folks -------------------------------------------------*/