3 * $Id: testrig.c,v 1.10 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
43 /*----- Static variables --------------------------------------------------*/
45 static dstr tok = DSTR_INIT;
52 /*----- Main code ---------------------------------------------------------*/
56 * Arguments: @int tok@ = token type to decode
58 * Returns: Pointer to a textual representation of the token.
60 * Use: Produces a readable representation of a token.
63 static const char *decode(int t)
77 return ("<buggy-program>");
82 * Arguments: @FILE *fp@ = file handle to read from
84 * Returns: Type of token read.
86 * Use: Reads a token from the input stream.
89 static int gettok(FILE *fp)
93 /* --- Clear the token accumulator --- */
97 /* --- Prime the lookahead character --- */
102 /* --- Skip leading whitespace --- */
104 while (isspace((unsigned char)ch))
107 /* --- Trap some special characters --- */
111 /* --- Comments --- */
114 do ch = getc(fp); while (ch != EOF && ch != '\n');
117 /* --- End of file --- */
122 /* --- Quote characters --- */
132 if (ch == EOF || ch == quote)
145 /* --- Self-delimiting things --- */
152 /* --- Anything else is a word --- */
168 if (isspace((unsigned char)ch))
184 /* --- @type_hex@ --- */
186 static void cvt_hex(const char *s, dstr *d)
188 while (s[0] && s[1]) {
189 int x = s[0], y = s[1];
190 if ('0' <= x && x <= '9') x -= '0';
191 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
192 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
194 if ('0' <= y && y <= '9') y -= '0';
195 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
196 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
198 DPUTC(d, (x << 4) + y);
203 static void dump_hex(dstr *d, FILE *fp)
206 for (p = d->buf, q = p + d->len; p < q; p++)
207 fprintf(fp, "%02x", *(unsigned char *)p);
210 const test_type type_hex = { cvt_hex, dump_hex };
212 /* --- @type_string@ --- */
214 static void cvt_string(const char *s, dstr *d)
219 static void dump_string(dstr *d, FILE *fp)
224 const test_type type_string = { cvt_string, dump_string };
226 /* --- @type_int@ --- */
228 static void cvt_int(const char *s, dstr *d)
230 DENSURE(d, sizeof(int));
231 sscanf(s, "%i", (int *)d->buf);
234 static void dump_int(dstr *d, FILE *fp)
236 fprintf(fp, "%i", *(int *)d->buf);
239 const test_type type_int = { cvt_int, dump_int };
241 /* --- @type_long@ --- */
243 static void cvt_long(const char *s, dstr *d)
245 DENSURE(d, sizeof(long));
246 *(long *)d->buf = strtol(s, 0, 0);
249 static void dump_long(dstr *d, FILE *fp)
251 fprintf(fp, "%li", *(long *)d->buf);
254 const test_type type_long = { cvt_long, dump_long };
256 /* --- @type_ulong@ --- */
258 static void cvt_ulong(const char *s, dstr *d)
260 DENSURE(d, sizeof(unsigned long));
261 *(unsigned long *)d->buf = strtoul(s, 0, 0);
264 static void dump_ulong(dstr *d, FILE *fp)
266 fprintf(fp, "%lu", *(unsigned long *)d->buf);
269 const test_type type_ulong = { cvt_ulong, dump_ulong };
271 /* --- @type_uint32@ --- */
273 static void cvt_uint32(const char *buf, dstr *d)
275 DENSURE(d, sizeof(uint32));
276 *(uint32 *)d->buf = strtoul(buf, 0, 0);
279 static void dump_uint32(dstr *d, FILE *fp)
281 fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
284 const test_type type_uint32 = { cvt_uint32, dump_uint32 };
286 /* --- @test_run@ --- *
288 * Arguments: @int argc@ = number of command line arguments
289 * @char *argv[]@ = pointer to command line arguments
290 * @const test_chunk chunk[]@ = pointer to chunk definitions
291 * @const char *vec@ = name of default test vector file
295 * Use: Runs a set of test vectors to ensure that a component is
299 void test_run(int argc, char *argv[],
300 const test_chunk chunk[],
305 const test_chunk *cch;
306 dstr dv[TEST_FIELDMAX];
307 int fail = 0, ok = 1;
310 /* --- Silly bits of initialization --- */
314 for (i = 0; i < TEST_FIELDMAX; i++)
317 /* --- Parse command line arguments --- */
328 if (strcmp(p, "--") == 0) {
332 if (p[0] != '-' || p[1] == 0)
338 printf("%s test driver\n"
339 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
344 die(1, "option `-f' expects an argument");
351 die(1, "option `-%c' unknown", p[-1]);
357 /* --- Start parsing from the file --- */
359 if ((fp = fopen(vec, "r")) == 0)
360 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
365 /* --- This is a reasonable place to stop --- */
370 /* --- Pick out the chunk name --- */
373 die(1, "expected <word>; found `%s'", decode(t));
375 /* --- Find the right chunk block --- */
377 for (cch = chunk; ; cch++) {
380 if (strcmp(tok.buf, cch->name) == 0)
384 /* --- Past the open brace to the first chunk --- */
386 if ((t = gettok(fp)) != '{')
387 die(1, "expected `{'; found `%s'", decode(t));
389 /* --- Start on the test data now --- */
391 printf("%s: ", cch->name);
399 /* --- Accept a close brace --- */
404 /* --- Otherwise I expect a list of words --- */
406 for (i = 0; cch->f[i]; i++) {
409 die(1, "expected <word>; found `%s'", decode(t));
410 cch->f[i]->cvt(tok.buf, &dv[i]);
414 /* --- And a terminating semicolon --- */
417 die(1, "expected `;'; found `%s'", decode(t));
419 /* --- Run the test code --- */
421 if (!cch->test(dv)) {
422 printf("%s: ", cch->name);
423 for (i = 0; i < sofar; i++) putchar('.');
431 puts(ok ? " ok" : " failed");
436 if ((t = gettok(fp)) != '{')
437 die(1, "expected '{'; found `%s'", decode(t));
442 while (t == TOK_WORD)
445 die(1, "expected `;'; found `%s'", decode(t));
452 /*----- That's all, folks -------------------------------------------------*/