3 * Main test vector driver
5 * (c) 2023 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 it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * 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 Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Output ------------------------------------------------------------*/
40 /* --- @tvec_error@, @tvec_error_v@ --- *
42 * Arguments: @struct tvec_state *tv@ = test-vector state
43 * @const char *msg@, @va_list ap@ = error message
47 * Use: Report an error. Errors are distinct from test failures,
48 * and indicate that a problem was encountered which compromised
49 * the activity of testing.
52 int tvec_error(struct tvec_state *tv, const char *msg, ...)
56 va_start(ap, msg); tvec_error_v(tv, msg, &ap); va_end(ap);
59 int tvec_error_v(struct tvec_state *tv, const char *msg, va_list *ap)
61 tv->output->ops->error(tv->output, msg, ap);
62 tv->f |= TVSF_ERROR; return (-1);
65 /* --- @tvec_notice@, @tvec_notice_v@ --- *
67 * Arguments: @struct tvec_state *tv@ = test-vector state
68 * @const char *msg@, @va_list ap@ = message
72 * Use: Output a notice: essentially, some important information
73 * which doesn't fit into any of the existing categories.
76 void tvec_notice(struct tvec_state *tv, const char *msg, ...)
79 va_start(ap, msg); tvec_notice_v(tv, msg, &ap); va_end(ap);
81 void tvec_notice_v(struct tvec_state *tv, const char *msg, va_list *ap)
82 { tv->output->ops->notice(tv->output, msg, ap); }
84 /*----- Test processing ---------------------------------------------------*/
86 void tvec_skipgroup(struct tvec_state *tv, const char *excuse, ...)
90 va_start(ap, excuse); tvec_skipgroup_v(tv, excuse, &ap); va_end(ap);
92 void tvec_skipgroup_v(struct tvec_state *tv, const char *excuse, va_list *ap)
94 tv->f |= TVSF_SKIP; tv->grps[TVOUT_SKIP]++;
95 tv->output->ops->skipgroup(tv->output, excuse, ap);
98 static void set_outcome(struct tvec_state *tv, unsigned out)
100 tv->f &= ~(TVSF_ACTIVE | TVSF_OUTMASK);
101 tv->f |= out << TVSF_OUTSHIFT;
104 void tvec_skip(struct tvec_state *tv, const char *excuse, ...)
107 va_start(ap, excuse); tvec_skip_v(tv, excuse, &ap); va_end(ap);
109 void tvec_skip_v(struct tvec_state *tv, const char *excuse, va_list *ap)
111 assert(tv->f&TVSF_ACTIVE);
112 set_outcome(tv, TVOUT_SKIP);
113 tv->output->ops->skip(tv->output, excuse, ap);
116 void tvec_fail(struct tvec_state *tv, const char *detail, ...)
119 va_start(ap, detail); tvec_fail_v(tv, detail, &ap); va_end(ap);
121 void tvec_fail_v(struct tvec_state *tv, const char *detail, va_list *ap)
123 assert((tv->f&TVSF_ACTIVE) ||
124 (tv->f&TVSF_OUTMASK) == (TVOUT_LOSE << TVSF_OUTSHIFT));
125 set_outcome(tv, TVOUT_LOSE); tv->output->ops->fail(tv->output, detail, ap);
128 void tvec_dumpreg(struct tvec_state *tv,
129 unsigned disp, const union tvec_regval *r,
130 const struct tvec_regdef *rd)
131 { tv->output->ops->dumpreg(tv->output, disp, r, rd); }
133 void tvec_mismatch(struct tvec_state *tv, unsigned f)
135 const struct tvec_regdef *rd;
136 const struct tvec_reg *rin, *rout;
138 for (rd = tv->test->regs; rd->name; rd++) {
139 if (rd->i >= tv->nrout) {
140 if (!(f&TVMF_IN)) continue;
141 rin = TVEC_REG(tv, in, rd->i);
142 tvec_dumpreg(tv, TVRD_INPUT, rin->f&TVRF_LIVE ? &rin->v : 0, rd);
144 if (!(f&TVMF_OUT)) continue;
145 rin = TVEC_REG(tv, in, rd->i); rout = TVEC_REG(tv, out, rd->i);
146 if (!(rin->f&TVRF_LIVE))
147 tvec_dumpreg(tv, TVRD_OUTPUT, rout->f&TVRF_LIVE ? &rout->v : 0, rd);
148 else if ((rout->f&TVRF_LIVE) && rd->ty->eq(&rin->v, &rout->v, rd))
149 tvec_dumpreg(tv, TVRD_MATCH, &rin->v, rd);
151 tvec_dumpreg(tv, TVRD_FOUND, rout->f&TVRF_LIVE ? &rout->v : 0, rd);
152 tvec_dumpreg(tv, TVRD_EXPECT, &rin->v, rd);
158 /*----- Parsing -----------------------------------------------------------*/
160 int tvec_syntax(struct tvec_state *tv, int ch, const char *expect, ...)
164 va_start(ap, expect); tvec_syntax_v(tv, ch, expect, &ap); va_end(ap);
167 int tvec_syntax_v(struct tvec_state *tv, int ch,
168 const char *expect, va_list *ap)
174 case EOF: strcpy(found, "#eof"); break;
175 case '\n': strcpy(found, "#eol"); ungetc(ch, tv->fp); break;
177 if (isprint(ch)) sprintf(found, "`%c'", ch);
178 else sprintf(found, "#\\x%02x", ch);
181 dstr_vputf(&d, expect, ap);
182 tvec_error(tv, "syntax error: expected %s but found %s", d.buf, found);
183 dstr_destroy(&d); return (-1);
186 void tvec_skipspc(struct tvec_state *tv)
192 if (ch == EOF) break;
193 else if (ch == '\n' || !isspace(ch)) { ungetc(ch, tv->fp); break; }
197 int tvec_flushtoeol(struct tvec_state *tv, unsigned f)
204 case '\n': tv->lno++; return (rc);
205 case EOF: return (rc);
206 case ';': f |= TVFF_ALLOWANY; break;
208 if (!(f&TVFF_ALLOWANY) && !isspace(ch)) {
209 tvec_syntax(tv, ch, "end-of-line");
210 rc = -1; f |= TVFF_ALLOWANY;
217 int tvec_nexttoken(struct tvec_state *tv)
219 enum { TAIL, NEWLINE, INDENT, COMMENT };
234 if (s == NEWLINE || s == INDENT) { ungetc(ch, tv->fp); return (-1); }
235 else { tv->lno++; s = NEWLINE; }
240 { if (s == NEWLINE) s = INDENT; }
241 else if (s != COMMENT) {
243 if (s == NEWLINE) return (-1);
251 int tvec_readword(struct tvec_state *tv, dstr *d, const char *delims,
252 const char *expect, ...)
257 va_start(ap, expect);
258 rc = tvec_readword_v(tv, d, delims, expect, &ap);
262 int tvec_readword_v(struct tvec_state *tv, dstr *d, const char *delims,
263 const char *expect, va_list *ap)
268 if (!ch || ch == '\n' || ch == EOF || ch == ';' ||
269 (delims && strchr(delims, ch))) {
270 if (expect) return (tvec_syntax(tv, ch, expect, ap));
271 else { ungetc(ch, tv->fp); return (-1); }
273 if (d->len) DPUTC(d, ' ');
277 } while (ch && ch != EOF && !isspace(ch) &&
278 (!delims || !strchr(delims, ch)));
279 DPUTZ(d); if (ch != EOF) ungetc(ch, tv->fp);
283 /*----- Main machinery ----------------------------------------------------*/
288 #define GROUPSTATE_INIT { 0 }
290 void tvec_resetoutputs(struct tvec_state *tv)
292 const struct tvec_regdef *rd;
295 for (rd = tv->test->regs; rd->name; rd++) {
296 assert(rd->i < tv->nreg);
297 if (rd->i >= tv->nrout) continue;
298 r = TVEC_REG(tv, out, rd->i);
299 rd->ty->release(&r->v, rd);
300 rd->ty->init(&r->v, rd);
301 r->f = TVEC_REG(tv, in, rd->i)->f&TVRF_LIVE;
305 static void init_registers(struct tvec_state *tv)
307 const struct tvec_regdef *rd;
310 for (rd = tv->test->regs; rd->name; rd++) {
311 assert(rd->i < tv->nreg); r = TVEC_REG(tv, in, rd->i);
312 rd->ty->init(&r->v, rd); r->f = 0;
313 if (rd->i < tv->nrout)
314 { r = TVEC_REG(tv, out, rd->i); rd->ty->init(&r->v, rd); r->f = 0; }
318 static void release_registers(struct tvec_state *tv)
320 const struct tvec_regdef *rd;
323 for (rd = tv->test->regs; rd->name; rd++) {
324 assert(rd->i < tv->nreg); r = TVEC_REG(tv, in, rd->i);
325 rd->ty->release(&r->v, rd); r->f = 0;
326 if (rd->i < tv->nrout)
327 { r = TVEC_REG(tv, out, rd->i); rd->ty->release(&r->v, rd); r->f = 0; }
331 int tvec_checkregs(struct tvec_state *tv)
333 const struct tvec_regdef *rd;
334 const struct tvec_reg *rin, *rout;
336 for (rd = tv->test->regs; rd->name; rd++) {
337 if (rd->i >= tv->nrout) continue;
338 rin = TVEC_REG(tv, in, rd->i); rout = TVEC_REG(tv, out, rd->i);
339 if (!rin->f&TVRF_LIVE) continue;
340 if (!(rout->f&TVRF_LIVE) || !rd->ty->eq(&rin->v, &rout->v, rd))
346 void tvec_check(struct tvec_state *tv, const char *detail, ...)
349 va_start(ap, detail); tvec_check_v(tv, detail, &ap); va_end(ap);
351 void tvec_check_v(struct tvec_state *tv, const char *detail, va_list *ap)
353 if (tvec_checkregs(tv))
354 { tvec_fail_v(tv, detail, ap); tvec_mismatch(tv, TVMF_IN | TVMF_OUT); }
357 static void begin_test(struct tvec_state *tv)
359 tv->f |= TVSF_ACTIVE; tv->f &= ~TVSF_OUTMASK;
360 tv->output->ops->btest(tv->output);
363 void tvec_endtest(struct tvec_state *tv)
367 if (tv->f&TVSF_ACTIVE) out = TVOUT_WIN;
368 else out = (tv->f&TVSF_OUTMASK) >> TVSF_OUTSHIFT;
369 assert(out < TVOUT_LIMIT); tv->curr[out]++;
370 tv->output->ops->etest(tv->output, out);
374 static void check(struct tvec_state *tv, struct groupstate *g)
376 const struct tvec_test *t = tv->test;
377 const struct tvec_env *env;
378 const struct tvec_regdef *rd;
380 if (!(tv->f&TVSF_OPEN)) return;
382 for (rd = t->regs; rd->name; rd++) {
383 if (TVEC_REG(tv, in, rd->i)->f&TVRF_LIVE)
384 { if (rd->i < tv->nrout) TVEC_REG(tv, out, rd->i)->f |= TVRF_LIVE; }
385 else if (!(rd->f&TVRF_OPT)) {
386 tvec_error(tv, "required register `%s' not set in test `%s'",
392 if (!(tv->f&TVSF_SKIP)) {
395 if (env && env->before && env->before(tv, g->ctx))
396 tvec_skip(tv, "test setup failed");
398 if (env && env->run) env->run(tv, t->fn, g->ctx);
399 else { t->fn(tv->in, tv->out, g->ctx); tvec_check(tv, 0); }
401 if (env && env->after) env->after(tv, g->ctx);
406 tv->f &= ~TVSF_OPEN; release_registers(tv); init_registers(tv);
409 static void begin_test_group(struct tvec_state *tv, struct groupstate *g)
411 const struct tvec_test *t = tv->test;
412 const struct tvec_env *env = t->env;
415 tv->output->ops->bgroup(tv->output);
418 for (i = 0; i < TVOUT_LIMIT; i++) tv->curr[i] = 0;
419 if (env && env->ctxsz) g->ctx = xmalloc(env->ctxsz);
420 if (env && env->setup && env->setup(tv, env, 0, g->ctx)) {
421 tvec_skipgroup(tv, "setup failed");
422 xfree(g->ctx); g->ctx = 0;
426 static void report_group(struct tvec_state *tv)
428 unsigned i, out, nrun;
430 for (i = 0, nrun = 0; i < TVOUT_LIMIT; i++)
431 { nrun += tv->curr[i]; tv->all[i] += tv->curr[i]; }
433 if (tv->curr[TVOUT_SKIP] == nrun)
434 { out = TVOUT_SKIP; tvec_skipgroup(tv, nrun ? 0 : "no tests to run"); }
436 if (tv->curr[TVOUT_LOSE]) out = TVOUT_LOSE;
437 else out = TVOUT_WIN;
438 tv->grps[out]++; tv->output->ops->egroup(tv->output);
442 static void end_test_group(struct tvec_state *tv, struct groupstate *g)
444 const struct tvec_test *t = tv->test;
445 const struct tvec_env *env;
448 if (tv->f&TVSF_OPEN) check(tv, g);
449 if (!(tv->f&TVSF_SKIP)) report_group(tv);
450 env = t->env; if (env && env->teardown) env->teardown(tv, g->ctx);
451 release_registers(tv); tv->test = 0; xfree(g->ctx); g->ctx = 0;
454 int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
457 const struct tvec_test *test;
458 const struct tvec_env *env;
459 const struct tvec_regdef *rd;
461 struct groupstate g = GROUPSTATE_INIT;
464 tv->infile = infile; tv->lno = 1; tv->fp = fp;
474 end_test_group(tv, &g);
476 DRESET(&d); tvec_readword(tv, &d, "];", "group name");
478 ch = getc(tv->fp); if (ch != ']') tvec_syntax(tv, ch, "`]'");
479 for (test = tv->tests; test->name; test++)
480 if (STRCMP(d.buf, ==, test->name)) goto found_test;
481 tvec_error(tv, "unknown test group `%s'", d.buf); goto flush_line;
483 tvec_flushtoeol(tv, 0); tv->test = test; begin_test_group(tv, &g);
488 if (tv->f&TVSF_OPEN) check(tv, &g);
495 if (ch == EOF) goto end;
496 else if (ch == ';') tvec_flushtoeol(tv, TVFF_ALLOWANY);
497 else if (tvec_flushtoeol(tv, 0)) rc = -1;
499 } else if (ch == ';')
500 tvec_flushtoeol(tv, TVFF_ALLOWANY);
504 if (tvec_readword(tv, &d, "=:;", "register name")) goto flush_line;
505 tvec_skipspc(tv); ch = getc(tv->fp);
506 if (ch != '=' && ch != ':')
507 { tvec_syntax(tv, ch, "`=' or `:'"); goto flush_line; }
510 { tvec_error(tv, "no current test"); goto flush_line; }
511 if (d.buf[0] == '@') {
513 if (!env || !env->set) ret = 0;
514 else ret = env->set(tv, d.buf, env, g.ctx);
517 tvec_error(tv, "unknown special register `%s'", d.buf);
520 if (!(tv->f&TVSF_OPEN))
521 { tv->test_lno = tv->lno; tv->f |= TVSF_OPEN; }
523 for (rd = tv->test->regs; rd->name; rd++)
524 if (STRCMP(rd->name, ==, d.buf)) goto found_reg;
525 tvec_error(tv, "unknown register `%s' for test `%s'",
526 d.buf, tv->test->name);
529 if (!(tv->f&TVSF_OPEN))
530 { tv->test_lno = tv->lno; tv->f |= TVSF_OPEN; }
532 r = TVEC_REG(tv, in, rd->i);
533 if (r->f&TVRF_LIVE) {
534 tvec_error(tv, "register `%s' already set", rd->name);
537 if (rd->ty->parse(&r->v, rd, tv)) goto flush_line;
546 tvec_flushtoeol(tv, TVFF_ALLOWANY); rc = -1;
549 { tvec_error(tv, "error reading input: %s", strerror(errno)); rc = -1; }
551 end_test_group(tv, &g);
552 tv->infile = 0; tv->fp = 0;
557 /*----- Session lifecycle -------------------------------------------------*/
559 void tvec_begin(struct tvec_state *tv_out,
560 const struct tvec_config *config,
561 struct tvec_output *o)
567 assert(config->nrout <= config->nreg);
568 tv_out->nrout = config->nrout; tv_out->nreg = config->nreg;
569 tv_out->regsz = config->regsz;
570 tv_out->in = xmalloc(tv_out->nreg*tv_out->regsz);
571 tv_out->out = xmalloc(tv_out->nrout*tv_out->regsz);
572 for (i = 0; i < tv_out->nreg; i++) {
573 TVEC_REG(tv_out, in, i)->f = 0;
574 if (i < tv_out->nrout) TVEC_REG(tv_out, out, i)->f = 0;
577 for (i = 0; i < TVOUT_LIMIT; i++)
578 tv_out->curr[i] = tv_out->all[i] = tv_out->grps[i] = 0;
580 tv_out->tests = config->tests; tv_out->test = 0;
581 tv_out->infile = 0; tv_out->lno = 0; tv_out->fp = 0;
582 tv_out->output = o; tv_out->output->ops->bsession(tv_out->output, tv_out);
585 int tvec_end(struct tvec_state *tv)
587 int rc = tv->output->ops->esession(tv->output);
589 tv->output->ops->destroy(tv->output);
590 xfree(tv->in); xfree(tv->out);
594 /*----- Serialization and deserialization ---------------------------------*/
596 int tvec_serialize(const struct tvec_reg *rv, buf *b,
597 const struct tvec_regdef *regs,
598 unsigned nr, size_t regsz)
600 unsigned char *bitmap;
601 size_t i, bitoff, nbits, bitsz;
602 const struct tvec_regdef *rd;
603 const struct tvec_reg *r;
606 for (rd = regs, nbits = 0; rd->name; rd++, nbits++);
607 bitsz = (nbits + 7)/8;
609 bitmap = buf_get(b, bitsz); if (!bitmap) return (-1);
610 memset(bitmap, 0, bitsz);
611 for (rd = regs, i = 0; rd->name; rd++, i++) {
612 if (rd->i >= nr) continue;
613 r = TVEC_GREG(rv, rd->i, regsz); if (!(r->f&TVRF_LIVE)) continue;
614 bitmap = BBASE(b) + bitoff; bitmap[rd->i/8] |= 1 << rd->i%8;
615 if (rd->ty->tobuf(b, &r->v, rd)) return (-1);
620 int tvec_deserialize(struct tvec_reg *rv, buf *b,
621 const struct tvec_regdef *regs,
622 unsigned nr, size_t regsz)
624 const unsigned char *bitmap;
625 size_t i, nbits, bitsz;
626 const struct tvec_regdef *rd;
629 for (rd = regs, nbits = 0; rd->name; rd++, nbits++);
630 bitsz = (nbits + 7)/8;
632 bitmap = buf_get(b, bitsz); if (!bitmap) return (-1);
633 for (rd = regs, i = 0; rd->name; rd++, i++) {
634 if (rd->i >= nr) continue;
635 if (!(bitmap[rd->i/8]&(1 << rd->i%8))) continue;
636 r = TVEC_GREG(rv, rd->i, regsz);
637 if (rd->ty->frombuf(b, &r->v, rd)) return (-1);
643 /*----- Ad-hoc testing ----------------------------------------------------*/
645 static const struct tvec_regdef no_regs = { 0, 0, 0, 0, { 0 } };
647 static void fakefn(const struct tvec_reg *in, struct tvec_reg *out, void *p)
648 { assert(!"fake test function"); }
650 void tvec_adhoc(struct tvec_state *tv, struct tvec_test *t)
652 t->name = "<unset>"; t->regs = &no_regs; t->env = 0; t->fn = fakefn;
656 void tvec_begingroup(struct tvec_state *tv, const char *name,
657 const char *file, unsigned lno)
659 struct tvec_test *t = (/*unconst*/ struct tvec_test *)tv->tests;
661 t->name = name; tv->test = t;
662 tv->infile = file; tv->lno = tv->test_lno = lno;
663 begin_test_group(tv, 0);
666 void tvec_endgroup(struct tvec_state *tv)
668 if (!(tv->f&TVSF_SKIP)) report_group(tv);
672 void tvec_begintest(struct tvec_state *tv, const char *file, unsigned lno)
674 tv->infile = file; tv->lno = tv->test_lno = lno;
675 begin_test(tv); tv->f |= TVSF_OPEN;
681 const char *saved_file; unsigned saved_lno;
684 static void adhoc_claim_setup(struct tvec_state *tv,
685 struct adhoc_claim *ck,
686 const struct tvec_regdef *regs,
687 const char *file, unsigned lno)
689 struct tvec_test *t = (/*unconst*/ struct tvec_test *)tv->test;
693 if (!(tv->f&TVSF_OPEN))
694 { ck->f |= ACF_FRESH; tvec_begintest(tv, file, lno); }
696 ck->saved_file = tv->infile; if (file) tv->infile = file;
697 ck->saved_lno = tv->test_lno; if (file) tv->test_lno = lno;
698 t->regs = regs ? regs : &no_regs;
701 static void adhoc_claim_teardown(struct tvec_state *tv,
702 struct adhoc_claim *ck)
704 struct tvec_test *t = (/*unconst*/ struct tvec_test *)tv->test;
707 tv->infile = ck->saved_file; tv->test_lno = ck->saved_lno;
709 if (ck->f&ACF_FRESH) tvec_endtest(tv);
712 int tvec_claim_v(struct tvec_state *tv, int ok,
713 const char *file, unsigned lno,
714 const char *msg, va_list *ap)
716 struct adhoc_claim ck;
718 adhoc_claim_setup(tv, &ck, 0, file, lno);
719 if (!ok) tvec_fail_v(tv, msg, ap);
720 adhoc_claim_teardown(tv, &ck);
724 int tvec_claim(struct tvec_state *tv, int ok,
725 const char *file, unsigned lno, const char *msg, ...)
729 va_start(ap, msg); tvec_claim_v(tv, ok, file, lno, msg, &ap); va_end(ap);
733 int tvec_claimeq(struct tvec_state *tv,
734 const struct tvec_regty *ty, const union tvec_misc *arg,
735 const char *file, unsigned lno, const char *expr)
737 struct tvec_regdef regs[2];
738 struct adhoc_claim ck;
741 tv->in[0].f = tv->out[0].f = TVRF_LIVE;
743 regs[0].name = "value"; regs[0].i = 0;
744 regs[0].ty = ty; regs[0].f = 0;
745 if (arg) regs[0].arg = *arg;
746 else regs[0].arg.p = 0;
750 adhoc_claim_setup(tv, &ck, regs, file, lno);
751 ok = ty->eq(&tv->in[0].v, &tv->out[0].v, ®s[0]);
753 { tvec_fail(tv, "%s", expr ); tvec_mismatch(tv, TVMF_IN | TVMF_OUT); }
754 adhoc_claim_teardown(tv, &ck);
758 /*----- That's all, folks -------------------------------------------------*/