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