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