3 * Test vector processing framework
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
35 /* Here's the overall flow for a testing session.
38 * -> output @bsession@
43 * -> type @init@ (input and output)
44 * -> type @parse@ (input)
48 * -> output @skipgroup@
83 * -> output @skipgroup@
88 * -> output @esession@
92 * -> type @dump@ (compact style)
97 * -> @tvec_benchreport@
99 * The output functions @error@ and @notice@ can be called at arbitrary
103 /*----- Header files ------------------------------------------------------*/
114 #ifndef MLIB_CONTROL_H
115 # include "control.h"
122 #ifndef MLIB_GPRINTF_H
123 # include "gprintf.h"
130 #ifndef MLIB_MACROS_H
134 /*----- Miscellaneous values ----------------------------------------------*/
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.
140 #define TVEC_MISCSLOTS(_) \
141 _(PTR, const void *, p) /* arbitrary pointer */ \
142 _(INT, long, i) /* signed integer */ \
143 _(UINT, unsigned long, u) /* signed integer */ \
144 _(FLT, double, f) /* floating point */
147 #define TVEC_DEFSLOT(tag, ty, slot) ty slot;
148 TVEC_MISCSLOTS(TVEC_DEFSLOT)
152 #define TVEC_DEFCONST(tag, ty, slot) TVMISC_##tag,
153 TVEC_MISCSLOTS(TVEC_DEFCONST)
157 /*----- Register values ---------------------------------------------------*/
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.
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.)
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.
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
191 long i; /* signed integer */
192 unsigned long u; /* unsigned integer */
193 void *p; /* pointer */
194 double f; /* floating point */
195 struct { char *p; size_t sz; } text; /* text string */
196 struct { unsigned char *p; size_t sz; } bytes; /* binary string of bytes */
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 */
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.)
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.
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.
225 unsigned f; /* flags */
226 #define TVRF_LIVE 1u /* used in current test */
227 union tvec_regval v; /* register value */
231 /* A register definition. Register definitions list the registers which
232 * are used by a particular test group (see `struct tvec_test' below).
234 * A vector of register definitions is terminated by a definition whose
235 * `name' slot is null.
238 const char *name; /* register name (for input files) */
239 const struct tvec_regty *ty; /* register type descriptor */
240 unsigned i; /* register index */
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 */
246 #define TVEC_ENDREGS { 0, 0, 0, 0, { 0 } }
248 /* @TVEC_GREG(vec, i, regsz)@
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.
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.
260 #define TVEC_GREG(vec, i, regsz) \
261 ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz)))
263 /*----- Register types ----------------------------------------------------*/
265 struct tvec_state; /* forward declaration */
268 /* A register type. */
270 void (*init)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/);
271 /* Initialize the value in @*rv@. This will be called before any other
272 * function acting on the value, including @release@.
275 void (*release)(union tvec_regval */*rv*/,
276 const struct tvec_regdef */*rd*/);
277 /* Release any resources associated with the value in @*rv@. */
279 int (*eq)(const union tvec_regval */*rv0*/,
280 const union tvec_regval */*rv1*/,
281 const struct tvec_regdef */*rd*/);
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@.
287 int (*tobuf)(buf */*b*/, const union tvec_regval */*rv*/,
288 const struct tvec_regdef */*rd*/);
289 /* Serialize the value @*rv@, writing the result to @b@. Return zero on
290 * success, or %$-1$% on error.
293 int (*frombuf)(buf */*b*/, union tvec_regval */*rv*/,
294 const struct tvec_regdef */*rd*/);
295 /* Deserialize a value from @b@, storing it in @*rv@. Return zero on
296 * success, or %$-1$% on error.
299 int (*parse)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/,
300 struct tvec_state */*tv*/);
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.
308 void (*dump)(const union tvec_regval */*rv*/,
309 const struct tvec_regdef */*rd*/,
311 const struct gprintf_ops */*gops*/, void */*go*/);
312 #define TVSF_COMPACT 1u
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
322 /*----- Test descriptions -------------------------------------------------*/
326 typedef void tvec_testfn(const struct tvec_reg */*in*/,
327 struct tvec_reg */*out*/,
329 /* A test function. It should read inputs from @in@ and write outputs to
330 * @out@. The @TVRF_LIVE@ is set on inputs which are actually present, and
331 * on outputs which are wanted to test. A test function can set additional
332 * `gratuitous outputs' by setting @TVRF_LIVE@ on them; clearing
333 * @TVRF_LIVE@ on a wanted output causes a mismatch.
335 * A test function may be called zero or more times by the environment. In
336 * particular, it may be called multiple times, though usually by prior
337 * arrangement with the environment.
339 * The @ctx@ is supplied by the environment's @run@ function (see below).
340 * The default environment calls the test function once, with a null
341 * @ctx@. There is no expectation that the environment's context has
342 * anything to do with the test function's context.
345 typedef 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.
352 size_t regsz; /* (minimum) register size */
353 tvec_setvarfn *setvar; /* function to set variable */
354 struct tvec_regdef def; /* register definition */
357 typedef 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
363 * @tvec_skipgroup@ with a suitable excuse. The @set@, @after@, and
364 * @teardown@ entry points will still be called, but @before@ and @run@
368 typedef 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.
378 typedef 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
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@
387 typedef 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.
397 typedef void tvec_envafterfn(struct tvec_state */*tv*/, void */*ctx*/);
398 /* Called after running or skipping a test. Typical actions involve
399 * resetting whatever things were established by @set@. This function
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.
405 typedef void tvec_envteardownfn(struct tvec_state */*tv*/, void */*ctx*/);
406 /* Tear down the environment: called at the end of a test group. */
410 /* A test environment sets things up for and arranges to run the test.
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.
418 size_t ctxsz; /* environment context size */
420 tvec_envsetupfn *setup; /* setup for group */
421 tvec_envfindvarfn *findvar; /* find variable */
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 */
429 /* A test description. */
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 */
436 #define TVEC_ENDTESTS { 0, 0, 0, 0 }
438 /*----- Test state --------------------------------------------------------*/
441 /* Possible test outcomes. */
443 TVOUT_LOSE, /* test failed */
444 TVOUT_SKIP, /* test skipped */
445 TVOUT_WIN, /* test passed */
446 TVOUT_XFAIL, /* test passed, but shouldn't have */
447 TVOUT_LIMIT /* (number of possible outcomes) */
451 /* An overall test configuration. */
453 const struct tvec_test *tests; /* the tests to be performed */
454 unsigned nrout, nreg; /* number of output/total regs */
455 size_t regsz; /* size of a register */
459 /* The primary state structure for the test vector machinery. */
461 /* Flags. Read-only for all callers. */
462 unsigned f; /* flags */
463 #define TVSF_SKIP 0x0001u /* skip this test group */
464 #define TVSF_OPEN 0x0002u /* test is open */
465 #define TVSF_ACTIVE 0x0004u /* test is active */
466 #define TVSF_ERROR 0x0008u /* an error occurred */
467 #define TVSF_OUTMASK 0x00f0u /* test outcome (@TVOUT_...@) */
468 #define TVSF_OUTSHIFT 4 /* shift applied to outcome */
469 #define TVSF_XFAIL 0x0100u /* test expected to fail */
470 #define TVSF_MUFFLE 0x0200u /* muffle errors */
472 /* Test configuration. Read-only for all callers. */
473 struct tvec_config cfg; /* test configuration */
475 /* Registers. Available to execution environments, which may modify the
476 * contents of the active registers, as defined by the current test group,
477 * but not the vector pointers themselves or inactive registers.
479 struct tvec_reg *in, *out; /* register vectors */
481 /* Test group state. Read-only for all callers. */
482 const struct tvec_test *test; /* current test */
484 /* Test scoreboard. Available to output formatters. */
485 unsigned curr[TVOUT_LIMIT], all[TVOUT_LIMIT], grps[TVOUT_LIMIT];
487 /* Output machinery. */
488 struct tvec_output *output; /* output formatter */
490 /* Input machinery. Available to type parsers. */
491 const char *infile; unsigned lno, test_lno; /* input file name, line */
492 FILE *fp; /* input file stream */
495 /* @TVEC_REG(tv, vec, i)@
497 * If @tv@ is a pointer to a @struct tvec_state@, @vec@ is either @in@ or
498 * @out@, and @i@ is an integer, then this evaluates to the address of the
499 * @i@th register in the selected vector.
501 #define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->cfg.regsz)
503 /*----- Output formatting -------------------------------------------------*/
506 /* An output formatter. */
507 const struct tvec_outops *ops; /* pointer to operations */
511 /* Register output dispositions. */
513 TVRD_INPUT, /* input-only register */
514 TVRD_OUTPUT, /* output-only (input is dead) */
515 TVRD_MATCH, /* matching (equal) registers */
516 TVRD_FOUND, /* mismatching output register */
517 TVRD_EXPECT, /* mismatching input register */
518 TVRD_LIMIT /* (number of dispositions) */
521 #define TVEC_LEVELS(_) \
522 _(NOTE, "notice", 4) \
525 #define TVEC_DEFLEVEL(tag, name, val) TVLEV_##tag = val,
526 TVEC_LEVELS(TVEC_DEFLEVEL)
531 /* Benchmarking details. */
533 TVBU_OP, /* counting operations of some kind */
534 TVBU_BYTE, /* counting bytes (@rbuf >= 0@) */
535 TVBU_LIMIT /* (number of units) */
537 struct bench_timing; /* include <mLib/bench.h> for the real definition */
540 /* Output operations. */
542 void (*bsession)(struct tvec_output */*o*/, struct tvec_state */*tv*/);
543 /* Begin a test session. The output driver will probably want to
544 * save @tv@, because this isn't provided to any other methods.
547 int (*esession)(struct tvec_output */*o*/);
548 /* End a session, and return the suggested exit code. */
550 void (*bgroup)(struct tvec_output */*o*/);
551 /* Begin a test group. The test group description is @tv->test@. */
553 void (*skipgroup)(struct tvec_output */*o*/,
554 const char */*excuse*/, va_list */*ap*/);
555 /* The group is being skipped; @excuse@ may be null or a format
556 * string explaining why. The @egroup@ method will not be called
560 void (*egroup)(struct tvec_output */*o*/);
561 /* End a test group. At least one test was attempted or @skipgroup@
562 * would have been called instead. If @tv->curr[TVOUT_LOSE]@ is nonzero
563 * then the test group as a whole failed; otherwise it passed.
566 void (*btest)(struct tvec_output */*o*/);
567 /* Begin a test case. */
569 void (*skip)(struct tvec_output */*o*/,
570 const char */*excuse*/, va_list */*ap*/);
571 /* The test case is being skipped; @excuse@ may be null or a format
572 * string explaining why. The @etest@ function will still be called (so
573 * this works differently from @skipgroup@ and @egroup@). A test case
574 * can be skipped at most once, and, if skipped, it cannot fail.
577 void (*fail)(struct tvec_output */*o*/,
578 const char */*detail*/, va_list */*ap*/);
579 /* The test case failed.
581 * The output driver should preferably report the filename (@infile@) and
582 * line number (@test_lno@, not @lno@) for the failing test.
584 * The @detail@ may be null or a format string describing detail about
585 * how the failing test was run which can't be determined from the
586 * registers; a @detail@ is usually provided when (and only when) the
587 * test environment potentially invokes the test function more than once.
589 * A single test case can fail multiple times!
592 void (*dumpreg)(struct tvec_output */*o*/,
593 unsigned /*disp*/, const union tvec_regval */*rv*/,
594 const struct tvec_regdef */*rd*/);
595 /* Dump a register. The `disposition' @disp@ explains what condition the
596 * register is in; see @tvec_dumpreg@ and the @TVRD_...@ codes. The
597 * register value is at @rv@, and its definition, including its type, at
598 * @rd@. Note that this function may be called on virtual registers
599 * which aren't in either of the register vectors or mentioned by the
600 * test description. It may also be called with @rv@ null, indicating
601 * that the register is not live.
604 void (*etest)(struct tvec_output */*o*/, unsigned /*outcome*/);
605 /* The test case concluded with the given @outcome@ (one of the
609 void (*bbench)(struct tvec_output */*o*/,
610 const char */*ident*/, unsigned /*unit*/);
611 /* Begin a benchmark. The @ident@ is a formatted string identifying the
612 * benchmark based on the values of the input registered marked
613 * @TVRF_ID@; the output driver is free to use this or come up with its
614 * own way to identify the test, e.g., by inspecting the register values
615 * for itself. The @unit@ is one of the @TVBU_...@ constants explaining
616 * what sort of thing is being measured.
619 void (*ebench)(struct tvec_output */*o*/,
620 const char */*ident*/, unsigned /*unit*/,
621 const struct bench_timing */*tm*/);
622 /* End a benchmark. The @ident@ and @unit@ arguments are as for
623 * @bbench@. If @tm@ is zero then the measurement failed; otherwise
624 * @tm->n@ counts total number of things (operations or bytes, as
625 * indicated by @unit@) processed in the indicated time.
628 void (*report)(struct tvec_output */*o*/, unsigned /*level*/,
629 const char */*msg*/, va_list */*ap*/);
630 /* Report a message. The driver should ideally report the filename
631 * (@infile@) and line number (@lno@) prompting the error.
634 void (*destroy)(struct tvec_output */*o*/);
635 /* Release any resources acquired by the driver. */
638 /*----- Session lifecycle -------------------------------------------------*/
640 /* --- @tvec_begin@ --- *
642 * Arguments: @struct tvec_state *tv_out@ = state structure to fill in
643 * @const struct tvec_config *config@ = test configuration
644 * @struct tvec_output *o@ = output driver
648 * Use: Initialize a state structure ready to do some testing.
651 extern void tvec_begin(struct tvec_state */*tv_out*/,
652 const struct tvec_config */*config*/,
653 struct tvec_output */*o*/);
655 /* --- @tvec_end@ --- *
657 * Arguments: @struct tvec_state *tv@ = test-vector state
659 * Returns: A proposed exit code.
661 * Use: Conclude testing and suggests an exit code to be returned to
662 * the calling program. (The exit code comes from the output
663 * driver's @esession@ method.)
666 extern int tvec_end(struct tvec_state */*tv*/);
668 /* --- @tvec_read@ --- *
670 * Arguments: @struct tvec_state *tv@ = test-vector state
671 * @const char *infile@ = the name of the input file
672 * @FILE *fp@ = stream to read from
674 * Returns: Zero on success, %$-1$% on error.
676 * Use: Read test vector data from @fp@ and exercise test functions.
677 * THe return code doesn't indicate test failures: it's only
678 * concerned with whether there were problems with the input
679 * file or with actually running the tests.
682 extern int tvec_read(struct tvec_state */*tv*/,
683 const char */*infile*/, FILE */*fp*/);
685 /*----- Command-line interface --------------------------------------------*/
687 extern const struct tvec_config tvec_adhocconfig;
688 /* A special @struct tvec_config@ to use for programs which perform ad-hoc
692 /* --- @tvec_parseargs@ --- *
694 * Arguments: @int argc@ = number of command-line arguments
695 * @char *argv[]@ = vector of argument strings
696 * @struct tvec_state *tv_out@ = test vector state to initialize
697 * @int *argpos_out@ = where to leave unread argument index
698 * @const struct tvec_config *cofig@ = test vector configuration
702 * Use: Parse arguments and set up the test vector state @*tv_out@.
703 * If errors occur, print messages to standard error and exit
707 extern void tvec_parseargs(int /*argc*/, char */*argv*/[],
708 struct tvec_state */*tv_out*/,
710 const struct tvec_config */*config*/);
712 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
714 * Arguments: @struct tvec_state *tv@ = test vector state
715 * @const char *file@ = pathname of file to read
716 * @const char *arg@ = argument to interpret
718 * Returns: Zero on success, %$-1$% on error.
720 * Use: Read test vector data from stdin or a named file. The
721 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
722 * and from the named file otherwise.
725 extern int tvec_readstdin(struct tvec_state */*tv*/);
726 extern int tvec_readfile(struct tvec_state */*tv*/, const char */*file*/);
727 extern int tvec_readarg(struct tvec_state */*tv*/, const char */*arg*/);
729 /* --- @tvec_readdflt@ --- *
731 * Arguments: @struct tvec_state *tv@ = test vector state
732 * @const char *dflt@ = defsault filename or null
734 * Returns: Zero on success, %$-1$% on error.
736 * Use: Reads from the default test vector data. If @file@ is null,
737 * then read from standard input, unless that's a terminal; if
738 * @file@ is not null, then read the named file, looking in the
739 * directory named by the `%|srcdir|%' environment variable if
740 * that's set, or otherwise in the current directory.
743 extern int tvec_readdflt(struct tvec_state */*tv*/, const char */*file*/);
745 /* --- @tvec_readargs@ --- *
747 * Arguments: @int argc@ = number of command-line arguments
748 * @char *argv[]@ = vector of argument strings
749 * @struct tvec_state *tv@ = test vector state
750 * @int *argpos_inout@ = current argument position (updated)
751 * @const char *dflt@ = default filename or null
753 * Returns: Zero on success, %$-1$% on error.
755 * Use: Reads from the sources indicated by the command-line
756 * arguments, in order, interpreting each as for @tvec_readarg@;
757 * if no arguments are given then read from @dflt@ as for
761 extern int tvec_readargs(int /*argc*/, char */*argv*/[],
762 struct tvec_state */*tv*/,
763 int */*argpos_inout*/, const char */*dflt*/);
765 /* --- @tvec_main@ --- *
767 * Arguments: @int argc@ = number of command-line arguments
768 * @char *argv[]@ = vector of argument strings
769 * @const struct tvec_config *cofig@ = test vector configuration
770 * @const char *dflt@ = default filename or null
772 * Returns: Exit code.
774 * Use: All-in-one test vector front-end. Parse options from the
775 * command-line as for @tvec_parseargs@, and then process the
776 * remaining positional arguments as for @tvec_readargs@. The
777 * function constructs and disposes of a test vector state.
780 extern int tvec_main(int /*argc*/, char */*argv*/[],
781 const struct tvec_config */*config*/,
782 const char */*dflt*/);
784 /*----- Test processing ---------------------------------------------------*/
786 /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
788 * Arguments: @struct tvec_state *tv@ = test-vector state
789 * @const char *excuse@, @va_list *ap@ = reason why skipped
793 * Use: Skip the current group. This should only be called from a
794 * test environment @setup@ function; a similar effect occurs if
795 * the @setup@ function fails.
798 extern PRINTF_LIKE(2, 3)
799 void tvec_skipgroup(struct tvec_state */*tv*/,
800 const char */*excuse*/, ...);
801 extern void tvec_skipgroup_v(struct tvec_state */*tv*/,
802 const char */*excuse*/, va_list */*ap*/);
804 /* --- @tvec_skip@, @tvec_skip_v@ --- *
806 * Arguments: @struct tvec_state *tv@ = test-vector state
807 * @const char *excuse@, @va_list *ap@ = reason why test skipped
811 * Use: Skip the current test. This should only be called from a
812 * test environment @run@ function; a similar effect occurs if
813 * the @before@ function fails.
816 extern PRINTF_LIKE(2, 3)
817 void tvec_skip(struct tvec_state */*tv*/, const char */*excuse*/, ...);
818 extern void tvec_skip_v(struct tvec_state */*tv*/,
819 const char */*excuse*/, va_list */*ap*/);
821 /* --- @tvec_fail@, @tvec_fail_v@ --- *
823 * Arguments: @struct tvec_state *tv@ = test-vector state
824 * @const char *detail@, @va_list *ap@ = description of test
828 * Use: Report the current test as a failure. This function can be
829 * called multiple times for a single test, e.g., if the test
830 * environment's @run@ function invokes the test function
831 * repeatedly; but a single test that fails repeatedly still
832 * only counts as a single failure in the statistics. The
833 * @detail@ string and its format parameters can be used to
834 * distinguish which of several invocations failed; it can
835 * safely be left null if the test function is run only once.
838 extern PRINTF_LIKE(2, 3)
839 void tvec_fail(struct tvec_state */*tv*/, const char */*detail*/, ...);
840 extern void tvec_fail_v(struct tvec_state */*tv*/,
841 const char */*detail*/, va_list */*ap*/);
843 /* --- @tvec_dumpreg@ --- *
845 * Arguments: @struct tvec_state *tv@ = test-vector state
846 * @unsigned disp@ = the register disposition (@TVRD_...@)
847 * @const union tvec_regval *tv@ = register value, or null
848 * @const struct tvec_regdef *rd@ = register definition
852 * Use: Dump a register value to the output. This is the lowest-
853 * level function for dumping registers, and calls the output
854 * formatter directly.
856 * Usually @tvec_mismatch@ is much more convenient. Low-level
857 * access is required for reporting `virtual' registers
858 * corresponding to test environment settings.
861 extern void tvec_dumpreg(struct tvec_state */*tv*/,
862 unsigned /*disp*/, const union tvec_regval */*rv*/,
863 const struct tvec_regdef */*rd*/);
865 /* --- @tvec_initregs@, @tvec_releaseregs@ --- *
867 * Arguments: @struct tvec_state *tv@ = test-vector state
871 * Use: Initialize or release, respectively, the registers required
872 * by the current test. All of the registers, both input and
873 * output, are effected. Initialized registers are not marked
877 extern void tvec_initregs(struct tvec_state */*tv*/);
878 extern void tvec_releaseregs(struct tvec_state */*tv*/);
880 /* --- @tvec_resetoutputs@ --- *
882 * Arguments: @struct tvec_state *tv@ = test-vector state
886 * Use: Reset (releases and reinitializes) the output registers in
887 * the test state. This is mostly of use to test environment
888 * @run@ functions, between invocations of the test function.
889 * Output registers are marked live if and only if the
890 * corresponding input register is live.
893 extern void tvec_resetoutputs(struct tvec_state */*tv*/);
895 /* --- @tvec_checkregs@ --- *
897 * Arguments: @struct tvec_state *tv@ = test-vector state
899 * Returns: Zero on success, %$-1$% on mismatch.
901 * Use: Compare the active output registers (according to the current
902 * test group definition) with the corresponding input register
903 * values. A mismatch occurs if the two values differ
904 * (according to the register type's @eq@ method), or if the
905 * input is live but the output is dead.
907 * This function only checks for a mismatch and returns the
908 * result; it takes no other action. In particular, it doesn't
909 * report a failure, or dump register values.
912 extern int tvec_checkregs(struct tvec_state */*tv*/);
914 /* --- @tvec_mismatch@ --- *
916 * Arguments: @struct tvec_state *tv@ = test-vector state
917 * @unsigned f@ = flags (@TVMF_...@)
921 * Use: Dumps registers suitably to report a mismatch. The flag bits
922 * @TVMF_IN@ and @TVF_OUT@ select input-only and output
923 * registers. If both are reset then nothing happens.
924 * Suppressing the output registers may be useful, e.g., if the
925 * test function crashed rather than returning outputs.
930 extern void tvec_mismatch(struct tvec_state */*tv*/, unsigned /*f*/);
932 /* --- @tvec_check@, @tvec_check_v@ --- *
934 * Arguments: @struct tvec_state *tv@ = test-vector state
935 * @const char *detail@, @va_list *ap@ = description of test
939 * Use: Check the register values, reporting a failure and dumping
940 * the registers in the event of a mismatch. This just wraps up
941 * @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
945 extern PRINTF_LIKE(2, 3)
946 void tvec_check(struct tvec_state */*tv*/, const char */*detail*/, ...);
947 extern void tvec_check_v(struct tvec_state */*tv*/,
948 const char */*detail*/, va_list */*ap*/);
950 /*----- Ad-hoc testing ----------------------------------------------------*/
952 /* --- @tvec_adhoc@ --- *
954 * Arguments: @struct tvec_state *tv@ = test-vector state
955 * @struct tvec_test *t@ = space for a test definition
959 * Use: Begin ad-hoc testing, i.e., without reading a file of
962 * The structure at @t@ will be used to record information about
963 * the tests underway, which would normally come from a static
964 * test definition. The other functions in this section assume
965 * that @tvec_adhoc@ has been called.
968 extern void tvec_adhoc(struct tvec_state */*tv*/, struct tvec_test */*t*/);
970 /* --- @tvec_begingroup@, @TVEC_BEGINGROUP@ --- *
972 * Arguments: @struct tvec_state *tv@ = test-vector state
973 * @const char *name@ = name for this test group
974 * @const char *file@, @unsigned @lno@ = calling file and line
978 * Use: Begin an ad-hoc test group with the given name. The @file@
979 * and @lno@ can be anything, but it's usually best if they
980 * refer to the source code performing the test: the macro
981 * @TVEC_BEGINGROUP@ does this automatically.
984 extern void tvec_begingroup(struct tvec_state */*tv*/, const char */*name*/,
985 const char */*file*/, unsigned /*lno*/);
986 #define TVEC_BEGINGROUP(tv, name) \
987 do tvec_begingroup(tv, name, __FILE__, __LINE__); while (0)
989 /* --- @tvec_endgroup@ --- *
991 * Arguments: @struct tvec_state *tv@ = test-vector state
995 * Use: End an ad-hoc test group. The statistics are updated and the
996 * outcome is reported to the output formatter.
999 extern void tvec_endgroup(struct tvec_state */*tv*/);
1001 /* --- @TVEC_TESTGROUP@, @TVEC_TESTGROUP_TAG@ --- *
1003 * Arguments: @tag@ = label-disambiguation tag
1004 * @const struct tvec_state *tv = test-vector state
1005 * @const char *name@ = test-group name
1009 * Use: Control-structure macro: @TVEC_TESTGROUP(tv, name) stmt@
1010 * establishes a test group with the given @name@ (attributing
1011 * it to the source file and lie number), executes @stmt@, and
1012 * ends the test group. If @stmt@ invokes @break@ then the test
1013 * group is skipped. @TVEC_TESTGROUP_TAG@ is the same, with an
1014 * additional @tag@ argument for use in higher-level macros.
1017 #define TVEC_TESTGROUP_TAG(tag, tv, name) \
1018 MC_WRAP(tag##__around, \
1019 { TVEC_BEGINGROUP(tv, name); }, \
1020 { tvec_endgroup(tv); }, \
1021 { if (!((tv)->f&TVSF_SKIP)) tvec_skipgroup(tv, 0); \
1022 tvec_endgroup(tv); })
1023 #define TVEC_TESTGROUP(tv, name) TVEC_TESTGROUP_TAG(grp, tv, name)
1025 /* --- @tvec_begintest@, @TVEC_BEGINTEST@ --- *
1027 * Arguments: @struct tvec_state *tv@ = test-vector state
1028 * @const char *file@, @unsigned @lno@ = calling file and line
1032 * Use: Begin an ad-hoc test case. The @file@ and @lno@ can be
1033 * anything, but it's usually best if they refer to the source
1034 * code performing the test: the macro @TVEC_BEGINGROUP@ does
1035 * this automatically.
1038 extern void tvec_begintest(struct tvec_state */*tv*/,
1039 const char */*file*/, unsigned /*lno*/);
1040 #define TVEC_BEGINTEST(tv) \
1041 do tvec_begintest(tv, __FILE__, __LINE__); while (0)
1043 /* --- @tvec_endtest@ --- *
1045 * Arguments: @struct tvec_state *tv@ = test-vector state
1049 * Use: End an ad-hoc test case, The statistics are updated and the
1050 * outcome is reported to the output formatter.
1053 extern void tvec_endtest(struct tvec_state */*tv*/);
1055 /* --- @TVEC_TEST@, @TVEC_TEST_TAG@ --- *
1057 * Arguments: @tag@ = label-disambiguation tag
1058 * @struct tvec_test *t@ = space for a test definition
1062 * Use: Control-structure macro: @TVEC_TEST(tv) stmt@ begins a test
1063 * case, executes @stmt@, and ends the test case. If @stmt@
1064 * invokes @break@ then the test case is skipped.
1065 * @TVEC_TEST_TAG@ is the same, with an additional @tag@ argumet
1066 * for use in higher-level macros.
1069 #define TVEC_TEST_TAG(tag, tv) \
1070 MC_WRAP(tag##__around, \
1071 { TVEC_BEGINTEST(tv); }, \
1072 { tvec_endtest(tv); }, \
1073 { if ((tv)->f&TVSF_ACTIVE) tvec_skip((tv), 0); \
1074 tvec_endtest(tv); })
1075 #define TVEC_TEST(tv) TVEC_TEST_TAG(test, tv)
1077 /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- *
1079 * Arguments: @struct tvec_state *tv@ = test-vector state
1081 * @const char *file@, @unsigned @lno@ = calling file and line
1082 * @const char *msg@, @va_list *ap@ = message to report on
1085 * Returns: The value @ok@.
1087 * Use: Check that a claimed condition holds, as (part of) a test.
1088 * If no test case is underway (i.e., if @TVSF_OPEN@ is reset in
1089 * @tv->f@), then a new test case is begun and ended. The
1090 * @file@ and @lno@ are passed to the output formatter to be
1091 * reported in case of a failure. If @ok@ is nonzero, then
1092 * nothing else happens; so, in particular, if @tvec_claim@
1093 * established a new test case, then the test case succeeds. If
1094 * @ok@ is zero, then a failure is reported, quoting @msg@.
1096 * The @TVEC_CLAIM@ macro is similar, only it (a) identifies the
1097 * file and line number of the call site automatically, and (b)
1098 * implicitly quotes the source text of the @ok@ condition in
1099 * the failure message.
1102 extern PRINTF_LIKE(5, 6)
1103 int tvec_claim(struct tvec_state */*tv*/, int /*ok*/,
1104 const char */*file*/, unsigned /*lno*/,
1105 const char */*msg*/, ...);
1106 extern int tvec_claim_v(struct tvec_state */*tv*/, int /*ok*/,
1107 const char */*file*/, unsigned /*lno*/,
1108 const char */*msg*/, va_list */*ap*/);
1109 #define TVEC_CLAIM(tv, cond) \
1110 (tvec_claim(tv, !!(cond), __FILE__, __LINE__, "%s untrue", #cond))
1112 /* --- @tvec_claimeq@ --- *
1114 * Arguments: @struct tvec_state *tv@ = test-vector state
1115 * @const struct tvec_regty *ty@ = register type
1116 * @const union tvec_misc *arg@ = register type argument
1117 * @const char *file@, @unsigned @lno@ = calling file and line
1118 * @const char *expr@ = the expression to quote on failure
1120 * Returns: Nonzero if the input and output values of register 0 are
1121 * equal, zero if they differ.
1123 * Use: Check that the input and output values of register 0 are
1124 * equal (according to the register type @ty@). As for
1125 * @tvec_claim@ above, a test case is automatically begun and
1126 * ended if none is already underway. If the values are
1127 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1128 * mismatched values are dumped.
1130 * This function is not expected to be called directly, but
1131 * through type-specific wrapper functions or macros such as
1132 * @TVEC_CLAIMEQ_INT@.
1135 extern int tvec_claimeq(struct tvec_state */*tv*/,
1136 const struct tvec_regty */*ty*/,
1137 const union tvec_misc */*arg*/,
1138 const char */*file*/, unsigned /*lno*/,
1139 const char */*expr*/);
1141 /*----- Benchmarking ------------------------------------------------------*/
1143 struct tvec_benchenv {
1144 struct tvec_env _env; /* benchmarking is an environment */
1145 struct bench_state **bst; /* benchmark state anchor or null */
1146 unsigned long niter; /* iterations done per unit */
1147 int riter, rbuf; /* iterations and buffer registers */
1148 const struct tvec_env *env; /* subordinate environment */
1151 struct tvec_benchctx {
1152 const struct tvec_benchenv *be; /* environment configuration */
1153 struct bench_state *bst; /* benchmark state */
1154 double dflt_target; /* default time in seconds */
1155 unsigned f; /* flags */
1156 #define TVBF_SETTRG 1u /* set `@target' */
1157 #define TVBF_SETMASK (TVBF_SETTRG)) /* mask of @TVBF_SET...@ */
1158 void *subctx; /* subsidiary environment context */
1161 extern struct bench_state *tvec_benchstate;
1163 /* --- Environment implementation --- *
1165 * The following special variables are supported.
1167 * * %|@target|% is the (approximate) number of seconds to run the
1170 * Unrecognized variables are passed to the subordinate environment, if there
1171 * is one. Other events are passed through to the subsidiary environment.
1174 extern tvec_envsetupfn tvec_benchsetup;
1175 extern tvec_envfindvarfn tvec_benchfindvar;
1176 extern tvec_envbeforefn tvec_benchbefore;
1177 extern tvec_envrunfn tvec_benchrun;
1178 extern tvec_envafterfn tvec_benchafter;
1179 extern tvec_envteardownfn tvec_benchteardown;
1181 #define TVEC_BENCHENV \
1182 { sizeof(struct tvec_benchctx), \
1184 tvec_benchfindvar, \
1188 tvec_benchteardown }
1189 #define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate
1191 /* --- @tvec_benchreport@ --- *
1193 * Arguments: @const struct gprintf_ops *gops@ = print operations
1194 * @void *go@ = print destination
1195 * @unsigned unit@ = the unit being measured (~TVBU_...@)
1196 * @const struct bench_timing *tm@ = the benchmark result
1200 * Use: Formats a report about the benchmark performance. This
1201 * function is intended to be called on by an output
1202 * @ebench@ function.
1205 extern void tvec_benchreport
1206 (const struct gprintf_ops */*gops*/, void */*go*/,
1207 unsigned /*unit*/, const struct bench_timing */*tm*/);
1209 /*----- Remote execution --------------------------------------------------*/
1211 struct tvec_remoteenv;
1213 struct tvec_remotecomms {
1214 int infd, outfd; /* input and output descriptors */
1215 dbuf bout; /* output buffer */
1216 unsigned char *bin; /* input buffer */
1217 size_t binoff, binlen, binsz; /* input offset, length, and size */
1218 size_t t; /* temporary offset */
1219 unsigned f; /* flags */
1220 #define TVRF_BROKEN 0x0001u /* communications have failed */
1222 #define TVEC_REMOTECOMMS_INIT { -1, -1, DBUF_INIT, 0, 0, 0, 0, 0, 0 }
1224 struct tvec_remotectx {
1225 struct tvec_state *tv; /* test vector state */
1226 struct tvec_remotecomms rc; /* communication state */
1227 const struct tvec_remoteenv *re; /* environment configuration */
1228 void *subctx; /* subenvironment context */
1229 struct tvec_vardef vd; /* temporary variable definition */
1230 unsigned ver; /* protocol version */
1231 pid_t kid; /* child process id */
1232 int errfd; /* child stderr descriptor */
1233 lbuf errbuf; /* child stderr line buffer */
1234 dstr prgwant, progress; /* progress: wanted/reported */
1235 unsigned exwant, exit; /* exit status wanted/reported */
1236 #define TVRF_RCNMASK 0x0300u /* reconnection behaviour: */
1237 #define TVRCN_DEMAND 0x0000u /* connect on demand */
1238 #define TVRCN_SKIP 0x0100u /* skip unless connected */
1239 #define TVRCN_FORCE 0x0200u /* force reconnection */
1240 #define TVRF_MUFFLE 0x0400u /* muffle child stderr */
1241 #define TVRF_SETEXIT 0x0800u /* set `@exit' */
1242 #define TVRF_SETPRG 0x1000u /* set `@progress' */
1243 #define TVRF_SETRCN 0x2000u /* set `@reconnect' */
1244 #define TVRF_SETMASK (TVRF_SETEXIT | TVRF_SETPRG | TVRF_SETRCN)
1245 /* mask of @TVTF_SET...@ */
1248 typedef int tvec_connectfn(pid_t */*kid_out*/, int */*infd_out*/,
1249 int */*outfd_out*/, int */*errfd_out*/,
1250 struct tvec_state */*tv*/,
1251 const struct tvec_remoteenv */*env*/);
1252 /* A connection function. On entry, @tv@ holds the test-vector state, and
1253 * @env@ is the test group's remote environment structure, which will
1254 * typically really be some subclass of @struct tvec_remoteenv@ containing
1255 * additional parameters for establishing the child process.
1257 * On successful completion, the function stores input and output
1258 * descriptors (which need not be distinct) in @*infd_out@ and
1259 * @*outfd_out@, and returns zero; if it creates a child process, it should
1260 * additionally store the child's process-id in @*kid_out@ and store in
1261 * @*errfd_out@ a descriptor from which the child's error output can be
1262 * read. On error, the function should report an appropriate message via
1263 * @tvec_error@ and return %$-1$%.
1266 struct tvec_remoteenv_slots {
1267 tvec_connectfn *connect; /* connection function */
1268 const struct tvec_env *env; /* subsidiary environment */
1269 unsigned dflt_reconn; /* default reconnection */
1272 struct tvec_remoteenv {
1273 struct tvec_env _env;
1274 struct tvec_remoteenv_slots r;
1277 struct tvec_remotefork_slots {
1278 const struct tvec_test *tests; /* child tests (or null) */
1281 struct tvec_remotefork {
1282 struct tvec_env _env;
1283 struct tvec_remoteenv_slots r;
1284 struct tvec_remotefork_slots f;
1287 struct tvec_remoteexec_slots {
1288 const char *const *args; /* command line to execute */
1291 struct tvec_remoteexec {
1292 struct tvec_env _env;
1293 struct tvec_remoteenv_slots r;
1294 struct tvec_remoteexec_slots x;
1297 union tvec_remoteenv_subclass_kludge {
1298 struct tvec_env _env;
1299 struct tvec_remoteenv renv;
1300 struct tvec_remotefork fork;
1301 struct tvec_remoteexec exec;
1306 * We don't use the conventional encoding returned by the @wait@(2) family of
1307 * system calls because it's too hard for our flags type to decode. Instead,
1308 * we use our own encoding.
1310 * The exit code or signal number ends up in the `value' field in the low 12
1311 * bits; bit 12 is set if the value field holds a signal, and it if holds an
1312 * exit code. Bits 13--15 hold a code which describes the status of a child
1313 * process or connection.
1315 #define TVXF_VALMASK 0x0fffu /* value (exit code or signal) */
1316 #define TVXF_SIG 0x1000u /* value is signal, not exit code */
1317 #define TVXF_CAUSEMASK 0xe000u /* mask for cause bits */
1318 #define TVXST_RUN 0x0000u /* still running */
1319 #define TVXST_EXIT 0x2000u /* child exited */
1320 #define TVXST_KILL 0x4000u /* child killed by signal */
1321 #define TVXST_CONT 0x6000u /* child continued (?) */
1322 #define TVXST_STOP 0x8000u /* child stopped (?) */
1323 #define TVXST_DISCONN 0xa000u /* disconnected */
1324 #define TVXST_UNK 0xc000u /* unknown */
1325 #define TVXST_ERR 0xe000u /* local error prevented diagnosis */
1327 /* Remote environment. */
1328 extern tvec_envsetupfn tvec_remotesetup;
1329 extern tvec_envfindvarfn tvec_remotefindvar;
1330 extern tvec_envbeforefn tvec_remotebefore;
1331 extern tvec_envrunfn tvec_remoterun;
1332 extern tvec_envafterfn tvec_remoteafter;
1333 extern tvec_envteardownfn tvec_remoteteardown;
1334 #define TVEC_REMOTEENV \
1335 { sizeof(struct tvec_remotectx), \
1337 tvec_remotefindvar, \
1338 tvec_remotebefore, \
1341 tvec_remoteteardown }
1343 /* --- @tvec_setprogress@, @tvec_setprogress_v@ --- *
1345 * Arguments: @const char *status@ = progress status token format
1346 * @va_list ap@ = argument tail
1350 * Use: Reports the progress of a test execution to the client.
1352 * The framework makes use of tokens beginning with %|%|%:
1354 * * %|%IDLE|%: during the top-level server code;
1356 * * %|%SETUP|%: during the enclosing environment's @before@
1359 * * %|%RUN|%: during the environment's @run@ function, or the
1360 * test function; and
1362 * * %|%DONE|%: during the enclosing environment's @after@
1365 * The intent is that a test can use the progress token to check
1366 * that a function which is expected to crash does so at the
1367 * correct point, so it's expected that more complex test
1368 * functions and/or environments will set their own progress
1369 * tokens to reflect what's going on.
1372 extern PRINTF_LIKE(1, 2) int tvec_setprogress(const char */*status*/, ...);
1373 extern int tvec_setprogress_v(const char */*status*/, va_list */*ap*/);
1375 /* --- @tvec_remoteserver@ --- *
1377 * Arguments: @int infd@, @int outfd@ = input and output file descriptors
1378 * @const struct tvec_config *config@ = test configuration
1380 * Returns: Suggested exit code.
1382 * Use: Run a test server, reading packets from @infd@ and writing
1383 * responses and notifications to @outfd@, and invoking tests as
1384 * described by @config@.
1386 * This function is not particularly general purpose. It
1387 * expects to `take over' the process, and makes use of private
1391 extern int tvec_remoteserver(int /*infd*/, int /*outfd*/,
1392 const struct tvec_config */*config*/);
1394 extern tvec_connectfn tvec_fork, tvec_exec;
1396 #define TVEC_REMOTEFORK(subenv, tests) \
1397 TVEC_REMOTEENV, { tvec_fork, subenv }, { tests }
1399 #define TVEC_REMOTEEXEC(subenv, args) \
1400 TVEC_REMOTEENV, { tvec_exec, subenv }, { args }
1402 /*----- Timeouts ----------------------------------------------------------*/
1404 struct tvec_timeoutenv {
1405 struct tvec_env _env;
1406 int timer; /* the timer (@ITIMER_...@) */
1407 double t; /* time to wait (in seconds) */
1408 const struct tvec_env *env; /* subsidiary environment */
1411 struct tvec_timeoutctx {
1412 const struct tvec_timeoutenv *te; /* saved environment description */
1413 int timer; /* timer code (as overridden) */
1414 double t; /* time to wait (as overridden) */
1415 unsigned f; /* flags */
1416 #define TVTF_SETTMO 1u /* set `@timeout' */
1417 #define TVTF_SETTMR 2u /* set `@timer' */
1418 #define TVTF_SETMASK (TVTF_SETTMO | TVTF_SETTMR)
1419 /* mask of @TVTF_SET...@ */
1423 extern tvec_envsetupfn tvec_timeoutsetup;
1424 extern tvec_envfindvarfn tvec_timeoutfindvar;
1425 extern tvec_envbeforefn tvec_timeoutbefore;
1426 extern tvec_envrunfn tvec_timeoutrun;
1427 extern tvec_envafterfn tvec_timeoutafter;
1428 extern tvec_envteardownfn tvec_timeoutteardown;
1429 #define TVEC_TIMEOUTENV \
1430 { sizeof(struct tvec_timeoutctx), \
1431 tvec_timeoutsetup, \
1432 tvec_timeoutfindvar, \
1433 tvec_timeoutbefore, \
1435 tvec_timeoutafter, \
1436 tvec_timeoutteardown }
1437 #define TVEC_TIMEOUTINIT(timer, t) TVEC_TIMEOUTENV, timer, t
1439 /*----- Output functions --------------------------------------------------*/
1441 /* --- @tvec_strlevel@ --- *
1443 * Arguments: @unsigned level@ = level code
1445 * Returns: A human-readable description.
1447 * Use: Converts a level code into something that you can print in a
1451 extern const char *tvec_strlevel(unsigned /*level*/);
1453 /* --- @tvec_report@, @tvec_report_v@ --- *
1455 * Arguments: @struct tvec_state *tv@ = test-vector state
1456 * @const char *msg@, @va_list ap@ = error message
1460 * Use: Report an message with a given severity. Messages with level
1461 * @TVLEV_ERR@ or higher force a nonzero exit code.
1464 extern PRINTF_LIKE(3, 4)
1465 void tvec_report(struct tvec_state */*tv*/, unsigned /*level*/,
1466 const char */*msg*/, ...);
1467 extern void tvec_report_v(struct tvec_state */*tv*/, unsigned /*level*/,
1468 const char */*msg*/, va_list */*ap*/);
1470 /* --- @tvec_error@, @tvec_notice@ --- *
1472 * Arguments: @struct tvec_state *tv@ = test-vector state
1473 * @const char *msg@, @va_list ap@ = error message
1475 * Returns: The @tvec_error@ function returns %$-1$% as a trivial
1476 * convenience; @tvec_notice@ does not return a value.
1478 * Use: Report an error or a notice. Errors are distinct from test
1479 * failures, and indicate that a problem was encountered which
1480 * compromised the activity of testing. Notices are important
1481 * information which doesn't fit into any other obvious
1485 extern PRINTF_LIKE(2, 3)
1486 int tvec_error(struct tvec_state */*tv*/, const char */*msg*/, ...);
1487 extern PRINTF_LIKE(2, 3)
1488 void tvec_notice(struct tvec_state */*tv*/, const char */*msg*/, ...);
1490 /* --- @tvec_unkreg@ --- *
1492 * Arguments: @struct tvec_state *tv@ = test-vector state
1493 * @const char *name@ = register or pseudoregister name
1497 * Use: Reports an error that the register or pseudoregister is
1501 extern int tvec_unkreg(struct tvec_state */*tv*/, const char */*name*/);
1503 /* --- @tvec_dupreg@ --- *
1505 * Arguments: @struct tvec_state *tv@ = test-vector state
1506 * @const char *name@ = register or pseudoregister name
1510 * Use: Reports an error that the register or pseudoregister has been
1511 * assigned already in the current test.
1514 extern int tvec_dupreg(struct tvec_state */*tv*/, const char */*name*/);
1516 /* --- @tvec_humanoutput@ --- *
1518 * Arguments: @FILE *fp@ = output file to write on
1520 * Returns: An output formatter.
1522 * Use: Return an output formatter which writes on @fp@ with the
1523 * expectation that a human will be watching and interpreting
1524 * the output. If @fp@ denotes a terminal, the display shows a
1525 * `scoreboard' indicating the outcome of each test case
1526 * attempted, and may in addition use colour and other
1530 extern struct tvec_output *tvec_humanoutput(FILE */*fp*/);
1532 /* --- @tvec_tapoutput@ --- *
1534 * Arguments: @FILE *fp@ = output file to write on
1536 * Returns: An output formatter.
1538 * Use: Return an output formatter which writes on @fp@ in `TAP'
1539 * (`Test Anything Protocol') format.
1541 * TAP comes from the Perl community, but has spread rather
1542 * further. This driver produces TAP version 14, but pretends
1543 * to be version 13. The driver produces a TAP `test point' --
1544 * i.e., a result reported as `ok' or `not ok' -- for each input
1545 * test group. Failure reports and register dumps are produced
1546 * as diagnostic messages before the final group result. (TAP
1547 * permits structuerd YAML data after the test-point result,
1548 * which could be used to report details, but (a) postponing the
1549 * details until after the report is inconvenient, and (b) there
1550 * is no standardization for the YAML anyway, so in practice
1551 * it's no more useful than the unstructured diagnostics.
1554 extern struct tvec_output *tvec_tapoutput(FILE */*fp*/);
1556 /* --- @tvec_dfltoutput@ --- *
1558 * Arguments: @FILE *fp@ = output file to write on
1560 * Returns: An output formatter.
1562 * Use: Selects and instantiates an output formatter suitable for
1563 * writing on @fp@. The policy is subject to change, but
1564 * currently the `human' output format is selected if @fp@ is
1565 * interactive (i.e., if @isatty(fileno(fp))@ is true), and
1566 * otherwise the `tap' format is used.
1569 extern struct tvec_output *tvec_dfltout(FILE */*fp*/);
1571 /*------ Serialization utilities ------------------------------------------*/
1573 /* Serialization format.
1575 * The `candidate register definitions' are those entries @r@ in the @regs@
1576 * vector whose index @r.i@ is strictly less than @nr@. The `selected
1577 * register definitions' are those candidate register definitions @r@ for
1578 * which the indicated register @rv[r.i]@ has the @TVRF_LIVE@ flag set. The
1579 * serialized output begins with a header bitmap: if there are %$n$%
1580 * candidate register definitions then the header bitmap consists of %$\lceil
1581 * n/8 \rceil$% bytes. Bits are ordered starting from the least significant
1582 * bit of the first byte, end ending at the most significant bit of the final
1583 * byte. The bit corresponding to a candidate register definition is set if
1584 * and only if that register defintion is selected. The header bitmap is
1585 * then followed by the serializations of the selected registers -- i.e., for
1586 * each selected register definition @r@, the serialized value of register
1587 * @rv[r.i]@ -- simply concatenated together, with no padding or alignment.
1590 /* --- @tvec_serialize@ --- *
1592 * Arguments: @const struct tvec_reg *rv@ = vector of registers
1593 * @buf *b@ = buffer to write on
1594 * @const struct tvec_regdef *regs@ = vector of register
1595 * descriptions, terminated by an entry with a null
1597 * @unsigned nr@ = number of entries in the @rv@ vector
1598 * @size_t regsz@ = true size of each element of @rv@
1600 * Returns: Zero on success, %$-1$% on failure.
1602 * Use: Serialize a collection of register values.
1604 * The serialized output is written to the buffer @b@. Failure
1605 * can be caused by running out of buffer space, or a failing
1609 extern int tvec_serialize(const struct tvec_reg */*rv*/, buf */*b*/,
1610 const struct tvec_regdef */*regs*/,
1611 unsigned /*nr*/, size_t /*regsz*/);
1613 /* --- @tvec_deserialize@ --- *
1615 * Arguments: @struct tvec_reg *rv@ = vector of registers
1616 * @buf *b@ = buffer to write on
1617 * @const struct tvec_regdef *regs@ = vector of register
1618 * descriptions, terminated by an entry with a null
1620 * @unsigned nr@ = number of entries in the @rv@ vector
1621 * @size_t regsz@ = true size of each element of @rv@
1623 * Returns: Zero on success, %$-1$% on failure.
1625 * Use: Deserialize a collection of register values.
1627 * The size of the register vector @nr@ and the register
1628 * definitions @regs@ must match those used when producing the
1629 * serialization. For each serialized register value,
1630 * deserialize and store the value into the appropriate register
1631 * slot, and set the @TVRF_LIVE@ flag on the register. See
1632 * @tvec_serialize@ for a description of the format.
1634 * Failure results only from a failing register type handler.
1637 extern int tvec_deserialize(struct tvec_reg */*rv*/, buf */*b*/,
1638 const struct tvec_regdef */*regs*/,
1639 unsigned /*nr*/, size_t /*regsz*/);
1641 /*----- Input utilities ---------------------------------------------------*/
1643 /* These are provided by the core for the benefit of type @parse@ methods,
1644 * and test-environment @set@ functions, which get to read from the test
1645 * input file. The latter are usually best implemented by calling on the
1648 * The two main rules are as follows.
1650 * * Leave the file position at the beginning of the line following
1651 * whatever it was that you read.
1653 * * When you read and consume a newline (which you do at least once, by
1654 * the previous rule), then increment @tv->lno@ to keep track of the
1655 * current line number.
1658 /* --- @tvec_syntax@, @tvec_syntax_v@ --- *
1660 * Arguments: @struct tvec_state *tv@ = test-vector state
1661 * @int ch@ = the character found (in @fgetc@ format)
1662 * @const char *expect@, @va_list ap@ = what was expected
1666 * Use: Report a syntax error quoting @ch@ and @expect@. If @ch@ is
1667 * a newline, then back up so that it can be read again (e.g.,
1668 * by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
1669 * advance the line number).
1672 extern PRINTF_LIKE(3, 4)
1673 int tvec_syntax(struct tvec_state */*tv*/, int /*ch*/,
1674 const char */*expect*/, ...);
1675 extern int tvec_syntax_v(struct tvec_state */*tv*/, int /*ch*/,
1676 const char */*expect*/, va_list */*ap*/);
1678 /* --- @tvec_skipspc@ --- *
1680 * Arguments: @struct tvec_state *tv@ = test-vector state
1684 * Use: Advance over any whitespace characters other than newlines.
1685 * This will stop at `;', end-of-file, or any other kind of
1686 * non-whitespace; and it won't consume a newline.
1689 extern void tvec_skipspc(struct tvec_state */*tv*/);
1691 /* --- @tvec_flushtoeol@ --- *
1693 * Arguments: @struct tvec_state *tv@ = test-vector state
1694 * @unsigned f@ = flags (@TVFF_...@)
1696 * Returns: Zero on success, %$-1$% on error.
1698 * Use: Advance to the start of the next line, consuming the
1699 * preceding newline.
1701 * A syntax error is reported if no newline character is found,
1702 * i.e., the file ends in mid-line. A syntax error is also
1703 * reported if material other than whitespace or a comment is
1704 * found before the end of the line end, and @TVFF_ALLOWANY@ is
1705 * not set in @f@. The line number count is updated
1709 #define TVFF_ALLOWANY 1u
1710 extern int tvec_flushtoeol(struct tvec_state */*tv*/, unsigned /*f*/);
1712 /* --- @tvec_nexttoken@ --- *
1714 * Arguments: @struct tvec_state *tv@ = test-vector state
1716 * Returns: Zero if there is a next token which can be read; %$-1$% if no
1717 * token is available.
1719 * Use: Advance to the next whitespace-separated token, which may be
1722 * Tokens are separated by non-newline whitespace, comments, and
1723 * newlines followed by whitespace; a newline /not/ followed by
1724 * whitespace instead begins the next assignment, and two
1725 * newlines separated only by whitespace terminate the data for
1728 * If this function returns zero, then the next character in the
1729 * file begins a suitable token which can be read and
1730 * processed. If it returns %$-1$% then there is no such token,
1731 * and the file position is left correctly. The line number
1732 * count is updated appropriately.
1735 extern int tvec_nexttoken(struct tvec_state */*tv*/);
1737 /* --- @tvec_readword@, @tvec_readword_v@ --- *
1739 * Arguments: @struct tvec_state *tv@ = test-vector state
1740 * @dstr *d@ = string to append the word to
1741 * @const char **p_inout@ = pointer into string, updated
1742 * @const char *delims@ = additional delimiters to stop at
1743 * @const char *expect@, @va_list ap@ = what was expected
1745 * Returns: Zero on success, %$-1$% on failure.
1747 * Use: A `word' consists of characters other than whitespace, null
1748 * characters, and other than those listed in @delims@;
1749 * furthermore, a word does not begin with a `;'. (If you want
1750 * reading to stop at comments not preceded by whitespace, then
1751 * include `;' in @delims@. This is a common behaviour.)
1753 * If there is no word beginning at the current file position,
1754 * then return %$-1$%; furthermore, if @expect@ is not null,
1755 * then report an appropriate error via @tvec_syntax@.
1757 * Otherwise, the word is accumulated in @d@ and zero is
1758 * returned; if @d@ was not empty at the start of the call, the
1759 * newly read word is separated from the existing material by a
1760 * single space character. Since null bytes are never valid
1761 * word constituents, a null terminator is written to @d@, and
1762 * it is safe to treat the string in @d@ as being null-
1765 * If @p_inout@ is not null, then @*p_inout@ must be a pointer
1766 * into @d->buf@, which will be adjusted so that it will
1767 * continue to point at the same position even if the buffer is
1768 * reallocated. As a subtle tweak, if @*p_inout@ initially
1769 * points at the end of the buffer, then it will be adjusted to
1770 * point at the beginning of the next word, rather than at the
1771 * additional intervening space.
1774 extern PRINTF_LIKE(5, 6)
1775 int tvec_readword(struct tvec_state */*tv*/, dstr */*d*/,
1776 const char **/*p_inout*/, const char */*delims*/,
1777 const char */*expect*/, ...);
1778 extern int tvec_readword_v(struct tvec_state */*tv*/, dstr */*d*/,
1779 const char **/*p_inout*/, const char */*delims*/,
1780 const char */*expect*/, va_list */*ap*/);
1782 /*----- Integer types: signed and unsigned --------------------------------*/
1784 /* Integers may be input in decimal, hex, binary, or octal, following
1785 * approximately usual conventions.
1787 * * Signed integers may be preceded with a `+' or `-' sign.
1789 * * Decimal integers are just a sequence of decimal digits `0' ... `9'.
1791 * * Octal integers are a sequence of digits `0' ... `7', preceded by `0o'
1794 * * Hexadecimal integers are a sequence of digits `0' ... `9', `a'
1795 * ... `f', or `A' ... `F', preceded by `0x' or `0X'.
1797 * * Radix-B integers are a sequence of digits `0' ... `9', `a' ... `f', or
1798 * `A' ... `F', each with value less than B, preceded by `Br' or `BR',
1799 * where 0 < B < 36 is expressed in decimal without any leading `0' or
1800 * internal underscores `_'.
1802 * * A digit sequence may contain internal underscore `_' separators, but
1803 * not before or after all of the digits; and two consecutive `_'
1804 * characters are not permitted.
1807 extern const struct tvec_regty tvty_int, tvty_uint;
1809 /* The @arg.p@ slot may be null or a pointer to @struct tvec_irange@ or
1810 * @struct tvec_urange@ as appropriate. The bounds are inclusive; use, e.g.,
1811 * @LONG_MAX@ explicitly if one or the other bound is logically inapplicable.
1813 struct tvec_irange { long min, max; };
1814 struct tvec_urange { unsigned long min, max; };
1816 /* Bounds corresponding to common integer types. */
1817 extern const struct tvec_irange
1818 tvrange_schar, tvrange_short, tvrange_int, tvrange_long,
1819 tvrange_sbyte, tvrange_i16, tvrange_i32;
1820 extern const struct tvec_urange
1821 tvrange_uchar, tvrange_ushort, tvrange_uint, tvrange_ulong, tvrange_size,
1822 tvrange_byte, tvrange_u16, tvrange_u32;
1824 /* --- @tvec_claimeq_int@, @TVEC_CLAIMEQ_INT@ --- *
1826 * Arguments: @struct tvec_state *tv@ = test-vector state
1827 * @long i0, i1@ = two signed integers
1828 * @const char *file@, @unsigned @lno@ = calling file and line
1829 * @const char *expr@ = the expression to quote on failure
1831 * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1833 * Use: Check that values of @i0@ and @i1@ are equal. As for
1834 * @tvec_claim@ above, a test case is automatically begun and
1835 * ended if none is already underway. If the values are
1836 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1837 * mismatched values are dumped: @i0@ is printed as the output
1838 * value and @i1@ is printed as the input reference.
1840 * The @TVEC_CLAIM_INT@ macro is similar, only it (a) identifies
1841 * the file and line number of the call site automatically, and
1842 * (b) implicitly quotes the source text of the @i0@ and @i1@
1843 * arguments in the failure message.
1846 extern int tvec_claimeq_int(struct tvec_state */*tv*/,
1847 long /*i0*/, long /*i1*/,
1848 const char */*file*/, unsigned /*lno*/,
1849 const char */*expr*/);
1850 #define TVEC_CLAIMEQ_INT(tv, i0, i1) \
1851 (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1))
1853 /* --- @tvec_claimeq_uint@, @TVEC_CLAIMEQ_UINT@ --- *
1855 * Arguments: @struct tvec_state *tv@ = test-vector state
1856 * @unsigned long u0, u1@ = two unsigned integers
1857 * @const char *file@, @unsigned @lno@ = calling file and line
1858 * @const char *expr@ = the expression to quote on failure
1860 * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1862 * Use: Check that values of @u0@ and @u1@ are equal. As for
1863 * @tvec_claim@ above, a test case is automatically begun and
1864 * ended if none is already underway. If the values are
1865 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1866 * mismatched values are dumped: @u0@ is printed as the output
1867 * value and @u1@ is printed as the input reference.
1869 * The @TVEC_CLAIM_UINT@ macro is similar, only it (a)
1870 * identifies the file and line number of the call site
1871 * automatically, and (b) implicitly quotes the source text of
1872 * the @u0@ and @u1@ arguments in the failure message.
1875 extern int tvec_claimeq_uint(struct tvec_state */*tv*/,
1876 unsigned long /*u0*/, unsigned long /*u1*/,
1877 const char */*file*/, unsigned /*lno*/,
1878 const char */*expr*/);
1879 #define TVEC_CLAIMEQ_UINT(tv, u0, u1) \
1880 (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1))
1882 /*----- Floating-point type -----------------------------------------------*/
1884 /* Floating-point values are either NaN (%|#nan|%, if supported by the
1885 * platform); positive or negative infinity (%|#inf|%, %|+#inf|%, or
1886 * %|#+inf|% (preferring the last), and %|-#inf|% or %|#-inf|% (preferring
1887 * the latter), if supported by the platform); or a number in strtod(3)
1890 * The comparison rules for floating-point numbers are complex: see
1891 * @tvec_claimeqish_float@ for details.
1894 extern const struct tvec_regty tvty_float;
1896 struct tvec_floatinfo {
1897 /* Details about acceptable floating-point values. */
1899 unsigned f; /* flags (@TVFF_...@ bits) */
1900 #define TVFF_NOMIN 1u /* ignore @min@ (allow -inf) */
1901 #define TVFF_NOMAX 2u /* ignore @max@ (allow +inf) */
1902 #define TVFF_NANOK 4u /* permit NaN */
1903 #define TVFF_EQMASK 0xf0 /* how to compare */
1904 #define TVFF_EXACT 0x00 /* must equal exactly */
1905 #define TVFF_ABSDELTA 0x10 /* must be within @delta@ */
1906 #define TVFF_RELDELTA 0x20 /* diff < @delta@ fraction */
1907 double min, max; /* smallest/largest value allowed */
1908 double delta; /* maximum tolerable difference */
1911 extern const struct tvec_floatinfo tvflt_finite, tvflt_nonneg;
1913 /* --- @tvec_claimeqish_float@, @TVEC_CLAIMEQISH_FLOAT@ --- *
1915 * Arguments: @struct tvec_state *tv@ = test-vector state
1916 * @double f0, f1@ = two floating-point numbers
1917 * @unsigned f@ = flags (@TVFF_...@)
1918 * @double delta@ = maximum tolerable difference
1919 * @const char *file@, @unsigned @lno@ = calling file and line
1920 * @const char *expr@ = the expression to quote on failure
1922 * Returns: Nonzero if @f0@ and @u1@ are sufficiently close, otherwise
1925 * Use: Check that values of @f0@ and @f1@ are sufficiently close.
1926 * As for @tvec_claim@ above, a test case is automatically begun
1927 * and ended if none is already underway. If the values are
1928 * too far apart, then @tvec_fail@ is called, quoting @expr@,
1929 * and the mismatched values are dumped: @f0@ is printed as the
1930 * output value and @f1@ is printed as the input reference.
1932 * The details for the comparison are as follows.
1934 * * A NaN value matches any other NaN, and nothing else.
1936 * * An infinity matches another infinity of the same sign,
1939 * * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
1940 * representable number matches only itself: in particular,
1941 * positive and negative zero are considered distinct.
1942 * (This allows tests to check that they land on the correct
1943 * side of branch cuts, for example.)
1945 * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
1946 * %$y$% when %$|x - y| < \delta$%.
1948 * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
1949 * %$y$% when %$|1 - y/x| < \delta$%. (Note that this
1950 * criterion is asymmetric FIXME
1952 * The @TVEC_CLAIM_FLOAT@ macro is similar, only it (a)
1953 * identifies the file and line number of the call site
1954 * automatically, and (b) implicitly quotes the source text of
1955 * the @f0@ and @f1@ arguments (and @delta@) in the failure
1959 extern int tvec_claimeqish_float(struct tvec_state */*tv*/,
1960 double /*f0*/, double /*f1*/,
1961 unsigned /*f*/, double /*delta*/,
1962 const char */*file*/, unsigned /*lno*/,
1963 const char */*expr*/);
1964 #define TVEC_CLAIMEQISH_FLOAT(tv, f0, f1, f, delta) \
1965 (tvec_claimeqish_float(tv, f0, f1, f, delta, , __FILE__, __LINE__, \
1966 #f0 " /= " #f1 " (+/- " #delta ")"))
1968 /* --- @tvec_claimeq_float@, @TVEC_CLAIMEQ_FLOAT@ --- *
1970 * Arguments: @struct tvec_state *tv@ = test-vector state
1971 * @double f0, f1@ = two floating-point numbers
1972 * @const char *file@, @unsigned @lno@ = calling file and line
1973 * @const char *expr@ = the expression to quote on failure
1975 * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero.
1977 * Use: Check that values of @f0@ and @f1@ are identical. The
1978 * function is exactly equivalent to @tvec_claimeqish_float@
1979 * with @f == TVFF_EXACT@; the macro is similarly like
1980 * @TVEC_CLAIMEQISH_FLOAT@ with @f == TVFF_EXACT@, except that
1981 * it doesn't bother to quote a delta.
1984 extern int tvec_claimeq_float(struct tvec_state */*tv*/,
1985 double /*f0*/, double /*f1*/,
1986 const char */*file*/, unsigned /*lno*/,
1987 const char */*expr*/);
1988 #define TVEC_CLAIMEQ_FLOAT(tv, f0, f1) \
1989 (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1))
1991 /*----- Durations ---------------------------------------------------------*/
1993 /* A duration measures a time interval in seconds. The input format consists
1994 * of a nonnegative decimal floating-point number in @strtod@ format followed
1995 * by an optional unit specification.
1997 * No @tvec_claimeq_...@ function is provided for durations: use
1998 * @tvec_claimeq_float@.
2001 extern const struct tvec_regty tvty_duration;
2003 /* --- @tvec_parsedurunit@ --- *
2005 * Arguments: @double *scale_out@ = where to leave the scale
2006 * @const char **p_inout@ = input unit string, updated
2008 * Returns: Zero on success, %$-1$% on error.
2010 * Use: If @*p_inout@ begins with a unit string followed by the end
2011 * of the string or some non-alphanumeric character, then store
2012 * the corresponding scale factor in @*scale_out@, advance
2013 * @*p_inout@ past the unit string, and return zero. Otherwise,
2017 extern int tvec_parsedurunit(double */*scale_out*/,
2018 const char **/*p_inout*/);
2020 /*----- Enumerated types --------------------------------------------------*/
2022 /* An enumeration describes a set of values of some underlying type, each of
2023 * which has a symbolic name. Values outside of the defined set can occur --
2024 * on output, because of bugs in the tested code, or on input to test
2025 * handling of unexpected values.
2027 * There is a distinct enumerated type for each of the branches of
2028 * @tvec_misc@. In the following, we write @t@ for the type code, which is
2029 * @i@ for signed integer, @u@ for unsigned integer, @f@ for floating-point,
2030 * and @p@ for pointer.
2032 * On input, an enumerated value may be given by name or as a literal value.
2033 * For enumerations based on numeric types, the literal values can be written
2034 * in the same syntax as the underlying values. For enumerations based on
2035 * pointers, the only permitted literal is %|#nil|%, which denotes a null
2036 * pointer. On output, names are preferred (with the underlying value given
2040 #define DEFENUMTY(tag, ty, slot) \
2041 extern const struct tvec_regty tvty_##slot##enum;
2042 TVEC_MISCSLOTS(DEFENUMTY)
2045 /* A @struct tvec_tassoc@ associates a string tag with a value. */
2046 #define DEFASSOC(tag_, ty, slot) \
2047 struct tvec_##slot##assoc { const char *tag; ty slot; };
2048 TVEC_MISCSLOTS(DEFASSOC)
2051 #define TVEC_ENDENUM { 0, 0 }
2053 /* Information about an enumerated type. */
2054 #define DEFINFO(tag, ty, slot) \
2055 struct tvec_##slot##enuminfo { \
2056 const char *name; /* type name for diagnostics */ \
2057 const struct tvec_##slot##assoc *av; /* name/value mappings */ \
2058 EXTRA_##tag##_INFOSLOTS /* type-specific extra info */ \
2061 #define EXTRA_INT_INFOSLOTS \
2062 const struct tvec_irange *ir; /* allowed range of raw values */
2064 #define EXTRA_UINT_INFOSLOTS \
2065 const struct tvec_urange *ur; /* allowed range of raw values */
2067 #define EXTRA_FLT_INFOSLOTS \
2068 const struct tvec_floatinfo *fi; /* range and matching policy */
2070 #define EXTRA_PTR_INFOSLOTS /* (nothing) */
2072 TVEC_MISCSLOTS(DEFINFO)
2074 #undef EXTRA_INT_INFOSLOTS
2075 #undef EXTRA_UINT_INFOSLOTS
2076 #undef EXTRA_FLT_INFOSLOTS
2077 #undef EXTRA_PTR_INFOSLOTS
2081 /* Standard enumerations. */
2082 extern const struct tvec_ienuminfo tvenum_bool;
2083 extern const struct tvec_ienuminfo tvenum_cmp;
2085 /* --- @tvec_claimeq_tenum@, @TVEC_CLAIMEQ_TENUM@ --- *
2087 * Arguments: @struct tvec_state *tv@ = test-vector state
2088 * @const struct tvec_typeenuminfo *ei@ = enumeration type info
2089 * @ty t0, t1@ = two values
2090 * @const char *file@, @unsigned @lno@ = calling file and line
2091 * @const char *expr@ = the expression to quote on failure
2093 * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero.
2095 * Use: Check that values of @t0@ and @t1@ are equal. As for
2096 * @tvec_claim@ above, a test case is automatically begun and
2097 * ended if none is already underway. If the values are
2098 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2099 * mismatched values are dumped: @t0@ is printed as the output
2100 * value and @t1@ is printed as the input reference.
2102 * The @TVEC_CLAIM_TENUM@ macro is similar, only it (a)
2103 * identifies the file and line number of the call site
2104 * automatically, and (b) implicitly quotes the source text of
2105 * the @t0@ and @t1@ arguments in the failure message.
2108 #define DECLCLAIM(tag, ty, slot) \
2109 extern int tvec_claimeq_##slot##enum \
2110 (struct tvec_state */*tv*/, \
2111 const struct tvec_##slot##enuminfo */*ei*/, \
2112 ty /*t0*/, ty /*t1*/, \
2113 const char */*file*/, unsigned /*lno*/, const char */*expr*/);
2114 TVEC_MISCSLOTS(DECLCLAIM)
2116 #define TVEC_CLAIMEQ_IENUM(tv, ei, i0, i1) \
2117 (tvec_claimeq_ienum(tv, ei, i0, i1, \
2118 __FILE__, __LINE__, #i0 " /= " #i1))
2119 #define TVEC_CLAIMEQ_UENUM(tv, ei, u0, u1) \
2120 (tvec_claimeq_uenum(tv, ei, u0, u1, \
2121 __FILE__, __LINE__, #u0 " /= " #u1))
2122 #define TVEC_CLAIMEQ_FENUM(tv, ei, f0, f1) \
2123 (tvec_claimeq_fenum(tv, ei, f0, f1, \
2124 __FILE__, __LINE__, #f0 " /= " #f1))
2125 #define TVEC_CLAIMEQ_PENUM(tv, ei, p0, p1) \
2126 (tvec_claimeq_penum(tv, ei, p0, p1, \
2127 __FILE__, __LINE__, #p0 " /= " #p1))
2129 /*----- Flags type --------------------------------------------------------*/
2131 /* A flags value packs a number of fields into a single nonnegative integer.
2132 * Symbolic names are associated with the possible values of the various
2133 * fields; more precisely, each name is associated with a value and a
2136 * The input syntax is a sequence of items separated by `%|||%' signs. Each
2137 * item may be the symbolic name of a field value, or a literal unsigned
2138 * integer. The masks associated with the given symbolic names must be
2139 * disjoint. The resulting numerical value is simply the bitwise OR of the
2142 * On output, the table of symbolic names and their associated values and
2143 * masks is repeatedly scanned, in order, to find disjoint matches -- i.e.,
2144 * entries whose value matches the target value in the bit positions
2145 * indicated by the mask, and whose mask doesn't overlap with any previously
2146 * found matches; the names are then output, separated by `%|||%'. Any
2147 * remaining nonzero bits not covered by any of the matching masks are output
2148 * as a single literal integer, in hex.
2151 extern const struct tvec_regty tvty_flags;
2154 /* Definition of a single flag or bitfield value.
2156 * Each named setting comes with a value @v@ and a mask @m@; the mask
2157 * should cover all of the value bits, i.e., @(v&~m) == 0@.
2160 const char *tag; /* name */
2161 unsigned long m, v; /* mask and value */
2164 #define TVEC_ENDFLAGS { 0, 0, 0 }
2166 struct tvec_flaginfo {
2167 /* Information about a flags type. */
2169 const char *name; /* type name for diagnostics */
2170 const struct tvec_flag *fv; /* name/mask/value mappings */
2171 const struct tvec_urange *range; /* permitted range for literals */
2174 /* --- @tvec_claimeq_flags@, @TVEC_CLAIMEQ_FLAGS@ --- *
2176 * Arguments: @struct tvec_state *tv@ = test-vector state
2177 * @const struct tvec_flaginfo *fi@ = flags type info
2178 * @unsigned long f0, f1@ = two values
2179 * @const char *file@, @unsigned @lno@ = calling file and line
2180 * @const char *expr@ = the expression to quote on failure
2182 * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero.
2184 * Use: Check that values of @f0@ and @f1@ are equal. As for
2185 * @tvec_claim@ above, a test case is automatically begun and
2186 * ended if none is already underway. If the values are
2187 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2188 * mismatched values are dumped: @f0@ is printed as the output
2189 * value and @f1@ is printed as the input reference.
2191 * The @TVEC_CLAIM_FLAGS@ macro is similar, only it (a)
2192 * identifies the file and line number of the call site
2193 * automatically, and (b) implicitly quotes the source text of
2194 * the @f0@ and @f1@ arguments in the failure message.
2197 extern int tvec_claimeq_flags(struct tvec_state */*tv*/,
2198 const struct tvec_flaginfo */*fi*/,
2199 unsigned long /*f0*/, unsigned long /*f1*/,
2200 const char */*file*/, unsigned /*lno*/,
2201 const char */*expr*/);
2202 #define TVEC_CLAIMEQ_FLAGS(tv, fi, f0, f1) \
2203 (tvec_claimeq_flags(tv, fi, f0, f1, \
2204 __FILE__, __LINE__, #f0 " /= " #f1))
2206 /*----- Character type ----------------------------------------------------*/
2208 /* A character value holds a character, as read by @fgetc@. The special
2209 * @EOF@ value can also be represented.
2211 * On input, a character value can be given by symbolic name, with a leading
2212 * `%|#|%'; or a character or `%|\|%'-escape sequence, optionally in single
2215 * The following escape sequences and character names are recognized.
2217 * * `%|#eof|%' is the special end-of-file marker.
2219 * * `%|#nul|%' is the NUL character, sometimes used to terminate strings.
2221 * * `%|bell|%', `%|bel|%', `%|ding|%', or `%|\a|%' is the BEL character
2222 * used to ring the terminal bell (or do some other thing to attract the
2223 * user's attention).
2225 * * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace character, used to
2226 * move the cursor backwords by one cell.
2228 * * %|#escape|% %|#esc|%, or%|\e|% is the escape character, used to
2229 * introduce special terminal commands.
2231 * * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed character, used to
2232 * separate pages of text.
2234 * * %|#newline|%, %|#linefeed|%, %|#lf|%, %|#nl|%, or %|\n|% is the
2235 * newline character, used to terminate lines of text or advance the
2236 * cursor to the next line (perhaps without returning it to the start of
2239 * * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is the
2240 * carriage-return character, used to return the cursor to the start of
2243 * * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the tab
2244 * character, used to advance the cursor to the next tab stop on the
2247 * * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab character.
2249 * * %|#space|%, %|#spc|% is the space character.
2251 * * %|#delete|%, %|#del|% is the delete character, used to erase the most
2254 * * %|\'|% is the single-quote character.
2256 * * %|\\|% is the backslash character.
2258 * * %|\"|% is the double-quote character.
2260 * * %|\NNN|% or %|\{NNN}|% is the character with code NNN in octal. The
2261 * NNN may be up to three digits long.
2263 * * %|\xNN|% or %|\x{NN}|% is the character with code NNN in hexadecimal.
2266 extern const struct tvec_regty tvty_char;
2268 /* --- @tvec_claimeq_char@, @TVEC_CLAIMEQ_CHAR@ --- *
2270 * Arguments: @struct tvec_state *tv@ = test-vector state
2271 * @int ch0, ch1@ = two character codes
2272 * @const char *file@, @unsigned @lno@ = calling file and line
2273 * @const char *expr@ = the expression to quote on failure
2275 * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero.
2277 * Use: Check that values of @ch0@ and @ch1@ are equal. As for
2278 * @tvec_claim@ above, a test case is automatically begun and
2279 * ended if none is already underway. If the values are
2280 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2281 * mismatched values are dumped: @ch0@ is printed as the output
2282 * value and @ch1@ is printed as the input reference.
2284 * The @TVEC_CLAIM_CHAR@ macro is similar, only it (a)
2285 * identifies the file and line number of the call site
2286 * automatically, and (b) implicitly quotes the source text of
2287 * the @ch0@ and @ch1@ arguments in the failure message.
2290 extern int tvec_claimeq_char(struct tvec_state */*tv*/,
2291 int /*ch0*/, int /*ch1*/,
2292 const char */*file*/, unsigned /*lno*/,
2293 const char */*expr*/);
2294 #define TVEC_CLAIMEQ_CHAR(tv, c0, c1) \
2295 (tvec_claimeq_char(tv, c0, c1, __FILE__, __LINE__, #c0 " /= " #c1))
2297 /*----- Text and binary string types --------------------------------------*/
2299 /* A string is a sequence of octets. Text and binary strings differ
2300 * primarily in presentation: text strings are shown as raw characters where
2301 * possible; binary strings are shown as hex dumps with an auxiliary text
2304 * The input format for both kinds of strings is basically the same: a
2305 * `compound string', consisting of
2307 * * single-quoted strings, which are interpreted entirely literally, but
2308 * can't contain single quotes or newlines;
2310 * * double-quoted strings, in which `%|\|%'-escapes are interpreted as for
2313 * * character names, marked by an initial `%|#|%' sign;
2315 * * special tokens marked by an initial `%|!|%' sign; or
2317 * * barewords interpreted according to the current coding scheme.
2319 * The special tokens are
2321 * * `%|!bare|%', which causes subsequent sequences of barewords to be
2322 * treated as plain text;
2324 * * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause subsequent
2325 * barewords to be decoded in the requested manner.
2327 * * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%', which includes
2328 * %$n$% copies of the (compound) string.
2330 * The only difference between text and binary strings is that the initial
2331 * coding scheme is %|bare|% for text strings and %|hex|% for binary strings.
2333 * Either kind of string can contain internal nul characters. A trailing nul
2334 * is appended -- beyond the stated input length -- to input strings as a
2335 * convenience to test functions. Test functions may include such a nul
2336 * character on output but this is not checked by the equality test.
2338 * A @struct tvec_urange@ may be supplied as an argument: the length of the
2339 * string (in bytes) will be checked against the permitted range.
2342 extern const struct tvec_regty tvty_text, tvty_bytes;
2344 /* --- @tvec_alloctext@, @tvec_allocbytes@ --- *
2346 * Arguments: @union tvec_regval *rv@ = register value
2347 * @size_t sz@ = required size
2351 * Use: Allocated space in a text or binary string register. If the
2352 * current register size is sufficient, its buffer is left
2353 * alone; otherwise, the old buffer, if any, is freed and a
2354 * fresh buffer allocated. These functions are not intended to
2355 * be used to adjust a buffer repeatedly, e.g., while building
2356 * output incrementally: (a) they will perform badly, and (b)
2357 * the old buffer contents are simply discarded if reallocation
2358 * is necessary. Instead, use a @dbuf@ or @dstr@.
2360 * The @tvec_alloctext@ function sneakily allocates an extra
2361 * byte for a terminating zero. The @tvec_allocbytes@ function
2365 extern void tvec_alloctext(union tvec_regval */*rv*/, size_t /*sz*/);
2366 extern void tvec_allocbytes(union tvec_regval */*rv*/, size_t /*sz*/);
2368 /* --- @tvec_claimeq_text@, @TVEC_CLAIMEQ_TEXT@ --- *
2370 * Arguments: @struct tvec_state *tv@ = test-vector state
2371 * @const char *p0@, @size_t sz0@ = first string with length
2372 * @const char *p1@, @size_t sz1@ = second string with length
2373 * @const char *file@, @unsigned @lno@ = calling file and line
2374 * @const char *expr@ = the expression to quote on failure
2376 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2379 * Use: Check that strings at @p0@ and @p1@ are equal. As for
2380 * @tvec_claim@ above, a test case is automatically begun and
2381 * ended if none is already underway. If the values are
2382 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2383 * mismatched values are dumped: @p0@ is printed as the output
2384 * value and @p1@ is printed as the input reference.
2386 * The @TVEC_CLAIM_TEXT@ macro is similar, only it (a)
2387 * identifies the file and line number of the call site
2388 * automatically, and (b) implicitly quotes the source text of
2389 * the @ch0@ and @ch1@ arguments in the failure message.
2392 extern int tvec_claimeq_text(struct tvec_state */*tv*/,
2393 const char */*p0*/, size_t /*sz0*/,
2394 const char */*p1*/, size_t /*sz1*/,
2395 const char */*file*/, unsigned /*lno*/,
2396 const char */*expr*/);
2397 #define TVEC_CLAIMEQ_TEXT(tv, p0, sz0, p1, sz1) \
2398 (tvec_claimeq_text(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
2399 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2401 /* --- @tvec_claimeq_textz@, @TVEC_CLAIMEQ_TEXTZ@ --- *
2403 * Arguments: @struct tvec_state *tv@ = test-vector state
2404 * @const char *p0, *p1@ = two strings to compare
2405 * @const char *file@, @unsigned @lno@ = calling file and line
2406 * @const char *expr@ = the expression to quote on failure
2408 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2411 * Use: Check that strings at @p0@ and @p1@ are equal, as for
2412 * @tvec_claimeq_string@, except that the strings are assumed
2413 * null-terminated, so their lengths don't need to be supplied
2414 * explicitly. The macro is similarly like @TVEC_CLAIMEQ_TEXT@.
2417 extern int tvec_claimeq_textz(struct tvec_state */*tv*/,
2418 const char */*p0*/, const char */*p1*/,
2419 const char */*file*/, unsigned /*lno*/,
2420 const char */*expr*/);
2421 #define TVEC_CLAIMEQ_TEXTZ(tv, p0, p1) \
2422 (tvec_claimeq_textz(tv, p0, p1, __FILE__, __LINE__, #p0 " /= " #p1))
2424 /* --- @tvec_claimeq_bytes@, @TVEC_CLAIMEQ_BYTES@ --- *
2426 * Arguments: @struct tvec_state *tv@ = test-vector state
2427 * @const void *p0@, @size_t sz0@ = first string with length
2428 * @const void *p1@, @size_t sz1@ = second string with length
2429 * @const char *file@, @unsigned @lno@ = calling file and line
2430 * @const char *expr@ = the expression to quote on failure
2432 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2435 * Use: Check that binary strings at @p0@ and @p1@ are equal. As for
2436 * @tvec_claim@ above, a test case is automatically begun and
2437 * ended if none is already underway. If the values are
2438 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2439 * mismatched values are dumped: @p0@ is printed as the output
2440 * value and @p1@ is printed as the input reference.
2442 * The @TVEC_CLAIM_STRING@ macro is similar, only it (a)
2443 * identifies the file and line number of the call site
2444 * automatically, and (b) implicitly quotes the source text of
2445 * the @ch0@ and @ch1@ arguments in the failure message.
2448 extern int tvec_claimeq_bytes(struct tvec_state */*tv*/,
2449 const void */*p0*/, size_t /*sz0*/,
2450 const void */*p1*/, size_t /*sz1*/,
2451 const char */*file*/, unsigned /*lno*/,
2452 const char */*expr*/);
2453 #define TVEC_CLAIMEQ_BYTES(tv, p0, sz0, p1, sz1) \
2454 (tvec_claimeq(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
2455 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2457 /*----- Buffer type -------------------------------------------------------*/
2459 /* Buffer registers are primarily used for benchmarking. Only a buffer's
2460 * allocation parameters are significant: its contents are ignored on
2461 * comparison and output, and unspecified on input.
2463 * The input format gives the buffer's size, and an optional alignment
2464 * specification, in the form %|SZ [`@' M [`+' A]]|%. Each of %|SZ|%, %|M|%
2465 * and %|A|% are sizes, as an integer, optionally suffixed with a unit `kB',
2466 * `MB', `GB', `TB', `PB', `EB', `ZB', `YB' (with or without the `B')
2467 * denoting a power of 1024. The %|SZ|% gives the (effective) buffer size.
2468 * %|M|% is the `alignment quantum' and %|A|% is the `alignment offset'; both
2469 * default to zero, but if %|M|% is nonzero then the start of the buffer is
2470 * aligned such that it is %|A|% more than a multiple of %|M|% bytes. Note
2471 * that %|M|% need not be a power of two, though this is common.
2473 * Units other than `B' are used on output only when the size would be
2474 * expressed exactly.
2476 * Buffers are %%\emph{not}%% allocated by default. In benchmarks, this is
2477 * best done in a @before@ function.
2479 * No @claimeq@ functions or macros are provided for buffers because they
2480 * don't seem very useful.
2483 extern const struct tvec_regty tvty_buffer;
2485 /* --- @tvec_initbuffer@ --- *
2487 * Arguments: @union tvec_regval *rv@ = register value
2488 * @const union tvec_regval *ref@ = reference buffer
2489 * @size_t sz@ = size to allocate
2493 * Use: Initialize the alignment parameters in @rv@ to match @ref@,
2494 * and the size to @sz@.
2497 extern void tvec_initbuffer(union tvec_regval */*rv*/,
2498 const union tvec_regval */*ref*/, size_t /*sz*/);
2500 /* --- @tvec_allocbuffer@ --- *
2502 * Arguments: @union tvec_regval *rv@ = register value
2506 * Use: Allocate @sz@ bytes to the buffer and fill the space with a
2507 * distinctive pattern.
2510 extern void tvec_allocbuffer(union tvec_regval */*rv*/);
2512 /*----- That's all, folks -------------------------------------------------*/