3 * $Id: testrig.c,v 1.2 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
40 /*----- Header files ------------------------------------------------------*/
53 /*----- Static variables --------------------------------------------------*/
55 static dstr test__tok;
62 /*----- Main code ---------------------------------------------------------*/
64 /* --- @test__decode@ --- *
66 * Arguments: @int tok@ = token type to decode
68 * Returns: Pointer to a textual representation of the token.
70 * Use: Produces a readable representation of a token.
73 static const char *test__decode(int tok)
81 return (test__tok.buf);
87 return ("<buggy-program>");
90 /* --- @test__gettok@ --- *
92 * Arguments: @FILE *fp@ = file handle to read from
94 * Returns: Type of token read.
96 * Use: Reads a token from the input stream.
99 static int test__gettok(FILE *fp)
103 /* --- Clear the token accumulator --- */
105 dstr_reset(&test__tok);
107 /* --- Prime the lookahead character --- */
112 /* --- Skip leading whitespace --- */
114 while (isspace((unsigned char)ch))
117 /* --- Trap some special characters --- */
121 /* --- Comments --- */
124 do ch = getc(fp); while (ch != EOF && ch != '\n');
127 /* --- End of file --- */
132 /* --- Quote characters --- */
142 if (ch == EOF || ch == quote)
149 DPUTC(&test__tok, ch);
155 /* --- Anything else is either a word or self-delimiting --- */
158 if (isalnum((unsigned char)ch)) {
160 DPUTC(&test__tok, ch);
164 ch == '\"' || ch == '\'' || ch == '`' ||
165 isspace((unsigned char)ch))
181 /* --- @type_hex@ --- */
183 static void cvt_hex(const char *s, dstr *d)
185 while (s[0] && s[1]) {
186 int x = s[0], y = s[1];
187 if ('0' <= x && x <= '9') x -= '0';
188 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
189 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
191 if ('0' <= y && y <= '9') y -= '0';
192 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
193 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
195 DPUTC(d, (x << 4) + y);
200 static void dump_hex(dstr *d, FILE *fp)
203 for (p = d->buf, q = p + d->len; p < q; p++)
204 fprintf(fp, "%02x", *(unsigned char *)p);
207 test_type type_hex = { cvt_hex, dump_hex };
209 /* --- @type_string@ --- */
211 static void cvt_string(const char *s, dstr *d)
216 static void dump_string(dstr *d, FILE *fp)
221 test_type type_string = { cvt_string, dump_string };
223 /* --- @type_int@ --- */
225 static void cvt_int(const char *s, dstr *d)
227 DENSURE(d, sizeof(long));
228 sscanf(s, "%i", (int *)d->buf + d->len);
231 static void dump_int(dstr *d, FILE *fp)
233 fprintf(fp, "%i", *(int *)(d->buf));
236 test_type type_int = { cvt_int, dump_int };
238 /* --- @test_run@ --- *
240 * Arguments: @int argc@ = number of command line arguments
241 * @char *argv[]@ = pointer to command line arguments
242 * @const test_chunk chunk[]@ = pointer to chunk definitions
243 * @const char *vec@ = name of default test vector file
247 * Use: Runs a set of test vectors to ensure that a component is
251 void test_run(int argc, char *argv[],
252 const test_chunk chunk[],
257 const test_chunk *cch;
258 dstr dv[TEST_FIELDMAX];
259 int fail = 0, ok = 1;
262 /* --- Silly bits of initialisation --- */
266 for (i = 0; i < TEST_FIELDMAX; i++)
269 /* --- Parse command line arguments --- */
280 if (strcmp(p, "--") == 0) {
284 if (p[0] != '-' || p[1] == 0)
290 printf("%s test driver\n"
291 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
296 die(1, "option `-f' expects an argument");
303 die(1, "option `-%c' unknown", p[-1]);
309 /* --- Start parsing from the file --- */
311 if ((fp = fopen(vec, "r")) == 0)
312 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
315 int tok = test__gettok(fp);
317 /* --- This is a reasonable place to stop --- */
322 /* --- Pick out the chunk name --- */
325 die(1, "expected <word>; found `%s'", test__decode(tok));
327 /* --- Find the right chunk block --- */
329 for (cch = chunk; ; cch++) {
332 if (strcmp(test__tok.buf, cch->name) == 0)
336 /* --- Past the open brace to the first chunk --- */
338 if ((tok = test__gettok(fp)) != '{')
339 die(1, "expected '{'; found `%s'", test__decode(tok));
341 /* --- Start on the test data now --- */
343 printf("%s: ", cch->name);
349 tok = test__gettok(fp);
351 /* --- Accept a close brace --- */
356 /* --- Otherwise I expect a list of words --- */
358 for (i = 0; cch->f[i]; i++) {
361 die(1, "expected <word>; found `%s'", test__decode(tok));
362 cch->f[i]->cvt(test__tok.buf, &dv[i]);
363 tok = test__gettok(fp);
366 /* --- And a terminating semicolon --- */
369 die(1, "expected `;'; found `%s'", test__decode(tok));
371 /* --- Run the test code --- */
373 if (!cch->test(dv)) {
374 printf("%s: ", cch->name);
375 for (i = 0; i < sofar; i++) putchar('.');
383 puts(ok ? " ok" : " failed");
388 if ((tok = test__gettok(fp)) != '{')
389 die(1, "expected '{'; found `%s'", test__decode(tok));
391 tok = test__gettok(fp);
394 while (tok == tok_word)
395 tok = test__gettok(fp);
397 die(1, "expected `;'; found `%s'", test__decode(tok));
404 /*----- That's all, folks -------------------------------------------------*/