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 ------------------------------------------------------*/
40 #include "tvec-adhoc.h"
41 #include "tvec-output.h"
42 #include "tvec-types.h"
44 /*----- Output ------------------------------------------------------------*/
46 /* --- @tvec_strlevel@ --- *
48 * Arguments: @unsigned level@ = level code
50 * Returns: A human-readable description.
52 * Use: Converts a level code into something that you can print in a
56 const char *tvec_strlevel(unsigned level)
59 #define CASE(tag, name, val) \
60 case TVLEV_##tag: return (name);
63 default: return ("??");
67 /* --- @tvec_report@, @tvec_report_v@ --- *
69 * Arguments: @struct tvec_state *tv@ = test-vector state
70 * @unsigned level@ = severity level (@TVlEV_...@)
71 * @const char *msg@, @va_list ap@ = error message
75 * Use: Report an message with a given severity. Messages with level
76 * @TVLEV_ERR@ or higher force a nonzero exit code.
79 void tvec_report(struct tvec_state *tv, unsigned level, const char *msg, ...)
83 va_start(ap, msg); tvec_report_v(tv, level, msg, &ap); va_end(ap);
86 void tvec_report_v(struct tvec_state *tv, unsigned level,
87 const char *msg, va_list *ap)
89 tv->output->ops->report(tv->output, level, msg, ap);
90 if (level >= TVLEV_ERR) tv->f |= TVSF_ERROR;
93 /* --- @tvec_error@, @tvec_notice@, @tvec_info@ --- *
95 * Arguments: @struct tvec_state *tv@ = test-vector state
96 * @const char *msg@, @va_list ap@ = error message
98 * Returns: The @tvec_error@ function returns %$-1$% as a trivial
99 * convenience; @tvec_notice@ does not return a value.
101 * Use: Report a message. Errors are distinct from test
102 * failures, and indicate that a problem was encountered which
103 * compromised the activity of testing. Notices are important
104 * information which doesn't fit into any other obvious
105 * category. Information is anything else, and is a reasonable
106 * fallback for writing unstructured information in the absence
107 * of dedicated support in an output driver.
109 * These functions are simple convenience wrappers around
110 * @tvec_report@. Use @tvec_report_v@ directly if you have a
111 * captured @va_list@ of arguments to format.
114 int tvec_error(struct tvec_state *tv, const char *msg, ...)
118 va_start(ap, msg); tvec_report_v(tv, TVLEV_ERR, msg, &ap); va_end(ap);
122 void tvec_notice(struct tvec_state *tv, const char *msg, ...)
126 va_start(ap, msg); tvec_report_v(tv, TVLEV_NOTE, msg, &ap); va_end(ap);
129 void tvec_info(struct tvec_state *tv, const char *msg, ...)
133 va_start(ap, msg); tvec_report_v(tv, TVLEV_INFO, msg, &ap); va_end(ap);
136 /* --- @tvec_outputext@ --- *
138 * Arguments: @struct tvec_state *tv@ = test-vector state
139 * @struct tvec_output **o_out@ = output object
140 * @struct tvec_fallbackoutput *fo@ = fallback output
142 * @const char *ext@ = extension name
143 * @const void *fops@ = fallback operations
145 * Returns: An output extension operations table.
147 * Use: Calls the output driver's @extend@ function, passing @ext@ as
148 * the extension name. If the output driver recognizes the
149 * extension, then @*o_out@ is set to the output driver object
150 * and the driver's extension-operations table is returned.
151 * Otherwise, a fallback output object is constructed in @*fo@,
152 * @*o_out@ is set to @&fo->_o@, and @fops@ is returned. In
153 * this way, a call to an extension function, passing @*o_out@
154 * as the output object, will either call the output driver's
155 * extension implementation or the fallback implementation as
159 const void *tvec_outputext(struct tvec_state *tv, struct tvec_output **o_out,
160 struct tvec_fallbackoutput *fo,
161 const char *ext, const void *fops)
164 struct tvec_output *o = tv->output;
166 ops = o->ops->extend(o, ext);
167 if (!ops) { fo->_o.ops = 0; fo->tv = tv; o = &fo->_o; ops = fops; }
168 *o_out = o; return (ops);
171 /*----- Test processing ---------------------------------------------------*/
173 /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
175 * Arguments: @struct tvec_state *tv@ = test-vector state
176 * @const char *excuse@, @va_list *ap@ = reason why skipped
180 * Use: Skip the current group. This should only be called from a
181 * test environment @setup@ function; a similar effect occurs if
182 * the @setup@ function fails.
185 void tvec_skipgroup(struct tvec_state *tv, const char *excuse, ...)
189 va_start(ap, excuse); tvec_skipgroup_v(tv, excuse, &ap); va_end(ap);
192 void tvec_skipgroup_v(struct tvec_state *tv, const char *excuse, va_list *ap)
194 if (!(tv->f&TVSF_SKIP)) {
195 tv->f |= TVSF_SKIP; tv->grps[TVOUT_SKIP]++;
196 tv->output->ops->skipgroup(tv->output, excuse, ap);
200 /* --- @set_outcome@ --- *
202 * Arguments: @struct tvec_state *tv@ = test-vector state
203 * @unsigned out@ = the new outcome
207 * Use: Sets the outcome bits in the test state flags, and clears
211 static void set_outcome(struct tvec_state *tv, unsigned out)
212 { tv->f = (tv->f&~(TVSF_ACTIVE | TVSF_OUTMASK)) | (out << TVSF_OUTSHIFT); }
214 /* --- @tvec_skip@, @tvec_skip_v@ --- *
216 * Arguments: @struct tvec_state *tv@ = test-vector state
217 * @const char *excuse@, @va_list *ap@ = reason why test skipped
221 * Use: Skip the current test. This should only be called from a
222 * test environment @run@ function; a similar effect occurs if
223 * the @before@ function fails.
226 void tvec_skip(struct tvec_state *tv, const char *excuse, ...)
229 va_start(ap, excuse); tvec_skip_v(tv, excuse, &ap); va_end(ap);
232 void tvec_skip_v(struct tvec_state *tv, const char *excuse, va_list *ap)
234 assert(tv->f&TVSF_ACTIVE);
235 set_outcome(tv, TVOUT_SKIP);
236 tv->output->ops->skip(tv->output, excuse, ap);
239 /* --- @tvec_fail@, @tvec_fail_v@ --- *
241 * Arguments: @struct tvec_state *tv@ = test-vector state
242 * @const char *detail@, @va_list *ap@ = description of test
246 * Use: Report the current test as a failure. This function can be
247 * called multiple times for a single test, e.g., if the test
248 * environment's @run@ function invokes the test function
249 * repeatedly; but a single test that fails repeatedly still
250 * only counts as a single failure in the statistics. The
251 * @detail@ string and its format parameters can be used to
252 * distinguish which of several invocations failed; it can
253 * safely be left null if the test function is run only once.
256 void tvec_fail(struct tvec_state *tv, const char *detail, ...)
259 va_start(ap, detail); tvec_fail_v(tv, detail, &ap); va_end(ap);
262 void tvec_fail_v(struct tvec_state *tv, const char *detail, va_list *ap)
264 assert((tv->f&TVSF_ACTIVE) ||
265 (tv->f&TVSF_OUTMASK) == (TVOUT_LOSE << TVSF_OUTSHIFT));
266 set_outcome(tv, TVOUT_LOSE); tv->output->ops->fail(tv->output, detail, ap);
269 /* --- @tvec_dumpreg@ --- *
271 * Arguments: @struct tvec_state *tv@ = test-vector state
272 * @unsigned disp@ = the register disposition (@TVRD_...@)
273 * @const union tvec_regval *tv@ = register value, or null
274 * @const struct tvec_regdef *rd@ = register definition
278 * Use: Dump a register value to the output. This is the lowest-
279 * level function for dumping registers, and calls the output
280 * formatter directly.
282 * Usually @tvec_mismatch@ is much more convenient. Low-level
283 * access is required for reporting `virtual' registers
284 * corresponding to test environment settings.
287 void tvec_dumpreg(struct tvec_state *tv,
288 unsigned disp, const union tvec_regval *r,
289 const struct tvec_regdef *rd)
290 { tv->output->ops->dumpreg(tv->output, disp, r, rd); }
292 /* --- @tvec_mismatch@ --- *
294 * Arguments: @struct tvec_state *tv@ = test-vector state
295 * @unsigned f@ = flags (@TVMF_...@)
299 * Use: Dumps registers suitably to report a mismatch. The flag bits
300 * @TVMF_IN@ and @TVF_OUT@ select input-only and output
301 * registers. If both are reset then nothing happens.
302 * Suppressing the output registers may be useful, e.g., if the
303 * test function crashed rather than returning outputs.
306 void tvec_mismatch(struct tvec_state *tv, unsigned f)
308 const struct tvec_regdef *rd;
309 const struct tvec_reg *rin, *rout;
311 for (rd = tv->test->regs; rd->name; rd++) {
312 if (rd->i >= tv->cfg.nrout) {
313 if (!(f&TVMF_IN)) continue;
314 rin = TVEC_REG(tv, in, rd->i);
315 tvec_dumpreg(tv, TVRD_INPUT, rin->f&TVRF_LIVE ? &rin->v : 0, rd);
317 if (!(f&TVMF_OUT)) continue;
318 rin = TVEC_REG(tv, in, rd->i); rout = TVEC_REG(tv, out, rd->i);
319 if (!(rin->f&TVRF_LIVE))
320 tvec_dumpreg(tv, TVRD_OUTPUT, rout->f&TVRF_LIVE ? &rout->v : 0, rd);
321 else if ((rout->f&TVRF_LIVE) && rd->ty->eq(&rin->v, &rout->v, rd))
322 tvec_dumpreg(tv, TVRD_MATCH, &rin->v, rd);
324 tvec_dumpreg(tv, TVRD_FOUND, rout->f&TVRF_LIVE ? &rout->v : 0, rd);
325 tvec_dumpreg(tv, TVRD_EXPECT, &rin->v, rd);
331 /*----- Parsing -----------------------------------------------------------*/
333 /* --- @tvec_syntax@, @tvec_syntax_v@ --- *
335 * Arguments: @struct tvec_state *tv@ = test-vector state
336 * @int ch@ = the character found (in @fgetc@ format)
337 * @const char *expect@, @va_list ap@ = what was expected
341 * Use: Report a syntax error quoting @ch@ and @expect@. If @ch@ is
342 * a newline, then back up so that it can be read again (e.g.,
343 * by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
344 * advance the line number).
347 int tvec_syntax(struct tvec_state *tv, int ch, const char *expect, ...)
351 va_start(ap, expect); tvec_syntax_v(tv, ch, expect, &ap); va_end(ap);
355 int tvec_syntax_v(struct tvec_state *tv, int ch,
356 const char *expect, va_list *ap)
362 case EOF: strcpy(found, "#eof"); break;
363 case '\n': strcpy(found, "#eol"); ungetc(ch, tv->fp); break;
365 if (isprint(ch)) sprintf(found, "`%c'", ch);
366 else sprintf(found, "#\\x%02x", ch);
369 dstr_vputf(&d, expect, ap);
370 tvec_error(tv, "syntax error: expected %s but found %s", d.buf, found);
371 dstr_destroy(&d); return (-1);
374 /* --- @tvec_unkregerr@ --- *
376 * Arguments: @struct tvec_state *tv@ = test-vector state
377 * @const char *name@ = register or pseudoregister name
381 * Use: Reports an error that the register or pseudoregister is
385 int tvec_unkregerr(struct tvec_state *tv, const char *name)
387 return (tvec_error(tv, "unknown special register `%s' for test `%s'",
388 name, tv->test->name));
391 /* --- @tvec_dupregerr@ --- *
393 * Arguments: @struct tvec_state *tv@ = test-vector state
394 * @const char *name@ = register or pseudoregister name
398 * Use: Reports an error that the register or pseudoregister has been
399 * assigned already in the current test.
402 int tvec_dupregerr(struct tvec_state *tv, const char *name)
403 { return (tvec_error(tv, "register `%s' is already set", name)); }
405 /* --- @tvec_skipspc@ --- *
407 * Arguments: @struct tvec_state *tv@ = test-vector state
411 * Use: Advance over any whitespace characters other than newlines.
412 * This will stop at `;', end-of-file, or any other kind of
413 * non-whitespace; and it won't consume a newline.
416 void tvec_skipspc(struct tvec_state *tv)
422 if (ch == EOF) break;
423 else if (ch == '\n' || !isspace(ch)) { ungetc(ch, tv->fp); break; }
427 /* --- @tvec_flushtoeol@ --- *
429 * Arguments: @struct tvec_state *tv@ = test-vector state
430 * @unsigned f@ = flags (@TVFF_...@)
432 * Returns: Zero on success, @-1@ on error.
434 * Use: Advance to the start of the next line, consuming the
437 * A syntax error is reported if no newline character is found,
438 * i.e., the file ends in mid-line. A syntax error is also
439 * reported if material other than whitespace or a comment is
440 * found before the end of the line end, and @TVFF_ALLOWANY@ is
441 * not set in @f@. The line number count is updated
445 int tvec_flushtoeol(struct tvec_state *tv, unsigned f)
452 case '\n': tv->lno++; return (rc);
453 case EOF: return (rc);
454 case ';': f |= TVFF_ALLOWANY; break;
456 if (!(f&TVFF_ALLOWANY) && !isspace(ch)) {
457 tvec_syntax(tv, ch, "end-of-line");
458 rc = -1; f |= TVFF_ALLOWANY;
465 /* --- @tvec_nexttoken@ --- *
467 * Arguments: @struct tvec_state *tv@ = test-vector state
469 * Returns: Zero if there is a next token which can be read; @-1@ if no
470 * token is available.
472 * Use: Advance to the next whitespace-separated token, which may be
475 * Tokens are separated by non-newline whitespace, comments, and
476 * newlines followed by whitespace; a newline /not/ followed by
477 * whitespace instead begins the next assignment, and two
478 * newlines separated only by whitespace terminate the data for
481 * If this function returns zero, then the next character in the
482 * file begins a suitable token which can be read and
483 * processed. If it returns @-1@ then there is no such token,
484 * and the file position is left correctly. The line number
485 * count is updated appropriately.
488 int tvec_nexttoken(struct tvec_state *tv)
490 enum { TAIL, NEWLINE, INDENT, COMMENT };
505 if (s == NEWLINE || s == INDENT) { ungetc(ch, tv->fp); return (-1); }
506 else { tv->lno++; s = NEWLINE; }
511 { if (s == NEWLINE) s = INDENT; }
512 else if (s != COMMENT) {
514 if (s == NEWLINE) return (-1);
522 /* --- @tvec_readword@, @tvec_readword_v@ --- *
524 * Arguments: @struct tvec_state *tv@ = test-vector state
525 * @dstr *d@ = string to append the word to
526 * @const char **p_inout@ = pointer into string, updated
527 * @const char *delims@ = additional delimiters to stop at
528 * @const char *expect@, @va_list ap@ = what was expected
530 * Returns: Zero on success, @-1@ on failure.
532 * Use: A `word' consists of characters other than whitespace, null
533 * characters, and other than those listed in @delims@;
534 * furthermore, a word does not begin with a `;'. (If you want
535 * reading to stop at comments not preceded by whitespace, then
536 * include `;' in @delims@. This is a common behaviour.)
538 * If there is no word beginning at the current file position,
539 * then return @-1@; furthermore, if @expect@ is not null, then
540 * report an appropriate error via @tvec_syntax@.
542 * Otherwise, the word is accumulated in @d@ and zero is
543 * returned; if @d@ was not empty at the start of the call, the
544 * newly read word is separated from the existing material by a
545 * single space character. Since null bytes are never valid
546 * word constituents, a null terminator is written to @d@, and
547 * it is safe to treat the string in @d@ as being null-
550 * If @p_inout@ is not null, then @*p_inout@ must be a pointer
551 * into @d->buf@, which will be adjusted so that it will
552 * continue to point at the same position even if the buffer is
553 * reallocated. As a subtle tweak, if @*p_inout@ initially
554 * points at the end of the buffer, then it will be adjusted to
555 * point at the beginning of the next word, rather than at the
556 * additional intervening space.
559 int tvec_readword(struct tvec_state *tv, dstr *d, const char **p_inout,
560 const char *delims, const char *expect, ...)
565 va_start(ap, expect);
566 rc = tvec_readword_v(tv, d, p_inout, delims, expect, &ap);
571 int tvec_readword_v(struct tvec_state *tv, dstr *d, const char **p_inout,
572 const char *delims, const char *expect, va_list *ap)
580 if (!ch || ch == '\n' || ch == EOF || ch == ';' ||
581 (delims && strchr(delims, ch))) {
582 if (expect) return (tvec_syntax(tv, ch, expect, ap));
583 else { ungetc(ch, tv->fp); return (-1); }
585 if (p_inout) pos = *p_inout - d->buf;
587 if (pos == d->len) pos++;
593 } while (ch && ch != EOF && !isspace(ch) &&
594 (!delims || !strchr(delims, ch)));
595 DPUTZ(d); if (ch != EOF) ungetc(ch, tv->fp);
596 if (p_inout) *p_inout = d->buf + pos;
600 /*----- Main machinery ----------------------------------------------------*/
603 void *ctx; /* test environment context */
604 unsigned f; /* flags */
605 #define GRPF_SETOUTC 1u /* set outcome */
606 #define GRPF_SETMASK (GRPF_SETOUTC) /* mask of all variable flags */
608 #define GROUPSTATE_INIT { 0, 0 }
610 /* --- @tvec_initregs@, @tvec_releaseregs@ --- *
612 * Arguments: @struct tvec_state *tv@ = test-vector state
616 * Use: Initialize or release, respectively, the registers required
617 * by the current test. All of the registers, both input and
618 * output, are effected. Initialized registers are not marked
622 void tvec_initregs(struct tvec_state *tv)
624 const struct tvec_regdef *rd;
627 for (rd = tv->test->regs; rd->name; rd++) {
628 assert(rd->i < tv->cfg.nreg); r = TVEC_REG(tv, in, rd->i);
629 rd->ty->init(&r->v, rd); r->f = 0;
630 if (rd->i < tv->cfg.nrout)
631 { r = TVEC_REG(tv, out, rd->i); rd->ty->init(&r->v, rd); r->f = 0; }
635 void tvec_releaseregs(struct tvec_state *tv)
637 const struct tvec_regdef *rd;
640 for (rd = tv->test->regs; rd->name; rd++) {
641 assert(rd->i < tv->cfg.nreg); r = TVEC_REG(tv, in, rd->i);
642 rd->ty->release(&r->v, rd); r->f = 0;
643 if (rd->i < tv->cfg.nrout)
644 { r = TVEC_REG(tv, out, rd->i); rd->ty->release(&r->v, rd); r->f = 0; }
648 /* --- @tvec_resetoutputs@ --- *
650 * Arguments: @struct tvec_state *tv@ = test-vector state
654 * Use: Reset (releases and reinitializes) the output registers in
655 * the test state. This is mostly of use to test environment
656 * @run@ functions, between invocations of the test function.
657 * Output registers are marked live if and only if the
658 * corresponding input register is live.
661 void tvec_resetoutputs(struct tvec_state *tv)
663 const struct tvec_regdef *rd;
666 for (rd = tv->test->regs; rd->name; rd++) {
667 assert(rd->i < tv->cfg.nreg);
668 if (rd->i >= tv->cfg.nrout) continue;
669 r = TVEC_REG(tv, out, rd->i);
670 rd->ty->release(&r->v, rd);
671 rd->ty->init(&r->v, rd);
672 r->f = TVEC_REG(tv, in, rd->i)->f&TVRF_LIVE;
676 /* --- @tvec_checkregs@ --- *
678 * Arguments: @struct tvec_state *tv@ = test-vector state
680 * Returns: Zero on success, @-1@ on mismatch.
682 * Use: Compare the active output registers (according to the current
683 * test group definition) with the corresponding input register
684 * values. A mismatch occurs if the two values differ
685 * (according to the register type's @eq@ method), or if the
686 * input is live but the output is dead.
688 * This function only checks for a mismatch and returns the
689 * result; it takes no other action. In particular, it doesn't
690 * report a failure, or dump register values.
693 int tvec_checkregs(struct tvec_state *tv)
695 const struct tvec_regdef *rd;
696 const struct tvec_reg *rin, *rout;
698 for (rd = tv->test->regs; rd->name; rd++) {
699 if (rd->i >= tv->cfg.nrout) continue;
700 rin = TVEC_REG(tv, in, rd->i); rout = TVEC_REG(tv, out, rd->i);
701 if (!(rin->f&TVRF_LIVE)) continue;
702 if (!(rout->f&TVRF_LIVE) || !rd->ty->eq(&rin->v, &rout->v, rd))
708 /* --- @tvec_check@, @tvec_check_v@ --- *
710 * Arguments: @struct tvec_state *tv@ = test-vector state
711 * @const char *detail@, @va_list *ap@ = description of test
715 * Use: Check the register values, reporting a failure and dumping
716 * the registers in the event of a mismatch. This just wraps up
717 * @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
721 void tvec_check(struct tvec_state *tv, const char *detail, ...)
724 va_start(ap, detail); tvec_check_v(tv, detail, &ap); va_end(ap);
727 void tvec_check_v(struct tvec_state *tv, const char *detail, va_list *ap)
729 if (tvec_checkregs(tv))
730 { tvec_fail_v(tv, detail, ap); tvec_mismatch(tv, TVMF_IN | TVMF_OUT); }
733 /* --- @open_test@ --- *
735 * Arguments: @struct tvec_state *tv@ = test-vector state
739 * Use: Note that we are now collecting data for a new test. The
740 * current line number is recorded in @test_lno@. The
741 * @TVSF_OPEN@ flag is set, and @TVSF_XFAIL@ is reset.
743 * If a test is already open, then do nothing.
746 static void open_test(struct tvec_state *tv)
748 if (!(tv->f&TVSF_OPEN)) {
749 tv->test_lno = tv->lno;
750 tv->f |= TVSF_OPEN; tv->f &= ~TVSF_XFAIL;
754 /* --- @begin_test@ --- *
756 * Arguments: @struct tvec_state *tv@ = test-vector state
760 * Use: Note that we're about to try running a state. This is called
761 * before the test environment's @before@ function. Mark the
762 * test as active, clear the outcome, and inform the output
766 static void begin_test(struct tvec_state *tv)
768 tv->f |= TVSF_ACTIVE; tv->f &= ~TVSF_OUTMASK;
769 tv->output->ops->btest(tv->output);
772 /* --- @tvec_endtest@ --- *
774 * Arguments: @struct tvec_state *tv@ = test-vector state
778 * Use: End an ad-hoc test case, The statistics are updated and the
779 * outcome is reported to the output formatter.
782 void tvec_endtest(struct tvec_state *tv)
786 if (!(tv->f&TVSF_ACTIVE)) /* nothing to do */;
787 else if (tv->f&TVSF_XFAIL) set_outcome(tv, TVOUT_XFAIL);
788 else set_outcome(tv, TVOUT_WIN);
789 out = (tv->f&TVSF_OUTMASK) >> TVSF_OUTSHIFT;
790 assert(out < TVOUT_LIMIT); tv->curr[out]++;
791 tv->output->ops->etest(tv->output, out);
795 /* --- @tvec_xfail@ --- *
797 * Arguments: @struct tvec_state *tv@ = test-vector state
801 * Use: Mark the current test as an `expected failure'. That is, the
802 * behaviour -- if everything works as expected -- is known to
803 * be incorrect, perhaps as a result of a longstanding bug, so
804 * calling it a `success' would be inappropriate. A failure, as
805 * reported by @tvec_fail@, means that the behaviour is not as
806 * expected -- either the long-standing bug has been fixed, or a
807 * new bug has been introduced -- so investigation is required.
809 * An expected failure doesn't cause the test group or the
810 * session as a whole to fail, but it isn't counted as a win
814 void tvec_xfail(struct tvec_state *tv)
815 { tv->f |= TVSF_XFAIL; }
819 * Arguments: @struct tvec_state *tv@ = test-vector state
820 * @struct groupstate *g@ = private test group state
824 * Use: Run the current test.
826 * This function is called once the data for a test has been
827 * collected. It's responsible for checking that all of the
828 * necessary registers have been assigned values. It marks the
829 * output registers as live if the corresponding inputs are
830 * live. It calls the environment's @before@, @run@, and
831 * @after@ functions if provided; if there is no @run@ function,
832 * then it calls the test function directly, passing it the
833 * environment's context pointer, and then calls @tvec_check@ to
834 * verify the output values.
837 static void check(struct tvec_state *tv, struct groupstate *g)
839 const struct tvec_test *t = tv->test;
840 const struct tvec_env *env = t->env;
841 const struct tvec_regdef *rd;
846 if (!(tv->f&TVSF_OPEN)) return;
848 for (rd = t->regs; rd->name; rd++) {
849 r = TVEC_REG(tv, in, rd->i);
850 if (r->f&TVRF_LIVE) {
851 if (rd->i < tv->cfg.nrout)
852 TVEC_REG(tv, out, rd->i)->f |= TVRF_LIVE;
853 } else if (!(r->f&TVRF_SEEN) && !(rd->f&TVRF_OPT)) {
854 tvec_error(tv, "required register `%s' not set in test `%s'",
860 if (!(tv->f&TVSF_SKIP)) {
862 if (f&f_err) tvec_skip(tv, "erroneous test data");
863 if (env && env->before) env->before(tv, g->ctx);
864 if (!(tv->f&TVSF_ACTIVE))
866 else if (env && env->run)
867 env->run(tv, t->fn, g->ctx);
869 t->fn(tv->in, tv->out, g->ctx);
875 if (env && env->after) env->after(tv, g->ctx);
876 g->f &= ~GRPF_SETMASK;
877 tv->f &= ~TVSF_OPEN; tvec_releaseregs(tv); tvec_initregs(tv);
882 /* --- @begin_test_group@ --- *
884 * Arguments: @struct tvec_state *tv@ = test-vector state
885 * @struct groupstate *g@ = private test group state
889 * Use: Begins a test group. Expects @tv->test@ to have been set
890 * already. Calls the output driver, initializes the registers,
891 * clears the @tv->curr@ counters, allocates the environment
892 * context and calls the environment @setup@ function.
895 static void begin_test_group(struct tvec_state *tv, struct groupstate *g)
897 const struct tvec_test *t = tv->test;
898 const struct tvec_env *env = t->env;
899 const struct tvec_regdef *rd0, *rd1;
903 /* Check that the register names and indices are distinct. */
904 for (rd0 = t->regs; rd0->name; rd0++) {
905 assert(rd0->i < tv->cfg.nreg);
906 for (rd1 = t->regs; rd1->name; rd1++)
908 assert(rd0->i != rd1->i);
909 assert(STRCMP(rd0->name, !=, rd1->name));
914 tv->output->ops->bgroup(tv->output);
915 tv->f &= ~(TVSF_SKIP | TVSF_MUFFLE);
917 for (i = 0; i < TVOUT_LIMIT; i++) tv->curr[i] = 0;
918 if (env && env->ctxsz) g->ctx = x_alloc(tv->a, env->ctxsz);
919 if (env && env->setup) env->setup(tv, env, 0, g->ctx);
922 /* --- @report_group@ --- *
924 * Arguments: @struct tvec_state *tv@ = test-vector state
928 * Use: Reports the result of the test group to the output driver.
930 * If all of the tests have been skipped then report this as a
931 * group skip. Otherwise, determine and report the group
935 static void report_group(struct tvec_state *tv)
937 unsigned i, out, nrun;
939 for (i = 0, nrun = 0; i < TVOUT_LIMIT; i++)
940 { nrun += tv->curr[i]; tv->all[i] += tv->curr[i]; }
942 if (tv->curr[TVOUT_SKIP] == nrun)
943 { out = TVOUT_SKIP; tvec_skipgroup(tv, nrun ? 0 : "no tests to run"); }
945 if (tv->curr[TVOUT_LOSE]) out = TVOUT_LOSE;
946 else out = TVOUT_WIN;
947 tv->grps[out]++; tv->output->ops->egroup(tv->output);
951 /* --- @end_test_group@ --- *
953 * Arguments: @struct tvec_state *tv@ = test-vector state
954 * @struct groupstate *g@ = private test group state
958 * Use: Handles the end of a test group. Called at the end of the
959 * input file or when a new test group header is found.
961 * If a test is open, call @check@ to see whether it worked. If
962 * the test group is not being skipped, report the group
963 * result. Call the test environment @teardown@ function. Free
964 * the environment context and release the registers.
966 * If there's no test group active, then nothing happens.
969 static void end_test_group(struct tvec_state *tv, struct groupstate *g)
971 const struct tvec_test *t = tv->test;
972 const struct tvec_env *env;
975 if (tv->f&TVSF_OPEN) check(tv, g);
976 if (!(tv->f&TVSF_SKIP)) report_group(tv);
977 env = t->env; if (env && env->teardown) env->teardown(tv, g->ctx);
978 tvec_releaseregs(tv); tv->test = 0; x_free(tv->a, g->ctx); g->ctx = 0;
981 /* --- @core_findvar@, @core_setvar@ --- *
983 * Arguments: @struct tvec_state *tv@ = test vector state
984 * @const char *var@ = variable name to set
985 * @const union tvec_regval *rv@ = register value
986 * @void **ctx_out@ = where to put the @setvar@ context
987 * @void *ctx@ = context pointer
989 * Returns: @core_findvar@ returns a pointer to the variable definition,
990 * or null; @core_setvar@ returns zero on success or %$-1$% on
993 * Use: Find a definition for a special variable. The following
994 * special variables are supported.
996 * * %|@outcome|% is a token describing how a successful
997 * outcome of the test should be interpreted: %|success|% or
998 * %|win|% are the default: a successful test is counted as
999 * a pass; or %|expected-failure|% or %|xfail|% means a
1000 * successful test is counted as an expected failure. A
1001 * mismatch is always considered a failure.
1004 enum { WIN, XFAIL, NOUT };
1006 static int core_setvar(struct tvec_state *tv, const char *name,
1007 const union tvec_regval *rv, void *ctx)
1009 struct groupstate *g = ctx;
1011 if (STRCMP(name, ==, "@outcome")) {
1012 if (g->f&GRPF_SETOUTC) return (tvec_dupregerr(tv, name));
1013 if (rv->u == XFAIL) tvec_xfail(tv);
1014 g->f |= GRPF_SETOUTC;
1015 } else assert(!"unknown var");
1019 static const struct tvec_uassoc outcome_assoc[] = {
1022 { "expected-failure", XFAIL },
1026 static const struct tvec_urange outcome_range = { 0, NOUT - 1 };
1027 static const struct tvec_uenuminfo outcome_enum =
1028 { "test-outcome", outcome_assoc, &outcome_range };
1029 static const struct tvec_vardef outcome_vardef =
1030 { sizeof(struct tvec_reg), core_setvar,
1031 { "@outcome", &tvty_uenum, -1, 0, { &outcome_enum } } };
1033 static const struct tvec_vardef *core_findvar
1034 (struct tvec_state *tv, const char *name, void **ctx_out, void *ctx)
1036 if (STRCMP(name, ==, "@outcome"))
1037 { *ctx_out = ctx; return (&outcome_vardef); }
1042 /* --- @tvec_read@ --- *
1044 * Arguments: @struct tvec_state *tv@ = test-vector state
1045 * @const char *infile@ = the name of the input file
1046 * @FILE *fp@ = stream to read from
1048 * Returns: Zero on success, @-1@ on error.
1050 * Use: Read test vector data from @fp@ and exercise test functions.
1051 * THe return code doesn't indicate test failures: it's only
1052 * concerned with whether there were problems with the input
1053 * file or with actually running the tests.
1056 int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
1059 const struct tvec_test *const *tt;
1060 const struct tvec_env *env;
1061 const struct tvec_regdef *rd;
1062 const struct tvec_vardef *vd = 0; void *varctx;
1063 struct tvec_reg *r = 0, rbuf, *r_alloc = 0; size_t rsz = 0;
1064 struct groupstate g = GROUPSTATE_INIT;
1067 /* Set the initial location. */
1068 tv->infile = infile; tv->lno = 1; tv->fp = fp;
1072 /* Get the next character and dispatch. Note that we're always at the
1073 * start of a line here.
1079 /* End of the file. Exit the loop. */
1084 /* A test group header. */
1086 /* End the current group, if there is one. */
1087 end_test_group(tv, &g);
1089 /* Read the group name. There may be leading and trailing
1092 DRESET(&d); tvec_readword(tv, &d, 0, "];", "group name");
1094 ch = getc(tv->fp); if (ch != ']') tvec_syntax(tv, ch, "`]'");
1096 /* Find the matching test definition. */
1097 for (tt = tv->cfg.tests; *tt; tt++)
1098 if (STRCMP(d.buf, ==, (*tt)->name)) goto found_test;
1100 /* There wasn't one. Report the error. Muffle errors about the
1101 * contents of this section because they won't be interesting.
1103 tvec_error(tv, "unknown test group `%s'", d.buf);
1104 tv->f |= TVSF_MUFFLE; goto flush_line;
1107 /* Eat trailing whitespace and comments. */
1108 tvec_flushtoeol(tv, 0);
1110 /* Set up the new test group. */
1111 tv->test = *tt; begin_test_group(tv, &g);
1115 /* A newline, so this was a completely empty line. Advance the line
1116 * counter, and run the current test.
1120 if (tv->f&TVSF_OPEN) check(tv, &g);
1124 /* A semicolon. Skip the comment. */
1126 tvec_flushtoeol(tv, TVFF_ALLOWANY);
1130 /* Something else. */
1133 /* Whitespace. Skip and see what we find. */
1135 tvec_skipspc(tv); ch = getc(tv->fp);
1137 /* If the file ends, then we're done. If we find a comment then we
1138 * skip it. If there's some non-whitespace, then report an error.
1139 * Otherwise the line was effectively blank, so run the test.
1141 if (ch == EOF) goto end;
1142 else if (ch == ';') tvec_flushtoeol(tv, TVFF_ALLOWANY);
1143 else if (tvec_flushtoeol(tv, 0)) rc = -1;
1146 /* Some non-whitespace thing. */
1148 /* If there's no test, then report an error. Set the muffle flag,
1149 * because there's no point in complaining about every assignment
1153 if (!(tv->f&TVSF_MUFFLE)) tvec_error(tv, "no current test");
1154 tv->f |= TVSF_MUFFLE; goto flush_line;
1157 /* Put the character back and read a word, which ought to be a
1162 if (tvec_readword(tv, &d, 0, "=:*;", "register name"))
1165 /* Open the test. This is syntactically a paragraph of settings,
1166 * so it's fair to report on missing register assignments.
1170 /* See what sort of thing we have found. */
1171 if (d.buf[0] == '@') {
1172 /* A special register assignment. */
1174 env = tv->test->env;
1176 /* Find a variable definition. */
1177 vd = core_findvar(tv, d.buf, &varctx, &g);
1178 if (vd) goto found_var;
1179 if (env && env->findvar) {
1180 vd = env->findvar(tv, d.buf, &varctx, g.ctx);
1181 if (vd) goto found_var;
1183 tvec_unkregerr(tv, d.buf); goto flush_line;
1187 /* A standard register. */
1189 /* Find the definition. */
1190 for (rd = tv->test->regs; rd->name; rd++)
1191 if (STRCMP(rd->name, ==, d.buf)) goto found_reg;
1192 tvec_error(tv, "unknown register `%s' for test `%s'",
1193 d.buf, tv->test->name);
1197 /* Complain if the register is already set. */
1198 r = TVEC_REG(tv, in, rd->i);
1200 { tvec_dupregerr(tv, rd->name); goto flush_line; }
1203 /* Now there should be a separator. */
1204 tvec_skipspc(tv); ch = getc(tv->fp);
1207 /* Register explicitly marked unset. */
1210 tvec_error(tv, "can't unset special variables");
1213 if (!(rd->f&(TVRF_OPT | TVRF_UNSET))) {
1214 tvec_error(tv, "register `%s' must be assigned "
1215 "a value in test `%s'", rd->name, tv->test->name);
1219 if (tvec_flushtoeol(tv, 0)) goto bad;
1221 /* Common case of a proper assignment. */
1223 /* We must have a separator. */
1224 if (ch != '=' && ch != ':')
1225 { tvec_syntax(tv, ch, "`=', `:', or `*'"); goto flush_line; }
1229 /* An ordinary register. Parse a value and mark the register
1233 if (rd->ty->parse(&r->v, rd, tv)) goto flush_line;
1234 r->f |= TVRF_LIVE | TVRF_SEEN;
1236 /* A special register defined by an environment. */
1238 /* Set up the register. */
1239 if (vd->regsz <= sizeof(rbuf))
1242 GROWBUF_REPLACE(size_t, tv->a, r_alloc, rsz, vd->regsz,
1243 8*sizeof(void *), 1);
1247 /* Read and set the value. */
1248 rd->ty->init(&r->v, rd);
1249 if (rd->ty->parse(&r->v, rd, tv)) goto flush_line;
1250 if (!(tv->f&TVSF_SKIP) && vd->setvar(tv, d.buf, &r->v, varctx))
1254 rd->ty->release(&r->v, &vd->def); vd = 0;
1263 /* This is a general parse-failure handler. Skip to the next line and
1264 * remember that things didn't go so well.
1266 tvec_flushtoeol(tv, TVFF_ALLOWANY);
1268 if (vd) { vd->def.ty->release(&r->v, &vd->def); vd = 0; }
1272 /* We reached the end. If that was actually an I/O error then report it.
1275 { tvec_error(tv, "error reading input: %s", strerror(errno)); rc = -1; }
1278 /* Process the final test, if there was one, and wrap up the final
1281 end_test_group(tv, &g);
1284 tv->infile = 0; tv->fp = 0;
1286 x_free(tv->a, r_alloc);
1292 /*----- Session lifecycle -------------------------------------------------*/
1294 /* --- @tvec_begin@ --- *
1296 * Arguments: @struct tvec_state *tv_out@ = state structure to fill in
1297 * @const struct tvec_config *config@ = test configuration
1298 * @struct tvec_output *o@ = output driver
1302 * Use: Initialize a state structure ready to do some testing.
1305 void tvec_begin(struct tvec_state *tv_out,
1306 const struct tvec_config *config,
1307 struct tvec_output *o)
1313 assert(config->nrout <= config->nreg);
1314 tv_out->cfg = *config; tv_out->a = arena_global;
1315 tv_out->in = x_allocv(tv_out->a, tv_out->cfg.nreg, tv_out->cfg.regsz);
1316 tv_out->out = x_allocv(tv_out->a, tv_out->cfg.nrout, tv_out->cfg.regsz);
1317 for (i = 0; i < tv_out->cfg.nreg; i++) {
1318 TVEC_REG(tv_out, in, i)->f = 0;
1319 if (i < tv_out->cfg.nrout) TVEC_REG(tv_out, out, i)->f = 0;
1322 for (i = 0; i < TVOUT_LIMIT; i++)
1323 tv_out->curr[i] = tv_out->all[i] = tv_out->grps[i] = 0;
1326 tv_out->infile = 0; tv_out->lno = 0; tv_out->fp = 0;
1327 tv_out->output = o; tv_out->output->ops->bsession(tv_out->output, tv_out);
1330 /* --- @tvec_end@ --- *
1332 * Arguments: @struct tvec_state *tv@ = test-vector state
1334 * Returns: A proposed exit code.
1336 * Use: Conclude testing and suggests an exit code to be returned to
1337 * the calling program. (The exit code comes from the output
1338 * driver's @esession@ method.)
1341 int tvec_end(struct tvec_state *tv)
1343 int rc = tv->output->ops->esession(tv->output);
1345 if (tv->test) tvec_releaseregs(tv);
1346 tv->output->ops->destroy(tv->output);
1347 x_free(tv->a, tv->in); x_free(tv->a, tv->out);
1351 /*----- Serialization and deserialization ---------------------------------*/
1353 #define FOREACH_CANDIDATE(rd, regs, mask, want, nr) \
1354 for (rd = (regs); rd->name; rd++) \
1355 if (!(rd->i < (nr) && (rd->f&(mask)) == (want))) /* skip */; \
1358 /* --- @tvec_serialize@ --- *
1360 * Arguments: @const struct tvec_reg *rv@ = vector of registers
1361 * @buf *b@ = buffer to write on
1362 * @const struct tvec_regdef *regs@ = vector of register
1363 * descriptions, terminated by an entry with a null
1365 * @unsigned mask, want@ = flag-based selection
1366 * @unsigned nr@ = number of entries in the @rv@ vector
1367 * @size_t regsz@ = true size of each element of @rv@
1369 * Returns: Zero on success, @-1@ on failure.
1371 * Use: Serialize a collection of register values.
1373 * The serialized output is written to the buffer @b@. Failure
1374 * can be caused by running out of buffer space, or a failing
1378 int tvec_serialize(const struct tvec_reg *rv, buf *b,
1379 const struct tvec_regdef *regs,
1380 unsigned mask, unsigned want,
1381 unsigned nr, size_t regsz)
1383 unsigned char *bitmap;
1384 size_t i, bitoff, nbits, bitsz;
1385 const struct tvec_regdef *rd;
1386 const struct tvec_reg *r;
1388 nbits = 0; FOREACH_CANDIDATE(rd, regs, mask, want, nr) nbits++;
1389 if (!nbits) return (0);
1390 bitoff = BLEN(b); bitsz = (nbits + 7)/8;
1392 bitmap = buf_get(b, bitsz); if (!bitmap) return (-1);
1393 memset(bitmap, 0, bitsz);
1394 i = 0; FOREACH_CANDIDATE(rd, regs, mask, want, nr) {
1395 r = TVEC_GREG(rv, rd->i, regsz);
1396 if (r->f&TVRF_LIVE) {
1397 bitmap = BBASE(b) + bitoff; bitmap[i/8] |= 1 << i%8;
1398 if (rd->ty->tobuf(b, &r->v, rd)) return (-1);
1405 /* --- @tvec_deserialize@ --- *
1407 * Arguments: @struct tvec_reg *rv@ = vector of registers
1408 * @buf *b@ = buffer to write on
1409 * @const struct tvec_regdef *regs@ = vector of register
1410 * descriptions, terminated by an entry with a null
1412 * @unsigned mask, want@ = flag-based selection
1413 * @unsigned nr@ = number of entries in the @rv@ vector
1414 * @size_t regsz@ = true size of each element of @rv@
1416 * Returns: Zero on success, @-1@ on failure.
1418 * Use: Deserialize a collection of register values.
1420 * The size of the register vector @nr@ and the register
1421 * definitions @regs@ must match those used when producing the
1422 * serialization. For each serialized register value,
1423 * deserialize and store the value into the appropriate register
1424 * slot, and set the @TVRF_LIVE@ flag on the register. See
1425 * @tvec_serialize@ for a description of the format.
1427 * Failure results only from an input too small for the initial
1428 * bitmap or a failing register type handler.
1431 int tvec_deserialize(struct tvec_reg *rv, buf *b,
1432 const struct tvec_regdef *regs,
1433 unsigned mask, unsigned want,
1434 unsigned nr, size_t regsz)
1436 const unsigned char *bitmap;
1437 size_t i, nbits, bitsz;
1438 const struct tvec_regdef *rd;
1441 nbits = 0; FOREACH_CANDIDATE(rd, regs, mask, want, nr) nbits++;
1442 if (!nbits) return (0);
1443 bitsz = (nbits + 7)/8;
1445 bitmap = buf_get(b, bitsz); if (!bitmap) return (-1);
1446 i = 0; FOREACH_CANDIDATE(rd, regs, mask, want, nr) {
1447 if (bitmap[i/8]&(1 << i%8)) {
1448 r = TVEC_GREG(rv, rd->i, regsz);
1449 if (rd->ty->frombuf(b, &r->v, rd)) return (-1);
1457 #undef FOREACH_CANDIDATE
1459 /*----- Ad-hoc testing ----------------------------------------------------*/
1461 static const struct tvec_regdef no_regs = { 0, 0, 0, 0, { 0 } };
1463 static void fakefn(const struct tvec_reg *in, struct tvec_reg *out, void *p)
1464 { assert(!"fake test function"); }
1466 /* --- @tvec_adhoc@ --- *
1468 * Arguments: @struct tvec_state *tv@ = test-vector state
1472 * Use: Begin ad-hoc testing, i.e., without reading a file of
1475 * The other functions in this section assume that @tvec_adhoc@
1479 void tvec_adhoc(struct tvec_state *tv)
1481 tv->adhoc_test.name = "<unset>";
1482 tv->adhoc_test.regs = &no_regs;
1483 tv->adhoc_test.env = 0;
1484 tv->adhoc_test.fn = fakefn;
1485 tv->adhoc_tests[0] = &tv->adhoc_test; tv->adhoc_tests[1] = 0;
1486 tv->cfg.tests = tv->adhoc_tests;
1489 /* --- @tvec_begingroup@ --- *
1491 * Arguments: @struct tvec_state *tv@ = test-vector state
1492 * @const char *name@ = name for this test group
1493 * @const char *file@, @unsigned @lno@ = calling file and line
1497 * Use: Begin an ad-hoc test group with the given name. The @file@
1498 * and @lno@ can be anything, but it's usually best if they
1499 * refer to the source code performing the test: the macro
1500 * @TVEC_BEGINGROUP@ does this automatically.
1503 void tvec_begingroup(struct tvec_state *tv, const char *name,
1504 const char *file, unsigned lno)
1506 tv->adhoc_test.name = name; tv->test = &tv->adhoc_test;
1507 tv->infile = file; tv->lno = tv->test_lno = lno;
1508 begin_test_group(tv, 0);
1511 /* --- @tvec_endgroup@ --- *
1513 * Arguments: @struct tvec_state *tv@ = test-vector state
1517 * Use: End an ad-hoc test group. The statistics are updated and the
1518 * outcome is reported to the output formatter.
1521 void tvec_endgroup(struct tvec_state *tv)
1523 if (!(tv->f&TVSF_SKIP)) report_group(tv);
1527 /* --- @tvec_begintest@ --- *
1529 * Arguments: @struct tvec_state *tv@ = test-vector state
1530 * @const char *file@, @unsigned @lno@ = calling file and line
1534 * Use: Begin an ad-hoc test case. The @file@ and @lno@ can be
1535 * anything, but it's usually best if they refer to the source
1536 * code performing the test: the macro @TVEC_BEGINGROUP@ does
1537 * this automatically.
1540 void tvec_begintest(struct tvec_state *tv, const char *file, unsigned lno)
1542 tv->infile = file; tv->lno = tv->test_lno = lno;
1543 open_test(tv); begin_test(tv);
1546 struct adhoc_claim {
1548 #define ACF_FRESH 1u
1549 const char *saved_file; unsigned saved_lno;
1552 static void adhoc_claim_setup(struct tvec_state *tv,
1553 struct adhoc_claim *ck,
1554 const struct tvec_regdef *regs,
1555 const char *file, unsigned lno)
1559 if (!(tv->f&TVSF_OPEN))
1560 { ck->f |= ACF_FRESH; tvec_begintest(tv, file, lno); }
1562 ck->saved_file = tv->infile; if (file) tv->infile = file;
1563 ck->saved_lno = tv->test_lno; if (file) tv->test_lno = lno;
1564 tv->adhoc_test.regs = regs ? regs : &no_regs;
1567 static void adhoc_claim_teardown(struct tvec_state *tv,
1568 struct adhoc_claim *ck)
1570 tv->adhoc_test.regs = &no_regs;
1571 tv->infile = ck->saved_file; tv->test_lno = ck->saved_lno;
1573 if (ck->f&ACF_FRESH) tvec_endtest(tv);
1576 /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- *
1578 * Arguments: @struct tvec_state *tv@ = test-vector state
1580 * @const char *file@, @unsigned @lno@ = calling file and line
1581 * @const char *msg@, @va_list *ap@ = message to report on
1584 * Returns: The value @ok@.
1586 * Use: Check that a claimed condition holds, as (part of) a test.
1587 * If no test case is underway (i.e., if @TVSF_OPEN@ is reset in
1588 * @tv->f@), then a new test case is begun and ended. The
1589 * @file@ and @lno@ are passed to the output formatter to be
1590 * reported in case of a failure. If @ok@ is nonzero, then
1591 * nothing else happens; so, in particular, if @tvec_claim@
1592 * established a new test case, then the test case succeeds. If
1593 * @ok@ is zero, then a failure is reported, quoting @msg@.
1595 * The @TVEC_CLAIM@ macro is similar, only it (a) identifies the
1596 * file and line number of the call site automatically, and (b)
1597 * implicitly quotes the source text of the @ok@ condition in
1598 * the failure message.
1601 int tvec_claim_v(struct tvec_state *tv, int ok,
1602 const char *file, unsigned lno,
1603 const char *msg, va_list *ap)
1605 struct adhoc_claim ck;
1607 adhoc_claim_setup(tv, &ck, 0, file, lno);
1608 if (!ok) tvec_fail_v(tv, msg, ap);
1609 adhoc_claim_teardown(tv, &ck);
1613 int tvec_claim(struct tvec_state *tv, int ok,
1614 const char *file, unsigned lno, const char *msg, ...)
1618 va_start(ap, msg); tvec_claim_v(tv, ok, file, lno, msg, &ap); va_end(ap);
1622 int tvec_claimeq(struct tvec_state *tv,
1623 const struct tvec_regty *ty, const union tvec_misc *arg,
1624 const char *file, unsigned lno, const char *expr)
1626 struct tvec_regdef regs[2];
1627 struct adhoc_claim ck;
1630 tv->in[0].f = tv->out[0].f = TVRF_LIVE;
1632 regs[0].name = "value"; regs[0].i = 0;
1633 regs[0].ty = ty; regs[0].f = 0;
1634 if (arg) regs[0].arg = *arg;
1635 else regs[0].arg.p = 0;
1639 adhoc_claim_setup(tv, &ck, regs, file, lno);
1640 ok = ty->eq(&tv->in[0].v, &tv->out[0].v, ®s[0]);
1642 { tvec_fail(tv, "%s", expr); tvec_mismatch(tv, TVMF_IN | TVMF_OUT); }
1643 adhoc_claim_teardown(tv, &ck);
1647 /*----- That's all, folks -------------------------------------------------*/