3 * $Id: testrig.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
37 /*----- Header files ------------------------------------------------------*/
50 /*----- Static variables --------------------------------------------------*/
52 static dstr test__tok;
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @test__decode@ --- *
63 * Arguments: @int tok@ = token type to decode
65 * Returns: Pointer to a textual representation of the token.
67 * Use: Produces a readable representation of a token.
70 static const char *test__decode(int tok)
78 return (test__tok.buf);
84 return ("<buggy-program>");
87 /* --- @test__gettok@ --- *
89 * Arguments: @FILE *fp@ = file handle to read from
91 * Returns: Type of token read.
93 * Use: Reads a token from the input stream.
96 static int test__gettok(FILE *fp)
100 /* --- Clear the token accumulator --- */
102 dstr_reset(&test__tok);
104 /* --- Prime the lookahead character --- */
109 /* --- Skip leading whitespace --- */
111 while (isspace((unsigned char)ch))
114 /* --- Trap some special characters --- */
118 /* --- Comments --- */
121 do ch = getc(fp); while (ch != EOF && ch != '\n');
124 /* --- End of file --- */
129 /* --- Quote characters --- */
139 if (ch == EOF || ch == quote)
146 DPUTC(&test__tok, ch);
152 /* --- Anything else is either a word or self-delimiting --- */
155 if (isalnum((unsigned char)ch)) {
157 DPUTC(&test__tok, ch);
161 ch == '\"' || ch == '\'' || ch == '`' ||
162 isspace((unsigned char)ch))
178 /* --- @type_hex@ --- */
180 static void cvt_hex(const char *s, dstr *d)
182 while (s[0] && s[1]) {
183 int x = s[0], y = s[1];
184 if ('0' <= x && x <= '9') x -= '0';
185 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
186 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
188 if ('0' <= y && y <= '9') y -= '0';
189 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
190 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
192 DPUTC(d, (x << 4) + y);
197 static void dump_hex(dstr *d, FILE *fp)
200 for (p = d->buf, q = p + d->len; p < q; p++)
201 fprintf(fp, "%02x", *(unsigned char *)p);
204 test_type type_hex = { cvt_hex, dump_hex };
206 /* --- @type_string@ --- */
208 static void cvt_string(const char *s, dstr *d)
213 static void dump_string(dstr *d, FILE *fp)
218 test_type type_string = { cvt_string, dump_string };
220 /* --- @type_int@ --- */
222 static void cvt_int(const char *s, dstr *d)
224 DENSURE(d, sizeof(long));
225 sscanf(s, "%i", (int *)d->buf + d->len);
228 static void dump_int(dstr *d, FILE *fp)
230 fprintf(fp, "%i", *(int *)(d->buf));
233 test_type type_int = { cvt_int, dump_int };
235 /* --- @test_run@ --- *
237 * Arguments: @int argc@ = number of command line arguments
238 * @char *argv[]@ = pointer to command line arguments
239 * @const test_chunk chunk[]@ = pointer to chunk definitions
240 * @const char *vec@ = name of default test vector file
244 * Use: Runs a set of test vectors to ensure that a component is
248 void test_run(int argc, char *argv[],
249 const test_chunk chunk[],
254 const test_chunk *cch;
255 dstr dv[TEST_FIELDMAX];
256 int fail = 0, ok = 1;
259 /* --- Silly bits of initialisation --- */
263 for (i = 0; i < TEST_FIELDMAX; i++)
266 /* --- Parse command line arguments --- */
277 if (strcmp(p, "--") == 0) {
281 if (p[0] != '-' || p[1] == 0)
287 printf("%s test driver\n"
288 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
293 die(1, "option `-f' expects an argument");
300 die(1, "option `-%c' unknown", p[-1]);
306 /* --- Start parsing from the file --- */
308 if ((fp = fopen(vec, "r")) == 0)
309 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
312 int tok = test__gettok(fp);
314 /* --- This is a reasonable place to stop --- */
319 /* --- Pick out the chunk name --- */
322 die(1, "expected <word>; found `%s'", test__decode(tok));
324 /* --- Find the right chunk block --- */
326 for (cch = chunk; ; cch++) {
329 if (strcmp(test__tok.buf, cch->name) == 0)
333 /* --- Past the open brace to the first chunk --- */
335 if ((tok = test__gettok(fp)) != '{')
336 die(1, "expected '{'; found `%s'", test__decode(tok));
338 /* --- Start on the test data now --- */
340 printf("%s: ", cch->name);
346 tok = test__gettok(fp);
348 /* --- Accept a close brace --- */
353 /* --- Otherwise I expect a list of words --- */
355 for (i = 0; cch->f[i]; i++) {
358 die(1, "expected <word>; found `%s'", test__decode(tok));
359 cch->f[i]->cvt(test__tok.buf, &dv[i]);
360 tok = test__gettok(fp);
363 /* --- And a terminating semicolon --- */
366 die(1, "expected `;'; found `%s'", test__decode(tok));
368 /* --- Run the test code --- */
370 if (!cch->test(dv)) {
371 printf("%s: ", cch->name);
372 for (i = 0; i < sofar; i++) putchar('.');
380 puts(ok ? " ok" : " failed");
385 if ((tok = test__gettok(fp)) != '{')
386 die(1, "expected '{'; found `%s'", test__decode(tok));
388 tok = test__gettok(fp);
391 while (tok == tok_word)
392 tok = test__gettok(fp);
394 die(1, "expected `;'; found `%s'", test__decode(tok));
401 /*----- That's all, folks -------------------------------------------------*/