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