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