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