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