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_do@ --- *
288 * Arguments: @const test_suite suites[]@ = pointer to suite definitions
289 * @FILE *fp@ = test vector file, ready opened
290 * @test_results *results@ = where to put results
292 * Returns: Negative if something bad happened, or the number of
295 * Use: Runs a collection of tests against a file of test vectors and
296 * reports the results.
299 int test_do(const test_suite suites[], FILE *fp, test_results *results)
302 dstr dv[TEST_FIELDMAX];
303 const test_suite *ss;
304 const test_chunk *chunks = suites[0].chunks;
305 const test_chunk *cch;
310 for (i = 0; i < TEST_FIELDMAX; i++)
321 /* --- This is a reasonable place to stop --- */
326 /* --- Pick out the chunk name --- */
329 moan("expected <word>; found `%s'", decode(t));
333 if (strcmp(tok.buf, "SUITE") == 0) {
336 moan("expected <word>; found `%s'", decode(t));
339 for (ss = suites; ; ss++) {
344 if (strcmp(tok.buf, ss->name) == 0) {
352 /* --- Find the right chunk block --- */
356 for (cch = chunks; ; cch++) {
359 if (strcmp(tok.buf, cch->name) == 0)
363 /* --- Past the open brace to the first chunk --- */
365 if ((t = gettok(fp)) != '{') {
366 moan("expected `{'; found `%s'", decode(t));
370 /* --- Start on the test data now --- */
372 printf("%s: ", cch->name);
379 /* --- Accept a close brace --- */
384 /* --- Otherwise I expect a list of words --- */
386 for (i = 0; cch->f[i]; i++) {
389 moan("expected <word>; found `%s'", decode(t));
392 cch->f[i]->cvt(tok.buf, &dv[i]);
396 /* --- And a terminating semicolon --- */
399 moan("expected `;'; found `%s'", decode(t));
403 /* --- Run the test code --- */
405 if (!cch->test(dv)) {
407 printf("%s: ", cch->name);
408 for (i = 0; i < results->tests; i++) putchar('.');
416 puts(ok ? " ok" : " failed");
421 if ((t = gettok(fp)) != '{') {
422 moan("expected '{'; found `%s'", decode(t));
429 while (t == TOK_WORD)
432 moan("expected `;'; found `%s'", decode(t));
437 rc = results->failed;
439 /* --- All done --- */
442 for (i = 0; i < TEST_FIELDMAX; i++)
443 dstr_destroy(&dv[i]);
447 /* --- @test_run@ --- *
449 * Arguments: @int argc@ = number of command line arguments
450 * @char *argv[]@ = pointer to command line arguments
451 * @const test_chunk chunk[]@ = pointer to chunk definitions
452 * @const char *vec@ = name of default test vector file
456 * Use: Runs a set of test vectors to ensure that a component is
460 void test_run(int argc, char *argv[],
461 const test_chunk chunk[],
469 /* --- Silly bits of initialization --- */
473 /* --- Parse command line arguments --- */
484 if (strcmp(p, "--") == 0) {
488 if (p[0] != '-' || p[1] == 0)
494 printf("%s test driver\n"
495 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
500 die(1, "option `-f' expects an argument");
507 die(1, "option `-%c' unknown", p[-1]);
513 /* --- Start parsing from the file --- */
515 if ((fp = fopen(vec, "r")) == 0)
516 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
517 suite[0].name = "simple";
518 suite[0].chunks = chunk;
520 rc = test_do(suite, fp, &res);
524 fprintf(stderr, "FAILED %u of %u test%s\n",
525 res.failed, res.tests, res.tests == 1 ? "" : "s");
527 fprintf(stderr, "PASSED all %u test%s\n",
528 res.tests, res.tests == 1 ? "" : "s");
533 /*----- That's all, folks -------------------------------------------------*/