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