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