chiark / gitweb /
293dccc0a90e70eb4a308033e40d9290c312f262
[mLib] / test / tvec.h
1 /* -*-c-*-
2  *
3  * Test vector processing framework
4  *
5  * (c) 2023 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * USA.
26  */
27
28 #ifndef MLIB_TVEC_H
29 #define MLIB_TVEC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /* Here's the overall flow for a testing session.
36  *
37  * @tvec_begin@
38  *                      -> output @bsession@
39  * @tvec_read@
40  *                      -> output @bgroup@
41  *                      -> env @setup@
42  *   one or more tests
43  *                      -> type @init@ (input and output)
44  *                      -> type @parse@ (input)
45  *                      -> output @btest@
46  *                      -> env @before@
47  *                              -> @tvec_skipgroup@
48  *                                      -> output @skipgroup@
49  *                      -> env @run@
50  *                              -> @tvec_skip@
51  *                                      -> output @skip@
52  *                              -> test @fn@
53  *                              -> @tvec_checkregs@
54  *                                      -> type @eq@
55  *                              -> @tvec_fail@
56  *                                      -> output @fail@
57  *                              -> @tvec_mismatch@
58  *                                      -> output @dumpreg@
59  *                                              -> type @dump@
60  *                      -> output @etest@
61  *                      -> env @after@
62  *   finally
63  *                      -> output @egroup@
64  *                      -> env @teardown@
65  *
66  * @tvec_adhoc@
67  *   @tvec_begingroup@
68  *                      -> output @bgroup@
69  *                      -> env @setup@
70  *     @tvec_begintest@
71  *                      -> output @btest@
72  *     @tvec_skip@
73  *                      -> output @skip@
74  *     @tvec_claimeq@
75  *                      -> @tvec_fail@
76  *                              -> output @fail@
77  *                      -> @tvec_mismatch@
78  *                              -> output @dumpreg@
79  *                              -> type @dump@
80  *     @tvec_endtest@
81  *                      -> output @etest@
82  *     or @tvec_skipgroup@
83  *                      -> output @skipgroup@
84  * @tvec_endgroup@
85  *                      -> output @egroup@
86  *
87  * @tvec_end@
88  *                      -> output @esession@
89  *                      -> output @destroy@
90  *
91  * @tvec_benchrun@
92  *                      -> type @dump@ (compact style)
93  *                      -> output @bbench@
94  *                      -> subenv @run@
95  *                              -> test @fn@
96  *                      -> output @ebench@
97  *                              -> @tvec_benchreport@
98  *
99  * The output functions @error@ and @notice@ can be called at arbitrary
100  * times.
101  */
102
103 /*----- Header files ------------------------------------------------------*/
104
105 #include <stdarg.h>
106 #include <stddef.h>
107 #include <stdio.h>
108 #include <string.h>
109
110 #ifndef MLIB_BUF_H
111 #  include "buf.h"
112 #endif
113
114 #ifndef MLIB_CONTROL_H
115 #  include "control.h"
116 #endif
117
118 #ifndef MLIB_BUF_H
119 #  include "dstr.h"
120 #endif
121
122 #ifndef MLIB_GPRINTF_H
123 #  include "gprintf.h"
124 #endif
125
126 #ifndef MLIB_LBUF_H
127 #  include "lbuf.h"
128 #endif
129
130 #ifndef MLIB_MACROS_H
131 #  include "macros.h"
132 #endif
133
134 /*----- Miscellaneous values ----------------------------------------------*/
135
136 /* These are attached to structures which represent extension points, as a
137  * way to pass an opaque parameter to whatever things are hooked onto them.
138  */
139
140 #define TVEC_MISCSLOTS(_)                                               \
141         _(PTR, const void *, p)         /* arbitrary pointer */         \
142         _(INT, long, i)                 /* signed integer */            \
143         _(UINT, unsigned long, u)       /* signed integer */            \
144         _(FLT, double, f)               /* floating point */
145
146 union tvec_misc {
147 #define TVEC_DEFSLOT(tag, ty, slot) ty slot;
148   TVEC_MISCSLOTS(TVEC_DEFSLOT)
149 #undef TVEC_DEFSLOT
150 };
151 enum {
152 #define TVEC_DEFCONST(tag, ty, slot) TVMISC_##tag,
153   TVEC_MISCSLOTS(TVEC_DEFCONST)
154   TVMISC_LIMIT
155 };
156
157 /*----- Register values ---------------------------------------------------*/
158
159 /* The framework doesn't have a preconceived idea about what's in a register
160  * value: it just allocates them and accesses them through the register type
161  * functions.  It doesn't even have a baked-in idea of how big a register
162  * value is: instead, it gets that via the `regsz' slot in `struct
163  * tvec_testinfo'.  So, as far as the framework is concerned, it's safe to
164  * add new slots to this union, even if they make the overall union larger.
165  * This can be done by defining the preprocessor macro `TVEC_REGSLOTS' to be
166  * a `union' fragment defining any additional union members.
167  *
168  * This creates a distinction between code which does and doesn't know the
169  * size of a register value.  Code which does, which typically means the test
170  * functions, benchmarking setup and teardown functions, and tightly-bound
171  * runner functions, is free to index the register vectors directly.  Code
172  * which doesn't, which means the framework core itself and output formatting
173  * machinery, must use the `TVEC_REG' macro (or its more general `TVEC_GREG'
174  * companion) for indexing register vectors.  (In principle, register type
175  * handlers also fit into this category, but they have no business peering
176  * into register values other than the one's they're given.)
177  */
178
179 union tvec_regval {
180   /* The actual register value.  This is what the type handler sees.
181    * Additional members can be added by setting `TVEC_REGSLOTS' before
182    * including this file.
183    *
184    * A register value can be /initialized/, which simply means that its
185    * contents represent a valid value according to its type -- the register
186    * can be compared, dumped, serialized, parsed into, etc.  You can't do
187    * anything safely to an uninitialized register value other than initialize
188    * it.
189    */
190
191   long i;                               /* signed integer */
192   unsigned long u;                      /* unsigned integer */
193   void *p;                              /* pointer */
194   double f;                             /* floating point */
195   struct { char *p; size_t sz; } text;  /* text string */
196   struct { unsigned char *p; size_t sz; } bytes; /* binary string of bytes */
197   struct {                              /* buffer */
198     unsigned char *p; size_t sz;        /* binary string */
199     size_t a, m;                        /* residue and modulus */
200     size_t off;                         /* offset into full buffer */
201   } buf;
202 #ifdef TVEC_REGSLOTS
203   TVEC_REGSLOTS
204 #endif
205 };
206
207 struct tvec_reg {
208   /* A register.
209    *
210    * Note that all of the registers listed as being used by a particular test
211    * group are initialized at all times[1] while that test group is being
212    * processed.  (The other register slots don't even have types associated
213    * with them, so there's nothing useful we could do with them.)
214    *
215    * The `TVRF_LIVE' flag indicates that the register was assigned a value by
216    * the test vector file: it's the right thing to use to check whether an
217    * optional register is actually present.  Even `dead' registers are still
218    * initialized, though.
219    *
220    * [1] This isn't quite true.  Between individual tests, the registers are
221    *     released and reinitialized in order to reset them to known values
222    *     ready for the next test.  But you won't see them at this point.
223    */
224
225   unsigned f;                           /* flags */
226 #define TVRF_LIVE 1u                    /*   used in current test  */
227   union tvec_regval v;                  /* register value */
228 };
229
230 struct tvec_regdef {
231   /* A register definition.  Register definitions list the registers which
232    * are used by a particular test group (see `struct tvec_test' below).
233    *
234    * A vector of register definitions is terminated by a definition whose
235    * `name' slot is null.
236    */
237
238   const char *name;                     /* register name (for input files) */
239   const struct tvec_regty *ty;          /* register type descriptor */
240   unsigned i;                           /* register index */
241   unsigned f;                           /* flags */
242 #define TVRF_OPT 1u                     /*   optional register */
243 #define TVRF_ID 2u                      /*   part of test identity  */
244   union tvec_misc arg;                  /* extra detail for the type */
245 };
246 #define TVEC_ENDREGS { 0, 0, 0, 0, { 0 } }
247
248 /* @TVEC_GREG(vec, i, regsz)@
249  *
250  * If @vec@ is a data pointer which happens to contain the address of a
251  * vector of @struct tvec_reg@ objects, @i@ is an integer, and @regsz@ is the
252  * size of a @struct tvec_reg@, then this evaluates to the address of the
253  * @i@th element of the vector.
254  *
255  * This is the general tool you need for accessing register vectors when you
256  * don't have absolute knowledge of the size of a @union tvec_regval@.
257  * Usually you want to access one of the register vectors in a @struct
258  * tvec_state@, and @TVEC_REG@ will be more convenient.
259  */
260 #define TVEC_GREG(vec, i, regsz)                                        \
261         ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz)))
262
263 /*----- Register types ----------------------------------------------------*/
264
265 struct tvec_state;                      /* forward declaration */
266
267 struct tvec_regty {
268   /* A register type. */
269
270   void (*init)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/);
271     /* Initialize the value in @*rv@.  This will be called before any other
272      * function acting on the value, including @release@.
273      */
274
275   void (*release)(union tvec_regval */*rv*/,
276                   const struct tvec_regdef */*rd*/);
277     /* Release any resources associated with the value in @*rv@. */
278
279   int (*eq)(const union tvec_regval */*rv0*/,
280             const union tvec_regval */*rv1*/,
281             const struct tvec_regdef */*rd*/);
282     /* Return nonzero if @*rv0@ and @*rv1@ are equal values.  Asymmetric
283      * criteria are permitted: @tvec_checkregs@ calls @eq@ with the input
284      * register as @rv0@ and the output as @rv1@.
285      */
286
287   int (*tobuf)(buf */*b*/, const union tvec_regval */*rv*/,
288                const struct tvec_regdef */*rd*/);
289     /* Serialize the value @*rv@, writing the result to @b@.  Return zero on
290      * success, or %$-1$% on error.
291      */
292
293   int (*frombuf)(buf */*b*/, union tvec_regval */*rv*/,
294                  const struct tvec_regdef */*rd*/);
295     /* Deserialize a value from @b@, storing it in @*rv@.  Return zero on
296      * success, or %$-1$% on error.
297      */
298
299   int (*parse)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/,
300                struct tvec_state */*tv*/);
301     /* Parse a value from @tv->fp@, storing it in @*rv@.  Return zero on
302      * success, or %$-1$% on error, having reported one or more errors via
303      * @tvec_error@ or @tvec_syntax@.  A successful return should leave the
304      * input position at the start of the next line; the caller will flush
305      * the remainder of the line itself.
306      */
307
308   void (*dump)(const union tvec_regval */*rv*/,
309                const struct tvec_regdef */*rd*/,
310                unsigned /*style*/,
311                const struct gprintf_ops */*gops*/, void */*go*/);
312 #define TVSF_COMPACT 1u
313     /* Write a human-readable representation of the value @*rv@ using
314      * @gprintf@ on @gops@ and @go@.  The @style@ is a collection of flags:
315      * if @TVSF_COMPACT@ is set, then output should be minimal, and must fit
316      * on a single line; otherwise, output may consist of multiple lines and
317      * may contain redundant information if that is likely to be useful to a
318      * human reader.
319      */
320 };
321
322 /*----- Test descriptions -------------------------------------------------*/
323
324 struct tvec_env;
325
326 typedef void tvec_testfn(const struct tvec_reg */*in*/,
327                          struct tvec_reg */*out*/,
328                          void */*ctx*/);
329   /* A test function.  It should read inputs from @in@ and write outputs to
330    * @out@.  The @TVRF_LIVE@ is set on inputs which are actually present, and
331    * on outputs which are wanted to test.  A test function can set additional
332    * `gratuitous outputs' by setting @TVRF_LIVE@ on them; clearing
333    * @TVRF_LIVE@ on a wanted output causes a mismatch.
334    *
335    * A test function may be called zero or more times by the environment.  In
336    * particular, it may be called multiple times, though usually by prior
337    * arrangement with the environment.
338    *
339    * The @ctx@ is supplied by the environment's @run@ function (see below).
340    * The default environment calls the test function once, with a null
341    * @ctx@.  There is no expectation that the environment's context has
342    * anything to do with the test function's context.
343    */
344
345 typedef int tvec_setvarfn(struct tvec_state */*tv*/, const char */*var*/,
346                           const union tvec_regval */*rv*/, void */*ctx*/);
347   /* Called after a variable is read.  Return zero on success or %$-1$% on
348    * error.  This function is never called if the test group is skipped.
349    */
350
351 struct tvec_vardef {
352   size_t regsz;                         /* (minimum) register size */
353   tvec_setvarfn *setvar;                /* function to set variable */
354   struct tvec_regdef def;               /* register definition */
355 };
356
357 typedef void tvec_envsetupfn(struct tvec_state */*tv*/,
358                              const struct tvec_env */*env*/,
359                              void */*pctx*/, void */*ctx*/);
360   /* Initialize the context; called at the start of a test group; @pctx@ is
361    * null for environments called by the core, but may be non-null for
362    * subordinate environments.  If setup fails, the function should call
363    * @tvec_skipgroup@ with a suitable excuse.  The @set@, @after@, and
364    * @teardown@ entry points will still be called, but @before@ and @run@
365    * will not.
366    */
367
368 typedef const struct tvec_vardef *tvec_envfindvarfn
369   (struct tvec_state */*tv*/, const char */*name*/,
370    void **/*ctx_out*/, void */*ctx*/);
371   /* Called when the parser finds a %|@var|%' special variable.  If a
372    * suitable variable was found, set @*ctx_out@ to a suitable context and
373    * return the variable definition; the context will be passed to the
374    * variable definition's @setvar@ function.  If no suitable variable was
375    * found, then return null.
376    */
377
378 typedef void tvec_envbeforefn(struct tvec_state */*tv*/, void */*ctx*/);
379   /* Called prior to running a test.  This is the right place to act on any
380    * `%|@var|%' settings.  If preparation fails, the function should call
381    * @tvec_skip@ with a suitable excuse.  This function is never called if
382    * the test group is skipped.  It %%\emph{is}%% called if the test will be
383    * skipped due to erroneous test data.  It should check the @TVSF_ACTIVE@
384    * flag if necessary.
385    */
386
387 typedef void tvec_envrunfn(struct tvec_state */*tv*/,
388                            tvec_testfn */*fn*/, void */*ctx*/);
389   /* Run the test.  It should either call @tvec_skip@, or run @fn@ one or
390    * more times.  In the latter case, it is responsible for checking the
391    * outputs, and calling @tvec_fail@ as necessary; @tvec_checkregs@ will
392    * check the register values against the supplied test vector, while
393    * @tvec_check@ does pretty much everything necessary.  This function is
394    * never called if the test group is skipped.
395    */
396
397 typedef void tvec_envafterfn(struct tvec_state */*tv*/, void */*ctx*/);
398   /* Called after running or skipping a test.  Typical actions involve
399    * resetting whatever things were established by @set@.  This function
400    * %%\emph{is}%% called if the test group is skipped or the test data is
401    * erroneous, so that the test environment can reset variables set by the
402    * @set@ entry point.  It should check the @TVSF_SKIP@ flag if necessary.
403    */
404
405 typedef void tvec_envteardownfn(struct tvec_state */*tv*/, void */*ctx*/);
406   /* Tear down the environment: called at the end of a test group. */
407
408
409 struct tvec_env {
410   /* A test environment sets things up for and arranges to run the test.
411    *
412    * The caller is responsible for allocating storage for the environment's
413    * context, based on the @ctxsz@ slot, and freeing it later; this space is
414    * passed in as the @ctx@ parameter to the remaining functions; if @ctxsz@
415    * is zero then @ctx@ is null.
416    */
417
418   size_t ctxsz;                         /* environment context size */
419
420   tvec_envsetupfn *setup;               /* setup for group */
421   tvec_envfindvarfn *findvar;           /* find variable */
422   tvec_envbeforefn *before;             /* prepare for test */
423   tvec_envrunfn *run;                   /* run test function */
424   tvec_envafterfn *after;               /* clean up after test */
425   tvec_envteardownfn *teardown;         /* tear down after group */
426 };
427
428 struct tvec_test {
429   /* A test description. */
430
431   const char *name;                     /* name of the test */
432   const struct tvec_regdef *regs;       /* descriptions of the registers */
433   const struct tvec_env *env;           /* environment to run test in */
434   tvec_testfn *fn;                      /* test function */
435 };
436 #define TVEC_ENDTESTS { 0, 0, 0, 0 }
437
438 /*----- Test state --------------------------------------------------------*/
439
440 enum {
441   /* Possible test outcomes. */
442
443   TVOUT_LOSE,                           /* test failed */
444   TVOUT_SKIP,                           /* test skipped */
445   TVOUT_WIN,                            /* test passed */
446   TVOUT_XFAIL,                          /* test passed, but shouldn't have */
447   TVOUT_LIMIT                           /* (number of possible outcomes) */
448 };
449
450 struct tvec_config {
451   /* An overall test configuration. */
452
453   const struct tvec_test *tests;        /* the tests to be performed */
454   unsigned nrout, nreg;                 /* number of output/total regs */
455   size_t regsz;                         /* size of a register */
456 };
457
458 struct tvec_state {
459   /* The primary state structure for the test vector machinery. */
460
461   /* Flags.  Read-only for all callers. */
462   unsigned f;                           /* flags */
463 #define TVSF_SKIP 0x0001u               /*   skip this test group */
464 #define TVSF_OPEN 0x0002u               /*   test is open */
465 #define TVSF_ACTIVE 0x0004u             /*   test is active */
466 #define TVSF_ERROR 0x0008u              /*   an error occurred */
467 #define TVSF_OUTMASK 0x00f0u            /*   test outcome (@TVOUT_...@) */
468 #define TVSF_OUTSHIFT 4                 /*   shift applied to outcome */
469 #define TVSF_XFAIL 0x0100u              /*   test expected to fail */
470 #define TVSF_MUFFLE 0x0200u             /*   muffle errors */
471
472   /* Test configuration.  Read-only for all callers. */
473   struct tvec_config cfg;               /* test configuration */
474
475   /* Registers.  Available to execution environments, which may modify the
476    * contents of the active registers, as defined by the current test group,
477    * but not the vector pointers themselves or inactive registers.
478    */
479   struct tvec_reg *in, *out;            /* register vectors */
480
481   /* Test group state.  Read-only for all callers. */
482   const struct tvec_test *test;         /* current test */
483
484   /* Test scoreboard.  Available to output formatters. */
485   unsigned curr[TVOUT_LIMIT], all[TVOUT_LIMIT], grps[TVOUT_LIMIT];
486
487   /* Output machinery. */
488   struct tvec_output *output;           /* output formatter */
489
490   /* Input machinery.  Available to type parsers. */
491   const char *infile; unsigned lno, test_lno; /* input file name, line */
492   FILE *fp;                             /* input file stream */
493 };
494
495 /* @TVEC_REG(tv, vec, i)@
496  *
497  * If @tv@ is a pointer to a @struct tvec_state@, @vec@ is either @in@ or
498  * @out@, and @i@ is an integer, then this evaluates to the address of the
499  * @i@th register in the selected vector.
500  */
501 #define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->cfg.regsz)
502
503 /*----- Output formatting -------------------------------------------------*/
504
505 struct tvec_output {
506   /* An output formatter. */
507   const struct tvec_outops *ops;        /* pointer to operations */
508 };
509
510 enum {
511   /* Register output dispositions. */
512
513   TVRD_INPUT,                           /* input-only register */
514   TVRD_OUTPUT,                          /* output-only (input is dead) */
515   TVRD_MATCH,                           /* matching (equal) registers */
516   TVRD_FOUND,                           /* mismatching output register */
517   TVRD_EXPECT,                          /* mismatching input register */
518   TVRD_LIMIT                            /* (number of dispositions) */
519 };
520
521 #define TVEC_LEVELS(_)                                                  \
522         _(NOTE, "notice", 4)                                            \
523         _(ERR, "ERROR", 8)
524 enum {
525 #define TVEC_DEFLEVEL(tag, name, val) TVLEV_##tag = val,
526   TVEC_LEVELS(TVEC_DEFLEVEL)
527 #undef TVEC_DEFLEVEL
528   TVLEV_LIMIT
529 };
530
531 /* Benchmarking details. */
532 enum {
533   TVBU_OP,                             /* counting operations of some kind */
534   TVBU_BYTE,                            /* counting bytes (@rbuf >= 0@) */
535   TVBU_LIMIT                            /* (number of units) */
536 };
537 struct bench_timing;     /* include <mLib/bench.h> for the real definition */
538
539 struct tvec_outops {
540   /* Output operations. */
541
542   void (*bsession)(struct tvec_output */*o*/, struct tvec_state */*tv*/);
543     /* Begin a test session.  The output driver will probably want to
544      * save @tv@, because this isn't provided to any other methods.
545      */
546
547   int (*esession)(struct tvec_output */*o*/);
548     /* End a session, and return the suggested exit code. */
549
550   void (*bgroup)(struct tvec_output */*o*/);
551     /* Begin a test group.  The test group description is @tv->test@. */
552
553   void (*skipgroup)(struct tvec_output */*o*/,
554                     const char */*excuse*/, va_list */*ap*/);
555     /* The group is being skipped; @excuse@ may be null or a format
556      * string explaining why.  The @egroup@ method will not be called
557      * separately.
558      */
559
560   void (*egroup)(struct tvec_output */*o*/);
561     /* End a test group.  At least one test was attempted or @skipgroup@
562      * would have been called instead.  If @tv->curr[TVOUT_LOSE]@ is nonzero
563      * then the test group as a whole failed; otherwise it passed.
564      */
565
566   void (*btest)(struct tvec_output */*o*/);
567     /* Begin a test case. */
568
569   void (*skip)(struct tvec_output */*o*/,
570                const char */*excuse*/, va_list */*ap*/);
571     /* The test case is being skipped; @excuse@ may be null or a format
572      * string explaining why.  The @etest@ function will still be called (so
573      * this works differently from @skipgroup@ and @egroup@).  A test case
574      * can be skipped at most once, and, if skipped, it cannot fail.
575      */
576
577   void (*fail)(struct tvec_output */*o*/,
578                const char */*detail*/, va_list */*ap*/);
579     /* The test case failed.
580      *
581      * The output driver should preferably report the filename (@infile@) and
582      * line number (@test_lno@, not @lno@) for the failing test.
583      *
584      * The @detail@ may be null or a format string describing detail about
585      * how the failing test was run which can't be determined from the
586      * registers; a @detail@ is usually provided when (and only when) the
587      * test environment potentially invokes the test function more than once.
588      *
589      * A single test case can fail multiple times!
590      */
591
592   void (*dumpreg)(struct tvec_output */*o*/,
593                   unsigned /*disp*/, const union tvec_regval */*rv*/,
594                   const struct tvec_regdef */*rd*/);
595     /* Dump a register.  The `disposition' @disp@ explains what condition the
596      * register is in; see @tvec_dumpreg@ and the @TVRD_...@ codes.  The
597      * register value is at @rv@, and its definition, including its type, at
598      * @rd@.  Note that this function may be called on virtual registers
599      * which aren't in either of the register vectors or mentioned by the
600      * test description.  It may also be called with @rv@ null, indicating
601      * that the register is not live.
602      */
603
604   void (*etest)(struct tvec_output */*o*/, unsigned /*outcome*/);
605     /* The test case concluded with the given @outcome@ (one of the
606      * @TVOUT_...@ codes.
607      */
608
609   void (*bbench)(struct tvec_output */*o*/,
610                  const char */*ident*/, unsigned /*unit*/);
611     /* Begin a benchmark.  The @ident@ is a formatted string identifying the
612      * benchmark based on the values of the input registered marked
613      * @TVRF_ID@; the output driver is free to use this or come up with its
614      * own way to identify the test, e.g., by inspecting the register values
615      * for itself.  The @unit@ is one of the @TVBU_...@ constants explaining
616      * what sort of thing is being measured.
617      */
618
619   void (*ebench)(struct tvec_output */*o*/,
620                  const char */*ident*/, unsigned /*unit*/,
621                  const struct bench_timing */*tm*/);
622     /* End a benchmark.  The @ident@ and @unit@ arguments are as for
623      * @bbench@.  If @tm@ is zero then the measurement failed; otherwise
624      * @tm->n@ counts total number of things (operations or bytes, as
625      * indicated by @unit@) processed in the indicated time.
626      */
627
628   void (*report)(struct tvec_output */*o*/, unsigned /*level*/,
629                  const char */*msg*/, va_list */*ap*/);
630     /* Report a message.  The driver should ideally report the filename
631      * (@infile@) and line number (@lno@) prompting the error.
632      */
633
634   void (*destroy)(struct tvec_output */*o*/);
635     /* Release any resources acquired by the driver. */
636 };
637
638 /*----- Session lifecycle -------------------------------------------------*/
639
640 /* --- @tvec_begin@ --- *
641  *
642  * Arguments:   @struct tvec_state *tv_out@ = state structure to fill in
643  *              @const struct tvec_config *config@ = test configuration
644  *              @struct tvec_output *o@ = output driver
645  *
646  * Returns:     ---
647  *
648  * Use:         Initialize a state structure ready to do some testing.
649  */
650
651 extern void tvec_begin(struct tvec_state */*tv_out*/,
652                        const struct tvec_config */*config*/,
653                        struct tvec_output */*o*/);
654
655 /* --- @tvec_end@ --- *
656  *
657  * Arguments:   @struct tvec_state *tv@ = test-vector state
658  *
659  * Returns:     A proposed exit code.
660  *
661  * Use:         Conclude testing and suggests an exit code to be returned to
662  *              the calling program.  (The exit code comes from the output
663  *              driver's @esession@ method.)
664  */
665
666 extern int tvec_end(struct tvec_state */*tv*/);
667
668 /* --- @tvec_read@ --- *
669  *
670  * Arguments:   @struct tvec_state *tv@ = test-vector state
671  *              @const char *infile@ = the name of the input file
672  *              @FILE *fp@ = stream to read from
673  *
674  * Returns:     Zero on success, %$-1$% on error.
675  *
676  * Use:         Read test vector data from @fp@ and exercise test functions.
677  *              THe return code doesn't indicate test failures: it's only
678  *              concerned with whether there were problems with the input
679  *              file or with actually running the tests.
680  */
681
682 extern int tvec_read(struct tvec_state */*tv*/,
683                      const char */*infile*/, FILE */*fp*/);
684
685 /*----- Command-line interface --------------------------------------------*/
686
687 extern const struct tvec_config tvec_adhocconfig;
688   /* A special @struct tvec_config@ to use for programs which perform ad-hoc
689    * testing.
690    */
691
692 /* --- @tvec_parseargs@ --- *
693  *
694  * Arguments:   @int argc@ = number of command-line arguments
695  *              @char *argv[]@ = vector of argument strings
696  *              @struct tvec_state *tv_out@ = test vector state to initialize
697  *              @int *argpos_out@ = where to leave unread argument index
698  *              @const struct tvec_config *cofig@ = test vector configuration
699  *
700  * Returns:     ---
701  *
702  * Use:         Parse arguments and set up the test vector state @*tv_out@.
703  *              If errors occur, print messages to standard error and exit
704  *              with status 2.
705  */
706
707 extern void tvec_parseargs(int /*argc*/, char */*argv*/[],
708                            struct tvec_state */*tv_out*/,
709                            int */*argpos_out*/,
710                            const struct tvec_config */*config*/);
711
712 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
713  *
714  * Arguments:   @struct tvec_state *tv@ = test vector state
715  *              @const char *file@ = pathname of file to read
716  *              @const char *arg@ = argument to interpret
717  *
718  * Returns:     Zero on success, %$-1$% on error.
719  *
720  * Use:         Read test vector data from stdin or a named file.  The
721  *              @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
722  *              and from the named file otherwise.
723  */
724
725 extern int tvec_readstdin(struct tvec_state */*tv*/);
726 extern int tvec_readfile(struct tvec_state */*tv*/, const char */*file*/);
727 extern int tvec_readarg(struct tvec_state */*tv*/, const char */*arg*/);
728
729 /* --- @tvec_readdflt@ --- *
730  *
731  * Arguments:   @struct tvec_state *tv@ = test vector state
732  *              @const char *dflt@ = defsault filename or null
733  *
734  * Returns:     Zero on success, %$-1$% on error.
735  *
736  * Use:         Reads from the default test vector data.  If @file@ is null,
737  *              then read from standard input, unless that's a terminal; if
738  *              @file@ is not null, then read the named file, looking in the
739  *              directory named by the `%|srcdir|%' environment variable if
740  *              that's set, or otherwise in the current directory.
741  */
742
743 extern int tvec_readdflt(struct tvec_state */*tv*/, const char */*file*/);
744
745 /* --- @tvec_readargs@ --- *
746  *
747  * Arguments:   @int argc@ = number of command-line arguments
748  *              @char *argv[]@ = vector of argument strings
749  *              @struct tvec_state *tv@ = test vector state
750  *              @int *argpos_inout@ = current argument position (updated)
751  *              @const char *dflt@ = default filename or null
752  *
753  * Returns:     Zero on success, %$-1$% on error.
754  *
755  * Use:         Reads from the sources indicated by the command-line
756  *              arguments, in order, interpreting each as for @tvec_readarg@;
757  *              if no arguments are given then read from @dflt@ as for
758  *              @tvec_readdflt@.
759  */
760
761 extern int tvec_readargs(int /*argc*/, char */*argv*/[],
762                          struct tvec_state */*tv*/,
763                          int */*argpos_inout*/, const char */*dflt*/);
764
765 /* --- @tvec_main@ --- *
766  *
767  * Arguments:   @int argc@ = number of command-line arguments
768  *              @char *argv[]@ = vector of argument strings
769  *              @const struct tvec_config *cofig@ = test vector configuration
770  *              @const char *dflt@ = default filename or null
771  *
772  * Returns:     Exit code.
773  *
774  * Use:         All-in-one test vector front-end.  Parse options from the
775  *              command-line as for @tvec_parseargs@, and then process the
776  *              remaining positional arguments as for @tvec_readargs@.  The
777  *              function constructs and disposes of a test vector state.
778  */
779
780 extern int tvec_main(int /*argc*/, char */*argv*/[],
781                      const struct tvec_config */*config*/,
782                      const char */*dflt*/);
783
784 /*----- Test processing ---------------------------------------------------*/
785
786 /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
787  *
788  * Arguments:   @struct tvec_state *tv@ = test-vector state
789  *              @const char *excuse@, @va_list *ap@ = reason why skipped
790  *
791  * Returns:     ---
792  *
793  * Use:         Skip the current group.  This should only be called from a
794  *              test environment @setup@ function; a similar effect occurs if
795  *              the @setup@ function fails.
796  */
797
798 extern PRINTF_LIKE(2, 3)
799   void tvec_skipgroup(struct tvec_state */*tv*/,
800                       const char */*excuse*/, ...);
801 extern void tvec_skipgroup_v(struct tvec_state */*tv*/,
802                              const char */*excuse*/, va_list */*ap*/);
803
804 /* --- @tvec_skip@, @tvec_skip_v@ --- *
805  *
806  * Arguments:   @struct tvec_state *tv@ = test-vector state
807  *              @const char *excuse@, @va_list *ap@ = reason why test skipped
808  *
809  * Returns:     ---
810  *
811  * Use:         Skip the current test.  This should only be called from a
812  *              test environment @run@ function; a similar effect occurs if
813  *              the @before@ function fails.
814  */
815
816 extern PRINTF_LIKE(2, 3)
817   void tvec_skip(struct tvec_state */*tv*/, const char */*excuse*/, ...);
818 extern void tvec_skip_v(struct tvec_state */*tv*/,
819                         const char */*excuse*/, va_list */*ap*/);
820
821 /* --- @tvec_fail@, @tvec_fail_v@ --- *
822  *
823  * Arguments:   @struct tvec_state *tv@ = test-vector state
824  *              @const char *detail@, @va_list *ap@ = description of test
825  *
826  * Returns:     ---
827  *
828  * Use:         Report the current test as a failure.  This function can be
829  *              called multiple times for a single test, e.g., if the test
830  *              environment's @run@ function invokes the test function
831  *              repeatedly; but a single test that fails repeatedly still
832  *              only counts as a single failure in the statistics.  The
833  *              @detail@ string and its format parameters can be used to
834  *              distinguish which of several invocations failed; it can
835  *              safely be left null if the test function is run only once.
836  */
837
838 extern PRINTF_LIKE(2, 3)
839   void tvec_fail(struct tvec_state */*tv*/, const char */*detail*/, ...);
840 extern void tvec_fail_v(struct tvec_state */*tv*/,
841                         const char */*detail*/, va_list */*ap*/);
842
843 /* --- @tvec_dumpreg@ --- *
844  *
845  * Arguments:   @struct tvec_state *tv@ = test-vector state
846  *              @unsigned disp@ = the register disposition (@TVRD_...@)
847  *              @const union tvec_regval *tv@ = register value, or null
848  *              @const struct tvec_regdef *rd@ = register definition
849  *
850  * Returns:     ---
851  *
852  * Use:         Dump a register value to the output.  This is the lowest-
853  *              level function for dumping registers, and calls the output
854  *              formatter directly.
855  *
856  *              Usually @tvec_mismatch@ is much more convenient.  Low-level
857  *              access is required for reporting `virtual' registers
858  *              corresponding to test environment settings.
859  */
860
861 extern void tvec_dumpreg(struct tvec_state */*tv*/,
862                          unsigned /*disp*/, const union tvec_regval */*rv*/,
863                          const struct tvec_regdef */*rd*/);
864
865 /* --- @tvec_initregs@, @tvec_releaseregs@ --- *
866  *
867  * Arguments:   @struct tvec_state *tv@ = test-vector state
868  *
869  * Returns:     ---
870  *
871  * Use:         Initialize or release, respectively, the registers required
872  *              by the current test.  All of the registers, both input and
873  *              output, are effected.  Initialized registers are not marked
874  *              live.
875  */
876
877 extern void tvec_initregs(struct tvec_state */*tv*/);
878 extern void tvec_releaseregs(struct tvec_state */*tv*/);
879
880 /* --- @tvec_resetoutputs@ --- *
881  *
882  * Arguments:   @struct tvec_state *tv@ = test-vector state
883  *
884  * Returns:     ---
885  *
886  * Use:         Reset (releases and reinitializes) the output registers in
887  *              the test state.  This is mostly of use to test environment
888  *              @run@ functions, between invocations of the test function.
889  *              Output registers are marked live if and only if the
890  *              corresponding input register is live.
891  */
892
893 extern void tvec_resetoutputs(struct tvec_state */*tv*/);
894
895 /* --- @tvec_checkregs@ --- *
896  *
897  * Arguments:   @struct tvec_state *tv@ = test-vector state
898  *
899  * Returns:     Zero on success, %$-1$% on mismatch.
900  *
901  * Use:         Compare the active output registers (according to the current
902  *              test group definition) with the corresponding input register
903  *              values.  A mismatch occurs if the two values differ
904  *              (according to the register type's @eq@ method), or if the
905  *              input is live but the output is dead.
906  *
907  *              This function only checks for a mismatch and returns the
908  *              result; it takes no other action.  In particular, it doesn't
909  *              report a failure, or dump register values.
910  */
911
912 extern int tvec_checkregs(struct tvec_state */*tv*/);
913
914 /* --- @tvec_mismatch@ --- *
915  *
916  * Arguments:   @struct tvec_state *tv@ = test-vector state
917  *              @unsigned f@ = flags (@TVMF_...@)
918  *
919  * Returns:     ---
920  *
921  * Use:         Dumps registers suitably to report a mismatch.  The flag bits
922  *              @TVMF_IN@ and @TVF_OUT@ select input-only and output
923  *              registers.  If both are reset then nothing happens.
924  *              Suppressing the output registers may be useful, e.g., if the
925  *              test function crashed rather than returning outputs.
926  */
927
928 #define TVMF_IN 1u
929 #define TVMF_OUT 2u
930 extern void tvec_mismatch(struct tvec_state */*tv*/, unsigned /*f*/);
931
932 /* --- @tvec_check@, @tvec_check_v@ --- *
933  *
934  * Arguments:   @struct tvec_state *tv@ = test-vector state
935  *              @const char *detail@, @va_list *ap@ = description of test
936  *
937  * Returns:     ---
938  *
939  * Use:         Check the register values, reporting a failure and dumping
940  *              the registers in the event of a mismatch.  This just wraps up
941  *              @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
942  *              obvious way.
943  */
944
945 extern PRINTF_LIKE(2, 3)
946   void tvec_check(struct tvec_state */*tv*/, const char */*detail*/, ...);
947 extern void tvec_check_v(struct tvec_state */*tv*/,
948                          const char */*detail*/, va_list */*ap*/);
949
950 /*----- Ad-hoc testing ----------------------------------------------------*/
951
952 /* --- @tvec_adhoc@ --- *
953  *
954  * Arguments:   @struct tvec_state *tv@ = test-vector state
955  *              @struct tvec_test *t@ = space for a test definition
956  *
957  * Returns:     ---
958  *
959  * Use:         Begin ad-hoc testing, i.e., without reading a file of
960  *              test-vector data.
961  *
962  *              The structure at @t@ will be used to record information about
963  *              the tests underway, which would normally come from a static
964  *              test definition.  The other functions in this section assume
965  *              that @tvec_adhoc@ has been called.
966  */
967
968 extern void tvec_adhoc(struct tvec_state */*tv*/, struct tvec_test */*t*/);
969
970 /* --- @tvec_begingroup@, @TVEC_BEGINGROUP@ --- *
971  *
972  * Arguments:   @struct tvec_state *tv@ = test-vector state
973  *              @const char *name@ = name for this test group
974  *              @const char *file@, @unsigned @lno@ = calling file and line
975  *
976  * Returns:     ---
977  *
978  * Use:         Begin an ad-hoc test group with the given name.  The @file@
979  *              and @lno@ can be anything, but it's usually best if they
980  *              refer to the source code performing the test: the macro
981  *              @TVEC_BEGINGROUP@ does this automatically.
982  */
983
984 extern void tvec_begingroup(struct tvec_state */*tv*/, const char */*name*/,
985                             const char */*file*/, unsigned /*lno*/);
986 #define TVEC_BEGINGROUP(tv, name)                                       \
987         do tvec_begingroup(tv, name, __FILE__, __LINE__); while (0)
988
989 /* --- @tvec_endgroup@ --- *
990  *
991  * Arguments:   @struct tvec_state *tv@ = test-vector state
992  *
993  * Returns:     ---
994  *
995  * Use:         End an ad-hoc test group.  The statistics are updated and the
996  *              outcome is reported to the output formatter.
997  */
998
999 extern void tvec_endgroup(struct tvec_state */*tv*/);
1000
1001 /* --- @TVEC_TESTGROUP@, @TVEC_TESTGROUP_TAG@ --- *
1002  *
1003  * Arguments:   @tag@ = label-disambiguation tag
1004  *              @const struct tvec_state *tv = test-vector state
1005  *              @const char *name@ = test-group name
1006  *
1007  * Returns:     ---
1008  *
1009  * Use:         Control-structure macro: @TVEC_TESTGROUP(tv, name) stmt@
1010  *              establishes a test group with the given @name@ (attributing
1011  *              it to the source file and lie number), executes @stmt@, and
1012  *              ends the test group.  If @stmt@ invokes @break@ then the test
1013  *              group is skipped.  @TVEC_TESTGROUP_TAG@ is the same, with an
1014  *              additional @tag@ argument for use in higher-level macros.
1015  */
1016
1017 #define TVEC_TESTGROUP_TAG(tag, tv, name)                               \
1018         MC_WRAP(tag##__around,                                          \
1019           { TVEC_BEGINGROUP(tv, name); },                               \
1020           { tvec_endgroup(tv); },                                       \
1021           { if (!((tv)->f&TVSF_SKIP)) tvec_skipgroup(tv, 0);            \
1022             tvec_endgroup(tv); })
1023 #define TVEC_TESTGROUP(tv, name) TVEC_TESTGROUP_TAG(grp, tv, name)
1024
1025 /* --- @tvec_begintest@, @TVEC_BEGINTEST@ --- *
1026  *
1027  * Arguments:   @struct tvec_state *tv@ = test-vector state
1028  *              @const char *file@, @unsigned @lno@ = calling file and line
1029  *
1030  * Returns:     ---
1031  *
1032  * Use:         Begin an ad-hoc test case.  The @file@ and @lno@ can be
1033  *              anything, but it's usually best if they refer to the source
1034  *              code performing the test: the macro @TVEC_BEGINGROUP@ does
1035  *              this automatically.
1036  */
1037
1038 extern void tvec_begintest(struct tvec_state */*tv*/,
1039                            const char */*file*/, unsigned /*lno*/);
1040 #define TVEC_BEGINTEST(tv)                                              \
1041         do tvec_begintest(tv, __FILE__, __LINE__); while (0)
1042
1043 /* --- @tvec_endtest@ --- *
1044  *
1045  * Arguments:   @struct tvec_state *tv@ = test-vector state
1046  *
1047  * Returns:     ---
1048  *
1049  * Use:         End an ad-hoc test case,  The statistics are updated and the
1050  *              outcome is reported to the output formatter.
1051  */
1052
1053 extern void tvec_endtest(struct tvec_state */*tv*/);
1054
1055 /* --- @TVEC_TEST@, @TVEC_TEST_TAG@ --- *
1056  *
1057  * Arguments:   @tag@ = label-disambiguation tag
1058  *              @struct tvec_test *t@ = space for a test definition
1059  *
1060  * Returns:     ---
1061  *
1062  * Use:         Control-structure macro: @TVEC_TEST(tv) stmt@ begins a test
1063  *              case, executes @stmt@, and ends the test case.  If @stmt@
1064  *              invokes @break@ then the test case is skipped.
1065  *              @TVEC_TEST_TAG@ is the same, with an additional @tag@ argumet
1066  *              for use in higher-level macros.
1067  */
1068
1069 #define TVEC_TEST_TAG(tag, tv)                                          \
1070         MC_WRAP(tag##__around,                                          \
1071           { TVEC_BEGINTEST(tv); },                                      \
1072           { tvec_endtest(tv); },                                        \
1073           { if ((tv)->f&TVSF_ACTIVE) tvec_skip((tv), 0);                \
1074             tvec_endtest(tv); })
1075 #define TVEC_TEST(tv) TVEC_TEST_TAG(test, tv)
1076
1077 /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- *
1078  *
1079  * Arguments:   @struct tvec_state *tv@ = test-vector state
1080  *              @int ok@ = a flag
1081  *              @const char *file@, @unsigned @lno@ = calling file and line
1082  *              @const char *msg@, @va_list *ap@ = message to report on
1083  *                      failure
1084  *
1085  * Returns:     The value @ok@.
1086  *
1087  * Use:         Check that a claimed condition holds, as (part of) a test.
1088  *              If no test case is underway (i.e., if @TVSF_OPEN@ is reset in
1089  *              @tv->f@), then a new test case is begun and ended.  The
1090  *              @file@ and @lno@ are passed to the output formatter to be
1091  *              reported in case of a failure.  If @ok@ is nonzero, then
1092  *              nothing else happens; so, in particular, if @tvec_claim@
1093  *              established a new test case, then the test case succeeds.  If
1094  *              @ok@ is zero, then a failure is reported, quoting @msg@.
1095  *
1096  *              The @TVEC_CLAIM@ macro is similar, only it (a) identifies the
1097  *              file and line number of the call site automatically, and (b)
1098  *              implicitly quotes the source text of the @ok@ condition in
1099  *              the failure message.
1100  */
1101
1102 extern PRINTF_LIKE(5, 6)
1103   int tvec_claim(struct tvec_state */*tv*/, int /*ok*/,
1104                  const char */*file*/, unsigned /*lno*/,
1105                  const char */*msg*/, ...);
1106 extern int tvec_claim_v(struct tvec_state */*tv*/, int /*ok*/,
1107                         const char */*file*/, unsigned /*lno*/,
1108                         const char */*msg*/, va_list */*ap*/);
1109 #define TVEC_CLAIM(tv, cond)                                            \
1110         (tvec_claim(tv, !!(cond), __FILE__, __LINE__, "%s untrue", #cond))
1111
1112 /* --- @tvec_claimeq@ --- *
1113  *
1114  * Arguments:   @struct tvec_state *tv@ = test-vector state
1115  *              @const struct tvec_regty *ty@ = register type
1116  *              @const union tvec_misc *arg@ = register type argument
1117  *              @const char *file@, @unsigned @lno@ = calling file and line
1118  *              @const char *expr@ = the expression to quote on failure
1119  *
1120  * Returns:     Nonzero if the input and output values of register 0 are
1121  *              equal, zero if they differ.
1122  *
1123  * Use:         Check that the input and output values of register 0 are
1124  *              equal (according to the register type @ty@).  As for
1125  *              @tvec_claim@ above, a test case is automatically begun and
1126  *              ended if none is already underway.  If the values are
1127  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1128  *              mismatched values are dumped.
1129  *
1130  *              This function is not expected to be called directly, but
1131  *              through type-specific wrapper functions or macros such as
1132  *              @TVEC_CLAIMEQ_INT@.
1133  */
1134
1135 extern int tvec_claimeq(struct tvec_state */*tv*/,
1136                         const struct tvec_regty */*ty*/,
1137                         const union tvec_misc */*arg*/,
1138                         const char */*file*/, unsigned /*lno*/,
1139                         const char */*expr*/);
1140
1141 /*----- Benchmarking ------------------------------------------------------*/
1142
1143 struct tvec_benchenv {
1144   struct tvec_env _env;                 /* benchmarking is an environment */
1145   struct bench_state **bst;             /* benchmark state anchor or null */
1146   unsigned long niter;                  /* iterations done per unit */
1147   int riter, rbuf;                      /* iterations and buffer registers */
1148   const struct tvec_env *env;           /* subordinate environment */
1149 };
1150
1151 struct tvec_benchctx {
1152   const struct tvec_benchenv *be;       /* environment configuration */
1153   struct bench_state *bst;              /* benchmark state */
1154   double dflt_target;                   /* default time in seconds */
1155   unsigned f;                           /* flags */
1156 #define TVBF_SETTRG 1u                  /*   set `@target' */
1157 #define TVBF_SETMASK (TVBF_SETTRG))     /*   mask of @TVBF_SET...@ */
1158   void *subctx;                         /* subsidiary environment context */
1159 };
1160
1161 extern struct bench_state *tvec_benchstate;
1162
1163 /* --- Environment implementation --- *
1164  *
1165  * The following special variables are supported.
1166  *
1167  *   * %|@target|% is the (approximate) number of seconds to run the
1168  *     benchmark.
1169  *
1170  * Unrecognized variables are passed to the subordinate environment, if there
1171  * is one.  Other events are passed through to the subsidiary environment.
1172  */
1173
1174 extern tvec_envsetupfn tvec_benchsetup;
1175 extern tvec_envfindvarfn tvec_benchfindvar;
1176 extern tvec_envbeforefn tvec_benchbefore;
1177 extern tvec_envrunfn tvec_benchrun;
1178 extern tvec_envafterfn tvec_benchafter;
1179 extern tvec_envteardownfn tvec_benchteardown;
1180
1181 #define TVEC_BENCHENV                                                   \
1182   { sizeof(struct tvec_benchctx),                                       \
1183     tvec_benchsetup,                                                    \
1184     tvec_benchfindvar,                                                  \
1185     tvec_benchbefore,                                                   \
1186     tvec_benchrun,                                                      \
1187     tvec_benchafter,                                                    \
1188     tvec_benchteardown }
1189 #define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate
1190
1191 /* --- @tvec_benchreport@ --- *
1192  *
1193  * Arguments:   @const struct gprintf_ops *gops@ = print operations
1194  *              @void *go@ = print destination
1195  *              @unsigned unit@ = the unit being measured (~TVBU_...@)
1196  *              @const struct bench_timing *tm@ = the benchmark result
1197  *
1198  * Returns:     ---
1199  *
1200  * Use:         Formats a report about the benchmark performance.  This
1201  *              function is intended to be called on by an output
1202  *              @ebench@ function.
1203  */
1204
1205 extern void tvec_benchreport
1206   (const struct gprintf_ops */*gops*/, void */*go*/,
1207    unsigned /*unit*/, const struct bench_timing */*tm*/);
1208
1209 /*----- Remote execution --------------------------------------------------*/
1210
1211 struct tvec_remoteenv;
1212
1213 struct tvec_remotecomms {
1214   int infd, outfd;                      /* input and output descriptors */
1215   dbuf bout;                            /* output buffer */
1216   unsigned char *bin;                   /* input buffer */
1217   size_t binoff, binlen, binsz;         /* input offset, length, and size */
1218   size_t t;                             /* temporary offset */
1219   unsigned f;                           /* flags */
1220 #define TVRF_BROKEN 0x0001u             /*   communications have failed */
1221 };
1222 #define TVEC_REMOTECOMMS_INIT { -1, -1, DBUF_INIT, 0, 0, 0, 0, 0, 0 }
1223
1224 struct tvec_remotectx {
1225   struct tvec_state *tv;                /* test vector state */
1226   struct tvec_remotecomms rc;           /* communication state */
1227   const struct tvec_remoteenv *re;      /* environment configuration */
1228   void *subctx;                         /* subenvironment context */
1229   struct tvec_vardef vd;                /* temporary variable definition */
1230   unsigned ver;                         /* protocol version */
1231   pid_t kid;                            /* child process id */
1232   int errfd;                            /* child stderr descriptor */
1233   lbuf errbuf;                          /* child stderr line buffer */
1234   dstr prgwant, progress;               /* progress: wanted/reported */
1235   unsigned exwant, exit;                /* exit status wanted/reported */
1236 #define TVRF_RCNMASK 0x0300u            /*   reconnection behaviour: */
1237 #define TVRCN_DEMAND 0x0000u            /*     connect on demand */
1238 #define TVRCN_SKIP 0x0100u              /*     skip unless connected */
1239 #define TVRCN_FORCE 0x0200u             /*     force reconnection */
1240 #define TVRF_MUFFLE 0x0400u             /*   muffle child stderr */
1241 #define TVRF_SETEXIT 0x0800u            /*   set `@exit' */
1242 #define TVRF_SETPRG 0x1000u             /*   set `@progress' */
1243 #define TVRF_SETRCN 0x2000u             /*   set `@reconnect' */
1244 #define TVRF_SETMASK (TVRF_SETEXIT | TVRF_SETPRG | TVRF_SETRCN)
1245                                         /*   mask of @TVTF_SET...@ */
1246 };
1247
1248 typedef int tvec_connectfn(pid_t */*kid_out*/, int */*infd_out*/,
1249                            int */*outfd_out*/, int */*errfd_out*/,
1250                            struct tvec_state */*tv*/,
1251                            const struct tvec_remoteenv */*env*/);
1252   /* A connection function.  On entry, @tv@ holds the test-vector state, and
1253    * @env@ is the test group's remote environment structure, which will
1254    * typically really be some subclass of @struct tvec_remoteenv@ containing
1255    * additional parameters for establishing the child process.
1256    *
1257    * On successful completion, the function stores input and output
1258    * descriptors (which need not be distinct) in @*infd_out@ and
1259    * @*outfd_out@, and returns zero; if it creates a child process, it should
1260    * additionally store the child's process-id in @*kid_out@ and store in
1261    * @*errfd_out@ a descriptor from which the child's error output can be
1262    * read.  On error, the function should report an appropriate message via
1263    * @tvec_error@ and return %$-1$%.
1264    */
1265
1266 struct tvec_remoteenv_slots {
1267   tvec_connectfn *connect;              /* connection function */
1268   const struct tvec_env *env;           /* subsidiary environment */
1269   unsigned dflt_reconn;                 /* default reconnection */
1270 };
1271
1272 struct tvec_remoteenv {
1273   struct tvec_env _env;
1274   struct tvec_remoteenv_slots r;
1275 };
1276
1277 struct tvec_remotefork_slots {
1278   const struct tvec_test *tests;        /* child tests (or null) */
1279 };
1280
1281 struct tvec_remotefork {
1282   struct tvec_env _env;
1283   struct tvec_remoteenv_slots r;
1284   struct tvec_remotefork_slots f;
1285 };
1286
1287 struct tvec_remoteexec_slots {
1288   const char *const *args;              /* command line to execute */
1289 };
1290
1291 struct tvec_remoteexec {
1292   struct tvec_env _env;
1293   struct tvec_remoteenv_slots r;
1294   struct tvec_remoteexec_slots x;
1295 };
1296
1297 union tvec_remoteenv_subclass_kludge {
1298   struct tvec_env _env;
1299   struct tvec_remoteenv renv;
1300   struct tvec_remotefork fork;
1301   struct tvec_remoteexec exec;
1302 };
1303
1304 /* Exit status.
1305  *
1306  * We don't use the conventional encoding returned by the @wait@(2) family of
1307  * system calls because it's too hard for our flags type to decode.  Instead,
1308  * we use our own encoding.
1309  *
1310  * The exit code or signal number ends up in the `value' field in the low 12
1311  * bits; bit 12 is set if the value field holds a signal, and it if holds an
1312  * exit code.  Bits 13--15 hold a code which describes the status of a child
1313  * process or connection.
1314  */
1315 #define TVXF_VALMASK 0x0fffu            /* value (exit code or signal) */
1316 #define TVXF_SIG 0x1000u                /* value is signal, not exit code */
1317 #define TVXF_CAUSEMASK 0xe000u          /* mask for cause bits */
1318 #define TVXST_RUN 0x0000u               /*   still running */
1319 #define TVXST_EXIT 0x2000u              /*   child exited */
1320 #define TVXST_KILL 0x4000u              /*   child killed by signal */
1321 #define TVXST_CONT 0x6000u              /*   child continued (?) */
1322 #define TVXST_STOP 0x8000u              /*   child stopped (?) */
1323 #define TVXST_DISCONN 0xa000u           /*   disconnected */
1324 #define TVXST_UNK 0xc000u               /*   unknown */
1325 #define TVXST_ERR 0xe000u             /*   local error prevented diagnosis */
1326
1327 /* Remote environment. */
1328 extern tvec_envsetupfn tvec_remotesetup;
1329 extern tvec_envfindvarfn tvec_remotefindvar;
1330 extern tvec_envbeforefn tvec_remotebefore;
1331 extern tvec_envrunfn tvec_remoterun;
1332 extern tvec_envafterfn tvec_remoteafter;
1333 extern tvec_envteardownfn tvec_remoteteardown;
1334 #define TVEC_REMOTEENV                                                  \
1335   { sizeof(struct tvec_remotectx),                                      \
1336     tvec_remotesetup,                                                   \
1337     tvec_remotefindvar,                                                 \
1338     tvec_remotebefore,                                                  \
1339     tvec_remoterun,                                                     \
1340     tvec_remoteafter,                                                   \
1341     tvec_remoteteardown }
1342
1343 /* --- @tvec_setprogress@, @tvec_setprogress_v@ --- *
1344  *
1345  * Arguments:   @const char *status@ = progress status token format
1346  *              @va_list ap@ = argument tail
1347  *
1348  * Returns:     ---
1349  *
1350  * Use:         Reports the progress of a test execution to the client.
1351  *
1352  *              The framework makes use of tokens beginning with %|%|%:
1353  *
1354  *                * %|%IDLE|%: during the top-level server code;
1355  *
1356  *                * %|%SETUP|%: during the enclosing environment's @before@
1357  *                  function;
1358  *
1359  *                * %|%RUN|%: during the environment's @run@ function, or the
1360  *                  test function; and
1361  *
1362  *                * %|%DONE|%: during the enclosing environment's @after@
1363  *                  function.
1364  *
1365  *              The intent is that a test can use the progress token to check
1366  *              that a function which is expected to crash does so at the
1367  *              correct point, so it's expected that more complex test
1368  *              functions and/or environments will set their own progress
1369  *              tokens to reflect what's going on.
1370  */
1371
1372 extern PRINTF_LIKE(1, 2) int tvec_setprogress(const char */*status*/, ...);
1373 extern int tvec_setprogress_v(const char */*status*/, va_list */*ap*/);
1374
1375 /* --- @tvec_remoteserver@ --- *
1376  *
1377  * Arguments:   @int infd@, @int outfd@ = input and output file descriptors
1378  *              @const struct tvec_config *config@ = test configuration
1379  *
1380  * Returns:     Suggested exit code.
1381  *
1382  * Use:         Run a test server, reading packets from @infd@ and writing
1383  *              responses and notifications to @outfd@, and invoking tests as
1384  *              described by @config@.
1385  *
1386  *              This function is not particularly general purpose.  It
1387  *              expects to `take over' the process, and makes use of private
1388  *              global variables.
1389  */
1390
1391 extern int tvec_remoteserver(int /*infd*/, int /*outfd*/,
1392                              const struct tvec_config */*config*/);
1393
1394 extern tvec_connectfn tvec_fork, tvec_exec;
1395
1396 #define TVEC_REMOTEFORK(subenv, tests)                                  \
1397         TVEC_REMOTEENV, { tvec_fork, subenv }, { tests }
1398
1399 #define TVEC_REMOTEEXEC(subenv, args)                                   \
1400         TVEC_REMOTEENV, { tvec_exec, subenv }, { args }
1401
1402 /*----- Timeouts ----------------------------------------------------------*/
1403
1404 struct tvec_timeoutenv {
1405   struct tvec_env _env;
1406   int timer;                            /* the timer (@ITIMER_...@) */
1407   double t;                             /* time to wait (in seconds) */
1408   const struct tvec_env *env;           /* subsidiary environment */
1409 };
1410
1411 struct tvec_timeoutctx {
1412   const struct tvec_timeoutenv *te;     /* saved environment description */
1413   int timer;                            /* timer code (as overridden) */
1414   double t;                             /* time to wait (as overridden) */
1415   unsigned f;                           /* flags */
1416 #define TVTF_SETTMO 1u                  /*   set `@timeout' */
1417 #define TVTF_SETTMR 2u                  /*   set `@timer' */
1418 #define TVTF_SETMASK (TVTF_SETTMO | TVTF_SETTMR)
1419                                         /*   mask of @TVTF_SET...@ */
1420   void *subctx;
1421 };
1422
1423 extern tvec_envsetupfn tvec_timeoutsetup;
1424 extern tvec_envfindvarfn tvec_timeoutfindvar;
1425 extern tvec_envbeforefn tvec_timeoutbefore;
1426 extern tvec_envrunfn tvec_timeoutrun;
1427 extern tvec_envafterfn tvec_timeoutafter;
1428 extern tvec_envteardownfn tvec_timeoutteardown;
1429 #define TVEC_TIMEOUTENV                                                 \
1430         { sizeof(struct tvec_timeoutctx),                               \
1431           tvec_timeoutsetup,                                            \
1432           tvec_timeoutfindvar,                                          \
1433           tvec_timeoutbefore,                                           \
1434           tvec_timeoutrun,                                              \
1435           tvec_timeoutafter,                                            \
1436           tvec_timeoutteardown }
1437 #define TVEC_TIMEOUTINIT(timer, t) TVEC_TIMEOUTENV, timer, t
1438
1439 /*----- Output functions --------------------------------------------------*/
1440
1441 /* --- @tvec_strlevel@ --- *
1442  *
1443  * Arguments:   @unsigned level@ = level code
1444  *
1445  * Returns:     A human-readable description.
1446  *
1447  * Use:         Converts a level code into something that you can print in a
1448  *              message.
1449  */
1450
1451 extern const char *tvec_strlevel(unsigned /*level*/);
1452
1453 /* --- @tvec_report@, @tvec_report_v@ --- *
1454  *
1455  * Arguments:   @struct tvec_state *tv@ = test-vector state
1456  *              @const char *msg@, @va_list ap@ = error message
1457  *
1458  * Returns:     ---
1459  *
1460  * Use:         Report an message with a given severity.  Messages with level
1461  *              @TVLEV_ERR@ or higher force a nonzero exit code.
1462  */
1463
1464 extern PRINTF_LIKE(3, 4)
1465   void tvec_report(struct tvec_state */*tv*/, unsigned /*level*/,
1466                    const char */*msg*/, ...);
1467 extern void tvec_report_v(struct tvec_state */*tv*/, unsigned /*level*/,
1468                           const char */*msg*/, va_list */*ap*/);
1469
1470 /* --- @tvec_error@, @tvec_notice@ --- *
1471  *
1472  * Arguments:   @struct tvec_state *tv@ = test-vector state
1473  *              @const char *msg@, @va_list ap@ = error message
1474  *
1475  * Returns:     The @tvec_error@ function returns %$-1$% as a trivial
1476  *              convenience; @tvec_notice@ does not return a value.
1477  *
1478  * Use:         Report an error or a notice.  Errors are distinct from test
1479  *              failures, and indicate that a problem was encountered which
1480  *              compromised the activity of testing.  Notices are important
1481  *              information which doesn't fit into any other obvious
1482  *              category.
1483  */
1484
1485 extern PRINTF_LIKE(2, 3)
1486   int tvec_error(struct tvec_state */*tv*/, const char */*msg*/, ...);
1487 extern PRINTF_LIKE(2, 3)
1488   void tvec_notice(struct tvec_state */*tv*/, const char */*msg*/, ...);
1489
1490 /* --- @tvec_unkreg@ --- *
1491  *
1492  * Arguments:   @struct tvec_state *tv@ = test-vector state
1493  *              @const char *name@ = register or pseudoregister name
1494  *
1495  * Returns:     %$-1$%.
1496  *
1497  * Use:         Reports an error that the register or pseudoregister is
1498  *              unrecognized.
1499  */
1500
1501 extern int tvec_unkreg(struct tvec_state */*tv*/, const char */*name*/);
1502
1503 /* --- @tvec_dupreg@ --- *
1504  *
1505  * Arguments:   @struct tvec_state *tv@ = test-vector state
1506  *              @const char *name@ = register or pseudoregister name
1507  *
1508  * Returns:     %$-1$%.
1509  *
1510  * Use:         Reports an error that the register or pseudoregister has been
1511  *              assigned already in the current test.
1512  */
1513
1514 extern int tvec_dupreg(struct tvec_state */*tv*/, const char */*name*/);
1515
1516 /* --- @tvec_humanoutput@ --- *
1517  *
1518  * Arguments:   @FILE *fp@ = output file to write on
1519  *
1520  * Returns:     An output formatter.
1521  *
1522  * Use:         Return an output formatter which writes on @fp@ with the
1523  *              expectation that a human will be watching and interpreting
1524  *              the output.  If @fp@ denotes a terminal, the display shows a
1525  *              `scoreboard' indicating the outcome of each test case
1526  *              attempted, and may in addition use colour and other
1527  *              highlighting.
1528  */
1529
1530 extern struct tvec_output *tvec_humanoutput(FILE */*fp*/);
1531
1532 /* --- @tvec_tapoutput@ --- *
1533  *
1534  * Arguments:   @FILE *fp@ = output file to write on
1535  *
1536  * Returns:     An output formatter.
1537  *
1538  * Use:         Return an output formatter which writes on @fp@ in `TAP'
1539  *              (`Test Anything Protocol') format.
1540  *
1541  *              TAP comes from the Perl community, but has spread rather
1542  *              further.  This driver produces TAP version 14, but pretends
1543  *              to be version 13.  The driver produces a TAP `test point' --
1544  *              i.e., a result reported as `ok' or `not ok' -- for each input
1545  *              test group.  Failure reports and register dumps are produced
1546  *              as diagnostic messages before the final group result.  (TAP
1547  *              permits structuerd YAML data after the test-point result,
1548  *              which could be used to report details, but (a) postponing the
1549  *              details until after the report is inconvenient, and (b) there
1550  *              is no standardization for the YAML anyway, so in practice
1551  *              it's no more useful than the unstructured diagnostics.
1552  */
1553
1554 extern struct tvec_output *tvec_tapoutput(FILE */*fp*/);
1555
1556 /* --- @tvec_dfltoutput@ --- *
1557  *
1558  * Arguments:   @FILE *fp@ = output file to write on
1559  *
1560  * Returns:     An output formatter.
1561  *
1562  * Use:         Selects and instantiates an output formatter suitable for
1563  *              writing on @fp@.  The policy is subject to change, but
1564  *              currently the `human' output format is selected if @fp@ is
1565  *              interactive (i.e., if @isatty(fileno(fp))@ is true), and
1566  *              otherwise the `tap' format is used.
1567  */
1568
1569 extern struct tvec_output *tvec_dfltout(FILE */*fp*/);
1570
1571 /*------ Serialization utilities ------------------------------------------*/
1572
1573 /* Serialization format.
1574  *
1575  * The `candidate register definitions' are those entries @r@ in the @regs@
1576  * vector whose index @r.i@ is strictly less than @nr@.  The `selected
1577  * register definitions' are those candidate register definitions @r@ for
1578  * which the indicated register @rv[r.i]@ has the @TVRF_LIVE@ flag set.  The
1579  * serialized output begins with a header bitmap: if there are %$n$%
1580  * candidate register definitions then the header bitmap consists of %$\lceil
1581  * n/8 \rceil$% bytes.  Bits are ordered starting from the least significant
1582  * bit of the first byte, end ending at the most significant bit of the final
1583  * byte.  The bit corresponding to a candidate register definition is set if
1584  * and only if that register defintion is selected.  The header bitmap is
1585  * then followed by the serializations of the selected registers -- i.e., for
1586  * each selected register definition @r@, the serialized value of register
1587  * @rv[r.i]@ -- simply concatenated together, with no padding or alignment.
1588  */
1589
1590 /* --- @tvec_serialize@ --- *
1591  *
1592  * Arguments:   @const struct tvec_reg *rv@ = vector of registers
1593  *              @buf *b@ = buffer to write on
1594  *              @const struct tvec_regdef *regs@ = vector of register
1595  *                      descriptions, terminated by an entry with a null
1596  *                      @name@ slot
1597  *              @unsigned nr@ = number of entries in the @rv@ vector
1598  *              @size_t regsz@ = true size of each element of @rv@
1599  *
1600  * Returns:     Zero on success, %$-1$% on failure.
1601  *
1602  * Use:         Serialize a collection of register values.
1603  *
1604  *              The serialized output is written to the buffer @b@.  Failure
1605  *              can be caused by running out of buffer space, or a failing
1606  *              type handler.
1607  */
1608
1609 extern int tvec_serialize(const struct tvec_reg */*rv*/, buf */*b*/,
1610                           const struct tvec_regdef */*regs*/,
1611                           unsigned /*nr*/, size_t /*regsz*/);
1612
1613 /* --- @tvec_deserialize@ --- *
1614  *
1615  * Arguments:   @struct tvec_reg *rv@ = vector of registers
1616  *              @buf *b@ = buffer to write on
1617  *              @const struct tvec_regdef *regs@ = vector of register
1618  *                      descriptions, terminated by an entry with a null
1619  *                      @name@ slot
1620  *              @unsigned nr@ = number of entries in the @rv@ vector
1621  *              @size_t regsz@ = true size of each element of @rv@
1622  *
1623  * Returns:     Zero on success, %$-1$% on failure.
1624  *
1625  * Use:         Deserialize a collection of register values.
1626  *
1627  *              The size of the register vector @nr@ and the register
1628  *              definitions @regs@ must match those used when producing the
1629  *              serialization.  For each serialized register value,
1630  *              deserialize and store the value into the appropriate register
1631  *              slot, and set the @TVRF_LIVE@ flag on the register.  See
1632  *              @tvec_serialize@ for a description of the format.
1633  *
1634  *              Failure results only from a failing register type handler.
1635  */
1636
1637 extern int tvec_deserialize(struct tvec_reg */*rv*/, buf */*b*/,
1638                             const struct tvec_regdef */*regs*/,
1639                             unsigned /*nr*/, size_t /*regsz*/);
1640
1641 /*----- Input utilities ---------------------------------------------------*/
1642
1643 /* These are provided by the core for the benefit of type @parse@ methods,
1644  * and test-environment @set@ functions, which get to read from the test
1645  * input file.  The latter are usually best implemented by calling on the
1646  * former.
1647  *
1648  * The two main rules are as follows.
1649  *
1650  *   * Leave the file position at the beginning of the line following
1651  *     whatever it was that you read.
1652  *
1653  *   * When you read and consume a newline (which you do at least once, by
1654  *     the previous rule), then increment @tv->lno@ to keep track of the
1655  *     current line number.
1656  */
1657
1658 /* --- @tvec_syntax@, @tvec_syntax_v@ --- *
1659  *
1660  * Arguments:   @struct tvec_state *tv@ = test-vector state
1661  *              @int ch@ = the character found (in @fgetc@ format)
1662  *              @const char *expect@, @va_list ap@ = what was expected
1663  *
1664  * Returns:     %$-1$%.
1665  *
1666  * Use:         Report a syntax error quoting @ch@ and @expect@.  If @ch@ is
1667  *              a newline, then back up so that it can be read again (e.g.,
1668  *              by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
1669  *              advance the line number).
1670  */
1671
1672 extern PRINTF_LIKE(3, 4)
1673   int tvec_syntax(struct tvec_state */*tv*/, int /*ch*/,
1674                   const char */*expect*/, ...);
1675 extern int tvec_syntax_v(struct tvec_state */*tv*/, int /*ch*/,
1676                          const char */*expect*/, va_list */*ap*/);
1677
1678 /* --- @tvec_skipspc@ --- *
1679  *
1680  * Arguments:   @struct tvec_state *tv@ = test-vector state
1681  *
1682  * Returns:     ---
1683  *
1684  * Use:         Advance over any whitespace characters other than newlines.
1685  *              This will stop at `;', end-of-file, or any other kind of
1686  *              non-whitespace; and it won't consume a newline.
1687  */
1688
1689 extern void tvec_skipspc(struct tvec_state */*tv*/);
1690
1691 /* --- @tvec_flushtoeol@ --- *
1692  *
1693  * Arguments:   @struct tvec_state *tv@ = test-vector state
1694  *              @unsigned f@ = flags (@TVFF_...@)
1695  *
1696  * Returns:     Zero on success, %$-1$% on error.
1697  *
1698  * Use:         Advance to the start of the next line, consuming the
1699  *              preceding newline.
1700  *
1701  *              A syntax error is reported if no newline character is found,
1702  *              i.e., the file ends in mid-line.  A syntax error is also
1703  *              reported if material other than whitespace or a comment is
1704  *              found before the end of the line end, and @TVFF_ALLOWANY@ is
1705  *              not set in @f@.  The line number count is updated
1706  *              appropriately.
1707  */
1708
1709 #define TVFF_ALLOWANY 1u
1710 extern int tvec_flushtoeol(struct tvec_state */*tv*/, unsigned /*f*/);
1711
1712 /* --- @tvec_nexttoken@ --- *
1713  *
1714  * Arguments:   @struct tvec_state *tv@ = test-vector state
1715  *
1716  * Returns:     Zero if there is a next token which can be read; %$-1$% if no
1717  *              token is available.
1718  *
1719  * Use:         Advance to the next whitespace-separated token, which may be
1720  *              on the next line.
1721  *
1722  *              Tokens are separated by non-newline whitespace, comments, and
1723  *              newlines followed by whitespace; a newline /not/ followed by
1724  *              whitespace instead begins the next assignment, and two
1725  *              newlines separated only by whitespace terminate the data for
1726  *              a test.
1727  *
1728  *              If this function returns zero, then the next character in the
1729  *              file begins a suitable token which can be read and
1730  *              processed.  If it returns %$-1$% then there is no such token,
1731  *              and the file position is left correctly.  The line number
1732  *              count is updated appropriately.
1733  */
1734
1735 extern int tvec_nexttoken(struct tvec_state */*tv*/);
1736
1737 /* --- @tvec_readword@, @tvec_readword_v@ --- *
1738  *
1739  * Arguments:   @struct tvec_state *tv@ = test-vector state
1740  *              @dstr *d@ = string to append the word to
1741  *              @const char **p_inout@ = pointer into string, updated
1742  *              @const char *delims@ = additional delimiters to stop at
1743  *              @const char *expect@, @va_list ap@ = what was expected
1744  *
1745  * Returns:     Zero on success, %$-1$% on failure.
1746  *
1747  * Use:         A `word' consists of characters other than whitespace, null
1748  *              characters, and other than those listed in @delims@;
1749  *              furthermore, a word does not begin with a `;'.  (If you want
1750  *              reading to stop at comments not preceded by whitespace, then
1751  *              include `;' in @delims@.  This is a common behaviour.)
1752  *
1753  *              If there is no word beginning at the current file position,
1754  *              then return %$-1$%; furthermore, if @expect@ is not null,
1755  *              then report an appropriate error via @tvec_syntax@.
1756  *
1757  *              Otherwise, the word is accumulated in @d@ and zero is
1758  *              returned; if @d@ was not empty at the start of the call, the
1759  *              newly read word is separated from the existing material by a
1760  *              single space character.  Since null bytes are never valid
1761  *              word constituents, a null terminator is written to @d@, and
1762  *              it is safe to treat the string in @d@ as being null-
1763  *              terminated.
1764  *
1765  *              If @p_inout@ is not null, then @*p_inout@ must be a pointer
1766  *              into @d->buf@, which will be adjusted so that it will
1767  *              continue to point at the same position even if the buffer is
1768  *              reallocated.  As a subtle tweak, if @*p_inout@ initially
1769  *              points at the end of the buffer, then it will be adjusted to
1770  *              point at the beginning of the next word, rather than at the
1771  *              additional intervening space.
1772  */
1773
1774 extern PRINTF_LIKE(5, 6)
1775   int tvec_readword(struct tvec_state */*tv*/, dstr */*d*/,
1776                     const char **/*p_inout*/, const char */*delims*/,
1777                     const char */*expect*/, ...);
1778 extern int tvec_readword_v(struct tvec_state */*tv*/, dstr */*d*/,
1779                            const char **/*p_inout*/, const char */*delims*/,
1780                            const char */*expect*/, va_list */*ap*/);
1781
1782 /*----- Integer types: signed and unsigned --------------------------------*/
1783
1784 /* Integers may be input in decimal, hex, binary, or octal, following
1785  * approximately usual conventions.
1786  *
1787  *   * Signed integers may be preceded with a `+' or `-' sign.
1788  *
1789  *   * Decimal integers are just a sequence of decimal digits `0' ... `9'.
1790  *
1791  *   * Octal integers are a sequence of digits `0' ... `7', preceded by `0o'
1792  *     or `0O'.
1793  *
1794  *   * Hexadecimal integers are a sequence of digits `0' ... `9', `a'
1795  *     ... `f', or `A' ... `F', preceded by `0x' or `0X'.
1796  *
1797  *   * Radix-B integers are a sequence of digits `0' ... `9', `a' ... `f', or
1798  *     `A' ... `F', each with value less than B, preceded by `Br' or `BR',
1799  *     where 0 < B < 36 is expressed in decimal without any leading `0' or
1800  *     internal underscores `_'.
1801  *
1802  *   * A digit sequence may contain internal underscore `_' separators, but
1803  *     not before or after all of the digits; and two consecutive `_'
1804  *     characters are not permitted.
1805  */
1806
1807 extern const struct tvec_regty tvty_int, tvty_uint;
1808
1809 /* The @arg.p@ slot may be null or a pointer to @struct tvec_irange@ or
1810  * @struct tvec_urange@ as appropriate.  The bounds are inclusive; use, e.g.,
1811  * @LONG_MAX@ explicitly if one or the other bound is logically inapplicable.
1812  */
1813 struct tvec_irange { long min, max; };
1814 struct tvec_urange { unsigned long min, max; };
1815
1816 /* Bounds corresponding to common integer types. */
1817 extern const struct tvec_irange
1818   tvrange_schar, tvrange_short, tvrange_int, tvrange_long,
1819   tvrange_sbyte, tvrange_i16, tvrange_i32;
1820 extern const struct tvec_urange
1821   tvrange_uchar, tvrange_ushort, tvrange_uint, tvrange_ulong, tvrange_size,
1822   tvrange_byte, tvrange_u16, tvrange_u32;
1823
1824 /* --- @tvec_claimeq_int@, @TVEC_CLAIMEQ_INT@ --- *
1825  *
1826  * Arguments:   @struct tvec_state *tv@ = test-vector state
1827  *              @long i0, i1@ = two signed integers
1828  *              @const char *file@, @unsigned @lno@ = calling file and line
1829  *              @const char *expr@ = the expression to quote on failure
1830  *
1831  * Returns:     Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1832  *
1833  * Use:         Check that values of @i0@ and @i1@ are equal.  As for
1834  *              @tvec_claim@ above, a test case is automatically begun and
1835  *              ended if none is already underway.  If the values are
1836  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1837  *              mismatched values are dumped: @i0@ is printed as the output
1838  *              value and @i1@ is printed as the input reference.
1839  *
1840  *              The @TVEC_CLAIM_INT@ macro is similar, only it (a) identifies
1841  *              the file and line number of the call site automatically, and
1842  *              (b) implicitly quotes the source text of the @i0@ and @i1@
1843  *              arguments in the failure message.
1844  */
1845
1846 extern int tvec_claimeq_int(struct tvec_state */*tv*/,
1847                             long /*i0*/, long /*i1*/,
1848                             const char */*file*/, unsigned /*lno*/,
1849                             const char */*expr*/);
1850 #define TVEC_CLAIMEQ_INT(tv, i0, i1)                                    \
1851         (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1))
1852
1853 /* --- @tvec_claimeq_uint@, @TVEC_CLAIMEQ_UINT@ --- *
1854  *
1855  * Arguments:   @struct tvec_state *tv@ = test-vector state
1856  *              @unsigned long u0, u1@ = two unsigned integers
1857  *              @const char *file@, @unsigned @lno@ = calling file and line
1858  *              @const char *expr@ = the expression to quote on failure
1859  *
1860  * Returns:     Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1861  *
1862  * Use:         Check that values of @u0@ and @u1@ are equal.  As for
1863  *              @tvec_claim@ above, a test case is automatically begun and
1864  *              ended if none is already underway.  If the values are
1865  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1866  *              mismatched values are dumped: @u0@ is printed as the output
1867  *              value and @u1@ is printed as the input reference.
1868  *
1869  *              The @TVEC_CLAIM_UINT@ macro is similar, only it (a)
1870  *              identifies the file and line number of the call site
1871  *              automatically, and (b) implicitly quotes the source text of
1872  *              the @u0@ and @u1@ arguments in the failure message.
1873  */
1874
1875 extern int tvec_claimeq_uint(struct tvec_state */*tv*/,
1876                             unsigned long /*u0*/, unsigned long /*u1*/,
1877                             const char */*file*/, unsigned /*lno*/,
1878                             const char */*expr*/);
1879 #define TVEC_CLAIMEQ_UINT(tv, u0, u1)                                   \
1880         (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1))
1881
1882 /*----- Floating-point type -----------------------------------------------*/
1883
1884 /* Floating-point values are either NaN (%|#nan|%, if supported by the
1885  * platform); positive or negative infinity (%|#inf|%, %|+#inf|%, or
1886  * %|#+inf|% (preferring the last), and %|-#inf|% or %|#-inf|% (preferring
1887  * the latter), if supported by the platform); or a number in strtod(3)
1888  * syntax.
1889  *
1890  * The comparison rules for floating-point numbers are complex: see
1891  * @tvec_claimeqish_float@ for details.
1892  */
1893
1894 extern const struct tvec_regty tvty_float;
1895
1896 struct tvec_floatinfo {
1897   /* Details about acceptable floating-point values. */
1898
1899   unsigned f;                           /* flags (@TVFF_...@ bits) */
1900 #define TVFF_NOMIN 1u                   /*   ignore @min@ (allow -inf) */
1901 #define TVFF_NOMAX 2u                   /*   ignore @max@ (allow +inf) */
1902 #define TVFF_NANOK 4u                   /*   permit NaN */
1903 #define TVFF_EQMASK 0xf0                /*   how to compare */
1904 #define TVFF_EXACT 0x00                 /*     must equal exactly */
1905 #define TVFF_ABSDELTA 0x10              /*     must be within @delta@ */
1906 #define TVFF_RELDELTA 0x20              /*     diff < @delta@ fraction */
1907   double min, max;                      /* smallest/largest value allowed */
1908   double delta;                         /* maximum tolerable difference */
1909 };
1910
1911 extern const struct tvec_floatinfo tvflt_finite, tvflt_nonneg;
1912
1913 /* --- @tvec_claimeqish_float@, @TVEC_CLAIMEQISH_FLOAT@ --- *
1914  *
1915  * Arguments:   @struct tvec_state *tv@ = test-vector state
1916  *              @double f0, f1@ = two floating-point numbers
1917  *              @unsigned f@ = flags (@TVFF_...@)
1918  *              @double delta@ = maximum tolerable difference
1919  *              @const char *file@, @unsigned @lno@ = calling file and line
1920  *              @const char *expr@ = the expression to quote on failure
1921  *
1922  * Returns:     Nonzero if @f0@ and @u1@ are sufficiently close, otherwise
1923  *              zero.
1924  *
1925  * Use:         Check that values of @f0@ and @f1@ are sufficiently close.
1926  *              As for @tvec_claim@ above, a test case is automatically begun
1927  *              and ended if none is already underway.  If the values are
1928  *              too far apart, then @tvec_fail@ is called, quoting @expr@,
1929  *              and the mismatched values are dumped: @f0@ is printed as the
1930  *              output value and @f1@ is printed as the input reference.
1931  *
1932  *              The details for the comparison are as follows.
1933  *
1934  *                * A NaN value matches any other NaN, and nothing else.
1935  *
1936  *                * An infinity matches another infinity of the same sign,
1937  *                  and nothing else.
1938  *
1939  *                * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
1940  *                  representable number matches only itself: in particular,
1941  *                  positive and negative zero are considered distinct.
1942  *                  (This allows tests to check that they land on the correct
1943  *                  side of branch cuts, for example.)
1944  *
1945  *                * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
1946  *                  %$y$% when %$|x - y| < \delta$%.
1947  *
1948  *                * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
1949  *                  %$y$% when %$|1 - y/x| < \delta$%.  (Note that this
1950  *                  criterion is asymmetric FIXME
1951  *
1952  *              The @TVEC_CLAIM_FLOAT@ macro is similar, only it (a)
1953  *              identifies the file and line number of the call site
1954  *              automatically, and (b) implicitly quotes the source text of
1955  *              the @f0@ and @f1@ arguments (and @delta@) in the failure
1956  *              message.
1957  */
1958
1959 extern int tvec_claimeqish_float(struct tvec_state */*tv*/,
1960                                  double /*f0*/, double /*f1*/,
1961                                  unsigned /*f*/, double /*delta*/,
1962                                  const char */*file*/, unsigned /*lno*/,
1963                                  const char */*expr*/);
1964 #define TVEC_CLAIMEQISH_FLOAT(tv, f0, f1, f, delta)                     \
1965         (tvec_claimeqish_float(tv, f0, f1, f, delta, , __FILE__, __LINE__, \
1966                                #f0 " /= " #f1 " (+/- " #delta ")"))
1967
1968 /* --- @tvec_claimeq_float@, @TVEC_CLAIMEQ_FLOAT@ --- *
1969  *
1970  * Arguments:   @struct tvec_state *tv@ = test-vector state
1971  *              @double f0, f1@ = two floating-point numbers
1972  *              @const char *file@, @unsigned @lno@ = calling file and line
1973  *              @const char *expr@ = the expression to quote on failure
1974  *
1975  * Returns:     Nonzero if @f0@ and @u1@ are identical, otherwise zero.
1976  *
1977  * Use:         Check that values of @f0@ and @f1@ are identical.  The
1978  *              function is exactly equivalent to @tvec_claimeqish_float@
1979  *              with @f == TVFF_EXACT@; the macro is similarly like
1980  *              @TVEC_CLAIMEQISH_FLOAT@ with @f == TVFF_EXACT@, except that
1981  *              it doesn't bother to quote a delta.
1982  */
1983
1984 extern int tvec_claimeq_float(struct tvec_state */*tv*/,
1985                               double /*f0*/, double /*f1*/,
1986                               const char */*file*/, unsigned /*lno*/,
1987                               const char */*expr*/);
1988 #define TVEC_CLAIMEQ_FLOAT(tv, f0, f1)                                  \
1989         (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1))
1990
1991 /*----- Durations ---------------------------------------------------------*/
1992
1993 /* A duration measures a time interval in seconds.  The input format consists
1994  * of a nonnegative decimal floating-point number in @strtod@ format followed
1995  * by an optional unit specification.
1996  *
1997  * No @tvec_claimeq_...@ function is provided for durations: use
1998  * @tvec_claimeq_float@.
1999  */
2000
2001 extern const struct tvec_regty tvty_duration;
2002
2003 /* --- @tvec_parsedurunit@ --- *
2004  *
2005  * Arguments:   @double *scale_out@ = where to leave the scale
2006  *              @const char **p_inout@ = input unit string, updated
2007  *
2008  * Returns:     Zero on success, %$-1$% on error.
2009  *
2010  * Use:         If @*p_inout@ begins with a unit string followed by the end
2011  *              of the string or some non-alphanumeric character, then store
2012  *              the corresponding scale factor in @*scale_out@, advance
2013  *              @*p_inout@ past the unit string, and return zero.  Otherwise,
2014  *              return %$-1$%.
2015  */
2016
2017 extern int tvec_parsedurunit(double */*scale_out*/,
2018                              const char **/*p_inout*/);
2019
2020 /*----- Enumerated types --------------------------------------------------*/
2021
2022 /* An enumeration describes a set of values of some underlying type, each of
2023  * which has a symbolic name.  Values outside of the defined set can occur --
2024  * on output, because of bugs in the tested code, or on input to test
2025  * handling of unexpected values.
2026  *
2027  * There is a distinct enumerated type for each of the branches of
2028  * @tvec_misc@.  In the following, we write @t@ for the type code, which is
2029  * @i@ for signed integer, @u@ for unsigned integer, @f@ for floating-point,
2030  * and @p@ for pointer.
2031  *
2032  * On input, an enumerated value may be given by name or as a literal value.
2033  * For enumerations based on numeric types, the literal values can be written
2034  * in the same syntax as the underlying values.  For enumerations based on
2035  * pointers, the only permitted literal is %|#nil|%, which denotes a null
2036  * pointer.  On output, names are preferred (with the underlying value given
2037  * in a comment).
2038  */
2039
2040 #define DEFENUMTY(tag, ty, slot)                                        \
2041         extern const struct tvec_regty tvty_##slot##enum;
2042 TVEC_MISCSLOTS(DEFENUMTY)
2043 #undef DEFENUMTY
2044
2045 /* A @struct tvec_tassoc@ associates a string tag with a value. */
2046 #define DEFASSOC(tag_, ty, slot)                                        \
2047         struct tvec_##slot##assoc { const char *tag; ty slot; };
2048 TVEC_MISCSLOTS(DEFASSOC)
2049 #undef DEFASSOC
2050
2051 #define TVEC_ENDENUM { 0, 0 }
2052
2053 /* Information about an enumerated type. */
2054 #define DEFINFO(tag, ty, slot)                                          \
2055         struct tvec_##slot##enuminfo {                                  \
2056           const char *name;             /* type name for diagnostics */ \
2057           const struct tvec_##slot##assoc *av; /* name/value mappings */ \
2058           EXTRA_##tag##_INFOSLOTS       /* type-specific extra info */  \
2059         };
2060
2061 #define EXTRA_INT_INFOSLOTS                                             \
2062         const struct tvec_irange *ir;   /* allowed range of raw values */
2063
2064 #define EXTRA_UINT_INFOSLOTS                                            \
2065         const struct tvec_urange *ur;   /* allowed range of raw values */
2066
2067 #define EXTRA_FLT_INFOSLOTS                                             \
2068         const struct tvec_floatinfo *fi; /* range and matching policy */
2069
2070 #define EXTRA_PTR_INFOSLOTS             /* (nothing) */
2071
2072 TVEC_MISCSLOTS(DEFINFO)
2073
2074 #undef EXTRA_INT_INFOSLOTS
2075 #undef EXTRA_UINT_INFOSLOTS
2076 #undef EXTRA_FLT_INFOSLOTS
2077 #undef EXTRA_PTR_INFOSLOTS
2078
2079 #undef DEFINFO
2080
2081 /* Standard enumerations. */
2082 extern const struct tvec_ienuminfo tvenum_bool;
2083 extern const struct tvec_ienuminfo tvenum_cmp;
2084
2085 /* --- @tvec_claimeq_tenum@, @TVEC_CLAIMEQ_TENUM@ --- *
2086  *
2087  * Arguments:   @struct tvec_state *tv@ = test-vector state
2088  *              @const struct tvec_typeenuminfo *ei@ = enumeration type info
2089  *              @ty t0, t1@ = two values
2090  *              @const char *file@, @unsigned @lno@ = calling file and line
2091  *              @const char *expr@ = the expression to quote on failure
2092  *
2093  * Returns:     Nonzero if @t0@ and @t1@ are equal, otherwise zero.
2094  *
2095  * Use:         Check that values of @t0@ and @t1@ are equal.  As for
2096  *              @tvec_claim@ above, a test case is automatically begun and
2097  *              ended if none is already underway.  If the values are
2098  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
2099  *              mismatched values are dumped: @t0@ is printed as the output
2100  *              value and @t1@ is printed as the input reference.
2101  *
2102  *              The @TVEC_CLAIM_TENUM@ macro is similar, only it (a)
2103  *              identifies the file and line number of the call site
2104  *              automatically, and (b) implicitly quotes the source text of
2105  *              the @t0@ and @t1@ arguments in the failure message.
2106  */
2107
2108 #define DECLCLAIM(tag, ty, slot)                                        \
2109         extern int tvec_claimeq_##slot##enum                            \
2110           (struct tvec_state */*tv*/,                                   \
2111            const struct tvec_##slot##enuminfo */*ei*/,                  \
2112            ty /*t0*/, ty /*t1*/,                                        \
2113            const char */*file*/, unsigned /*lno*/, const char */*expr*/);
2114 TVEC_MISCSLOTS(DECLCLAIM)
2115 #undef DECLCLAIM
2116 #define TVEC_CLAIMEQ_IENUM(tv, ei, i0, i1)                              \
2117         (tvec_claimeq_ienum(tv, ei, i0, i1,                             \
2118                             __FILE__, __LINE__, #i0 " /= " #i1))
2119 #define TVEC_CLAIMEQ_UENUM(tv, ei, u0, u1)                              \
2120         (tvec_claimeq_uenum(tv, ei, u0, u1,                             \
2121                             __FILE__, __LINE__, #u0 " /= " #u1))
2122 #define TVEC_CLAIMEQ_FENUM(tv, ei, f0, f1)                              \
2123         (tvec_claimeq_fenum(tv, ei, f0, f1,                             \
2124                             __FILE__, __LINE__, #f0 " /= " #f1))
2125 #define TVEC_CLAIMEQ_PENUM(tv, ei, p0, p1)                              \
2126         (tvec_claimeq_penum(tv, ei, p0, p1,                             \
2127                             __FILE__, __LINE__, #p0 " /= " #p1))
2128
2129 /*----- Flags type --------------------------------------------------------*/
2130
2131 /* A flags value packs a number of fields into a single nonnegative integer.
2132  * Symbolic names are associated with the possible values of the various
2133  * fields; more precisely, each name is associated with a value and a
2134  * covering bitmask.
2135  *
2136  * The input syntax is a sequence of items separated by `%|||%' signs.  Each
2137  * item may be the symbolic name of a field value, or a literal unsigned
2138  * integer.  The masks associated with the given symbolic names must be
2139  * disjoint.  The resulting numerical value is simply the bitwise OR of the
2140  * given values.
2141  *
2142  * On output, the table of symbolic names and their associated values and
2143  * masks is repeatedly scanned, in order, to find disjoint matches -- i.e.,
2144  * entries whose value matches the target value in the bit positions
2145  * indicated by the mask, and whose mask doesn't overlap with any previously
2146  * found matches; the names are then output, separated by `%|||%'.  Any
2147  * remaining nonzero bits not covered by any of the matching masks are output
2148  * as a single literal integer, in hex.
2149  */
2150
2151 extern const struct tvec_regty tvty_flags;
2152
2153 struct tvec_flag {
2154   /* Definition of a single flag or bitfield value.
2155    *
2156    * Each named setting comes with a value @v@ and a mask @m@; the mask
2157    * should cover all of the value bits, i.e., @(v&~m) == 0@.
2158    */
2159
2160   const char *tag;                      /* name */
2161   unsigned long m, v;                   /* mask and value */
2162 };
2163
2164 #define TVEC_ENDFLAGS { 0, 0, 0 }
2165
2166 struct tvec_flaginfo {
2167   /* Information about a flags type. */
2168
2169   const char *name;                     /* type name for diagnostics  */
2170   const struct tvec_flag *fv;           /* name/mask/value mappings */
2171   const struct tvec_urange *range;      /* permitted range for literals */
2172 };
2173
2174 /* --- @tvec_claimeq_flags@, @TVEC_CLAIMEQ_FLAGS@ --- *
2175  *
2176  * Arguments:   @struct tvec_state *tv@ = test-vector state
2177  *              @const struct tvec_flaginfo *fi@ = flags type info
2178  *              @unsigned long f0, f1@ = two values
2179  *              @const char *file@, @unsigned @lno@ = calling file and line
2180  *              @const char *expr@ = the expression to quote on failure
2181  *
2182  * Returns:     Nonzero if @f0@ and @f1@ are equal, otherwise zero.
2183  *
2184  * Use:         Check that values of @f0@ and @f1@ are equal.  As for
2185  *              @tvec_claim@ above, a test case is automatically begun and
2186  *              ended if none is already underway.  If the values are
2187  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
2188  *              mismatched values are dumped: @f0@ is printed as the output
2189  *              value and @f1@ is printed as the input reference.
2190  *
2191  *              The @TVEC_CLAIM_FLAGS@ macro is similar, only it (a)
2192  *              identifies the file and line number of the call site
2193  *              automatically, and (b) implicitly quotes the source text of
2194  *              the @f0@ and @f1@ arguments in the failure message.
2195  */
2196
2197 extern int tvec_claimeq_flags(struct tvec_state */*tv*/,
2198                               const struct tvec_flaginfo */*fi*/,
2199                               unsigned long /*f0*/, unsigned long /*f1*/,
2200                               const char */*file*/, unsigned /*lno*/,
2201                               const char */*expr*/);
2202 #define TVEC_CLAIMEQ_FLAGS(tv, fi, f0, f1)                              \
2203         (tvec_claimeq_flags(tv, fi, f0, f1,                             \
2204                             __FILE__, __LINE__, #f0 " /= " #f1))
2205
2206 /*----- Character type ----------------------------------------------------*/
2207
2208 /* A character value holds a character, as read by @fgetc@.  The special
2209  * @EOF@ value can also be represented.
2210  *
2211  * On input, a character value can be given by symbolic name, with a leading
2212  * `%|#|%'; or a character or `%|\|%'-escape sequence, optionally in single
2213  * quotes.
2214  *
2215  * The following escape sequences and character names are recognized.
2216  *
2217  *   * `%|#eof|%' is the special end-of-file marker.
2218  *
2219  *   * `%|#nul|%' is the NUL character, sometimes used to terminate strings.
2220  *
2221  *   * `%|bell|%', `%|bel|%', `%|ding|%', or `%|\a|%' is the BEL character
2222  *     used to ring the terminal bell (or do some other thing to attract the
2223  *     user's attention).
2224  *
2225  *   * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace character, used to
2226  *     move the cursor backwords by one cell.
2227  *
2228  *   * %|#escape|% %|#esc|%, or%|\e|% is the escape character, used to
2229  *     introduce special terminal commands.
2230  *
2231  *   * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed character, used to
2232  *     separate pages of text.
2233  *
2234  *   * %|#newline|%, %|#linefeed|%, %|#lf|%, %|#nl|%, or %|\n|% is the
2235  *     newline character, used to terminate lines of text or advance the
2236  *     cursor to the next line (perhaps without returning it to the start of
2237  *     the line).
2238  *
2239  *   * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is the
2240  *     carriage-return character, used to return the cursor to the start of
2241  *     the line.
2242  *
2243  *   * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the tab
2244  *     character, used to advance the cursor to the next tab stop on the
2245  *     current line.
2246  *
2247  *   * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab character.
2248  *
2249  *   * %|#space|%, %|#spc|% is the space character.
2250  *
2251  *   * %|#delete|%, %|#del|% is the delete character, used to erase the most
2252  *     recent character.
2253  *
2254  *   * %|\'|% is the single-quote character.
2255  *
2256  *   * %|\\|% is the backslash character.
2257  *
2258  *   * %|\"|% is the double-quote character.
2259  *
2260  *   * %|\NNN|% or %|\{NNN}|% is the character with code NNN in octal.  The
2261  *     NNN may be up to three digits long.
2262  *
2263  *   * %|\xNN|% or %|\x{NN}|% is the character with code NNN in hexadecimal.
2264  */
2265
2266 extern const struct tvec_regty tvty_char;
2267
2268 /* --- @tvec_claimeq_char@, @TVEC_CLAIMEQ_CHAR@ --- *
2269  *
2270  * Arguments:   @struct tvec_state *tv@ = test-vector state
2271  *              @int ch0, ch1@ = two character codes
2272  *              @const char *file@, @unsigned @lno@ = calling file and line
2273  *              @const char *expr@ = the expression to quote on failure
2274  *
2275  * Returns:     Nonzero if @ch0@ and @ch1@ are equal, otherwise zero.
2276  *
2277  * Use:         Check that values of @ch0@ and @ch1@ are equal.  As for
2278  *              @tvec_claim@ above, a test case is automatically begun and
2279  *              ended if none is already underway.  If the values are
2280  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
2281  *              mismatched values are dumped: @ch0@ is printed as the output
2282  *              value and @ch1@ is printed as the input reference.
2283  *
2284  *              The @TVEC_CLAIM_CHAR@ macro is similar, only it (a)
2285  *              identifies the file and line number of the call site
2286  *              automatically, and (b) implicitly quotes the source text of
2287  *              the @ch0@ and @ch1@ arguments in the failure message.
2288  */
2289
2290 extern int tvec_claimeq_char(struct tvec_state */*tv*/,
2291                              int /*ch0*/, int /*ch1*/,
2292                              const char */*file*/, unsigned /*lno*/,
2293                              const char */*expr*/);
2294 #define TVEC_CLAIMEQ_CHAR(tv, c0, c1)                                   \
2295         (tvec_claimeq_char(tv, c0, c1, __FILE__, __LINE__, #c0 " /= " #c1))
2296
2297 /*----- Text and binary string types --------------------------------------*/
2298
2299 /* A string is a sequence of octets.  Text and binary strings differ
2300  * primarily in presentation: text strings are shown as raw characters where
2301  * possible; binary strings are shown as hex dumps with an auxiliary text
2302  * display.
2303  *
2304  * The input format for both kinds of strings is basically the same: a
2305  * `compound string', consisting of
2306  *
2307  *   * single-quoted strings, which are interpreted entirely literally, but
2308  *     can't contain single quotes or newlines;
2309  *
2310  *   * double-quoted strings, in which `%|\|%'-escapes are interpreted as for
2311  *     characters;
2312  *
2313  *   * character names, marked by an initial `%|#|%' sign;
2314  *
2315  *   * special tokens marked by an initial `%|!|%' sign; or
2316  *
2317  *   * barewords interpreted according to the current coding scheme.
2318  *
2319  * The special tokens are
2320  *
2321  *   * `%|!bare|%', which causes subsequent sequences of barewords to be
2322  *     treated as plain text;
2323  *
2324  *   * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause subsequent
2325  *     barewords to be decoded in the requested manner.
2326  *
2327  *   * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%', which includes
2328  *     %$n$% copies of the (compound) string.
2329  *
2330  * The only difference between text and binary strings is that the initial
2331  * coding scheme is %|bare|% for text strings and %|hex|% for binary strings.
2332  *
2333  * Either kind of string can contain internal nul characters.  A trailing nul
2334  * is appended -- beyond the stated input length -- to input strings as a
2335  * convenience to test functions.  Test functions may include such a nul
2336  * character on output but this is not checked by the equality test.
2337  *
2338  * A @struct tvec_urange@ may be supplied as an argument: the length of the
2339  * string (in bytes) will be checked against the permitted range.
2340  */
2341
2342 extern const struct tvec_regty tvty_text, tvty_bytes;
2343
2344 /* --- @tvec_alloctext@, @tvec_allocbytes@ --- *
2345  *
2346  * Arguments:   @union tvec_regval *rv@ = register value
2347  *              @size_t sz@ = required size
2348  *
2349  * Returns:     ---
2350  *
2351  * Use:         Allocated space in a text or binary string register.  If the
2352  *              current register size is sufficient, its buffer is left
2353  *              alone; otherwise, the old buffer, if any, is freed and a
2354  *              fresh buffer allocated.  These functions are not intended to
2355  *              be used to adjust a buffer repeatedly, e.g., while building
2356  *              output incrementally: (a) they will perform badly, and (b)
2357  *              the old buffer contents are simply discarded if reallocation
2358  *              is necessary.  Instead, use a @dbuf@ or @dstr@.
2359  *
2360  *              The @tvec_alloctext@ function sneakily allocates an extra
2361  *              byte for a terminating zero.  The @tvec_allocbytes@ function
2362  *              doesn't do this.
2363  */
2364
2365 extern void tvec_alloctext(union tvec_regval */*rv*/, size_t /*sz*/);
2366 extern void tvec_allocbytes(union tvec_regval */*rv*/, size_t /*sz*/);
2367
2368 /* --- @tvec_claimeq_text@, @TVEC_CLAIMEQ_TEXT@ --- *
2369  *
2370  * Arguments:   @struct tvec_state *tv@ = test-vector state
2371  *              @const char *p0@, @size_t sz0@ = first string with length
2372  *              @const char *p1@, @size_t sz1@ = second string with length
2373  *              @const char *file@, @unsigned @lno@ = calling file and line
2374  *              @const char *expr@ = the expression to quote on failure
2375  *
2376  * Returns:     Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2377  *              zero.
2378  *
2379  * Use:         Check that strings at @p0@ and @p1@ are equal.  As for
2380  *              @tvec_claim@ above, a test case is automatically begun and
2381  *              ended if none is already underway.  If the values are
2382  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
2383  *              mismatched values are dumped: @p0@ is printed as the output
2384  *              value and @p1@ is printed as the input reference.
2385  *
2386  *              The @TVEC_CLAIM_TEXT@ macro is similar, only it (a)
2387  *              identifies the file and line number of the call site
2388  *              automatically, and (b) implicitly quotes the source text of
2389  *              the @ch0@ and @ch1@ arguments in the failure message.
2390  */
2391
2392 extern int tvec_claimeq_text(struct tvec_state */*tv*/,
2393                              const char */*p0*/, size_t /*sz0*/,
2394                              const char */*p1*/, size_t /*sz1*/,
2395                              const char */*file*/, unsigned /*lno*/,
2396                              const char */*expr*/);
2397 #define TVEC_CLAIMEQ_TEXT(tv, p0, sz0, p1, sz1)                         \
2398         (tvec_claimeq_text(tv, p0, sz0, p1, sz1, __FILE__, __LINE__,    \
2399                            #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2400
2401 /* --- @tvec_claimeq_textz@, @TVEC_CLAIMEQ_TEXTZ@ --- *
2402  *
2403  * Arguments:   @struct tvec_state *tv@ = test-vector state
2404  *              @const char *p0, *p1@ = two strings to compare
2405  *              @const char *file@, @unsigned @lno@ = calling file and line
2406  *              @const char *expr@ = the expression to quote on failure
2407  *
2408  * Returns:     Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2409  *              zero.
2410  *
2411  * Use:         Check that strings at @p0@ and @p1@ are equal, as for
2412  *              @tvec_claimeq_string@, except that the strings are assumed
2413  *              null-terminated, so their lengths don't need to be supplied
2414  *              explicitly.  The macro is similarly like @TVEC_CLAIMEQ_TEXT@.
2415  */
2416
2417 extern int tvec_claimeq_textz(struct tvec_state */*tv*/,
2418                               const char */*p0*/, const char */*p1*/,
2419                               const char */*file*/, unsigned /*lno*/,
2420                               const char */*expr*/);
2421 #define TVEC_CLAIMEQ_TEXTZ(tv, p0, p1)                                  \
2422         (tvec_claimeq_textz(tv, p0, p1, __FILE__, __LINE__, #p0 " /= " #p1))
2423
2424 /* --- @tvec_claimeq_bytes@, @TVEC_CLAIMEQ_BYTES@ --- *
2425  *
2426  * Arguments:   @struct tvec_state *tv@ = test-vector state
2427  *              @const void *p0@, @size_t sz0@ = first string with length
2428  *              @const void *p1@, @size_t sz1@ = second string with length
2429  *              @const char *file@, @unsigned @lno@ = calling file and line
2430  *              @const char *expr@ = the expression to quote on failure
2431  *
2432  * Returns:     Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2433  *              zero.
2434  *
2435  * Use:         Check that binary strings at @p0@ and @p1@ are equal.  As for
2436  *              @tvec_claim@ above, a test case is automatically begun and
2437  *              ended if none is already underway.  If the values are
2438  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
2439  *              mismatched values are dumped: @p0@ is printed as the output
2440  *              value and @p1@ is printed as the input reference.
2441  *
2442  *              The @TVEC_CLAIM_STRING@ macro is similar, only it (a)
2443  *              identifies the file and line number of the call site
2444  *              automatically, and (b) implicitly quotes the source text of
2445  *              the @ch0@ and @ch1@ arguments in the failure message.
2446  */
2447
2448 extern int tvec_claimeq_bytes(struct tvec_state */*tv*/,
2449                                const void */*p0*/, size_t /*sz0*/,
2450                                const void */*p1*/, size_t /*sz1*/,
2451                                const char */*file*/, unsigned /*lno*/,
2452                                const char */*expr*/);
2453 #define TVEC_CLAIMEQ_BYTES(tv, p0, sz0, p1, sz1)                        \
2454         (tvec_claimeq(tv, p0, sz0, p1, sz1, __FILE__, __LINE__,         \
2455                       #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2456
2457 /*----- Buffer type -------------------------------------------------------*/
2458
2459 /* Buffer registers are primarily used for benchmarking.  Only a buffer's
2460  * allocation parameters are significant: its contents are ignored on
2461  * comparison and output, and unspecified on input.
2462  *
2463  * The input format gives the buffer's size, and an optional alignment
2464  * specification, in the form %|SZ [`@' M [`+' A]]|%.  Each of %|SZ|%, %|M|%
2465  * and %|A|% are sizes, as an integer, optionally suffixed with a unit `kB',
2466  * `MB', `GB', `TB', `PB', `EB', `ZB', `YB' (with or without the `B')
2467  * denoting a power of 1024.  The %|SZ|% gives the (effective) buffer size.
2468  * %|M|% is the `alignment quantum' and %|A|% is the `alignment offset'; both
2469  * default to zero, but if %|M|% is nonzero then the start of the buffer is
2470  * aligned such that it is %|A|% more than a multiple of %|M|% bytes.  Note
2471  * that %|M|% need not be a power of two, though this is common.
2472  *
2473  * Units other than `B' are used on output only when the size would be
2474  * expressed exactly.
2475  *
2476  * Buffers are %%\emph{not}%% allocated by default.  In benchmarks, this is
2477  * best done in a @before@ function.
2478  *
2479  * No @claimeq@ functions or macros are provided for buffers because they
2480  * don't seem very useful.
2481  */
2482
2483 extern const struct tvec_regty tvty_buffer;
2484
2485 /* --- @tvec_initbuffer@ --- *
2486  *
2487  * Arguments:   @union tvec_regval *rv@ = register value
2488  *              @const union tvec_regval *ref@ = reference buffer
2489  *              @size_t sz@ = size to allocate
2490  *
2491  * Returns:     ---
2492  *
2493  * Use:         Initialize the alignment parameters in @rv@ to match @ref@,
2494  *              and the size to @sz@.
2495  */
2496
2497 extern void tvec_initbuffer(union tvec_regval */*rv*/,
2498                             const union tvec_regval */*ref*/, size_t /*sz*/);
2499
2500 /* --- @tvec_allocbuffer@ --- *
2501  *
2502  * Arguments:   @union tvec_regval *rv@ = register value
2503  *
2504  * Returns:     ---
2505  *
2506  * Use:         Allocate @sz@ bytes to the buffer and fill the space with a
2507  *              distinctive pattern.
2508  */
2509
2510 extern void tvec_allocbuffer(union tvec_regval */*rv*/);
2511
2512 /*----- That's all, folks -------------------------------------------------*/
2513
2514 #ifdef __cplusplus
2515   }
2516 #endif
2517
2518 #endif