chiark / gitweb /
@@@ tvec wip
[mLib] / test / tvec.h
1 /* -*-c-*-
2  *
3  * Test vector processing framework
4  *
5  * (c) 2023 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU Library General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or (at
15  * your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib.  If not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  */
27
28 #ifndef MLIB_TVEC_H
29 #define MLIB_TVEC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdarg.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #ifndef MLIB_BUF_H
43 #  include "buf.h"
44 #endif
45
46 #ifndef MLIB_CONTROL_H
47 #  include "control.h"
48 #endif
49
50 #ifndef MLIB_BUF_H
51 #  include "dstr.h"
52 #endif
53
54 #ifndef MLIB_GPRINTF_H
55 #  include "gprintf.h"
56 #endif
57
58 #ifndef MLIB_MACROS_H
59 #  include "macros.h"
60 #endif
61
62 /*----- Miscellaneous values ----------------------------------------------*/
63
64 /* These are attached to structures which represent extension points, as a
65  * way to pass an opaque parameter to whatever things are hooked onto them.
66  */
67
68 #define TVEC_MISCSLOTS(_)                                               \
69         _(PTR, const void *, p)         /* arbitrary pointer */         \
70         _(INT, long, i)                 /* signed integer */            \
71         _(UINT, unsigned long, u)       /* signed integer */            \
72         _(FLT, double, f)               /* floating point */
73
74 union tvec_misc {
75 #define TVEC_DEFSLOT(tag, ty, slot) ty slot;
76   TVEC_MISCSLOTS(TVEC_DEFSLOT)
77 #undef TVEC_DEFSLOT
78 };
79 enum {
80 #define TVEC_DEFCONST(tag, ty, slot) TVMISC_##tag,
81   TVEC_MISCSLOTS(TVEC_DEFCONST)
82   TVMISC_LIMIT
83 };
84
85 /*----- Register values ---------------------------------------------------*/
86
87 /* The framework doesn't have a preconceived idea about what's in a register
88  * value: it just allocates them and accesses them through the register type
89  * functions.  It doesn't even have a baked-in idea of how big a register
90  * value is: instead, it gets that via the `regsz' slot in `struct
91  * tvec_testinfo'.  So, as far as the framework is concerned, it's safe to
92  * add new slots to this union, even if they make the overall union larger.
93  * This can be done by defining the preprocessor macro `TVEC_REGSLOTS' to be
94  * a `union' fragment defining any additional union members.
95  *
96  * This creates a distinction between code which does and doesn't know the
97  * size of a register value.  Code which does, which typically means the test
98  * functions, benchmarking setup and teardown functions, and tightly-bound
99  * runner functions, is free to index the register vectors directly.  Code
100  * which doesn't, which means the framework core itself and output formatting
101  * machinery, must use the `TVEC_REG' macro (or its more general `TVEC_GREG'
102  * companion) for indexing register vectors.  (In principle, register type
103  * handlers also fit into this category, but they have no business peering
104  * into register values other than the one's they're given.)
105  */
106
107 union tvec_regval {
108         /* The actual register value.  This is what the type handler sees.
109          * Additional members can be added by setting `TVEC_REGSLOTS' before
110          * including this file.
111          *
112          * A register value can be /initialized/, which simply means that its
113          * contents represent a valid value according to its type -- the
114          * register can be compared, dumped, serialized, parsed into, etc.
115          * You can't do anything safely to an uninitialized register value
116          * other than initialize it.
117          */
118
119   long i;                               /* signed integer */
120   unsigned long u;                      /* unsigned integer */
121   void *p;                              /* pointer */
122   double f;                             /* floating point */
123   struct { unsigned char *p; size_t sz; } bytes; /* binary string of bytes */
124   struct { char *p; size_t sz; } str;   /* text string */
125 #ifdef TVEC_REGSLOTS
126   TVEC_REGSLOTS
127 #endif
128 };
129
130 struct tvec_reg {
131         /* A register.
132          *
133          * Note that all of the registers listed as being used by a
134          * particular test group are initialized at all times[1] while that
135          * test group is being processed.  (The other register slots don't
136          * even have types associated with them, so there's nothing useful we
137          * could do with them.)
138          *
139          * The `TVRF_LIVE' flag indicates that the register was assigned a
140          * value by the test vector file: it's the right thing to use to
141          * check whether an optional register is actually present.  Even
142          * `dead' registers are still initialized, though.
143          *
144          * [1] This isn't quite true.  Between individual tests, the
145          *     registers are released and reinitialized in order to reset
146          *     them to known values ready for the next test.  But you won't
147          *     see them at this point.
148          */
149
150   unsigned f;                           /* flags */
151 #define TVRF_LIVE 1u                    /*   used in current test  */
152   union tvec_regval v;                  /* register value */
153 };
154
155 struct tvec_regdef {
156         /* A register definition.  Register definitions list the registers
157          * which are used by a particular test group (see `struct tvec_test'
158          * below).
159          *
160          * A vector of register definitions is terminated by a definition
161          * whose `name' slot is null.
162          */
163
164   const char *name;                     /* register name (for input files) */
165   unsigned i;                           /* register index */
166   const struct tvec_regty *ty;          /* register type descriptor */
167   unsigned f;                           /* flags */
168 #define TVRF_OPT 1u                     /*   optional register */
169 #define TVRF_ID 2u                      /*   part of test identity  */
170   union tvec_misc arg;                  /* extra detail for the type */
171 };
172 #define TVEC_ENDREGS { 0, 0, 0, 0, { 0 } }
173
174 /* @TVEC_GREG(vec, i, regsz)@
175  *
176  * If @vec@ is a data pointer which happens to contain the address of a
177  * vector of @struct tvec_reg@ objects, @i@ is an integer, and @regsz@ is the
178  * size of a @struct tvec_reg@, then this evaluates to the address of the
179  * @i@th element of the vector.
180  *
181  * This is the general tool you need for accessing register vectors when you
182  * don't have absolute knowledge of the size of a @union tvec_regval@.
183  * Usually you want to access one of the register vectors in a @struct
184  * tvec_state@, and @TVEC_REG@ will be more convenient.
185  */
186 #define TVEC_GREG(vec, i, regsz)                                        \
187         ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz)))
188
189 /*------ Serialization utilities ------------------------------------------*/
190
191 /* --- @tvec_serialize@ --- *
192  *
193  * Arguments:   @const struct tvec_reg *rv@ = vector of registers
194  *              @buf *b@ = buffer to write on
195  *              @const struct tvec_regdef *regs@ = vector of register
196  *                      descriptions, terminated by an entry with a null
197  *                      @name@ slot
198  *              @unsigned nr@ = number of entries in the @rv@ vector
199  *              @size_t regsz@ = true size of each element of @rv@
200  *
201  * Returns:     Zero on success, @-1@ on failure.
202  *
203  * Use:         Serialize a collection of register values.
204  *
205  *              The `candidate register definitions' are those entries @r@ in
206  *              the @regs@ vector whose index @r.i@ is strictly less than
207  *              @nr@.  The `selected register definitions' are those
208  *              candidate register definitions @r@ for which the indicated
209  *              register @rv[r.i]@ has the @TVRF_LIVE@ flag set.  The
210  *              serialized output begins with a header bitmap: if there are
211  *              %$n$% candidate register definitions then the header bitmap
212  *              consists of %$\lceil n/8 \rceil$% bytes.  Bits are ordered
213  *              starting from the least significant bit of the first byte,
214  *              end ending at the most significant bit of the final byte.
215  *              The bit corresponding to a candidate register definition is
216  *              set if and only if that register defintion is selected.  The
217  *              header bitmap is then followed by the serializations of the
218  *              selected registers -- i.e., for each selected register
219  *              definition @r@, the serialized value of register @rv[r.i]@ --
220  *              simply concatenated together, with no padding or alignment.
221  *
222  *              The serialized output is written to the buffer @b@.  Failure
223  *              can be caused by running out of buffer space, or a failing
224  *              type handler.
225  */
226
227 extern int tvec_serialize(const struct tvec_reg */*rv*/, buf */*b*/,
228                           const struct tvec_regdef */*regs*/,
229                           unsigned /*nr*/, size_t /*regsz*/);
230
231 /* --- @tvec_deserialize@ --- *
232  *
233  * Arguments:   @struct tvec_reg *rv@ = vector of registers
234  *              @buf *b@ = buffer to write on
235  *              @const struct tvec_regdef *regs@ = vector of register
236  *                      descriptions, terminated by an entry with a null
237  *                      @name@ slot
238  *              @unsigned nr@ = number of entries in the @rv@ vector
239  *              @size_t regsz@ = true size of each element of @rv@
240  *
241  * Returns:     Zero on success, @-1@ on failure.
242  *
243  * Use:         Deserialize a collection of register values.
244  *
245  *              The size of the register vector @nr@ and the register
246  *              definitions @regs@ must match those used when producing the
247  *              serialization.  For each serialized register value,
248  *              deserialize and store the value into the appropriate register
249  *              slot, and set the @TVRF_LIVE@ flag on the register.  See
250  *              @tvec_serialize@ for a description of the format.
251  *
252  *              On successful completion, store the address of the first byte
253  *              after the serialized data in @*end_out@ if @end_out@ is not
254  *              null.  Failure results only from a failing register type
255  *              handler.
256  */
257
258 extern int tvec_deserialize(struct tvec_reg */*rv*/, buf */*b*/,
259                             const struct tvec_regdef */*regs*/,
260                             unsigned /*nr*/, size_t /*regsz*/);
261
262 /*----- Test state --------------------------------------------------------*/
263
264 enum {
265   /* Possible test outcomes. */
266
267   TVOUT_LOSE,                           /* test failed */
268   TVOUT_SKIP,                           /* test skipped */
269   TVOUT_WIN,                            /* test passed */
270   TVOUT_LIMIT                           /* (number of possible outcomes) */
271 };
272
273 struct tvec_state {
274   /* The primary state structure for the test vector machinery. */
275
276   unsigned f;                           /* flags */
277 #define TVSF_SKIP 1u                    /*   skip this test group */
278 #define TVSF_OPEN 2u                    /*   test is open */
279 #define TVSF_ACTIVE 4u                  /*   test is active */
280 #define TVSF_ERROR 8u                   /*   an error occurred */
281 #define TVSF_OUTMASK 0xf0               /*   test outcome (@TVOUT_...@) */
282 #define TVSF_OUTSHIFT 4                 /*   shift applied to outcome */
283
284   /* Registers.  Available to execution environments. */
285   unsigned nrout, nreg;                /* number of output/total registers */
286   size_t regsz;                         /* size of register entry */
287   struct tvec_reg *in, *out;            /* register vectors */
288
289   /* Test groups state.  Available to output formatters. */
290   const struct tvec_test *tests, *test; /* all tests and current test */
291
292   /* Test scoreboard.  Available to output formatters. */
293   unsigned curr[TVOUT_LIMIT], all[TVOUT_LIMIT], grps[TVOUT_LIMIT];
294
295   /* Output machinery. */
296   struct tvec_output *output;           /* output formatter */
297
298   /* Input machinery.  Available to type parsers. */
299   const char *infile; unsigned lno, test_lno; /* input file name, line */
300   FILE *fp;                             /* input file stream */
301 };
302
303 /* @TVEC_REG(tv, vec, i)@
304  *
305  * If @tv@ is a pointer to a @struct tvec_state@, @vec@ is either @in@ or
306  * @out@, and @i@ is an integer, then this evaluates to the address of the
307  * @i@th register in the selected vector.
308  */
309 #define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->regsz)
310
311 /*----- Test descriptions -------------------------------------------------*/
312
313 typedef void tvec_testfn(const struct tvec_reg */*in*/,
314                          struct tvec_reg */*out*/,
315                          void */*ctx*/);
316   /* A test function.  It should read inputs from @in@ and write outputs to
317    * @out@.  The @TVRF_LIVE@ is set on inputs which are actually present, and
318    * on outputs which are wanted to test.  A test function can set additional
319    * `gratuitous outputs' by setting @TVRF_LIVE@ on them; clearing
320    * @TVRF_LIVE@ on a wanted output causes a mismatch.
321    *
322    * A test function may be called zero or more times by the environment.  In
323    * particular, it may be called multiple times, though usually by prior
324    * arrangement with the environment.
325    *
326    * The @ctx@ is supplied by the environment's @run@ function (see below).
327    * The default environment calls the test function once, with a null
328    * @ctx@.  There is no expectation that the environment's context has
329    * anything to do with the test function's context.
330    */
331
332 struct tvec_env {
333   /* A test environment sets things up for and arranges to run the test.
334    *
335    * The caller is responsible for allocating storage for the environment's
336    * context, based on the @ctxsz@ slot, and freeing it later; this space is
337    * passed in as the @ctx@ parameter to the remaining functions; if @ctxsz@
338    * is zero then @ctx@ is null.
339    */
340
341   size_t ctxsz;                       /* environment context size */
342
343   int (*setup)(struct tvec_state */*tv*/, const struct tvec_env */*env*/,
344                void */*pctx*/, void */*ctx*/);
345     /* Initialize the context; called at the start of a test group.  Return
346      * zero on success, or @-1@ on failure.  If setup fails, the context is
347      * freed, and the test group is skipped.
348      */
349
350   int (*set)(struct tvec_state */*tv*/, const char */*var*/,
351              const struct tvec_env */*env*/, void */*ctx*/);
352     /* Called when the parser finds a %|@var|%' setting to parse and store
353      * the value.  If @setup@ failed, this is still called (so as to skip the
354      * value), but @ctx@ is null.
355      */
356
357   int (*before)(struct tvec_state */*tv*/, void */*ctx*/);
358     /* Called prior to running a test.  This is the right place to act on any
359      * `%|@var|%' settings.  Return zero on success or @-1@ on failure (which
360      * causes the test to be skipped).  This function is never called if the
361      * test group is skipped.
362      */
363
364   void (*run)(struct tvec_state */*tv*/, tvec_testfn */*fn*/, void */*ctx*/);
365     /* Run the test.  It should either call @tvec_skip@, or run @fn@ one or
366      * more times.  In the latter case, it is responsible for checking the
367      * outputs, and calling @tvec_fail@ as necessary; @tvec_checkregs@ will
368      * check the register values against the supplied test vector, while
369      * @tvec_check@ does pretty much everything necessary.  This function is
370      * never called if the test group is skipped.
371      */
372
373   void (*after)(struct tvec_state */*tv*/, void */*ctx*/);
374     /* Called after running or skipping a test.  Typical actions involve
375      * resetting whatever things were established by @set@.  This function is
376      * never called if the test group is skipped.
377      */
378
379   void (*teardown)(struct tvec_state */*tv*/, void */*ctx*/);
380     /* Tear down the environment: called at the end of a test group.  If the
381      * setup failed, then this function is still called, with a null @ctx@.
382      */
383 };
384
385 struct tvec_test {
386   /* A test description. */
387
388   const char *name;                     /* name of the test */
389   const struct tvec_regdef *regs;       /* descriptions of the registers */
390   const struct tvec_env *env;           /* environment to run test in */
391   tvec_testfn *fn;                      /* test function */
392 };
393 #define TVEC_ENDTESTS { 0, 0, 0, 0 }
394
395 enum {
396   /* Register output dispositions. */
397
398   TVRD_INPUT,                           /* input-only register */
399   TVRD_OUTPUT,                          /* output-only (input is dead) */
400   TVRD_MATCH,                           /* matching (equal) registers */
401   TVRD_FOUND,                           /* mismatching output register */
402   TVRD_EXPECT                           /* mismatching input register */
403 };
404
405 /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
406  *
407  * Arguments:   @struct tvec_state *tv@ = test-vector state
408  *              @const char *excuse@, @va_list *ap@ = reason why skipped
409  *
410  * Returns:     ---
411  *
412  * Use:         Skip the current group.  This should only be called from a
413  *              test environment @setup@ function; a similar effect occurs if
414  *              the @setup@ function fails.
415  */
416
417 extern void PRINTF_LIKE(2, 3)
418   tvec_skipgroup(struct tvec_state */*tv*/, const char */*excuse*/, ...);
419 extern void tvec_skipgroup_v(struct tvec_state */*tv*/,
420                              const char */*excuse*/, va_list */*ap*/);
421
422 /* --- @tvec_skip@, @tvec_skip_v@ --- *
423  *
424  * Arguments:   @struct tvec_state *tv@ = test-vector state
425  *              @const char *excuse@, @va_list *ap@ = reason why test skipped
426  *
427  * Returns:     ---
428  *
429  * Use:         Skip the current test.  This should only be called from a
430  *              test environment @run@ function; a similar effect occurs if
431  *              the @before@ function fails.
432  */
433
434 extern void PRINTF_LIKE(2, 3)
435   tvec_skip(struct tvec_state */*tv*/, const char */*excuse*/, ...);
436 extern void tvec_skip_v(struct tvec_state */*tv*/,
437                         const char */*excuse*/, va_list */*ap*/);
438
439 /* --- @tvec_resetoutputs@ --- *
440  *
441  * Arguments:   @struct tvec_state *tv@ = test-vector state
442  *
443  * Returns:     ---
444  *
445  * Use:         Reset (releases and reinitializes) the output registers in
446  *              the test state.  This is mostly of use to test environment
447  *              @run@ functions, between invocations of the test function.
448  */
449
450 extern void tvec_resetoutputs(struct tvec_state */*tv*/);
451
452 /* --- @tvec_checkregs@ --- *
453  *
454  * Arguments:   @struct tvec_state *tv@ = test-vector state
455  *
456  * Returns:     Zero on success, @-1@ on mismatch.
457  *
458  * Use:         Compare the active output registers (according to the current
459  *              test group definition) with the corresponding input register
460  *              values.  A mismatch occurs if the two values differ
461  *              (according to the register type's @eq@ method), or if the
462  *              input is live but the output is dead.
463  *
464  *              This function only checks for a mismatch and returns the
465  *              result; it takes no other action.  In particular, it doesn't
466  *              report a failure, or dump register values.
467  */
468
469 extern int tvec_checkregs(struct tvec_state */*tv*/);
470
471 /* --- @tvec_fail@, @tvec_fail_v@ --- *
472  *
473  * Arguments:   @struct tvec_state *tv@ = test-vector state
474  *              @const char *detail@, @va_list *ap@ = description of test
475  *
476  * Returns:     ---
477  *
478  * Use:         Report the current test as a failure.  This function can be
479  *              called multiple times for a single test, e.g., if the test
480  *              environment's @run@ function invokes the test function
481  *              repeatedly; but a single test that fails repeatedly still
482  *              only counts as a single failure in the statistics.  The
483  *              @detail@ string and its format parameters can be used to
484  *              distinguish which of several invocations failed; it can
485  *              safely be left null if the test function is run only once.
486  */
487
488 extern void PRINTF_LIKE(2, 3)
489   tvec_fail(struct tvec_state */*tv*/, const char */*detail*/, ...);
490 extern void tvec_fail_v(struct tvec_state */*tv*/,
491                         const char */*detail*/, va_list */*ap*/);
492
493 /* --- @tvec_dumpreg@ --- *
494  *
495  * Arguments:   @struct tvec_state *tv@ = test-vector state
496  *              @unsigned disp@ = the register disposition (@TVRD_...@)
497  *              @const union tvec_regval *tv@ = register value
498  *              @const struct tvec_regdef *rd@ = register definition
499  *
500  * Returns:     ---
501  *
502  * Use:         Dump a register value to the output.  This is the lowest-
503  *              level function for dumping registers, and calls the output
504  *              formatter directly.
505  *
506  *              Usually @tvec_mismatch@ is much more convenient.  Low-level
507  *              access is required for reporting `virtual' registers
508  *              corresponding to test environment settings.
509  */
510
511 extern void tvec_dumpreg(struct tvec_state */*tv*/,
512                          unsigned /*disp*/, const union tvec_regval */*rv*/,
513                          const struct tvec_regdef */*rd*/);
514
515 /* --- @tvec_mismatch@ --- *
516  *
517  * Arguments:   @struct tvec_state *tv@ = test-vector state
518  *              @unsigned f@ = flags (@TVMF_...@)
519  *
520  * Returns:     ---
521  *
522  * Use:         Dumps registers suitably to report a mismatch.  The flag bits
523  *              @TVMF_IN@ and @TVF_OUT@ select input-only and output
524  *              registers.  If both are reset then nothing happens.
525  *              Suppressing the output registers may be useful, e.g., if the
526  *              test function crashed rather than returning outputs.
527  */
528
529 #define TVMF_IN 1u
530 #define TVMF_OUT 2u
531 extern void tvec_mismatch(struct tvec_state */*tv*/, unsigned /*f*/);
532
533 /* --- @tvec_check@, @tvec_check_v@ --- *
534  *
535  * Arguments:   @struct tvec_state *tv@ = test-vector state
536  *              @const char *detail@, @va_list *ap@ = description of test
537  *
538  * Returns:     ---
539  *
540  * Use:         Check the register values, reporting a failure and dumping
541  *              the registers in the event of a mismatch.  This just wraps up
542  *              @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
543  *              obvious way.
544  */
545
546 extern void PRINTF_LIKE(2, 3)
547   tvec_check(struct tvec_state */*tv*/, const char */*detail*/, ...);
548 extern void tvec_check_v(struct tvec_state */*tv*/,
549                          const char */*detail*/, va_list */*ap*/);
550
551 /*----- Session lifecycle -------------------------------------------------*/
552
553 struct tvec_config {
554   /* An overall test configuration. */
555
556   const struct tvec_test *tests;        /* the tests to be performed */
557   unsigned nrout, nreg;                 /* number of output/total regs */
558   size_t regsz;                         /* size of a register */
559 };
560
561 /* --- @tvec_begin@ --- *
562  *
563  * Arguments:   @struct tvec_state *tv_out@ = state structure to fill in
564  *              @const struct tvec_config *config@ = test configuration
565  *              @struct tvec_output *o@ = output driver
566  *
567  * Returns:     ---
568  *
569  * Use:         Initialize a state structure ready to do some testing.
570  */
571
572 extern void tvec_begin(struct tvec_state */*tv_out*/,
573                        const struct tvec_config */*config*/,
574                        struct tvec_output */*o*/);
575
576 /* --- @tvec_end@ --- *
577  *
578  * Arguments:   @struct tvec_state *tv@ = test-vector state
579  *
580  * Returns:     A proposed exit code.
581  *
582  * Use:         Conclude testing and suggests an exit code to be returned to
583  *              the calling program.  (The exit code comes from the output
584  *              driver's @esession@ method.)
585  */
586
587 extern int tvec_end(struct tvec_state */*tv*/);
588
589 /* --- @tvec_read@ --- *
590  *
591  * Arguments:   @struct tvec_state *tv@ = test-vector state
592  *              @const char *infile@ = the name of the input file
593  *              @FILE *fp@ = stream to read from
594  *
595  * Returns:     Zero on success, @-1@ on error.
596  *
597  * Use:         Read test vector data from @fp@ and exercise test functions.
598  *              THe return code doesn't indicate test failures: it's only
599  *              concerned with whether there were problems with the input
600  *              file or with actually running the tests.
601  */
602
603 extern int tvec_read(struct tvec_state */*tv*/,
604                      const char */*infile*/, FILE */*fp*/);
605
606 /*----- Input utilities ---------------------------------------------------*/
607
608 /* These are provided by the core for the benefit of type @parse@ methods,
609  * and test-environment @set@ functions, which get to read from the test
610  * input file.  The latter are usually best implemented by calling on the
611  * former.
612  *
613  * The two main rules are as follows.
614  *
615  *   * Leave the file position at the beginning of the line following
616  *     whatever it was that you read.
617  *
618  *   * When you read and consume a newline (which you do at least once, by
619  *     the previous rule), then increment @tv->lno@ to keep track of the
620  *     current line number.
621  */
622
623 /* --- @tvec_skipspc@ --- *
624  *
625  * Arguments:   @struct tvec_state *tv@ = test-vector state
626  *
627  * Returns:     ---
628  *
629  * Use:         Advance over any whitespace characters other than newlines.
630  *              This will stop at `;', end-of-file, or any other kind of
631  *              non-whitespace; and it won't consume a newline.
632  */
633
634 extern void tvec_skipspc(struct tvec_state */*tv*/);
635
636 /* --- @tvec_syntax@, @tvec_syntax_v@ --- *
637  *
638  * Arguments:   @struct tvec_state *tv@ = test-vector state
639  *              @int ch@ = the character found (in @fgetc@ format)
640  *              @const char *expect@, @va_list ap@ = what was expected
641  *
642  * Returns:     @-1@
643  *
644  * Use:         Report a syntax error quoting @ch@ and @expect@.  If @ch@ is
645  *              a newline, then back up so that it can be read again (e.g.,
646  *              by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
647  *              advance the line number).
648  */
649
650 extern int PRINTF_LIKE(3, 4)
651   tvec_syntax(struct tvec_state */*tv*/, int /*ch*/,
652               const char */*expect*/, ...);
653 extern int tvec_syntax_v(struct tvec_state */*tv*/, int /*ch*/,
654                          const char */*expect*/, va_list */*ap*/);
655
656 /* --- @tvec_flushtoeol@ --- *
657  *
658  * Arguments:   @struct tvec_state *tv@ = test-vector state
659  *              @unsigned f@ = flags (@TVFF_...@)
660  *
661  * Returns:     Zero on success, @-1@ on error.
662  *
663  * Use:         Advance to the start of the next line, consuming the
664  *              preceding newline.
665  *
666  *              A syntax error is reported if no newline character is found,
667  *              i.e., the file ends in mid-line.  A syntax error is also
668  *              reported if material other than whitespace or a comment is
669  *              found before the end of the line end, and @TVFF_ALLOWANY@ is
670  *              not set in @f@.  The line number count is updated
671  *              appropriately.
672  */
673
674 #define TVFF_ALLOWANY 1u
675 extern int tvec_flushtoeol(struct tvec_state */*tv*/, unsigned /*f*/);
676
677 /* --- @tvec_nexttoken@ --- *
678  *
679  * Arguments:   @struct tvec_state *tv@ = test-vector state
680  *
681  * Returns:     Zero if there is a next token which can be read; @-1@ if no
682  *              token is available.
683  *
684  * Use:         Advance to the next whitespace-separated token, which may be
685  *              on the next line.
686  *
687  *              Tokens are separated by non-newline whitespace, comments, and
688  *              newlines followed by whitespace; a newline /not/ followed by
689  *              whitespace instead begins the next assignment, and two
690  *              newlines separated only by whitespace terminate the data for
691  *              a test.
692  *
693  *              If this function returns zero, then the next character in the
694  *              file begins a suitable token which can be read and
695  *              processed.  If it returns @-1@ then there is no such token,
696  *              and the file position is left correctly.  The line number
697  *              count is updated appropriately.
698  */
699
700 extern int tvec_nexttoken(struct tvec_state */*tv*/);
701
702 /* --- @tvec_readword@ --- *
703  *
704  * Arguments:   @struct tvec_state *tv@ = test-vector state
705  *              @dstr *d@ = string to append the word to
706  *              @const char *delims@ = additional delimiters to stop at
707  *              @const char *expect@, @va_list ap@ = what was expected
708  *
709  * Returns:     Zero on success, @-1@ on failure.
710  *
711  * Use:         A `word' consists of characters other than whitespace, null
712  *              characters, and other than those listed in @delims@;
713  *              furthermore, a word does not begin with a `;'.  (If you want
714  *              reading to stop at comments not preceded by whitespace, then
715  *              include `;' in @delims@.  This is a common behaviour.)
716  *
717  *              If there is no word beginning at the current file position,
718  *              then return @-1@; furthermore, if @expect@ is not null, then
719  *              report an appropriate error via @tvec_syntax@.
720  *
721  *              Otherwise, the word is accumulated in @d@ and zero is
722  *              returned; if @d@ was not empty at the start of the call, the
723  *              newly read word is separated from the existing material by a
724  *              single space character.  Since null bytes are never valid
725  *              word constituents, a null terminator is written to @d@, and
726  *              it is safe to treat the string in @d@ as being null-
727  *              terminated.
728  */
729
730 extern int PRINTF_LIKE(4, 5)
731   tvec_readword(struct tvec_state */*tv*/, dstr */*d*/,
732                 const char */*delims*/, const char */*expect*/, ...);
733 extern int tvec_readword_v(struct tvec_state */*tv*/, dstr */*d*/,
734                            const char */*delims*/, const char */*expect*/,
735                            va_list */*ap*/);
736
737 /*----- Ad-hoc testing ----------------------------------------------------*/
738
739 /* --- @tvec_adhoc@ --- *
740  *
741  * Arguments:   @struct tvec_state *tv@ = test-vector state
742  *              @struct tvec_test *t@ = space for a test definition
743  *
744  * Returns:     ---
745  *
746  * Use:         Begin ad-hoc testing, i.e., without reading a file of
747  *              test-vector data.
748  *
749  *              The structure at @t@ will be used to record information about
750  *              the tests underway, which would normally come from a static
751  *              test definition.  The other functions in this section assume
752  *              that @tvec_adhoc@ has been called.
753  */
754
755 extern void tvec_adhoc(struct tvec_state */*tv*/, struct tvec_test */*t*/);
756
757 /* --- @tvec_begingroup@, @TVEC_BEGINGROUP@ --- *
758  *
759  * Arguments:   @struct tvec_state *tv@ = test-vector state
760  *              @const char *name@ = name for this test group
761  *              @const char *file@, @unsigned @lno@ = calling file and line
762  *
763  * Returns:     ---
764  *
765  * Use:         Begin an ad-hoc test group with the given name.  The @file@
766  *              and @lno@ can be anything, but it's usually best if they
767  *              refer to the source code performing the test: the macro
768  *              @TVEC_BEGINGROUP@ does this automatically.
769  */
770
771 extern void tvec_begingroup(struct tvec_state */*tv*/, const char */*name*/,
772                             const char */*file*/, unsigned /*lno*/);
773 #define TVEC_BEGINGROUP(tv, name)                                       \
774         do tvec_begingroup(tv, name, __FILE__, __LINE__); while (0)
775
776 /* --- @tvec_endgroup@ --- *
777  *
778  * Arguments:   @struct tvec_state *tv@ = test-vector state
779  *
780  * Returns:     ---
781  *
782  * Use:         End an ad-hoc test group.  The statistics are updated and the
783  *              outcome is reported to the output formatter.
784  */
785
786 extern void tvec_endgroup(struct tvec_state */*tv*/);
787
788 /* --- @TVEC_TESTGROUP@, @TVEC_TESTGROUP_TAG@ --- *
789  *
790  * Arguments:   @tag@ = label-disambiguation tag
791  *              @const struct tvec_state *tv = test-vector state
792  *              @const char *name@ = test-group name
793  *
794  * Returns:     ---
795  *
796  * Use:         Control-structure macro: @TVEC_TESTGROUP(tv, name) stmt@
797  *              establishes a test group with the given @name@ (attributing
798  *              it to the source file and lie number), executes @stmt@, and
799  *              ends the test group.  If @stmt@ invokes @break@ then the test
800  *              group is skipped.  @TVEC_TESTGROUP_TAG@ is the same, with an
801  *              additional @tag@ argument for use in higher-level macros.
802  */
803
804 #define TVEC_TESTGROUP_TAG(tag, tv, name)                               \
805         MC_WRAP(tag##__around,                                          \
806           { TVEC_BEGINGROUP(tv, name); },                               \
807           { tvec_endgroup(tv); },                                       \
808           { if (!((tv)->f&TVSF_SKIP)) tvec_skipgroup(tv, 0);            \
809             tvec_endgroup(tv); })
810 #define TVEC_TESTGROUP(tv, name) TVEC_TESTGROUP_TAG(grp, tv, name)
811
812 /* --- @tvec_begintest@, @TVEC_BEGINTEST@ --- *
813  *
814  * Arguments:   @struct tvec_state *tv@ = test-vector state
815  *              @const char *file@, @unsigned @lno@ = calling file and line
816  *
817  * Returns:     ---
818  *
819  * Use:         Begin an ad-hoc test case.  The @file@ and @lno@ can be
820  *              anything, but it's usually best if they refer to the source
821  *              code performing the test: the macro @TVEC_BEGINGROUP@ does
822  *              this automatically.
823  */
824
825 extern void tvec_begintest(struct tvec_state */*tv*/,
826                            const char */*file*/, unsigned /*lno*/);
827 #define TVEC_BEGINTEST(tv)                                              \
828         do tvec_begintest(tv, __FILE__, __LINE__); while (0)
829
830 /* --- *tvec_endtest@ --- *
831  *
832  * Arguments:   @struct tvec_state *tv@ = test-vector state
833  *
834  * Returns:     ---
835  *
836  * Use:         End a ad-hoc test case,  The statistics are updated and the
837  *              outcome is reported to the output formatter.
838  */
839
840 extern void tvec_endtest(struct tvec_state */*tv*/);
841
842 /* --- @TVEC_TEST@, @TVEC_TEST_TAG@ --- *
843  *
844  * Arguments:   @tag@ = label-disambiguation tag
845  *              @struct tvec_test *t@ = space for a test definition
846  *
847  * Returns:     ---
848  *
849  * Use:         Control-structure macro: @TVEC_TEST(tv) stmt@ begins a test
850  *              case, executes @stmt@, and ends the test case.  If @stmt@
851  *              invokes @break@ then the test case is skipped.
852  *              @TVEC_TEST_TAG@ is the same, with an additional @tag@ argumet
853  *              for use in higher-level macros.
854  */
855
856 #define TVEC_TEST_TAG(tag, tv)                                          \
857         MC_WRAP(tag##__around,                                          \
858           { TVEC_BEGINTEST(tv); },                                      \
859           { tvec_endtest(tv); },                                        \
860           { if ((tv)->f&TVSF_ACTIVE) tvec_skip((tv), 0);                \
861             tvec_endtest(tv); })
862 #define TVEC_TEST(tv) TVEC_TEST_TAG(test, tv)
863
864 /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- *
865  *
866  * Arguments:   @struct tvec_state *tv@ = test-vector state
867  *              @int ok@ = a flag
868  *              @const char *file@, @unsigned @lno@ = calling file and line
869  *              @const char *msg@, @va_list *ap@ = message to report on
870  *                      failure
871  *
872  * Returns:     The value @ok@.
873  *
874  * Use:         Check that a claimed condition holds, as (part of) a test.
875  *              If no test case is underway (i.e., if @TVSF_OPEN@ is reset in
876  *              @tv->f@), then a new test case is begun and ended.  The
877  *              @file@ and @lno@ are passed to the output formatter to be
878  *              reported in case of a failure.  If @ok@ is nonzero, then
879  *              nothing else happens; so, in particular, if @tvec_claim@
880  *              established a new test case, then the test case succeeds.  If
881  *              @ok@ is zero, then a failure is reported, quoting @msg@.
882  *
883  *              The @TVEC_CLAIM@ macro is similar, only it (a) identifies the
884  *              file and line number of the call site automatically, and (b)
885  *              implicitly quotes the source text of the @ok@ condition in
886  *              the failure message.
887  */
888
889 extern int PRINTF_LIKE(5, 6)
890   tvec_claim(struct tvec_state */*tv*/, int /*ok*/,
891              const char */*file*/, unsigned /*lno*/,
892              const char */*msg*/, ...);
893 extern int tvec_claim_v(struct tvec_state */*tv*/, int /*ok*/,
894                         const char */*file*/, unsigned /*lno*/,
895                         const char */*msg*/, va_list */*ap*/);
896 #define TVEC_CLAIM(tv, cond)                                            \
897         (tvec_claim(tv, !!(cond), __FILE__, __LINE__, "%s untrue", #cond))
898
899 /* --- @tvec_claimeq@ --- *
900  *
901  * Arguments:   @struct tvec_state *tv@ = test-vector state
902  *              @const struct tvec_regty *ty@ = register type
903  *              @const union tvec_misc *arg@ = register type argument
904  *              @const char *file@, @unsigned @lno@ = calling file and line
905  *              @const char *expr@ = the expression to quote on failure
906  *
907  * Returns:     Nonzero if the input and output values of register 0 are
908  *              equal, zero if they differ.
909  *
910  * Use:         Check that the input and output values of register 0 are
911  *              equal (according to the register type @ty@).  As for
912  *              @tvec_claim@ above, a test case is automatically begun and
913  *              ended if none is already underway.  If the values are
914  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
915  *              mismatched values are dumped.
916  *
917  *              This function is not expected to be called directly, but
918  *              through type-specific wrapper functions or macros such as
919  *              @TVEC_CLAIMEQ_INT@.
920  */
921
922 extern int tvec_claimeq(struct tvec_state */*tv*/,
923                         const struct tvec_regty */*ty*/,
924                         const union tvec_misc */*arg*/,
925                         const char */*file*/, unsigned /*lno*/,
926                         const char */*expr*/);
927
928 /*----- Output formatting -------------------------------------------------*/
929
930 struct tvec_output {
931   /* An output formatter. */
932   const struct tvec_outops *ops;        /* pointer to operations */
933 };
934
935 /* Benchmarking details. */
936 enum {
937   TVBU_OP,                             /* counting operations of some kind */
938   TVBU_BYTE                             /* counting bytes (@RBUF >= 0@) */
939 };
940 struct bench_timing;                    /* forward declaration */
941
942 struct tvec_outops {
943   /* Output operations. */
944
945   void (*bsession)(struct tvec_output */*o*/, struct tvec_state */*tv*/);
946         /* Begin a test session.  The output driver will probably want to
947          * save @tv@, because this isn't provided to any other methods.
948          */
949
950   int (*esession)(struct tvec_output */*o*/);
951         /* End a session, and return the suggested exit code. */
952
953   void (*bgroup)(struct tvec_output */*o*/);
954         /* Begin a test group.  The test group description is @tv->test@. */
955
956   void (*skipgroup)(struct tvec_output */*o*/,
957                     const char */*excuse*/, va_list */*ap*/);
958         /* The group is being skipped; @excuse@ may be null or a format
959          * string explaining why.  The @egroup@ method will not be called
960          * separately.
961          */
962
963   void (*egroup)(struct tvec_output */*o*/);
964         /* End a test group.  At least one test was attempted or @skipgroup@
965          * would have been called instead.  If @tv->curr[TVOUT_LOSE]@ is
966          * nonzero then the test group as a whole failed; otherwise it
967          * passed.
968          */
969
970   void (*btest)(struct tvec_output */*o*/);
971         /* Begin a test case. */
972
973   void (*skip)(struct tvec_output */*o*/,
974                const char */*excuse*/, va_list */*ap*/);
975         /* The test case is being skipped; @excuse@ may be null or a format
976          * string explaining why.  The @etest@ function will still be called
977          * (so this works differently from @skipgroup@ and @egroup@).  A test
978          * case can be skipped at most once, and, if skipped, it cannot fail.
979          */
980
981   void (*fail)(struct tvec_output */*o*/,
982                const char */*detail*/, va_list */*ap*/);
983         /* The test case failed.
984          *
985          * The output driver should preferably report the filename (@infile@)
986          * and line number (@test_lno@, not @lno@) for the failing test.
987          *
988          * The @detail@ may be null or a format string describing detail
989          * about how the failing test was run which can't be determined from
990          * the registers; a @detail@ is usually provided when (and only when)
991          * the test environment potentially invokes the test function more
992          * than once.
993          *
994          * A single test case can fail multiple times!
995          */
996
997   void (*dumpreg)(struct tvec_output */*o*/,
998                   unsigned /*disp*/, const union tvec_regval */*rv*/,
999                   const struct tvec_regdef */*rd*/);
1000         /* Dump a register.  The `disposition' @disp@ explains what condition
1001          * the register is in; see @tvec_dumpreg@ and the @TVRD_...@ codes.
1002          * The register value is at @rv@, and its definition, including its
1003          * type, at @rd@.  Note that this function may be called on virtual
1004          * registers which aren't in either of the register vectors or
1005          * mentioned by the test description.
1006          */
1007
1008   void (*etest)(struct tvec_output */*o*/, unsigned /*outcome*/);
1009         /* The test case concluded with the given @outcome@ (one of the
1010          * @TVOUT_...@ codes.
1011          */
1012
1013   void (*bbench)(struct tvec_output */*o*/,
1014                  const char */*ident*/, unsigned /*unit*/);
1015         /* Begin a benchmark.  The @ident@ is a formatted string identifying
1016          * the benchmark based on the values of the input registered marked
1017          * @TVRF_ID@; the output driver is free to use this or come up with
1018          * its own way to identify the test, e.g., by inspecting the register
1019          * values for itself.  The @unit@ is one of the @TVBU_...@ constants
1020          * explaining what sort of thing is being measured.
1021          */
1022
1023   void (*ebench)(struct tvec_output */*o*/,
1024                  const char */*ident*/, unsigned /*unit*/,
1025                  const struct bench_timing */*tm*/);
1026         /* End a benchmark.  The @ident@ and @unit@ arguments are as for
1027          * @bbench@.  If @tm@ is zero then the measurement failed; otherwise
1028          * @tm->n@ counts total number of things (operations or bytes, as
1029          * indicated by @unit@) processed in the indicated time.
1030          */
1031
1032   void (*error)(struct tvec_output */*o*/,
1033                 const char */*msg*/, va_list */*ap*/);
1034         /* Report an error.  The driver should ideally report the filename
1035          * (@infile@) and line number (@lno@) prompting the error.
1036          */
1037
1038   void (*notice)(struct tvec_output */*o*/,
1039                  const char */*msg*/, va_list */*ap*/);
1040         /* Report a miscellaneous message.  The driver should ideally report
1041          * the filename (@infile@) and line number (@lno@) prompting the
1042          * error.
1043          */
1044
1045   void (*destroy)(struct tvec_output */*o*/);
1046         /* Release any resources acquired by the driver. */
1047 };
1048
1049 /* --- @tvec_error@, @tvec_error_v@ --- *
1050  *
1051  * Arguments:   @struct tvec_state *tv@ = test-vector state
1052  *              @const char *msg@, @va_list ap@ = error message
1053  *
1054  * Returns:     @-1@.
1055  *
1056  * Use:         Report an error.  Errors are distinct from test failures,
1057  *              and indicate that a problem was encountered which compromised
1058  *              the activity of testing.
1059  */
1060
1061 extern int PRINTF_LIKE(2, 3)
1062   tvec_error(struct tvec_state */*tv*/, const char */*msg*/, ...);
1063 extern int tvec_error_v(struct tvec_state */*tv*/,
1064                         const char */*msg*/, va_list */*ap*/);
1065
1066 /* --- @tvec_notice@, @tvec_notice_v@ --- *
1067  *
1068  * Arguments:   @struct tvec_state *tv@ = test-vector state
1069  *              @const char *msg@, @va_list ap@ = message
1070  *
1071  * Returns:     ---
1072  *
1073  * Use:         Output a notice: essentially, some important information
1074  *              which doesn't fit into any of the existing categories.
1075  */
1076
1077 extern void PRINTF_LIKE(2, 3)
1078   tvec_notice(struct tvec_state */*tv*/, const char */*msg*/, ...);
1079 extern void tvec_notice_v(struct tvec_state */*tv*/,
1080                           const char */*msg*/, va_list */*ap*/);
1081
1082 /* --- @tvec_humanoutput@ --- *
1083  *
1084  * Arguments:   @FILE *fp@ = output file to write on
1085  *
1086  * Returns:     An output formatter.
1087  *
1088  * Use:         Return an output formatter which writes on @fp@ with the
1089  *              expectation that a human will be watching and interpreting
1090  *              the output.  If @fp@ denotes a terminal, the display shows a
1091  *              `scoreboard' indicating the outcome of each test case
1092  *              attempted, and may in addition use colour and other
1093  *              highlighting.
1094  */
1095
1096 extern struct tvec_output *tvec_humanoutput(FILE */*fp*/);
1097
1098 /* --- @tvec_tapoutput@ --- *
1099  *
1100  * Arguments:   @FILE *fp@ = output file to write on
1101  *
1102  * Returns:     An output formatter.
1103  *
1104  * Use:         Return an output formatter which writes on @fp@ in `TAP'
1105  *              (`Test Anything Protocol') format.
1106  *
1107  *              TAP comes from the Perl community, but has spread rather
1108  *              further.  This driver produces TAP version 13.  The driver
1109  *              produces a TAP `test point' -- i.e., a result reported as
1110  *              `ok' or `not ok' -- for each input test group.  Failure
1111  *              reports and register dumps are produced as diagnostic
1112  *              messages before the final group result.  (TAP permits
1113  *              structuerd YAML data after the test-point result, which could
1114  *              be used to report details, but (a) postponing the details
1115  *              until after the report is inconvenient, and (b) there is no
1116  *              standardization for the YAML anyway, so in practice it's no
1117  *              more useful than the unstructured diagnostics.
1118  */
1119
1120 extern struct tvec_output *tvec_tapoutput(FILE */*fp*/);
1121
1122 /* --- @tvec_dfltoutput@ --- *
1123  *
1124  * Arguments:   @FILE *fp@ = output file to write on
1125  *
1126  * Returns:     An output formatter.
1127  *
1128  * Use:         Selects and instantiates an output formatter suitable for
1129  *              writing on @fp@.  The policy is subject to change, but
1130  *              currently the `human' output format is selected if @fp@ is
1131  *              interactive (i.e., if @isatty(fileno(fp))@ is true), and
1132  *              otherwise the `tap' format is used.
1133  */
1134
1135 extern struct tvec_output *tvec_dfltout(FILE */*fp*/);
1136
1137 /*----- Register types ----------------------------------------------------*/
1138
1139 struct tvec_regty {
1140   /* A register type. */
1141
1142   void (*init)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/);
1143         /* Initialize the value in @*rv@.  This will be called before any
1144          * other function acting on the value, including @release@.
1145          */
1146
1147   void (*release)(union tvec_regval */*rv*/,
1148                   const struct tvec_regdef */*rd*/);
1149         /* Release any resources associated with the value in @*rv@. */
1150
1151   int (*eq)(const union tvec_regval */*rv0*/,
1152             const union tvec_regval */*rv1*/,
1153             const struct tvec_regdef */*rd*/);
1154         /* Return nonzero if @*rv0@ and @*rv1@ are equal values.
1155          * Asymmetric criteria are permitted: @tvec_checkregs@ calls @eq@
1156          * with the input register as @rv0@ and the output as @rv1@.
1157          */
1158
1159   int (*tobuf)(buf */*b*/, const union tvec_regval */*rv*/,
1160                const struct tvec_regdef */*rd*/);
1161         /* Serialize the value @*rv@, writing the result to @b@.  Return
1162          * zero on success, or @-1@ on error.
1163          */
1164
1165   int (*frombuf)(buf */*b*/, union tvec_regval */*rv*/,
1166                  const struct tvec_regdef */*rd*/);
1167         /* Deserialize a value from @b@, storing it in @*rv@.  Return zero on
1168          * success, or @-1@ on error.
1169          */
1170
1171   int (*parse)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/,
1172                struct tvec_state */*tv*/);
1173         /* Parse a value from @tv->fp@, storing it in @*rv@.  Return zero on
1174          * success, or @-1@ on error, having reported one or more errors via
1175          * @tvec_error@ or @tvec_syntax@.  A successful return should leave
1176          * the input position at the start of the next line; the caller will
1177          * flush the remainder of the line itself.
1178          */
1179
1180   void (*dump)(const union tvec_regval */*rv*/,
1181                const struct tvec_regdef */*rd*/,
1182                unsigned /*style*/,
1183                const struct gprintf_ops */*gops*/, void */*go*/);
1184 #define TVSF_COMPACT 1u
1185         /* Write a human-readable representation of the value @*rv@ using
1186          * @gprintf@ on @gops@ and @go@.  The @style@ is a collection of
1187          * flags: if @TVSF_COMPACT@ is set, then output should be minimal,
1188          * and must fit on a single line; otherwise, output may consist of
1189          * multiple lines and may contain redundant information if that is
1190          * likely to be useful to a human reader.
1191          */
1192 };
1193
1194 /*----- Integer types: signed and unsigned ------------------------------- */
1195
1196 /* Integers may be input in decimal, hex, binary, or octal, following
1197  * approximately usual conventions.
1198  *
1199  *   * Signed integers may be preceded with a `+' or `-' sign.
1200  *
1201  *   * Decimal integers are just a sequence of decimal digits `0' ... `9'.
1202  *
1203  *   * Octal integers are a sequence of digits `0' ... `7', preceded by `0o'
1204  *     or `0O'.
1205  *
1206  *   * Hexadecimal integers are a sequence of digits `0' ... `9', `a'
1207  *     ... `f', or `A' ... `F', preceded by `0x' or `0X'.
1208  *
1209  *   * Radix-B integers are a sequence of digits `0' ... `9', `a' ... `f', or
1210  *     `A' ... `F', each with value less than B, preceded by `Br' or `BR',
1211  *     where 0 < B < 36 is expressed in decimal without any leading `0' or
1212  *     internal underscores `_'.
1213  *
1214  *   * A digit sequence may contain internal underscore `_' separators, but
1215  *     not before or after all of the digits; and two consecutive `_'
1216  *     characters are not permitted.
1217  */
1218
1219 extern const struct tvec_regty tvty_int, tvty_uint;
1220
1221 /* The @arg.p@ slot may be null or a pointer to @struct tvec_irange@ or
1222  * @struct tvec_urange@ as appropriate.  The bounds are inclusive; use, e.g.,
1223  * @LONG_MAX@ explicitly if one or the other bound is logically inapplicable.
1224  */
1225 struct tvec_irange { long min, max; };
1226 struct tvec_urange { unsigned long min, max; };
1227
1228 /* Bounds corresponding to common integer types. */
1229 extern const struct tvec_irange
1230   tvrange_schar, tvrange_short, tvrange_int, tvrange_long,
1231   tvrange_sbyte, tvrange_i16, tvrange_i32;
1232 extern const struct tvec_urange
1233   tvrange_uchar, tvrange_ushort, tvrange_uint, tvrange_ulong, tvrange_size,
1234   tvrange_byte, tvrange_u16, tvrange_u32;
1235
1236 /* --- tvec_claimeq_int@, @TVEC_CLAIMEQ_INT@ --- *
1237  *
1238  * Arguments:   @struct tvec_state *tv@ = test-vector state
1239  *              @long i0, i1@ = two signed integers
1240  *              @const char *file@, @unsigned @lno@ = calling file and line
1241  *              @const char *expr@ = the expression to quote on failure
1242  *
1243  * Returns:     Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1244  *
1245  * Use:         Check that values of @i0@ and @i1@ are equal.  As for
1246  *              @tvec_claim@ above, a test case is automatically begun and
1247  *              ended if none is already underway.  If the values are
1248  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1249  *              mismatched values are dumped: @i0@ is printed as the output
1250  *              value and @i1@ is printed as the input reference.
1251  *
1252  *              The @TVEC_CLAIM_INT@ macro is similar, only it (a) identifies
1253  *              the file and line number of the call site automatically, and
1254  *              (b) implicitly quotes the source text of the @i0@ and @i1@
1255  *              arguments in the failure message.
1256  */
1257
1258 extern int tvec_claimeq_int(struct tvec_state */*tv*/,
1259                             long /*i0*/, long /*i1*/,
1260                             const char */*file*/, unsigned /*lno*/,
1261                             const char */*expr*/);
1262 #define TVEC_CLAIMEQ_INT(tv, i0, i1)                                    \
1263         (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1))
1264
1265 /* --- @tvec_claimeq_uint@, @TVEC_CLAIMEQ_UINT@ --- *
1266  *
1267  * Arguments:   @struct tvec_state *tv@ = test-vector state
1268  *              @unsigned long u0, u1@ = two unsigned integers
1269  *              @const char *file@, @unsigned @lno@ = calling file and line
1270  *              @const char *expr@ = the expression to quote on failure
1271  *
1272  * Returns:     Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1273  *
1274  * Use:         Check that values of @u0@ and @u1@ are equal.  As for
1275  *              @tvec_claim@ above, a test case is automatically begun and
1276  *              ended if none is already underway.  If the values are
1277  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1278  *              mismatched values are dumped: @u0@ is printed as the output
1279  *              value and @u1@ is printed as the input reference.
1280  *
1281  *              The @TVEC_CLAIM_UINT@ macro is similar, only it (a)
1282  *              identifies the file and line number of the call site
1283  *              automatically, and (b) implicitly quotes the source text of
1284  *              the @u0@ and @u1@ arguments in the failure message.
1285  */
1286
1287 extern int tvec_claimeq_uint(struct tvec_state */*tv*/,
1288                             unsigned long /*u0*/, unsigned long /*u1*/,
1289                             const char */*file*/, unsigned /*lno*/,
1290                             const char */*expr*/);
1291 #define TVEC_CLAIMEQ_UINT(tv, u0, u1)                                   \
1292         (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1))
1293
1294 /*----- Floating-point type -----------------------------------------------*/
1295
1296 /* Floating-point are either NaN (`#nan', if supported by the platform);
1297  * positive or negative infinity (`#inf', `+#inf', or, preferred, `#+inf',
1298  * and `-#inf' or, preferred, `#-inf', if supported by the platform), or a
1299  * number in strtod(3) syntax.
1300  *
1301  * The comparison rules for floating-point numbers are complex: see
1302  * @tvec_claimeqish_float@ for details.
1303  */
1304
1305 extern const struct tvec_regty tvty_float;
1306
1307 struct tvec_floatinfo {
1308   /* Details about acceptable floating-point values. */
1309
1310   unsigned f;                           /* flags (@TVFF_...@ bits) */
1311 #define TVFF_NOMIN 1u                   /*   ignore @min@ (allow -inf) */
1312 #define TVFF_NOMAX 2u                   /*   ignore @max@ (allow +inf) */
1313 #define TVFF_NANOK 4u                   /*   permit NaN */
1314 #define TVFF_EQMASK 0xf0                /*   how to compare */
1315 #define TVFF_EXACT 0x00                 /*     must equal exactly */
1316 #define TVFF_ABSDELTA 0x10              /*     must be within @delta@ */
1317 #define TVFF_RELDELTA 0x20              /*     diff < @delta@ fraction */
1318   double min, max;                      /* smallest/largest value allowed */
1319   double delta;                         /* maximum tolerable difference */
1320 };
1321
1322 /* --- @tvec_claimeqish_float@, @TVEC_CLAIMEQISH_FLOAT@ --- *
1323  *
1324  * Arguments:   @struct tvec_state *tv@ = test-vector state
1325  *              @double f0, f1@ = two floating-point numbers
1326  *              @unsigned f@ = flags (@TVFF_...@)
1327  *              @double delta@ = maximum tolerable difference
1328  *              @const char *file@, @unsigned @lno@ = calling file and line
1329  *              @const char *expr@ = the expression to quote on failure
1330  *
1331  * Returns:     Nonzero if @f0@ and @u1@ are sufficiently close, otherwise
1332  *              zero.
1333  *
1334  * Use:         Check that values of @f0@ and @f1@ are sufficiently close.
1335  *              As for @tvec_claim@ above, a test case is automatically begun
1336  *              and ended if none is already underway.  If the values are
1337  *              too far apart, then @tvec_fail@ is called, quoting @expr@,
1338  *              and the mismatched values are dumped: @f0@ is printed as the
1339  *              output value and @f1@ is printed as the input reference.
1340  *
1341  *              The details for the comparison are as follows.
1342  *
1343  *                * A NaN value matches any other NaN, and nothing else.
1344  *
1345  *                * An infinity matches another infinity of the same sign,
1346  *                  and nothing else.
1347  *
1348  *                * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
1349  *                  representable number matches only itself: in particular,
1350  *                  positive and negative zero are considered distinct.
1351  *                  (This allows tests to check that they land on the correct
1352  *                  side of branch cuts, for example.)
1353  *
1354  *                * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
1355  *                  %$y$% when %$|x - y| < \delta$%.
1356  *
1357  *                * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
1358  *                  %$y$% when %$|1 - y/x| < \delta$%.  (Note that this
1359  *                  criterion is asymmetric FIXME
1360  *
1361  *              The @TVEC_CLAIM_FLOAT@ macro is similar, only it (a)
1362  *              identifies the file and line number of the call site
1363  *              automatically, and (b) implicitly quotes the source text of
1364  *              the @f0@ and @f1@ arguments (and @delta@) in the failure
1365  *              message.
1366  */
1367
1368 extern int tvec_claimeqish_float(struct tvec_state */*tv*/,
1369                                  double /*f0*/, double /*f1*/,
1370                                  unsigned /*f*/, double /*delta*/,
1371                                  const char */*file*/, unsigned /*lno*/,
1372                                  const char */*expr*/);
1373 #define TVEC_CLAIMEQISH_FLOAT(tv, f0, f1, f, delta)                     \
1374         (tvec_claimeqish_float(tv, f0, f1, f, delta, , __FILE__, __LINE__, \
1375                                #f0 " /= " #f1 " (+/- " #delta ")"))
1376
1377 /* --- @tvec_claimeq_float@, @TVEC_CLAIMEQ_FLOAT@ --- *
1378  *
1379  * Arguments:   @struct tvec_state *tv@ = test-vector state
1380  *              @double f0, f1@ = two floating-point numbers
1381  *              @const char *file@, @unsigned @lno@ = calling file and line
1382  *              @const char *expr@ = the expression to quote on failure
1383  *
1384  * Returns:     Nonzero if @f0@ and @u1@ are identical, otherwise zero.
1385  *
1386  * Use:         Check that values of @f0@ and @f1@ are identical.  The
1387  *              function is exactly equivalent to @tvec_claimeqish_float@
1388  *              with @f == TVFF_EXACT@; the macro is similarly like
1389  *              @TVEC_CLAIMEQISH_FLOAT@ with @f == TVFF_EXACT@, except that
1390  *              it doesn't bother to quote a delta.
1391  */
1392
1393 extern int tvec_claimeq_float(struct tvec_state */*tv*/,
1394                               double /*f0*/, double /*f1*/,
1395                               const char */*file*/, unsigned /*lno*/,
1396                               const char */*expr*/);
1397 #define TVEC_CLAIMEQ_FLOAT(tv, f0, f1)                                  \
1398         (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1))
1399
1400 /*----- Enumerated types --------------------------------------------------*/
1401
1402 /* There is a distinct enumerated type for each of the branches of
1403  * @tvec_misc@.  In the following, we write @t@ for the type code, which is
1404  * @i@ for signed integer, @u@ for unsigned integer, @f@ for floating-point,
1405  * and @p@ for pointer.
1406  */
1407
1408 #define DEFENUMTY(tag, ty, slot)                                        \
1409         extern const struct tvec_regty tvty_##slot##enum;
1410 TVEC_MISCSLOTS(DEFENUMTY)
1411 #undef DEFENUMTY
1412
1413 /* A @struct tvec_tassoc@ associates a string tag with a value. */
1414 #define DEFASSOC(tag_, ty, slot)                                        \
1415         struct tvec_##slot##assoc { const char *tag; ty slot; };
1416 TVEC_MISCSLOTS(DEFASSOC)
1417 #undef DEFASSOC
1418
1419 /* Information about an enumerated type. */
1420 #define DEFINFO(tag, ty, slot)                                          \
1421         struct tvec_##slot##enuminfo {                                  \
1422           const char *name;             /* type name for diagnostics */ \
1423           const struct tvec_##slot##assoc *av; /* name/value mappings */ \
1424           EXTRA_##tag##_INFOSLOTS       /* type-specific extra info */  \
1425         };
1426
1427 #define EXTRA_INT_INFOSLOTS                                             \
1428         const struct tvec_irange *ir;   /* allowed range of raw values */
1429
1430 #define EXTRA_UINT_INFOSLOTS                                            \
1431         const struct tvec_urange *ur;   /* allowed range of raw values */
1432
1433 #define EXTRA_FLT_INFOSLOTS                                             \
1434         const struct tvec_floatinfo *fi; /* range and matching policy */
1435
1436 #define EXTRA_PTR_INFOSLOTS             /* (nothing) */
1437
1438 TVEC_MISCSLOTS(DEFINFO)
1439
1440 #undef EXTRA_INT_INFOSLOTS
1441 #undef EXTRA_UINT_INFOSLOTS
1442 #undef EXTRA_FLT_INFOSLOTS
1443 #undef EXTRA_PTR_INFOSLOTS
1444
1445 #undef DEFINFO
1446
1447 /* Standard enumerations. */
1448 const struct tvec_ienuminfo tvenum_bool;
1449
1450 /* --- @tvec_claimeq_tenum@, @TVEC_CLAIMEQ_TENUM@ --- *
1451  *
1452  * Arguments:   @struct tvec_state *tv@ = test-vector state
1453  *              @ty t0, t1@ = two values
1454  *              @const char *file@, @unsigned @lno@ = calling file and line
1455  *              @const char *expr@ = the expression to quote on failure
1456  *
1457  * Returns:     Nonzero if @t0@ and @t1@ are equal, otherwise zero.
1458  *
1459  * Use:         Check that values of @t0@ and @t1@ are equal.  As for
1460  *              @tvec_claim@ above, a test case is automatically begun and
1461  *              ended if none is already underway.  If the values are
1462  *              unequal, then @tvec_fail@ is called, quoting @expr@, and the
1463  *              mismatched values are dumped: @u0@ is printed as the output
1464  *              value and @u1@ is printed as the input reference.
1465  *
1466  *              The @TVEC_CLAIM_TENUM@ macro is similar, only it (a)
1467  *              identifies the file and line number of the call site
1468  *              automatically, and (b) implicitly quotes the source text of
1469  *              the @u0@ and @u1@ arguments in the failure message.
1470  */
1471
1472 #define DECLCLAIM(tag, ty, slot)                                        \
1473         extern int tvec_claimeq_##slot##enum                            \
1474           (struct tvec_state */*tv*/,                                   \
1475            const struct tvec_##slot##enuminfo */*ei*/,                  \
1476            ty /*t0*/, ty /*t1*/,                                        \
1477            const char */*file*/, unsigned /*lno*/, const char */*expr*/);
1478 TVEC_MISCSLOTS(DECLCLAIM)
1479 #undef DECLCLAIM
1480 #define TVEC_CLAIMEQ_IENUM(tv, ei, i0, i1)                              \
1481         (tvec_claimeq_ienum(tv, ei, i0, i1,                             \
1482                             __FILE__, __LINE__, #i0 " /= " #i1))
1483 #define TVEC_CLAIMEQ_UENUM(tv, ei, u0, u1)                              \
1484         (tvec_claimeq_uenum(tv, ei, u0, u1,                             \
1485                             __FILE__, __LINE__, #u0 " /= " #u1))
1486 #define TVEC_CLAIMEQ_FENUM(tv, ei, f0, f1)                              \
1487         (tvec_claimeq_fenum(tv, ei, f0, f1,                             \
1488                             __FILE__, __LINE__, #f0 " /= " #f1))
1489 #define TVEC_CLAIMEQ_PENUM(tv, ei, p0, p1)                              \
1490         (tvec_claimeq_penum(tv, ei, p0, p1,                             \
1491                             __FILE__, __LINE__, #p0 " /= " #p1))
1492
1493 /*----- Flags type -------------------------------------------------------*/
1494
1495 extern const struct tvec_regty tvty_flags;
1496
1497 struct tvec_flag {
1498   /* Definition of a single flag or bitfield value.
1499    *
1500    * Each named setting comes with a value @v@ and a mask @m@; the mask
1501    * should cover all of the value bits, i.e., @(v&~m) == 0@.  On input,
1502    * exactly 
1503    */
1504
1505   const char *tag;                      /* name */
1506   unsigned long m, v;                   /* mask and value */
1507 };
1508
1509 struct tvec_flaginfo {
1510   const char *name;
1511   const struct tvec_flag *fv;
1512   const struct tvec_urange *range;
1513 };
1514
1515 extern int tvec_claimeq_flags(struct tvec_state */*tv*/,
1516                               const struct tvec_flaginfo */*fi*/,
1517                               unsigned long /*f0*/, unsigned long /*f1*/,
1518                               const char */*file*/, unsigned /*lno*/,
1519                               const char */*expr*/);
1520 #define TVEC_CLAIMEQ_FLAGS(tv, fi, f0, f1)                              \
1521         (tvec_claimeq_flags(tv, fi, f0, f1,                             \
1522                             __FILE__, __LINE__, #f0 " /= " #f1))
1523
1524 extern const struct tvec_regty tvty_char;
1525 extern int tvec_claimeq_char(struct tvec_state */*tv*/,
1526                              int /*ch0*/, int /*ch1*/,
1527                              const char */*file*/, unsigned /*lno*/,
1528                              const char */*expr*/);
1529 #define TVEC_CLAIMEQ_CHAR(tv, c0, c1)                                   \
1530         (tvec_claimeq_char(tv, c0, c1, __FILE__, __LINE__, #c0 " /= " #c1))
1531
1532 extern const struct tvec_regty tvty_string, tvty_bytes;
1533
1534 extern int tvec_claimeq_string(struct tvec_state */*tv*/,
1535                                const char */*p0*/, size_t /*sz0*/,
1536                                const char */*p1*/, size_t /*sz1*/,
1537                                const char */*file*/, unsigned /*lno*/,
1538                                const char */*expr*/);
1539 extern int tvec_claimeq_strz(struct tvec_state */*tv*/,
1540                              const char */*p0*/, const char */*p1*/,
1541                              const char */*file*/, unsigned /*lno*/,
1542                              const char */*expr*/);
1543 extern int tvec_claimeq_bytes(struct tvec_state */*tv*/,
1544                                const void */*p0*/, size_t /*sz0*/,
1545                                const void */*p1*/, size_t /*sz1*/,
1546                                const char */*file*/, unsigned /*lno*/,
1547                                const char */*expr*/);
1548 #define TVEC_CLAIMEQ_STRING(tv, p0, sz0, p1, sz1)                       \
1549         (tvec_claimeq_string(tv, p0, sz0, p1, sz1, __FILE__, __LINE__,  \
1550                              #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
1551 #define TVEC_CLAIMEQ_STRZ(tv, p0, p1)                                   \
1552         (tvec_claimeq_strz(tv, p0, p1, __FILE__, __LINE__, #p0 " /= " #p1))
1553 #define TVEC_CLAIMEQ_BYTES(tv, p0, sz0, p1, sz1)                        \
1554         (tvec_claimeq(tv, p0, sz0, p1, sz1, __FILE__, __LINE__,         \
1555                       #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
1556
1557 extern const struct tvec_regty tvty_buffer;
1558
1559 extern void tvec_allocstring(union tvec_regval */*rv*/, size_t /*sz*/);
1560 extern void tvec_allocbytes(union tvec_regval */*rv*/, size_t /*sz*/);
1561
1562 /*----- Benchmarking ------------------------------------------------------*/
1563
1564 struct tvec_bench {
1565   struct tvec_env _env;                 /* benchmarking is an environment */
1566   struct bench_state **bst;             /* benchmark state anchor or null */
1567   unsigned long niter;                  /* iterations done per unit */
1568   int riter, rbuf;                      /* iterations and buffer registers */
1569   const struct tvec_env *env;           /* environment (per test, not grp) */
1570 };
1571 #define TVEC_BENCHENV                                                   \
1572   { sizeof(struct tvec_benchctx),                                       \
1573     tvec_benchsetup,                                                    \
1574     tvec_benchset,                                                      \
1575     tvec_benchbefore,                                                   \
1576     tvec_benchrun,                                                      \
1577     tvec_benchafter,                                                    \
1578     tvec_benchteardown }
1579 #define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate
1580
1581 struct tvec_benchctx {
1582   const struct tvec_bench *b;
1583   struct bench_state *bst;
1584   double dflt_target;
1585   void *subctx;
1586 };
1587
1588 extern struct bench_state *tvec_benchstate;
1589
1590 extern int tvec_benchsetup(struct tvec_state */*tv*/,
1591                            const struct tvec_env */*env*/,
1592                            void */*pctx*/, void */*ctx*/);
1593 extern int tvec_benchset(struct tvec_state */*tv*/, const char */*var*/,
1594                          const struct tvec_env */*env*/, void */*ctx*/);
1595 extern int tvec_benchbefore(struct tvec_state */*tv*/, void */*ctx*/);
1596 extern void tvec_benchrun(struct tvec_state */*tv*/,
1597                           tvec_testfn */*fn*/, void */*ctx*/);
1598 extern void tvec_benchafter(struct tvec_state */*tv*/, void */*ctx*/);
1599 extern void tvec_benchteardown(struct tvec_state */*tv*/, void */*ctx*/);
1600
1601 extern void tvec_benchreport
1602   (const struct gprintf_ops */*gops*/, void */*go*/,
1603    unsigned /*unit*/, const struct bench_timing */*tm*/);
1604
1605 /*----- Command-line interface --------------------------------------------*/
1606
1607 extern const struct tvec_config tvec_adhocconfig;
1608
1609 extern void tvec_parseargs(int /*argc*/, char */*argv*/[],
1610                            struct tvec_state */*tv_out*/,
1611                            int */*argpos_out*/,
1612                            const struct tvec_config */*config*/);
1613
1614 extern int tvec_readstdin(struct tvec_state */*tv*/);
1615 extern int tvec_readfile(struct tvec_state */*tv*/, const char */*file*/);
1616 extern int tvec_readdflt(struct tvec_state */*tv*/, const char */*file*/);
1617 extern int tvec_readarg(struct tvec_state */*tv*/, const char */*arg*/);
1618
1619 extern int tvec_readargs(int /*argc*/, char */*argv*/[],
1620                          struct tvec_state */*tv*/,
1621                          int */*argpos_inout*/, const char */*dflt*/);
1622
1623 extern int tvec_main(int /*argc*/, char */*argv*/[],
1624                      const struct tvec_config */*config*/,
1625                      const char */*dflt*/);
1626
1627 /*----- That's all, folks -------------------------------------------------*/
1628
1629 #ifdef __cplusplus
1630   }
1631 #endif
1632
1633 #endif