0875b58f |
1 | /* -*-c-*- |
2 | * |
0bfb1431 |
3 | * $Id: testrig.c,v 1.5 1999/05/21 22:14:30 mdw Exp $ |
0875b58f |
4 | * |
5 | * Generic test driver |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
c846879c |
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. |
0875b58f |
18 | * |
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 |
c846879c |
22 | * GNU Library General Public License for more details. |
0875b58f |
23 | * |
c846879c |
24 | * You should have received a copy of the GNU Library General Public |
0bd98442 |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
0875b58f |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: testrig.c,v $ |
0bfb1431 |
33 | * Revision 1.5 1999/05/21 22:14:30 mdw |
34 | * Take advantage of the new dynamic string macros. |
35 | * |
083cdbed |
36 | * Revision 1.4 1999/05/19 19:02:17 mdw |
37 | * Aesthetic changes: fix spelling of `initialize'; use uppercase token |
38 | * constants; abandon old double-underscore convention for internal |
39 | * functions and variables. |
40 | * |
0bd98442 |
41 | * Revision 1.3 1999/05/06 19:51:35 mdw |
42 | * Reformatted the LGPL notice a little bit. |
43 | * |
c846879c |
44 | * Revision 1.2 1999/05/05 18:50:31 mdw |
45 | * Change licensing conditions to LGPL. |
46 | * |
47 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
48 | * Initial version of mLib |
0875b58f |
49 | * |
50 | */ |
51 | |
52 | /*----- Header files ------------------------------------------------------*/ |
53 | |
54 | #include <ctype.h> |
55 | #include <errno.h> |
56 | #include <stdio.h> |
57 | #include <stdlib.h> |
58 | #include <string.h> |
59 | |
60 | #include "dstr.h" |
61 | #include "report.h" |
62 | #include "quis.h" |
63 | #include "testrig.h" |
64 | |
65 | /*----- Static variables --------------------------------------------------*/ |
66 | |
083cdbed |
67 | static dstr tok; |
0875b58f |
68 | |
69 | enum { |
083cdbed |
70 | TOK_EOF = 0x100, |
71 | TOK_WORD |
0875b58f |
72 | }; |
73 | |
74 | /*----- Main code ---------------------------------------------------------*/ |
75 | |
083cdbed |
76 | /* --- @decode@ --- * |
0875b58f |
77 | * |
78 | * Arguments: @int tok@ = token type to decode |
79 | * |
80 | * Returns: Pointer to a textual representation of the token. |
81 | * |
82 | * Use: Produces a readable representation of a token. |
83 | */ |
84 | |
083cdbed |
85 | static const char *decode(int t) |
0875b58f |
86 | { |
87 | static char buf[4]; |
88 | |
083cdbed |
89 | switch (t) { |
90 | case TOK_EOF: |
0875b58f |
91 | return ("<eof>"); |
083cdbed |
92 | case TOK_WORD: |
93 | return (tok.buf); |
0875b58f |
94 | default: |
083cdbed |
95 | buf[0] = t; |
0875b58f |
96 | buf[1] = 0; |
97 | return (buf); |
98 | } |
99 | return ("<buggy-program>"); |
100 | } |
101 | |
083cdbed |
102 | /* --- @gettok@ --- * |
0875b58f |
103 | * |
104 | * Arguments: @FILE *fp@ = file handle to read from |
105 | * |
106 | * Returns: Type of token read. |
107 | * |
108 | * Use: Reads a token from the input stream. |
109 | */ |
110 | |
083cdbed |
111 | static int gettok(FILE *fp) |
0875b58f |
112 | { |
113 | int ch; |
114 | |
115 | /* --- Clear the token accumulator --- */ |
116 | |
0bfb1431 |
117 | DRESET(&tok); |
0875b58f |
118 | |
119 | /* --- Prime the lookahead character --- */ |
120 | |
121 | again: |
122 | ch = getc(fp); |
123 | |
124 | /* --- Skip leading whitespace --- */ |
125 | |
126 | while (isspace((unsigned char)ch)) |
127 | ch = getc(fp); |
128 | |
129 | /* --- Trap some special characters --- */ |
130 | |
131 | switch (ch) { |
132 | |
133 | /* --- Comments --- */ |
134 | |
135 | case '#': |
136 | do ch = getc(fp); while (ch != EOF && ch != '\n'); |
137 | goto again; |
138 | |
139 | /* --- End of file --- */ |
140 | |
141 | case EOF: |
083cdbed |
142 | return (TOK_EOF); |
0875b58f |
143 | |
144 | /* --- Quote characters --- */ |
145 | |
146 | case '`': |
147 | ch = '\''; |
148 | case '\'': |
149 | case '\"': { |
150 | int quote = ch; |
151 | |
152 | for (;;) { |
153 | ch = getc(fp); |
154 | if (ch == EOF || ch == quote) |
155 | break; |
156 | if (ch == '\\') { |
157 | ch = getc(fp); |
158 | if (ch == EOF) |
159 | ch = '\\'; |
160 | } |
083cdbed |
161 | DPUTC(&tok, ch); |
0875b58f |
162 | } |
083cdbed |
163 | DPUTZ(&tok); |
164 | return (TOK_WORD); |
0875b58f |
165 | } |
166 | |
167 | /* --- Anything else is either a word or self-delimiting --- */ |
168 | |
169 | default: |
170 | if (isalnum((unsigned char)ch)) { |
171 | for (;;) { |
083cdbed |
172 | DPUTC(&tok, ch); |
0875b58f |
173 | ch = getc(fp); |
174 | if (ch == EOF || |
175 | ch == ';' || |
176 | ch == '\"' || ch == '\'' || ch == '`' || |
177 | isspace((unsigned char)ch)) |
178 | break; |
179 | if (ch == '\\') { |
180 | ch = getc(fp); |
181 | if (ch == EOF) |
182 | ch = '\\'; |
183 | } |
184 | } |
185 | ungetc(ch, fp); |
083cdbed |
186 | DPUTZ(&tok); |
187 | return (TOK_WORD); |
0875b58f |
188 | } else |
189 | return (ch); |
190 | } |
191 | } |
192 | |
193 | /* --- @type_hex@ --- */ |
194 | |
195 | static void cvt_hex(const char *s, dstr *d) |
196 | { |
197 | while (s[0] && s[1]) { |
198 | int x = s[0], y = s[1]; |
199 | if ('0' <= x && x <= '9') x -= '0'; |
200 | else if ('A' <= x && x <= 'F') x -= 'A' - 10; |
201 | else if ('a' <= x && x <= 'f') x -= 'a' - 10; |
202 | else x = 0; |
203 | if ('0' <= y && y <= '9') y -= '0'; |
204 | else if ('A' <= y && y <= 'F') y -= 'A' - 10; |
205 | else if ('a' <= y && y <= 'f') y -= 'a' - 10; |
206 | else y = 0; |
207 | DPUTC(d, (x << 4) + y); |
208 | s += 2; |
209 | } |
210 | } |
211 | |
212 | static void dump_hex(dstr *d, FILE *fp) |
213 | { |
214 | const char *p, *q; |
215 | for (p = d->buf, q = p + d->len; p < q; p++) |
216 | fprintf(fp, "%02x", *(unsigned char *)p); |
217 | } |
218 | |
219 | test_type type_hex = { cvt_hex, dump_hex }; |
220 | |
221 | /* --- @type_string@ --- */ |
222 | |
223 | static void cvt_string(const char *s, dstr *d) |
224 | { |
225 | DPUTS(d, s); |
226 | } |
227 | |
228 | static void dump_string(dstr *d, FILE *fp) |
229 | { |
0bfb1431 |
230 | DWRITE(d, fp); |
0875b58f |
231 | } |
232 | |
233 | test_type type_string = { cvt_string, dump_string }; |
234 | |
235 | /* --- @type_int@ --- */ |
236 | |
237 | static void cvt_int(const char *s, dstr *d) |
238 | { |
239 | DENSURE(d, sizeof(long)); |
240 | sscanf(s, "%i", (int *)d->buf + d->len); |
241 | } |
242 | |
243 | static void dump_int(dstr *d, FILE *fp) |
244 | { |
245 | fprintf(fp, "%i", *(int *)(d->buf)); |
246 | } |
247 | |
248 | test_type type_int = { cvt_int, dump_int }; |
249 | |
250 | /* --- @test_run@ --- * |
251 | * |
252 | * Arguments: @int argc@ = number of command line arguments |
253 | * @char *argv[]@ = pointer to command line arguments |
254 | * @const test_chunk chunk[]@ = pointer to chunk definitions |
255 | * @const char *vec@ = name of default test vector file |
256 | * |
257 | * Returns: Doesn't. |
258 | * |
259 | * Use: Runs a set of test vectors to ensure that a component is |
260 | * working properly. |
261 | */ |
262 | |
263 | void test_run(int argc, char *argv[], |
264 | const test_chunk chunk[], |
265 | const char *vec) |
266 | { |
267 | FILE *fp; |
268 | int i; |
269 | const test_chunk *cch; |
270 | dstr dv[TEST_FIELDMAX]; |
271 | int fail = 0, ok = 1; |
272 | int sofar = 0; |
273 | |
083cdbed |
274 | /* --- Silly bits of initialization --- */ |
0875b58f |
275 | |
276 | ego(argv[0]); |
277 | |
278 | for (i = 0; i < TEST_FIELDMAX; i++) |
0bfb1431 |
279 | DCREATE(&dv[i]); |
0875b58f |
280 | |
281 | /* --- Parse command line arguments --- */ |
282 | |
283 | { |
284 | const char *p = 0; |
285 | |
286 | i = 0; |
287 | for (;;) { |
288 | if (!p || !*p) { |
289 | if (i >= argc - 1) |
290 | break; |
291 | p = argv[++i]; |
292 | if (strcmp(p, "--") == 0) { |
293 | i++; |
294 | break; |
295 | } |
296 | if (p[0] != '-' || p[1] == 0) |
297 | break; |
298 | p++; |
299 | } |
300 | switch (*p++) { |
301 | case 'h': |
302 | printf("%s test driver\n" |
303 | "Usage: %s [-f FILENAME]\n", QUIS, QUIS); |
304 | exit(0); |
305 | case 'f': |
306 | if (!*p) { |
307 | if (i >= argc - 1) |
308 | die(1, "option `-f' expects an argument"); |
309 | p = argv[++i]; |
310 | } |
311 | vec = p; |
312 | p = 0; |
313 | break; |
314 | default: |
315 | die(1, "option `-%c' unknown", p[-1]); |
316 | break; |
317 | } |
318 | } |
319 | } |
320 | |
321 | /* --- Start parsing from the file --- */ |
322 | |
323 | if ((fp = fopen(vec, "r")) == 0) |
324 | die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno)); |
325 | |
326 | for (;;) { |
083cdbed |
327 | int t = gettok(fp); |
0875b58f |
328 | |
329 | /* --- This is a reasonable place to stop --- */ |
330 | |
083cdbed |
331 | if (t == TOK_EOF) |
0875b58f |
332 | break; |
333 | |
334 | /* --- Pick out the chunk name --- */ |
335 | |
083cdbed |
336 | if (t != TOK_WORD) |
337 | die(1, "expected <word>; found `%s'", decode(t)); |
0875b58f |
338 | |
339 | /* --- Find the right chunk block --- */ |
340 | |
341 | for (cch = chunk; ; cch++) { |
342 | if (!cch->name) |
343 | goto skip_chunk; |
083cdbed |
344 | if (strcmp(tok.buf, cch->name) == 0) |
0875b58f |
345 | break; |
346 | } |
347 | |
348 | /* --- Past the open brace to the first chunk --- */ |
349 | |
083cdbed |
350 | if ((t = gettok(fp)) != '{') |
351 | die(1, "expected '{'; found `%s'", decode(t)); |
0875b58f |
352 | |
353 | /* --- Start on the test data now --- */ |
354 | |
355 | printf("%s: ", cch->name); |
356 | fflush(stdout); |
357 | sofar = 0; |
358 | ok = 1; |
359 | |
360 | for (;;) { |
083cdbed |
361 | t = gettok(fp); |
0875b58f |
362 | |
363 | /* --- Accept a close brace --- */ |
364 | |
083cdbed |
365 | if (t == '}') |
0875b58f |
366 | break; |
367 | |
368 | /* --- Otherwise I expect a list of words --- */ |
369 | |
370 | for (i = 0; cch->f[i]; i++) { |
0bfb1431 |
371 | DRESET(&dv[i]); |
083cdbed |
372 | if (t != TOK_WORD) |
373 | die(1, "expected <word>; found `%s'", decode(t)); |
374 | cch->f[i]->cvt(tok.buf, &dv[i]); |
375 | t = gettok(fp); |
0875b58f |
376 | } |
377 | |
378 | /* --- And a terminating semicolon --- */ |
379 | |
083cdbed |
380 | if (t != ';') |
381 | die(1, "expected `;'; found `%s'", decode(t)); |
0875b58f |
382 | |
383 | /* --- Run the test code --- */ |
384 | |
385 | if (!cch->test(dv)) { |
386 | printf("%s: ", cch->name); |
387 | for (i = 0; i < sofar; i++) putchar('.'); |
388 | fail = 1; ok = 0; |
389 | } |
390 | sofar++; |
391 | putchar('.'); |
392 | fflush(stdout); |
393 | } |
394 | |
395 | puts(ok ? " ok" : " failed"); |
396 | fflush(stdout); |
397 | continue; |
398 | |
399 | skip_chunk: |
083cdbed |
400 | if ((t = gettok(fp)) != '{') |
401 | die(1, "expected '{'; found `%s'", decode(t)); |
0875b58f |
402 | for (;;) { |
083cdbed |
403 | t = gettok(fp); |
404 | if (t == '}') |
0875b58f |
405 | break; |
083cdbed |
406 | while (t == TOK_WORD) |
407 | t = gettok(fp); |
408 | if (t != ';') |
409 | die(1, "expected `;'; found `%s'", decode(t)); |
0875b58f |
410 | } |
411 | } |
412 | |
413 | exit(fail); |
414 | } |
415 | |
416 | /*----- That's all, folks -------------------------------------------------*/ |