X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/c81c35dfd10050ffef85d57dc2ad73f52f38a3f2..5c0f2e080603967952db43eb7b12d44dd64f7169:/test/tvec-output.c diff --git a/test/tvec-output.c b/test/tvec-output.c index 8c9c214..46c3442 100644 --- a/test/tvec-output.c +++ b/test/tvec-output.c @@ -61,8 +61,8 @@ static const char *regdisp(unsigned disp) case TVRD_INPUT: return "input"; case TVRD_OUTPUT: return "output"; case TVRD_MATCH: return "matched"; - case TVRD_EXPECT: return "expected"; case TVRD_FOUND: return "found"; + case TVRD_EXPECT: return "expected"; default: abort(); } } @@ -673,7 +673,8 @@ static int human_nwritef(void *go, size_t maxsz, const char *p, ...) va_start(ap, p); n = gprintf_memputf(&h->outbuf, &h->outsz, maxsz, p, ap); va_end(ap); - return (layout_string(&h->lyt, h->outbuf, n)); + if (layout_string(&h->lyt, h->outbuf, n)) return (-1); + return (n); } static const struct gprintf_ops human_printops = @@ -890,7 +891,7 @@ static void human_egroup(struct tvec_output *o) static void human_btest(struct tvec_output *o) { struct human_output *h = (struct human_output *)o; show_progress(h); } -/* --- @report_location@ --- * +/* --- @human_report_location@ --- * * * Arguments: @struct human_output *h@ = output state * @FILE *fp@ = stream to write the location on @@ -905,8 +906,8 @@ static void human_btest(struct tvec_output *o) * nothing. */ -static void report_location(struct human_output *h, FILE *fp, - const char *file, unsigned lno) +static void human_report_location(struct human_output *h, FILE *fp, + const char *file, unsigned lno) { unsigned f = 0; #define f_flush 1u @@ -969,7 +970,7 @@ static void human_outcome(struct tvec_output *o, struct tvec_state *tv = h->tv; clear_progress(h); - report_location(h, h->lyt.fp, tv->infile, tv->test_lno); + human_report_location(h, h->lyt.fp, tv->infile, tv->test_lno); fprintf(h->lyt.fp, "`%s' ", tv->test->name); setattr(h, attr); fputs(outcome, h->lyt.fp); setattr(h, HA_PLAIN); if (detail) { fputs(": ", h->lyt.fp); vfprintf(h->lyt.fp, detail, *ap); } @@ -1075,7 +1076,8 @@ static void human_bbench(struct tvec_output *o, struct tvec_state *tv = h->tv; clear_progress(h); - fprintf(h->lyt.fp, "%s: %s: ", tv->test->name, ident); fflush(h->lyt.fp); + gprintf(&human_printops, h, "%s %s: ", tv->test->name, ident); + if (h->f&HOF_TTY) fflush(h->lyt.fp); } /* --- @human_ebench@ --- * @@ -1100,8 +1102,8 @@ static void human_ebench(struct tvec_output *o, { struct human_output *h = (struct human_output *)o; - tvec_benchreport(&human_printops, h, unit, tm); - fputc('\n', h->lyt.fp); + tvec_benchreport(&human_printops, h, unit, 0, tm); + layout_char(&h->lyt, '\n'); } /* --- @human_report@ --- * @@ -1148,14 +1150,14 @@ static void human_report(struct tvec_output *o, unsigned level, if (h->f^HOF_PROGRESS) { clear_progress(h); fflush(h->lyt.fp); f |= f_progress; } fprintf(stderr, "%s: ", QUIS); - report_location(h, stderr, tv->infile, tv->lno); + human_report_location(h, stderr, tv->infile, tv->lno); setattr(h, levattr); FLUSH; fputs(levstr, stderr); setattr(h, 0); FLUSH; fputs(": ", stderr); fwrite(d.buf, 1, d.len, stderr); #undef FLUSH if (h->f&HOF_DUPERR) { - report_location(h, h->lyt.fp, tv->infile, tv->lno); + human_report_location(h, h->lyt.fp, tv->infile, tv->lno); fprintf(h->lyt.fp, "%s: ", levstr); fwrite(d.buf, 1, d.len, h->lyt.fp); } @@ -1242,6 +1244,529 @@ struct tvec_output *tvec_humanoutput(FILE *fp) return (&h->_o); } +/*----- Machine-readable output -------------------------------------------*/ + +struct machine_output { + struct tvec_output _o; /* output base class */ + struct tvec_state *tv; /* stashed testing state */ + FILE *fp; /* output stream */ + char *outbuf; size_t outsz; /* buffer for formatted output */ + unsigned grpix, testix; /* group and test indices */ + unsigned f; /* flags */ +#define MF_BENCH 1u +}; + +/* --- @machine_writech@, @machine_write@, @machine_writef@ --- * + * + * Arguments: @void *go@ = output sink, secretly a @struct machine_output@ + * @int ch@ = character to write + * @const char *@p@, @size_t sz@ = string (with explicit length) + * to write + * @const char *p, ...@ = format control string and arguments to + * write + * + * Returns: --- + * + * Use: Write characters, strings, or formatted strings to the + * output, applying appropriate quoting. + */ + +static void machine_escape(struct machine_output *m, int ch) +{ + switch (ch) { + case 0: fputs("\\0", m->fp); break; + case '\a': fputs("\\a", m->fp); break; + case '\b': fputs("\\b", m->fp); break; + case '\x1b': fputs("\\e", m->fp); break; + case '\f': fputs("\\f", m->fp); break; + case '\n': fputs("\\n", m->fp); break; + case '\r': fputs("\\r", m->fp); break; + case '\t': fputs("\\t", m->fp); break; + case '\v': fputs("\\v", m->fp); break; + case '"': fputs("\\\"", m->fp); break; + case '\\': fputs("\\\\", m->fp); break; + default: fprintf(m->fp, "\\x{%02x}", ch); + } +} + +static int machine_writech(void *go, int ch) +{ + struct machine_output *m = go; + + if (ISPRINT(ch)) putc(ch, m->fp); + else machine_escape(m, ch); + return (1); +} + +static int machine_writem(void *go, const char *p, size_t sz) +{ + struct machine_output *m = go; + const char *q, *l = p + sz; + + for (;;) { + q = p; + for (;;) { + if (q >= l) goto final; + if (!ISPRINT(*q) || *q == '\\' || *q == '"') break; + q++; + } + if (p < q) fwrite(p, 1, q - p, m->fp); + p = q; machine_escape(m, (unsigned char)*p++); + } +final: + if (p < l) fwrite(p, 1, l - p, m->fp); + return (sz); +} + +static int machine_nwritef(void *go, size_t maxsz, const char *p, ...) +{ + struct machine_output *m = go; + int n; + va_list ap; + + va_start(ap, p); + n = gprintf_memputf(&m->outbuf, &m->outsz, maxsz, p, ap); + va_end(ap); + return (machine_writem(m, m->outbuf, n)); +} + +static const struct gprintf_ops machine_printops = + { machine_writech, machine_writem, machine_nwritef }; + +/* --- @machine_maybe_quote@ --- * + * + * Arguments: @struct machine_output *m@ = output sink + * @const char *p@ = pointer to string + * + * Returns: --- + * + * Use: Print the string @p@, quoting it if necessary. + */ + +static void machine_maybe_quote(struct machine_output *m, const char *p) +{ + const char *q; + + for (q = p; *q; q++) + if (!ISPRINT(*q)) goto quote; + fputs(p, m->fp); return; +quote: + putc('"', m->fp); machine_writem(m, p, strlen(p)); putc('"', m->fp); +} + +/* --- @machine_bsession@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @struct tvec_state *tv@ = the test state producing output + * + * Returns: --- + * + * Use: Begin a test session. + * + * The TAP driver records the test state for later reference, + * initializes the group index counter, and prints the version + * number. + */ + +static void machine_bsession(struct tvec_output *o, struct tvec_state *tv) +{ + struct machine_output *m = (struct machine_output *)o; + + m->tv = tv; m->grpix = 0; +} + +/* --- @machine_show_stats@ --- * + * + * Arguments: @struct machine_output *m@ = output sink + * @unsigned *out[TVOUT_LIMIT]@ = outcome counter table + * + * Returns: --- + * + * Use: Print a machine readable outcome statistics table + */ + +static void machine_show_stats(struct machine_output *m, + unsigned out[TVOUT_LIMIT]) +{ + static const char *outtab[] = { "lose", "skip", "xfail", "win" }; + unsigned i; + + for (i = 0; i < TVOUT_LIMIT; i++) { + if (i) putc(' ', m->fp); + fprintf(m->fp, "%s=%u", outtab[i], out[i]); + } +} + +/* --- @machine_esession@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * + * Returns: Suggested exit code. + * + * Use: End a test session. + * + * The TAP driver prints a final summary of the rest results + * and returns a suitable exit code. If errors occurred, it + * instead prints a `Bail out!' line forcing the reader to + * report a failure. + */ + +static int machine_esession(struct tvec_output *o) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fputs("END groups: ", m->fp); + machine_show_stats(m, tv->grps); + fputs("; tests: ", m->fp); + machine_show_stats(m, tv->all); + putc('\n', m->fp); + m->tv = 0; return (tv->f&TVSF_ERROR ? 2 : tv->all[TVOUT_LOSE] ? 1 : 0); +} + +/* --- @machine_bgroup@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * + * Returns: --- + * + * Use: Begin a test group. + * + * The TAP driver determines the length of the longest + * register name, resets the group progress scoreboard, and + * activates the progress display. + */ + +static void machine_bgroup(struct tvec_output *o) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fputs("BGROUP ", m->fp); + machine_maybe_quote(m, tv->test->name); + putc('\n', m->fp); + m->grpix++; m->testix = 0; +} + +/* --- @machine_skipgroup@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @const char *excuse@, @va_list *ap@ = reason for skipping the + * group, or null + * + * Returns: --- + * + * Use: Report that a test group is being skipped. + * + * The TAP driver just reports the situation to its output + * stream. + */ + +static void machine_skipgroup(struct tvec_output *o, + const char *excuse, va_list *ap) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fputs("BGROUP ", m->fp); + machine_maybe_quote(m, tv->test->name); + if (excuse) { + fputs(" \"", m->fp); + vgprintf(&machine_printops, m, excuse, ap); + putc('\"', m->fp); + } + putc('\n', m->fp); +} + +/* --- @machine_egroup@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * + * Returns: --- + * + * Use: Report that a test group has finished. + * + * The TAP driver reports a summary of the group's tests. + */ + +static void machine_egroup(struct tvec_output *o) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fputs("EGROUP ", m->fp); machine_maybe_quote(m, tv->test->name); + putc(' ', m->fp); machine_show_stats(m, tv->curr); + putc('\n', m->fp); +} + +/* --- @machine_btest@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * + * Returns: --- + * + * Use: Report that a test is starting. + * + * The TAP driver advances its test counter. (We could do this + * by adding up up the counters in @tv->curr@, and add on the + * current test, but it's easier this way.) + */ + +static void machine_btest(struct tvec_output *o) { ; } + +/* --- @machine_report_location@ --- * + * + * Arguments: @struct human_output *h@ = output state + * @const char *file@ = filename + * @unsigned lno@ = line number + * + * Returns: --- + * + * Use: Print the filename and line number to the output stream. + */ + +static void machine_report_location(struct machine_output *m, + const char *file, unsigned lno) + { machine_maybe_quote(m, file); fprintf(m->fp, ":%u", lno); } + +/* --- @machine_outcome@, @machine_skip@, @machine_fail@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @const char *outcome@ = outcome string to report + * @const char *detail@, @va_list *ap@ = a detail message + * @const char *excuse@, @va_list *ap@ = reason for skipping the + * test + * + * Returns: --- + * + * Use: Report that a test has been skipped or failed. + */ + +static void machine_outcome(struct tvec_output *o, const char *outcome, + const char *detail, va_list *ap) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fprintf(m->fp, "%s %u ", outcome, m->testix); + machine_report_location(m, tv->infile, tv->test_lno); + if (detail) + { putc(' ', m->fp); vgprintf(&machine_printops, m, detail, ap); } + putc('\n', m->fp); +} + +static void machine_skip(struct tvec_output *o, + const char *excuse, va_list *ap) + { machine_outcome(o, "SKIP", excuse, ap); } +static void machine_fail(struct tvec_output *o, + const char *detail, va_list *ap) + { machine_outcome(o, "FAIL", detail, ap); } + +/* --- @machine_dumpreg@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @unsigned disp@ = register disposition + * @const union tvec_regval *rv@ = register value + * @const struct tvec_regdef *rd@ = register definition + * + * Returns: --- + * + * Use: Dump a register. + * + * The TAP driver applies highlighting to mismatching output + * registers, but otherwise delegates to the register type + * handler and the layout machinery. The result is that the + * register dump is marked as a comment and indented. + */ + +static void machine_dumpreg(struct tvec_output *o, + unsigned disp, const union tvec_regval *rv, + const struct tvec_regdef *rd) +{ + struct machine_output *m = (struct machine_output *)o; + unsigned f = 0; +#define f_reg 1u +#define f_nl 2u + + switch (disp) { + case TVRD_INPUT: fputs("\tINPUT ", m->fp); f |= f_reg | f_nl; break; + case TVRD_OUTPUT: fputs("\tOUTPUT ", m->fp); f |= f_reg | f_nl; break; + case TVRD_MATCH: fputs("\tMATCH ", m->fp); f |= f_reg | f_nl; break; + case TVRD_FOUND: fputs("\tMISMATCH ", m->fp); f |= f_reg; break; + case TVRD_EXPECT: fputs(" /= ", m->fp); f |= f_nl; break; + default: abort(); + } + + if (f&f_reg) fprintf(m->fp, "%s = ", rd->name); + rd->ty->dump(rv, rd, TVSF_RAW, &file_printops, m->fp); + if (f&f_nl) putc('\n', m->fp); + +#undef f_reg +#undef f_newline +} + +/* --- @machine_etest@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @unsigned outcome@ = the test outcome + * + * Returns: --- + * + * Use: Report that a test has finished. + * + * The TAP driver reports the outcome of the test, if that's not + * already decided. + */ + +static void machine_etest(struct tvec_output *o, unsigned outcome) +{ + struct machine_output *m = (struct machine_output *)o; + + if (!(m->f&MF_BENCH)) switch (outcome) { + case TVOUT_WIN: machine_outcome(o, "WIN", 0, 0); break; + case TVOUT_XFAIL: machine_outcome(o, "XFAIL", 0, 0); break; + } + m->testix++; m->f &= ~MF_BENCH; +} + +/* --- @machine_bbench@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @const char *ident@ = identifying register values + * @unsigned unit@ = measurement unit (@TVBU_...@) + * + * Returns: --- + * + * Use: Report that a benchmark has started. + * + * The TAP driver does nothing here. All of the reporting + * happens in @machine_ebench@. + */ + +static void machine_bbench(struct tvec_output *o, + const char *ident, unsigned unit) + { ; } + +/* --- @machine_ebench@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @const char *ident@ = identifying register values + * @unsigned unit@ = measurement unit (@TVBU_...@) + * @const struct bench_timing *tm@ = measurement + * + * Returns: --- + * + * Use: Report a benchmark's results + * + * The TAP driver just delegates to the default benchmark + * reporting, via the layout machinery so that the result is + * printed as a comment. + */ + +static void machine_ebench(struct tvec_output *o, + const char *ident, unsigned unit, + const struct bench_timing *tm) +{ + struct machine_output *m = (struct machine_output *)o; + struct tvec_state *tv = m->tv; + + fprintf(m->fp, "BENCH %u ", m->testix); + machine_report_location(m, tv->infile, tv->test_lno); + fprintf(m->fp, " %s: ", ident); + tvec_benchreport(&file_printops, m->fp, unit, TVSF_RAW, tm); + putc('\n', m->fp); m->f |= MF_BENCH; +} + +/* --- @machine_report@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * @unsigned level@ = message level (@TVLEV_...@) + * @const char *msg@, @va_list *ap@ = format string and + * arguments + * + * Returns: --- + * + * Use: Report a message to the user. + * + * Messages are reported as comments, so that they can be + * accumulated by the reader. An error will cause a later + * bailout or, if we crash before then, a missing plan line, + * either of which will cause the reader to report a serious + * problem. + */ + +static void machine_report(struct tvec_output *o, unsigned level, + const char *msg, va_list *ap) +{ + struct machine_output *m = (struct machine_output *)o; + const char *p; + + for (p = tvec_strlevel(level); *p; p++) putc(TOUPPER(*p), m->fp); + putc(' ', m->fp); + putc('"', m->fp); + vgprintf(&machine_printops, m, msg, ap); + putc('"', m->fp); + putc('\n', m->fp); +} + +/* --- @machine_destroy@ --- * + * + * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct + * machine_output@ + * + * Returns: --- + * + * Use: Release the resources held by the output driver. + */ + +static void machine_destroy(struct tvec_output *o) +{ + struct machine_output *m = (struct machine_output *)o; + + if (m->fp != stdout && m->fp != stderr) fclose(m->fp); + xfree(m->outbuf); xfree(m); +} + +static const struct tvec_outops machine_ops = { + machine_bsession, machine_esession, + machine_bgroup, machine_skipgroup, machine_egroup, + machine_btest, machine_skip, machine_fail, machine_dumpreg, machine_etest, + machine_bbench, machine_ebench, + machine_report, + machine_destroy +}; + +/* --- @tvec_machineoutput@ --- * + * + * Arguments: @FILE *fp@ = output file to write on + * + * Returns: An output formatter. + * + * Use: Return an output formatter which writes on @fp@ in a + * moderately simple machine-readable format. + */ + +struct tvec_output *tvec_machineoutput(FILE *fp) +{ + struct machine_output *m; + + m = xmalloc(sizeof(*m)); m->_o.ops = &machine_ops; + m->fp = fp; m->outbuf = 0; m->outsz = 0; m->testix = 0; + return (&m->_o); +} + /*----- Perl's `Test Anything Protocol' -----------------------------------*/ struct tap_output { @@ -1273,10 +1798,20 @@ struct tap_output { */ static int tap_writech(void *go, int ch) - { struct tap_output *t = go; return (layout_char(&t->lyt, ch)); } +{ + struct tap_output *t = go; + + if (layout_char(&t->lyt, ch)) return (-1); + else return (1); +} static int tap_writem(void *go, const char *p, size_t sz) - { struct tap_output *t = go; return (layout_string(&t->lyt, p, sz)); } +{ + struct tap_output *t = go; + + if (layout_string(&t->lyt, p, sz)) return (-1); + else return (sz); +} static int tap_nwritef(void *go, size_t maxsz, const char *p, ...) { @@ -1287,7 +1822,8 @@ static int tap_nwritef(void *go, size_t maxsz, const char *p, ...) va_start(ap, p); n = gprintf_memputf(&t->outbuf, &t->outsz, maxsz, p, ap); va_end(ap); - return (layout_string(&t->lyt, t->outbuf, n)); + if (layout_string(&t->lyt, t->outbuf, n)) return (-1); + return (n); } static const struct gprintf_ops tap_printops = @@ -1441,8 +1977,7 @@ static void tap_btest(struct tvec_output *o) * * Arguments: @struct tvec_output *o@ = output sink, secretly a @struct * tap_output@ - * @unsigned attr@ = attribute to apply to the outcome - * @const char *outcome@ = outcome string to report + * @const char *head, *tail@ = outcome strings to report * @const char *detail@, @va_list *ap@ = a detail message * @const char *excuse@, @va_list *ap@ = reason for skipping the * test @@ -1583,8 +2118,8 @@ static void tap_ebench(struct tvec_output *o, struct tvec_state *tv = t->tv; set_layout_prefix(&t->lyt, " ## "); - gprintf(&tap_printops, t, "%s: %s: ", tv->test->name, ident); - tvec_benchreport(&tap_printops, t, unit, tm); + gprintf(&tap_printops, t, "%s %s: ", tv->test->name, ident); + tvec_benchreport(&tap_printops, t, unit, 0, tm); layout_char(&t->lyt, '\n'); }