5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
41 /*----- Static variables --------------------------------------------------*/
43 static dstr tok = DSTR_INIT;
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @int tok@ = token type to decode
56 * Returns: Pointer to a textual representation of the token.
58 * Use: Produces a readable representation of a token.
61 static const char *decode(int t)
75 return ("<buggy-program>");
80 * Arguments: @FILE *fp@ = file handle to read from
82 * Returns: Type of token read.
84 * Use: Reads a token from the input stream.
87 static int gettok(FILE *fp)
91 /* --- Clear the token accumulator --- */
95 /* --- Prime the lookahead character --- */
100 /* --- Skip leading whitespace --- */
102 while (isspace((unsigned char)ch))
105 /* --- Trap some special characters --- */
109 /* --- Comments --- */
112 do ch = getc(fp); while (ch != EOF && ch != '\n');
115 /* --- End of file --- */
120 /* --- Quote characters --- */
130 if (ch == EOF || ch == quote)
143 /* --- Self-delimiting things --- */
150 /* --- Anything else is a word --- */
166 if (isspace((unsigned char)ch))
182 /* --- @type_hex@ --- */
184 static void cvt_hex(const char *s, dstr *d)
186 while (s[0] && s[1]) {
187 int x = s[0], y = s[1];
188 if ('0' <= x && x <= '9') x -= '0';
189 else if ('A' <= x && x <= 'F') x -= 'A' - 10;
190 else if ('a' <= x && x <= 'f') x -= 'a' - 10;
192 if ('0' <= y && y <= '9') y -= '0';
193 else if ('A' <= y && y <= 'F') y -= 'A' - 10;
194 else if ('a' <= y && y <= 'f') y -= 'a' - 10;
196 DPUTC(d, (x << 4) + y);
201 static void dump_hex(dstr *d, FILE *fp)
204 for (p = d->buf, q = p + d->len; p < q; p++)
205 fprintf(fp, "%02x", *(unsigned char *)p);
208 const test_type type_hex = { cvt_hex, dump_hex };
210 /* --- @type_string@ --- */
212 static void cvt_string(const char *s, dstr *d)
217 static void dump_string(dstr *d, FILE *fp)
222 const test_type type_string = { cvt_string, dump_string };
224 /* --- @type_int@ --- */
226 static void cvt_int(const char *s, dstr *d)
228 DENSURE(d, sizeof(int));
229 sscanf(s, "%i", (int *)d->buf);
232 static void dump_int(dstr *d, FILE *fp)
234 fprintf(fp, "%i", *(int *)d->buf);
237 const test_type type_int = { cvt_int, dump_int };
239 /* --- @type_long@ --- */
241 static void cvt_long(const char *s, dstr *d)
243 DENSURE(d, sizeof(long));
244 *(long *)d->buf = strtol(s, 0, 0);
247 static void dump_long(dstr *d, FILE *fp)
249 fprintf(fp, "%li", *(long *)d->buf);
252 const test_type type_long = { cvt_long, dump_long };
254 /* --- @type_ulong@ --- */
256 static void cvt_ulong(const char *s, dstr *d)
258 DENSURE(d, sizeof(unsigned long));
259 *(unsigned long *)d->buf = strtoul(s, 0, 0);
262 static void dump_ulong(dstr *d, FILE *fp)
264 fprintf(fp, "%lu", *(unsigned long *)d->buf);
267 const test_type type_ulong = { cvt_ulong, dump_ulong };
269 /* --- @type_uint32@ --- */
271 static void cvt_uint32(const char *buf, dstr *d)
273 DENSURE(d, sizeof(uint32));
274 *(uint32 *)d->buf = strtoul(buf, 0, 0);
277 static void dump_uint32(dstr *d, FILE *fp)
279 fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
282 const test_type type_uint32 = { cvt_uint32, dump_uint32 };
284 /* --- @test_do@ --- *
286 * Arguments: @const test_suite suites[]@ = pointer to suite definitions
287 * @FILE *fp@ = test vector file, ready opened
288 * @test_results *results@ = where to put results
290 * Returns: Negative if something bad happened, or the number of
293 * Use: Runs a collection of tests against a file of test vectors and
294 * reports the results.
297 int test_do(const test_suite suites[], FILE *fp, test_results *results)
300 dstr dv[TEST_FIELDMAX];
301 const test_suite *ss;
302 const test_chunk *chunks = suites[0].chunks;
303 const test_chunk *cch;
308 for (i = 0; i < TEST_FIELDMAX; i++)
319 /* --- This is a reasonable place to stop --- */
324 /* --- Pick out the chunk name --- */
327 moan("expected <word>; found `%s'", decode(t));
331 if (strcmp(tok.buf, "SUITE") == 0) {
334 moan("expected <word>; found `%s'", decode(t));
337 for (ss = suites; ; ss++) {
342 if (strcmp(tok.buf, ss->name) == 0) {
350 /* --- Find the right chunk block --- */
354 for (cch = chunks; ; cch++) {
357 if (strcmp(tok.buf, cch->name) == 0)
361 /* --- Past the open brace to the first chunk --- */
363 if ((t = gettok(fp)) != '{') {
364 moan("expected `{'; found `%s'", decode(t));
368 /* --- Start on the test data now --- */
370 printf("%s: ", cch->name);
377 /* --- Accept a close brace --- */
382 /* --- Otherwise I expect a list of words --- */
384 for (i = 0; cch->f[i]; i++) {
387 moan("expected <word>; found `%s'", decode(t));
390 cch->f[i]->cvt(tok.buf, &dv[i]);
394 /* --- And a terminating semicolon --- */
397 moan("expected `;'; found `%s'", decode(t));
401 /* --- Run the test code --- */
403 if (!cch->test(dv)) {
405 printf("%s: ", cch->name);
406 for (i = 0; i < results->tests; i++) putchar('.');
414 puts(ok ? " ok" : " failed");
419 if ((t = gettok(fp)) != '{') {
420 moan("expected '{'; found `%s'", decode(t));
427 while (t == TOK_WORD)
430 moan("expected `;'; found `%s'", decode(t));
435 rc = results->failed;
437 /* --- All done --- */
440 for (i = 0; i < TEST_FIELDMAX; i++)
441 dstr_destroy(&dv[i]);
445 /* --- @test_run@ --- *
447 * Arguments: @int argc@ = number of command line arguments
448 * @char *argv[]@ = pointer to command line arguments
449 * @const test_chunk chunk[]@ = pointer to chunk definitions
450 * @const char *vec@ = name of default test vector file
454 * Use: Runs a set of test vectors to ensure that a component is
458 void test_run(int argc, char *argv[],
459 const test_chunk chunk[],
467 /* --- Silly bits of initialization --- */
471 /* --- Parse command line arguments --- */
482 if (strcmp(p, "--") == 0) {
486 if (p[0] != '-' || p[1] == 0)
492 printf("%s test driver\n"
493 "Usage: %s [-f FILENAME]\n", QUIS, QUIS);
498 die(1, "option `-f' expects an argument");
505 die(1, "option `-%c' unknown", p[-1]);
511 /* --- Start parsing from the file --- */
513 if ((fp = fopen(vec, "r")) == 0)
514 die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
515 suite[0].name = "simple";
516 suite[0].chunks = chunk;
518 rc = test_do(suite, fp, &res);
522 fprintf(stderr, "FAILED %u of %u test%s\n",
523 res.failed, res.tests, res.tests == 1 ? "" : "s");
525 fprintf(stderr, "PASSED all %u test%s\n",
526 res.tests, res.tests == 1 ? "" : "s");
531 /*----- That's all, folks -------------------------------------------------*/