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