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 ------------------------------------------------------*/
42 /*----- Static variables --------------------------------------------------*/
44 static dstr tok
= DSTR_INIT
;
51 /*----- Main code ---------------------------------------------------------*/
55 * Arguments: @int tok@ = token type to decode
57 * Returns: Pointer to a textual representation of the token.
59 * Use: Produces a readable representation of a token.
62 static const char *decode(int t
)
76 return ("<buggy-program>");
81 * Arguments: @FILE *fp@ = file handle to read from
83 * Returns: Type of token read.
85 * Use: Reads a token from the input stream.
88 static int gettok(FILE *fp
)
92 /* --- Clear the token accumulator --- */
96 /* --- Prime the lookahead character --- */
101 /* --- Skip leading whitespace --- */
106 /* --- Trap some special characters --- */
110 /* --- Comments --- */
113 do ch
= getc(fp
); while (ch
!= EOF
&& ch
!= '\n');
116 /* --- End of file --- */
121 /* --- Quote characters --- */
131 if (ch
== EOF
|| ch
== quote
)
144 /* --- Self-delimiting things --- */
151 /* --- Anything else is a word --- */
183 /* --- @type_hex@ --- */
185 static void cvt_hex(const char *s
, dstr
*d
)
187 while (s
[0] && s
[1]) {
188 int x
= s
[0], y
= s
[1];
189 if ('0' <= x
&& x
<= '9') x
-= '0';
190 else if ('A' <= x
&& x
<= 'F') x
-= 'A' - 10;
191 else if ('a' <= x
&& x
<= 'f') x
-= 'a' - 10;
193 if ('0' <= y
&& y
<= '9') y
-= '0';
194 else if ('A' <= y
&& y
<= 'F') y
-= 'A' - 10;
195 else if ('a' <= y
&& y
<= 'f') y
-= 'a' - 10;
197 DPUTC(d
, (x
<< 4) + y
);
202 static void dump_hex(dstr
*d
, FILE *fp
)
205 for (p
= d
->buf
, q
= p
+ d
->len
; p
< q
; p
++)
206 fprintf(fp
, "%02x", *(unsigned char *)p
);
209 const test_type type_hex
= { cvt_hex
, dump_hex
};
211 /* --- @type_string@ --- */
213 static void cvt_string(const char *s
, dstr
*d
)
218 static void dump_string(dstr
*d
, FILE *fp
)
223 const test_type type_string
= { cvt_string
, dump_string
};
225 /* --- @type_int@ --- */
227 static void cvt_int(const char *s
, dstr
*d
)
229 DENSURE(d
, sizeof(int));
230 sscanf(s
, "%i", (int *)d
->buf
);
233 static void dump_int(dstr
*d
, FILE *fp
)
235 fprintf(fp
, "%i", *(int *)d
->buf
);
238 const test_type type_int
= { cvt_int
, dump_int
};
240 /* --- @type_long@ --- */
242 static void cvt_long(const char *s
, dstr
*d
)
244 DENSURE(d
, sizeof(long));
245 *(long *)d
->buf
= strtol(s
, 0, 0);
248 static void dump_long(dstr
*d
, FILE *fp
)
250 fprintf(fp
, "%li", *(long *)d
->buf
);
253 const test_type type_long
= { cvt_long
, dump_long
};
255 /* --- @type_ulong@ --- */
257 static void cvt_ulong(const char *s
, dstr
*d
)
259 DENSURE(d
, sizeof(unsigned long));
260 *(unsigned long *)d
->buf
= strtoul(s
, 0, 0);
263 static void dump_ulong(dstr
*d
, FILE *fp
)
265 fprintf(fp
, "%lu", *(unsigned long *)d
->buf
);
268 const test_type type_ulong
= { cvt_ulong
, dump_ulong
};
270 /* --- @type_uint32@ --- */
272 static void cvt_uint32(const char *buf
, dstr
*d
)
274 DENSURE(d
, sizeof(uint32
));
275 *(uint32
*)d
->buf
= strtoul(buf
, 0, 0);
278 static void dump_uint32(dstr
*d
, FILE *fp
)
280 fprintf(fp
, "%lu\n", (unsigned long)*(uint32
*)d
->buf
);
283 const test_type type_uint32
= { cvt_uint32
, dump_uint32
};
285 /* --- @test_do@ --- *
287 * Arguments: @const test_suite suites[]@ = pointer to suite definitions
288 * @FILE *fp@ = test vector file, ready opened
289 * @test_results *results@ = where to put results
291 * Returns: Negative if something bad happened, or the number of
294 * Use: Runs a collection of tests against a file of test vectors and
295 * reports the results.
298 int test_do(const test_suite suites
[], FILE *fp
, test_results
*results
)
301 dstr dv
[TEST_FIELDMAX
];
302 const test_suite
*ss
;
303 const test_chunk
*chunks
= suites
[0].chunks
;
304 const test_chunk
*cch
;
309 for (i
= 0; i
< TEST_FIELDMAX
; i
++)
320 /* --- This is a reasonable place to stop --- */
325 /* --- Pick out the chunk name --- */
328 moan("expected <word>; found `%s'", decode(t
));
332 if (STRCMP(tok
.buf
, ==, "SUITE")) {
335 moan("expected <word>; found `%s'", decode(t
));
338 for (ss
= suites
; ; ss
++) {
343 if (STRCMP(tok
.buf
, ==, ss
->name
)) {
351 /* --- Find the right chunk block --- */
355 for (cch
= chunks
; ; cch
++) {
358 if (STRCMP(tok
.buf
, ==, cch
->name
))
362 /* --- Past the open brace to the first chunk --- */
364 if ((t
= gettok(fp
)) != '{') {
365 moan("expected `{'; found `%s'", decode(t
));
369 /* --- Start on the test data now --- */
371 printf("%s: ", cch
->name
);
378 /* --- Accept a close brace --- */
383 /* --- Otherwise I expect a list of words --- */
385 for (i
= 0; cch
->f
[i
]; i
++) {
388 moan("expected <word>; found `%s'", decode(t
));
391 cch
->f
[i
]->cvt(tok
.buf
, &dv
[i
]);
395 /* --- And a terminating semicolon --- */
398 moan("expected `;'; found `%s'", decode(t
));
402 /* --- Run the test code --- */
404 if (!cch
->test(dv
)) {
406 printf("%s: ", cch
->name
);
407 for (i
= 0; i
< results
->tests
; i
++) putchar('.');
415 puts(ok ?
" ok" : " failed");
420 if ((t
= gettok(fp
)) != '{') {
421 moan("expected '{'; found `%s'", decode(t
));
428 while (t
== TOK_WORD
)
431 moan("expected `;'; found `%s'", decode(t
));
436 rc
= results
->failed
;
438 /* --- All done --- */
441 for (i
= 0; i
< TEST_FIELDMAX
; i
++)
442 dstr_destroy(&dv
[i
]);
446 /* --- @test_run@ --- *
448 * Arguments: @int argc@ = number of command line arguments
449 * @char *argv[]@ = pointer to command line arguments
450 * @const test_chunk chunk[]@ = pointer to chunk definitions
451 * @const char *vec@ = name of default test vector file
455 * Use: Runs a set of test vectors to ensure that a component is
459 void test_run(int argc
, char *argv
[],
460 const test_chunk chunk
[],
468 /* --- Silly bits of initialization --- */
472 /* --- Parse command line arguments --- */
483 if (STRCMP(p
, ==, "--")) {
487 if (p
[0] != '-' || p
[1] == 0)
493 printf("%s test driver\n"
494 "Usage: %s [-f FILENAME]\n", QUIS
, QUIS
);
499 die(1, "option `-f' expects an argument");
506 die(1, "option `-%c' unknown", p
[-1]);
512 /* --- Start parsing from the file --- */
514 if ((fp
= fopen(vec
, "r")) == 0)
515 die(1, "couldn't open test vector file `%s': %s", vec
, strerror(errno
));
516 suite
[0].name
= "simple";
517 suite
[0].chunks
= chunk
;
519 rc
= test_do(suite
, fp
, &res
);
523 fprintf(stderr
, "FAILED %u of %u test%s\n",
524 res
.failed
, res
.tests
, res
.tests
== 1 ?
"" : "s");
526 fprintf(stderr
, "PASSED all %u test%s\n",
527 res
.tests
, res
.tests
== 1 ?
"" : "s");
532 /*----- That's all, folks -------------------------------------------------*/