3 * $Id: testrig.c,v 1.3 1999/05/06 19:51:35 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.3 1999/05/06 19:51:35 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.2 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
44 /*----- Header files ------------------------------------------------------*/
57 /*----- Static variables --------------------------------------------------*/
59 static dstr test__tok;
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @test__decode@ --- *
70 * Arguments: @int tok@ = token type to decode
72 * Returns: Pointer to a textual representation of the token.
74 * Use: Produces a readable representation of a token.
77 static const char *test__decode(int tok)
85 return (test__tok.buf);
91 return ("<buggy-program>");
94 /* --- @test__gettok@ --- *
96 * Arguments: @FILE *fp@ = file handle to read from
98 * Returns: Type of token read.
100 * Use: Reads a token from the input stream.
103 static int test__gettok(FILE *fp)
107 /* --- Clear the token accumulator --- */
109 dstr_reset(&test__tok);
111 /* --- Prime the lookahead character --- */
116 /* --- Skip leading whitespace --- */
118 while (isspace((unsigned char)ch))
121 /* --- Trap some special characters --- */
125 /* --- Comments --- */
128 do ch = getc(fp); while (ch != EOF && ch != '\n');
131 /* --- End of file --- */
136 /* --- Quote characters --- */
146 if (ch == EOF || ch == quote)
153 DPUTC(&test__tok, ch);
159 /* --- Anything else is either a word or self-delimiting --- */
162 if (isalnum((unsigned char)ch)) {
164 DPUTC(&test__tok, ch);
168 ch == '\"' || ch == '\'' || ch == '`' ||
169 isspace((unsigned char)ch))
185 /* --- @type_hex@ --- */
187 static void cvt_hex(const char *s, dstr *d)
189 while (s[0] && s[1]) {
190 int x = s[0], y = s[1];
191 if ('0' <= x && x <= '9') x -= '0';
192 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
193 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
195 if ('0' <= y && y <= '9') y -= '0';
196 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
197 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
199 DPUTC(d, (x << 4) + y);
204 static void dump_hex(dstr *d, FILE *fp)
207 for (p = d->buf, q = p + d->len; p < q; p++)
208 fprintf(fp, "%02x", *(unsigned char *)p);
211 test_type type_hex = { cvt_hex, dump_hex };
213 /* --- @type_string@ --- */
215 static void cvt_string(const char *s, dstr *d)
220 static void dump_string(dstr *d, FILE *fp)
225 test_type type_string = { cvt_string, dump_string };
227 /* --- @type_int@ --- */
229 static void cvt_int(const char *s, dstr *d)
231 DENSURE(d, sizeof(long));
232 sscanf(s, "%i", (int *)d->buf + d->len);
235 static void dump_int(dstr *d, FILE *fp)
237 fprintf(fp, "%i", *(int *)(d->buf));
240 test_type type_int = { cvt_int, dump_int };
242 /* --- @test_run@ --- *
244 * Arguments: @int argc@ = number of command line arguments
245 * @char *argv[]@ = pointer to command line arguments
246 * @const test_chunk chunk[]@ = pointer to chunk definitions
247 * @const char *vec@ = name of default test vector file
251 * Use: Runs a set of test vectors to ensure that a component is
255 void test_run(int argc, char *argv[],
256 const test_chunk chunk[],
261 const test_chunk *cch;
262 dstr dv[TEST_FIELDMAX];
263 int fail = 0, ok = 1;
266 /* --- Silly bits of initialisation --- */
270 for (i = 0; i < TEST_FIELDMAX; i++)
273 /* --- Parse command line arguments --- */
284 if (strcmp(p, "--") == 0) {
288 if (p[0] != '-' || p[1] == 0)
294 printf("%s test driver\n"
295 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
300 die(1, "option `-f' expects an argument");
307 die(1, "option `-%c' unknown", p[-1]);
313 /* --- Start parsing from the file --- */
315 if ((fp = fopen(vec, "r")) == 0)
316 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
319 int tok = test__gettok(fp);
321 /* --- This is a reasonable place to stop --- */
326 /* --- Pick out the chunk name --- */
329 die(1, "expected <word>; found `%s'", test__decode(tok));
331 /* --- Find the right chunk block --- */
333 for (cch = chunk; ; cch++) {
336 if (strcmp(test__tok.buf, cch->name) == 0)
340 /* --- Past the open brace to the first chunk --- */
342 if ((tok = test__gettok(fp)) != '{')
343 die(1, "expected '{'; found `%s'", test__decode(tok));
345 /* --- Start on the test data now --- */
347 printf("%s: ", cch->name);
353 tok = test__gettok(fp);
355 /* --- Accept a close brace --- */
360 /* --- Otherwise I expect a list of words --- */
362 for (i = 0; cch->f[i]; i++) {
365 die(1, "expected <word>; found `%s'", test__decode(tok));
366 cch->f[i]->cvt(test__tok.buf, &dv[i]);
367 tok = test__gettok(fp);
370 /* --- And a terminating semicolon --- */
373 die(1, "expected `;'; found `%s'", test__decode(tok));
375 /* --- Run the test code --- */
377 if (!cch->test(dv)) {
378 printf("%s: ", cch->name);
379 for (i = 0; i < sofar; i++) putchar('.');
387 puts(ok ? " ok" : " failed");
392 if ((tok = test__gettok(fp)) != '{')
393 die(1, "expected '{'; found `%s'", test__decode(tok));
395 tok = test__gettok(fp);
398 while (tok == tok_word)
399 tok = test__gettok(fp);
401 die(1, "expected `;'; found `%s'", test__decode(tok));
408 /*----- That's all, folks -------------------------------------------------*/