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