| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: testrig.c,v 1.5 1999/05/21 22:14:30 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.5 1999/05/21 22:14:30 mdw |
| 34 | * Take advantage of the new dynamic string macros. |
| 35 | * |
| 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 | * |
| 41 | * Revision 1.3 1999/05/06 19:51:35 mdw |
| 42 | * Reformatted the LGPL notice a little bit. |
| 43 | * |
| 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 |
| 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 | |
| 67 | static dstr tok; |
| 68 | |
| 69 | enum { |
| 70 | TOK_EOF = 0x100, |
| 71 | TOK_WORD |
| 72 | }; |
| 73 | |
| 74 | /*----- Main code ---------------------------------------------------------*/ |
| 75 | |
| 76 | /* --- @decode@ --- * |
| 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 | |
| 85 | static const char *decode(int t) |
| 86 | { |
| 87 | static char buf[4]; |
| 88 | |
| 89 | switch (t) { |
| 90 | case TOK_EOF: |
| 91 | return ("<eof>"); |
| 92 | case TOK_WORD: |
| 93 | return (tok.buf); |
| 94 | default: |
| 95 | buf[0] = t; |
| 96 | buf[1] = 0; |
| 97 | return (buf); |
| 98 | } |
| 99 | return ("<buggy-program>"); |
| 100 | } |
| 101 | |
| 102 | /* --- @gettok@ --- * |
| 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 | |
| 111 | static int gettok(FILE *fp) |
| 112 | { |
| 113 | int ch; |
| 114 | |
| 115 | /* --- Clear the token accumulator --- */ |
| 116 | |
| 117 | DRESET(&tok); |
| 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: |
| 142 | return (TOK_EOF); |
| 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 | } |
| 161 | DPUTC(&tok, ch); |
| 162 | } |
| 163 | DPUTZ(&tok); |
| 164 | return (TOK_WORD); |
| 165 | } |
| 166 | |
| 167 | /* --- Anything else is either a word or self-delimiting --- */ |
| 168 | |
| 169 | default: |
| 170 | if (isalnum((unsigned char)ch)) { |
| 171 | for (;;) { |
| 172 | DPUTC(&tok, ch); |
| 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); |
| 186 | DPUTZ(&tok); |
| 187 | return (TOK_WORD); |
| 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 | { |
| 230 | DWRITE(d, fp); |
| 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 | |
| 274 | /* --- Silly bits of initialization --- */ |
| 275 | |
| 276 | ego(argv[0]); |
| 277 | |
| 278 | for (i = 0; i < TEST_FIELDMAX; i++) |
| 279 | DCREATE(&dv[i]); |
| 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 (;;) { |
| 327 | int t = gettok(fp); |
| 328 | |
| 329 | /* --- This is a reasonable place to stop --- */ |
| 330 | |
| 331 | if (t == TOK_EOF) |
| 332 | break; |
| 333 | |
| 334 | /* --- Pick out the chunk name --- */ |
| 335 | |
| 336 | if (t != TOK_WORD) |
| 337 | die(1, "expected <word>; found `%s'", decode(t)); |
| 338 | |
| 339 | /* --- Find the right chunk block --- */ |
| 340 | |
| 341 | for (cch = chunk; ; cch++) { |
| 342 | if (!cch->name) |
| 343 | goto skip_chunk; |
| 344 | if (strcmp(tok.buf, cch->name) == 0) |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | /* --- Past the open brace to the first chunk --- */ |
| 349 | |
| 350 | if ((t = gettok(fp)) != '{') |
| 351 | die(1, "expected '{'; found `%s'", decode(t)); |
| 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 (;;) { |
| 361 | t = gettok(fp); |
| 362 | |
| 363 | /* --- Accept a close brace --- */ |
| 364 | |
| 365 | if (t == '}') |
| 366 | break; |
| 367 | |
| 368 | /* --- Otherwise I expect a list of words --- */ |
| 369 | |
| 370 | for (i = 0; cch->f[i]; i++) { |
| 371 | DRESET(&dv[i]); |
| 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); |
| 376 | } |
| 377 | |
| 378 | /* --- And a terminating semicolon --- */ |
| 379 | |
| 380 | if (t != ';') |
| 381 | die(1, "expected `;'; found `%s'", decode(t)); |
| 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: |
| 400 | if ((t = gettok(fp)) != '{') |
| 401 | die(1, "expected '{'; found `%s'", decode(t)); |
| 402 | for (;;) { |
| 403 | t = gettok(fp); |
| 404 | if (t == '}') |
| 405 | break; |
| 406 | while (t == TOK_WORD) |
| 407 | t = gettok(fp); |
| 408 | if (t != ';') |
| 409 | die(1, "expected `;'; found `%s'", decode(t)); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | exit(fail); |
| 414 | } |
| 415 | |
| 416 | /*----- That's all, folks -------------------------------------------------*/ |