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 { unsigned char *p
; size_t sz
; } bytes
; /* binary string of bytes */
196 struct { char *p
; size_t sz
; } text
; /* text string */
205 * Note that all of the registers listed as being used by a particular test
206 * group are initialized at all times[1] while that test group is being
207 * processed. (The other register slots don't even have types associated
208 * with them, so there's nothing useful we could do with them.)
210 * The `TVRF_LIVE' flag indicates that the register was assigned a value by
211 * the test vector file: it's the right thing to use to check whether an
212 * optional register is actually present. Even `dead' registers are still
213 * initialized, though.
215 * [1] This isn't quite true. Between individual tests, the registers are
216 * released and reinitialized in order to reset them to known values
217 * ready for the next test. But you won't see them at this point.
220 unsigned f
; /* flags */
221 #define TVRF_LIVE 1u /* used in current test */
222 union tvec_regval v
; /* register value */
226 /* A register definition. Register definitions list the registers which
227 * are used by a particular test group (see `struct tvec_test' below).
229 * A vector of register definitions is terminated by a definition whose
230 * `name' slot is null.
233 const char *name
; /* register name (for input files) */
234 unsigned i
; /* register index */
235 const struct tvec_regty
*ty
; /* register type descriptor */
236 unsigned f
; /* flags */
237 #define TVRF_OPT 1u /* optional register */
238 #define TVRF_ID 2u /* part of test identity */
239 union tvec_misc arg
; /* extra detail for the type */
241 #define TVEC_ENDREGS { 0, 0, 0, 0, { 0 } }
243 /* @TVEC_GREG(vec, i, regsz)@
245 * If @vec@ is a data pointer which happens to contain the address of a
246 * vector of @struct tvec_reg@ objects, @i@ is an integer, and @regsz@ is the
247 * size of a @struct tvec_reg@, then this evaluates to the address of the
248 * @i@th element of the vector.
250 * This is the general tool you need for accessing register vectors when you
251 * don't have absolute knowledge of the size of a @union tvec_regval@.
252 * Usually you want to access one of the register vectors in a @struct
253 * tvec_state@, and @TVEC_REG@ will be more convenient.
255 #define TVEC_GREG(vec, i, regsz) \
256 ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz)))
258 /*----- Register types ----------------------------------------------------*/
260 struct tvec_state
; /* forward declaration */
263 /* A register type. */
265 void (*init
)(union tvec_regval */
*rv*/
, const struct tvec_regdef */
*rd*/
);
266 /* Initialize the value in @*rv@. This will be called before any other
267 * function acting on the value, including @release@.
270 void (*release
)(union tvec_regval */
*rv*/
,
271 const struct tvec_regdef */
*rd*/
);
272 /* Release any resources associated with the value in @*rv@. */
274 int (*eq
)(const union tvec_regval */
*rv0*/
,
275 const union tvec_regval */
*rv1*/
,
276 const struct tvec_regdef */
*rd*/
);
277 /* Return nonzero if @*rv0@ and @*rv1@ are equal values. Asymmetric
278 * criteria are permitted: @tvec_checkregs@ calls @eq@ with the input
279 * register as @rv0@ and the output as @rv1@.
282 int (*tobuf
)(buf */
*b*/
, const union tvec_regval */
*rv*/
,
283 const struct tvec_regdef */
*rd*/
);
284 /* Serialize the value @*rv@, writing the result to @b@. Return zero on
285 * success, or %$-1$% on error.
288 int (*frombuf
)(buf */
*b*/
, union tvec_regval */
*rv*/
,
289 const struct tvec_regdef */
*rd*/
);
290 /* Deserialize a value from @b@, storing it in @*rv@. Return zero on
291 * success, or %$-1$% on error.
294 int (*parse
)(union tvec_regval */
*rv*/
, const struct tvec_regdef */
*rd*/
,
295 struct tvec_state */
*tv*/
);
296 /* Parse a value from @tv->fp@, storing it in @*rv@. Return zero on
297 * success, or %$-1$% on error, having reported one or more errors via
298 * @tvec_error@ or @tvec_syntax@. A successful return should leave the
299 * input position at the start of the next line; the caller will flush
300 * the remainder of the line itself.
303 void (*dump
)(const union tvec_regval */
*rv*/
,
304 const struct tvec_regdef */
*rd*/
,
306 const struct gprintf_ops */
*gops*/
, void */
*go*/
);
307 #define TVSF_COMPACT 1u
308 /* Write a human-readable representation of the value @*rv@ using
309 * @gprintf@ on @gops@ and @go@. The @style@ is a collection of flags:
310 * if @TVSF_COMPACT@ is set, then output should be minimal, and must fit
311 * on a single line; otherwise, output may consist of multiple lines and
312 * may contain redundant information if that is likely to be useful to a
317 /*----- Test descriptions -------------------------------------------------*/
319 typedef void tvec_testfn(const struct tvec_reg */
*in*/
,
320 struct tvec_reg */
*out*/
,
322 /* A test function. It should read inputs from @in@ and write outputs to
323 * @out@. The @TVRF_LIVE@ is set on inputs which are actually present, and
324 * on outputs which are wanted to test. A test function can set additional
325 * `gratuitous outputs' by setting @TVRF_LIVE@ on them; clearing
326 * @TVRF_LIVE@ on a wanted output causes a mismatch.
328 * A test function may be called zero or more times by the environment. In
329 * particular, it may be called multiple times, though usually by prior
330 * arrangement with the environment.
332 * The @ctx@ is supplied by the environment's @run@ function (see below).
333 * The default environment calls the test function once, with a null
334 * @ctx@. There is no expectation that the environment's context has
335 * anything to do with the test function's context.
340 typedef int tvec_setvarfn(struct tvec_state */
*tv*/
, const char */
*var*/
,
341 const union tvec_regval */
*rv*/
, void */
*ctx*/
);
342 /* Called after a variable is read. Return zero on success or %$-1$% on
343 * error. This function is never called if the test group is skipped.
347 size_t regsz
; /* (minimum) register size */
348 tvec_setvarfn
*setvar
; /* function to set variable */
349 struct tvec_regdef def
; /* register definition */
352 typedef void tvec_envsetupfn(struct tvec_state */
*tv*/
,
353 const struct tvec_env */
*env*/
,
354 void */
*pctx*/
, void */
*ctx*/
);
355 /* Initialize the context; called at the start of a test group; @pctx@ is
356 * null for environments called by the core, but may be non-null for
357 * subordinate environments. If setup fails, the function should call
358 * @tvec_skipgroup@ with a suitable excuse. The @set@, @after@, and
359 * @teardown@ entry points will still be called, but @before@ and @run@
363 typedef const struct tvec_vardef
*tvec_envfindvarfn
364 (struct tvec_state */
*tv*/
, const char */
*name*/
,
365 void **/
*ctx_out*/
, void */
*ctx*/
);
366 /* Called when the parser finds a %|@var|%' special variable. If a
367 * suitable variable was found, set @*ctx_out@ to a suitable context and
368 * return the variable definition; the context will be passed to the
369 * variable definition's @setvar@ function. If no suitable variable was
370 * found, then return null.
373 typedef void tvec_envbeforefn(struct tvec_state */
*tv*/
, void */
*ctx*/
);
374 /* Called prior to running a test. This is the right place to act on any
375 * `%|@var|%' settings. If preparation fails, the function should call
376 * @tvec_skip@ with a suitable excuse. This function is never called if
377 * the test group is skipped. It %%\emph{is}%% called if the test will be
378 * skipped due to erroneous test data. It should check the @TVSF_ACTIVE@
382 typedef void tvec_envrunfn(struct tvec_state */
*tv*/
,
383 tvec_testfn */
*fn*/
, void */
*ctx*/
);
384 /* Run the test. It should either call @tvec_skip@, or run @fn@ one or
385 * more times. In the latter case, it is responsible for checking the
386 * outputs, and calling @tvec_fail@ as necessary; @tvec_checkregs@ will
387 * check the register values against the supplied test vector, while
388 * @tvec_check@ does pretty much everything necessary. This function is
389 * never called if the test group is skipped.
392 typedef void tvec_envafterfn(struct tvec_state */
*tv*/
, void */
*ctx*/
);
393 /* Called after running or skipping a test. Typical actions involve
394 * resetting whatever things were established by @set@. This function
395 * %%\emph{is}%% called if the test group is skipped or the test data is
396 * erroneous, so that the test environment can reset variables set by the
397 * @set@ entry point. It should check the @TVSF_SKIP@ flag if necessary.
400 typedef void tvec_envteardownfn(struct tvec_state */
*tv*/
, void */
*ctx*/
);
401 /* Tear down the environment: called at the end of a test group. */
405 /* A test environment sets things up for and arranges to run the test.
407 * The caller is responsible for allocating storage for the environment's
408 * context, based on the @ctxsz@ slot, and freeing it later; this space is
409 * passed in as the @ctx@ parameter to the remaining functions; if @ctxsz@
410 * is zero then @ctx@ is null.
413 size_t ctxsz
; /* environment context size */
415 tvec_envsetupfn
*setup
; /* setup for group */
416 tvec_envfindvarfn
*findvar
; /* find variable */
417 tvec_envbeforefn
*before
; /* prepare for test */
418 tvec_envrunfn
*run
; /* run test function */
419 tvec_envafterfn
*after
; /* clean up after test */
420 tvec_envteardownfn
*teardown
; /* tear down after group */
424 /* A test description. */
426 const char *name
; /* name of the test */
427 const struct tvec_regdef
*regs
; /* descriptions of the registers */
428 const struct tvec_env
*env
; /* environment to run test in */
429 tvec_testfn
*fn
; /* test function */
431 #define TVEC_ENDTESTS { 0, 0, 0, 0 }
434 /* Register output dispositions. */
436 TVRD_INPUT
, /* input-only register */
437 TVRD_OUTPUT
, /* output-only (input is dead) */
438 TVRD_MATCH
, /* matching (equal) registers */
439 TVRD_FOUND
, /* mismatching output register */
440 TVRD_EXPECT
, /* mismatching input register */
441 TVRD_LIMIT
/* (number of dispositions) */
444 /*----- Test state --------------------------------------------------------*/
447 /* Possible test outcomes. */
449 TVOUT_LOSE
, /* test failed */
450 TVOUT_SKIP
, /* test skipped */
451 TVOUT_WIN
, /* test passed */
452 TVOUT_XFAIL
, /* test passed, but shouldn't have */
453 TVOUT_LIMIT
/* (number of possible outcomes) */
457 /* The primary state structure for the test vector machinery. */
459 unsigned f
; /* flags */
460 #define TVSF_SKIP 0x0001u /* skip this test group */
461 #define TVSF_OPEN 0x0002u /* test is open */
462 #define TVSF_ACTIVE 0x0004u /* test is active */
463 #define TVSF_ERROR 0x0008u /* an error occurred */
464 #define TVSF_OUTMASK 0x00f0u /* test outcome (@TVOUT_...@) */
465 #define TVSF_OUTSHIFT 4 /* shift applied to outcome */
466 #define TVSF_XFAIL 0x0100u /* test expected to fail */
467 #define TVSF_MUFFLE 0x0200u /* muffle errors */
469 /* Registers. Available to execution environments. */
470 unsigned nrout
, nreg
; /* number of output/total registers */
471 size_t regsz
; /* size of register entry */
472 struct tvec_reg
*in
, *out
; /* register vectors */
474 /* Test groups state. Available to output formatters. */
475 const struct tvec_test
*tests
, *test
; /* all tests and current test */
477 /* Test scoreboard. Available to output formatters. */
478 unsigned curr
[TVOUT_LIMIT
], all
[TVOUT_LIMIT
], grps
[TVOUT_LIMIT
];
480 /* Output machinery. */
481 struct tvec_output
*output
; /* output formatter */
483 /* Input machinery. Available to type parsers. */
484 const char *infile
; unsigned lno
, test_lno
; /* input file name, line */
485 FILE *fp
; /* input file stream */
488 /* @TVEC_REG(tv, vec, i)@
490 * If @tv@ is a pointer to a @struct tvec_state@, @vec@ is either @in@ or
491 * @out@, and @i@ is an integer, then this evaluates to the address of the
492 * @i@th register in the selected vector.
494 #define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->regsz)
497 /* An overall test configuration. */
499 const struct tvec_test
*tests
; /* the tests to be performed */
500 unsigned nrout
, nreg
; /* number of output/total regs */
501 size_t regsz
; /* size of a register */
504 /*----- Output formatting -------------------------------------------------*/
507 /* An output formatter. */
508 const struct tvec_outops
*ops
; /* pointer to operations */
511 /* Benchmarking details. */
513 TVBU_OP
, /* counting operations of some kind */
514 TVBU_BYTE
, /* counting bytes (@rbuf >= 0@) */
515 TVBU_LIMIT
/* (number of units) */
517 struct bench_timing
; /* forward declaration */
520 /* Output operations. */
522 void (*bsession
)(struct tvec_output */
*o*/
, struct tvec_state */
*tv*/
);
523 /* Begin a test session. The output driver will probably want to
524 * save @tv@, because this isn't provided to any other methods.
527 int (*esession
)(struct tvec_output */
*o*/
);
528 /* End a session, and return the suggested exit code. */
530 void (*bgroup
)(struct tvec_output */
*o*/
);
531 /* Begin a test group. The test group description is @tv->test@. */
533 void (*skipgroup
)(struct tvec_output */
*o*/
,
534 const char */
*excuse*/
, va_list */
*ap*/
);
535 /* The group is being skipped; @excuse@ may be null or a format
536 * string explaining why. The @egroup@ method will not be called
540 void (*egroup
)(struct tvec_output */
*o*/
);
541 /* End a test group. At least one test was attempted or @skipgroup@
542 * would have been called instead. If @tv->curr[TVOUT_LOSE]@ is nonzero
543 * then the test group as a whole failed; otherwise it passed.
546 void (*btest
)(struct tvec_output */
*o*/
);
547 /* Begin a test case. */
549 void (*skip
)(struct tvec_output */
*o*/
,
550 const char */
*excuse*/
, va_list */
*ap*/
);
551 /* The test case is being skipped; @excuse@ may be null or a format
552 * string explaining why. The @etest@ function will still be called (so
553 * this works differently from @skipgroup@ and @egroup@). A test case
554 * can be skipped at most once, and, if skipped, it cannot fail.
557 void (*fail
)(struct tvec_output */
*o*/
,
558 const char */
*detail*/
, va_list */
*ap*/
);
559 /* The test case failed.
561 * The output driver should preferably report the filename (@infile@) and
562 * line number (@test_lno@, not @lno@) for the failing test.
564 * The @detail@ may be null or a format string describing detail about
565 * how the failing test was run which can't be determined from the
566 * registers; a @detail@ is usually provided when (and only when) the
567 * test environment potentially invokes the test function more than once.
569 * A single test case can fail multiple times!
572 void (*dumpreg
)(struct tvec_output */
*o*/
,
573 unsigned /*disp*/, const union tvec_regval */
*rv*/
,
574 const struct tvec_regdef */
*rd*/
);
575 /* Dump a register. The `disposition' @disp@ explains what condition the
576 * register is in; see @tvec_dumpreg@ and the @TVRD_...@ codes. The
577 * register value is at @rv@, and its definition, including its type, at
578 * @rd@. Note that this function may be called on virtual registers
579 * which aren't in either of the register vectors or mentioned by the
580 * test description. It may also be called with @rv@ null, indicating
581 * that the register is not live.
584 void (*etest
)(struct tvec_output */
*o*/
, unsigned /*outcome*/);
585 /* The test case concluded with the given @outcome@ (one of the
589 void (*bbench
)(struct tvec_output */
*o*/
,
590 const char */
*ident*/
, unsigned /*unit*/);
591 /* Begin a benchmark. The @ident@ is a formatted string identifying the
592 * benchmark based on the values of the input registered marked
593 * @TVRF_ID@; the output driver is free to use this or come up with its
594 * own way to identify the test, e.g., by inspecting the register values
595 * for itself. The @unit@ is one of the @TVBU_...@ constants explaining
596 * what sort of thing is being measured.
599 void (*ebench
)(struct tvec_output */
*o*/
,
600 const char */
*ident*/
, unsigned /*unit*/,
601 const struct bench_timing */
*tm*/
);
602 /* End a benchmark. The @ident@ and @unit@ arguments are as for
603 * @bbench@. If @tm@ is zero then the measurement failed; otherwise
604 * @tm->n@ counts total number of things (operations or bytes, as
605 * indicated by @unit@) processed in the indicated time.
608 void (*report
)(struct tvec_output */
*o*/
, unsigned /*level*/,
609 const char */
*msg*/
, va_list */
*ap*/
);
610 /* Report a message. The driver should ideally report the filename
611 * (@infile@) and line number (@lno@) prompting the error.
614 void (*destroy
)(struct tvec_output */
*o*/
);
615 /* Release any resources acquired by the driver. */
618 #define TVEC_LEVELS(_) \
619 _(NOTE, "notice", 4) \
623 #define TVEC_DEFLEVEL(tag, name, val) TVLEV_##tag = val,
624 TVEC_LEVELS(TVEC_DEFLEVEL
)
629 /*----- Session lifecycle -------------------------------------------------*/
631 /* --- @tvec_begin@ --- *
633 * Arguments: @struct tvec_state *tv_out@ = state structure to fill in
634 * @const struct tvec_config *config@ = test configuration
635 * @struct tvec_output *o@ = output driver
639 * Use: Initialize a state structure ready to do some testing.
642 extern void tvec_begin(struct tvec_state */
*tv_out*/
,
643 const struct tvec_config */
*config*/
,
644 struct tvec_output */
*o*/
);
646 /* --- @tvec_end@ --- *
648 * Arguments: @struct tvec_state *tv@ = test-vector state
650 * Returns: A proposed exit code.
652 * Use: Conclude testing and suggests an exit code to be returned to
653 * the calling program. (The exit code comes from the output
654 * driver's @esession@ method.)
657 extern int tvec_end(struct tvec_state */
*tv*/
);
659 /* --- @tvec_read@ --- *
661 * Arguments: @struct tvec_state *tv@ = test-vector state
662 * @const char *infile@ = the name of the input file
663 * @FILE *fp@ = stream to read from
665 * Returns: Zero on success, %$-1$% on error.
667 * Use: Read test vector data from @fp@ and exercise test functions.
668 * THe return code doesn't indicate test failures: it's only
669 * concerned with whether there were problems with the input
670 * file or with actually running the tests.
673 extern int tvec_read(struct tvec_state */
*tv*/
,
674 const char */
*infile*/
, FILE */
*fp*/
);
676 /*----- Command-line interface --------------------------------------------*/
678 extern const struct tvec_config tvec_adhocconfig
;
679 /* A special @struct tvec_config@ to use for programs which perform ad-hoc
683 /* --- @tvec_parseargs@ --- *
685 * Arguments: @int argc@ = number of command-line arguments
686 * @char *argv[]@ = vector of argument strings
687 * @struct tvec_state *tv_out@ = test vector state to initialize
688 * @int *argpos_out@ = where to leave unread argument index
689 * @const struct tvec_config *cofig@ = test vector configuration
693 * Use: Parse arguments and set up the test vector state @*tv_out@.
694 * If errors occur, print messages to standard error and exit
698 extern void tvec_parseargs(int /*argc*/, char */
*argv*/
[],
699 struct tvec_state */
*tv_out*/
,
701 const struct tvec_config */
*config*/
);
703 /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- *
705 * Arguments: @struct tvec_state *tv@ = test vector state
706 * @const char *file@ = pathname of file to read
707 * @const char *arg@ = argument to interpret
709 * Returns: Zero on success, %$-1$% on error.
711 * Use: Read test vector data from stdin or a named file. The
712 * @tvec_readarg@ function reads from stdin if @arg@ is `%|-|%',
713 * and from the named file otherwise.
716 extern int tvec_readstdin(struct tvec_state */
*tv*/
);
717 extern int tvec_readfile(struct tvec_state */
*tv*/
, const char */
*file*/
);
718 extern int tvec_readarg(struct tvec_state */
*tv*/
, const char */
*arg*/
);
720 /* --- @tvec_readdflt@ --- *
722 * Arguments: @struct tvec_state *tv@ = test vector state
723 * @const char *dflt@ = defsault filename or null
725 * Returns: Zero on success, %$-1$% on error.
727 * Use: Reads from the default test vector data. If @file@ is null,
728 * then read from standard input, unless that's a terminal; if
729 * @file@ is not null, then read the named file, looking in the
730 * directory named by the `%|srcdir|%' environment variable if
731 * that's set, or otherwise in the current directory.
734 extern int tvec_readdflt(struct tvec_state */
*tv*/
, const char */
*file*/
);
736 /* --- @tvec_readargs@ --- *
738 * Arguments: @int argc@ = number of command-line arguments
739 * @char *argv[]@ = vector of argument strings
740 * @struct tvec_state *tv@ = test vector state
741 * @int *argpos_inout@ = current argument position (updated)
742 * @const char *dflt@ = default filename or null
744 * Returns: Zero on success, %$-1$% on error.
746 * Use: Reads from the sources indicated by the command-line
747 * arguments, in order, interpreting each as for @tvec_readarg@;
748 * if no arguments are given then read from @dflt@ as for
752 extern int tvec_readargs(int /*argc*/, char */
*argv*/
[],
753 struct tvec_state */
*tv*/
,
754 int */
*argpos_inout*/
, const char */
*dflt*/
);
756 /* --- @tvec_main@ --- *
758 * Arguments: @int argc@ = number of command-line arguments
759 * @char *argv[]@ = vector of argument strings
760 * @const struct tvec_config *cofig@ = test vector configuration
761 * @const char *dflt@ = default filename or null
763 * Returns: Exit code.
765 * Use: All-in-one test vector front-end. Parse options from the
766 * command-line as for @tvec_parseargs@, and then process the
767 * remaining positional arguments as for @tvec_readargs@. The
768 * function constructs and disposes of a test vector state.
771 extern int tvec_main(int /*argc*/, char */
*argv*/
[],
772 const struct tvec_config */
*config*/
,
773 const char */
*dflt*/
);
775 /*----- Test processing ---------------------------------------------------*/
777 /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
779 * Arguments: @struct tvec_state *tv@ = test-vector state
780 * @const char *excuse@, @va_list *ap@ = reason why skipped
784 * Use: Skip the current group. This should only be called from a
785 * test environment @setup@ function; a similar effect occurs if
786 * the @setup@ function fails.
789 extern PRINTF_LIKE(2, 3)
790 void tvec_skipgroup(struct tvec_state */
*tv*/
,
791 const char */
*excuse*/
, ...);
792 extern void tvec_skipgroup_v(struct tvec_state */
*tv*/
,
793 const char */
*excuse*/
, va_list */
*ap*/
);
795 /* --- @tvec_skip@, @tvec_skip_v@ --- *
797 * Arguments: @struct tvec_state *tv@ = test-vector state
798 * @const char *excuse@, @va_list *ap@ = reason why test skipped
802 * Use: Skip the current test. This should only be called from a
803 * test environment @run@ function; a similar effect occurs if
804 * the @before@ function fails.
807 extern PRINTF_LIKE(2, 3)
808 void tvec_skip(struct tvec_state */
*tv*/
, const char */
*excuse*/
, ...);
809 extern void tvec_skip_v(struct tvec_state */
*tv*/
,
810 const char */
*excuse*/
, va_list */
*ap*/
);
812 /* --- @tvec_fail@, @tvec_fail_v@ --- *
814 * Arguments: @struct tvec_state *tv@ = test-vector state
815 * @const char *detail@, @va_list *ap@ = description of test
819 * Use: Report the current test as a failure. This function can be
820 * called multiple times for a single test, e.g., if the test
821 * environment's @run@ function invokes the test function
822 * repeatedly; but a single test that fails repeatedly still
823 * only counts as a single failure in the statistics. The
824 * @detail@ string and its format parameters can be used to
825 * distinguish which of several invocations failed; it can
826 * safely be left null if the test function is run only once.
829 extern PRINTF_LIKE(2, 3)
830 void tvec_fail(struct tvec_state */
*tv*/
, const char */
*detail*/
, ...);
831 extern void tvec_fail_v(struct tvec_state */
*tv*/
,
832 const char */
*detail*/
, va_list */
*ap*/
);
834 /* --- @tvec_dumpreg@ --- *
836 * Arguments: @struct tvec_state *tv@ = test-vector state
837 * @unsigned disp@ = the register disposition (@TVRD_...@)
838 * @const union tvec_regval *tv@ = register value, or null
839 * @const struct tvec_regdef *rd@ = register definition
843 * Use: Dump a register value to the output. This is the lowest-
844 * level function for dumping registers, and calls the output
845 * formatter directly.
847 * Usually @tvec_mismatch@ is much more convenient. Low-level
848 * access is required for reporting `virtual' registers
849 * corresponding to test environment settings.
852 extern void tvec_dumpreg(struct tvec_state */
*tv*/
,
853 unsigned /*disp*/, const union tvec_regval */
*rv*/
,
854 const struct tvec_regdef */
*rd*/
);
856 /* --- @tvec_initregs@, @tvec_releaseregs@ --- *
858 * Arguments: @struct tvec_state *tv@ = test-vector state
862 * Use: Initialize or release, respectively, the registers required
863 * by the current test. All of the registers, both input and
864 * output, are effected. Initialized registers are not marked
868 extern void tvec_initregs(struct tvec_state */
*tv*/
);
869 extern void tvec_releaseregs(struct tvec_state */
*tv*/
);
871 /* --- @tvec_resetoutputs@ --- *
873 * Arguments: @struct tvec_state *tv@ = test-vector state
877 * Use: Reset (releases and reinitializes) the output registers in
878 * the test state. This is mostly of use to test environment
879 * @run@ functions, between invocations of the test function.
880 * Output registers are marked live if and only if the
881 * corresponding input register is live.
884 extern void tvec_resetoutputs(struct tvec_state */
*tv*/
);
886 /* --- @tvec_checkregs@ --- *
888 * Arguments: @struct tvec_state *tv@ = test-vector state
890 * Returns: Zero on success, %$-1$% on mismatch.
892 * Use: Compare the active output registers (according to the current
893 * test group definition) with the corresponding input register
894 * values. A mismatch occurs if the two values differ
895 * (according to the register type's @eq@ method), or if the
896 * input is live but the output is dead.
898 * This function only checks for a mismatch and returns the
899 * result; it takes no other action. In particular, it doesn't
900 * report a failure, or dump register values.
903 extern int tvec_checkregs(struct tvec_state */
*tv*/
);
905 /* --- @tvec_mismatch@ --- *
907 * Arguments: @struct tvec_state *tv@ = test-vector state
908 * @unsigned f@ = flags (@TVMF_...@)
912 * Use: Dumps registers suitably to report a mismatch. The flag bits
913 * @TVMF_IN@ and @TVF_OUT@ select input-only and output
914 * registers. If both are reset then nothing happens.
915 * Suppressing the output registers may be useful, e.g., if the
916 * test function crashed rather than returning outputs.
921 extern void tvec_mismatch(struct tvec_state */
*tv*/
, unsigned /*f*/);
923 /* --- @tvec_check@, @tvec_check_v@ --- *
925 * Arguments: @struct tvec_state *tv@ = test-vector state
926 * @const char *detail@, @va_list *ap@ = description of test
930 * Use: Check the register values, reporting a failure and dumping
931 * the registers in the event of a mismatch. This just wraps up
932 * @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
936 extern PRINTF_LIKE(2, 3)
937 void tvec_check(struct tvec_state */
*tv*/
, const char */
*detail*/
, ...);
938 extern void tvec_check_v(struct tvec_state */
*tv*/
,
939 const char */
*detail*/
, va_list */
*ap*/
);
941 /*----- Ad-hoc testing ----------------------------------------------------*/
943 /* --- @tvec_adhoc@ --- *
945 * Arguments: @struct tvec_state *tv@ = test-vector state
946 * @struct tvec_test *t@ = space for a test definition
950 * Use: Begin ad-hoc testing, i.e., without reading a file of
953 * The structure at @t@ will be used to record information about
954 * the tests underway, which would normally come from a static
955 * test definition. The other functions in this section assume
956 * that @tvec_adhoc@ has been called.
959 extern void tvec_adhoc(struct tvec_state */
*tv*/
, struct tvec_test */
*t*/
);
961 /* --- @tvec_begingroup@, @TVEC_BEGINGROUP@ --- *
963 * Arguments: @struct tvec_state *tv@ = test-vector state
964 * @const char *name@ = name for this test group
965 * @const char *file@, @unsigned @lno@ = calling file and line
969 * Use: Begin an ad-hoc test group with the given name. The @file@
970 * and @lno@ can be anything, but it's usually best if they
971 * refer to the source code performing the test: the macro
972 * @TVEC_BEGINGROUP@ does this automatically.
975 extern void tvec_begingroup(struct tvec_state */
*tv*/
, const char */
*name*/
,
976 const char */
*file*/
, unsigned /*lno*/);
977 #define TVEC_BEGINGROUP(tv, name) \
978 do tvec_begingroup(tv, name, __FILE__, __LINE__); while (0)
980 /* --- @tvec_endgroup@ --- *
982 * Arguments: @struct tvec_state *tv@ = test-vector state
986 * Use: End an ad-hoc test group. The statistics are updated and the
987 * outcome is reported to the output formatter.
990 extern void tvec_endgroup(struct tvec_state */
*tv*/
);
992 /* --- @TVEC_TESTGROUP@, @TVEC_TESTGROUP_TAG@ --- *
994 * Arguments: @tag@ = label-disambiguation tag
995 * @const struct tvec_state *tv = test-vector state
996 * @const char *name@ = test-group name
1000 * Use: Control-structure macro: @TVEC_TESTGROUP(tv, name) stmt@
1001 * establishes a test group with the given @name@ (attributing
1002 * it to the source file and lie number), executes @stmt@, and
1003 * ends the test group. If @stmt@ invokes @break@ then the test
1004 * group is skipped. @TVEC_TESTGROUP_TAG@ is the same, with an
1005 * additional @tag@ argument for use in higher-level macros.
1008 #define TVEC_TESTGROUP_TAG(tag, tv, name) \
1009 MC_WRAP(tag##__around, \
1010 { TVEC_BEGINGROUP(tv, name); }, \
1011 { tvec_endgroup(tv); }, \
1012 { if (!((tv)->f&TVSF_SKIP)) tvec_skipgroup(tv, 0); \
1013 tvec_endgroup(tv); })
1014 #define TVEC_TESTGROUP(tv, name) TVEC_TESTGROUP_TAG(grp, tv, name)
1016 /* --- @tvec_begintest@, @TVEC_BEGINTEST@ --- *
1018 * Arguments: @struct tvec_state *tv@ = test-vector state
1019 * @const char *file@, @unsigned @lno@ = calling file and line
1023 * Use: Begin an ad-hoc test case. The @file@ and @lno@ can be
1024 * anything, but it's usually best if they refer to the source
1025 * code performing the test: the macro @TVEC_BEGINGROUP@ does
1026 * this automatically.
1029 extern void tvec_begintest(struct tvec_state */
*tv*/
,
1030 const char */
*file*/
, unsigned /*lno*/);
1031 #define TVEC_BEGINTEST(tv) \
1032 do tvec_begintest(tv, __FILE__, __LINE__); while (0)
1034 /* --- @tvec_endtest@ --- *
1036 * Arguments: @struct tvec_state *tv@ = test-vector state
1040 * Use: End an ad-hoc test case, The statistics are updated and the
1041 * outcome is reported to the output formatter.
1044 extern void tvec_endtest(struct tvec_state */
*tv*/
);
1046 /* --- @TVEC_TEST@, @TVEC_TEST_TAG@ --- *
1048 * Arguments: @tag@ = label-disambiguation tag
1049 * @struct tvec_test *t@ = space for a test definition
1053 * Use: Control-structure macro: @TVEC_TEST(tv) stmt@ begins a test
1054 * case, executes @stmt@, and ends the test case. If @stmt@
1055 * invokes @break@ then the test case is skipped.
1056 * @TVEC_TEST_TAG@ is the same, with an additional @tag@ argumet
1057 * for use in higher-level macros.
1060 #define TVEC_TEST_TAG(tag, tv) \
1061 MC_WRAP(tag##__around, \
1062 { TVEC_BEGINTEST(tv); }, \
1063 { tvec_endtest(tv); }, \
1064 { if ((tv)->f&TVSF_ACTIVE) tvec_skip((tv), 0); \
1065 tvec_endtest(tv); })
1066 #define TVEC_TEST(tv) TVEC_TEST_TAG(test, tv)
1068 /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- *
1070 * Arguments: @struct tvec_state *tv@ = test-vector state
1072 * @const char *file@, @unsigned @lno@ = calling file and line
1073 * @const char *msg@, @va_list *ap@ = message to report on
1076 * Returns: The value @ok@.
1078 * Use: Check that a claimed condition holds, as (part of) a test.
1079 * If no test case is underway (i.e., if @TVSF_OPEN@ is reset in
1080 * @tv->f@), then a new test case is begun and ended. The
1081 * @file@ and @lno@ are passed to the output formatter to be
1082 * reported in case of a failure. If @ok@ is nonzero, then
1083 * nothing else happens; so, in particular, if @tvec_claim@
1084 * established a new test case, then the test case succeeds. If
1085 * @ok@ is zero, then a failure is reported, quoting @msg@.
1087 * The @TVEC_CLAIM@ macro is similar, only it (a) identifies the
1088 * file and line number of the call site automatically, and (b)
1089 * implicitly quotes the source text of the @ok@ condition in
1090 * the failure message.
1093 extern PRINTF_LIKE(5, 6)
1094 int tvec_claim(struct tvec_state */
*tv*/
, int /*ok*/,
1095 const char */
*file*/
, unsigned /*lno*/,
1096 const char */
*msg*/
, ...);
1097 extern int tvec_claim_v(struct tvec_state */
*tv*/
, int /*ok*/,
1098 const char */
*file*/
, unsigned /*lno*/,
1099 const char */
*msg*/
, va_list */
*ap*/
);
1100 #define TVEC_CLAIM(tv, cond) \
1101 (tvec_claim(tv, !!(cond), __FILE__, __LINE__, "%s untrue", #cond))
1103 /* --- @tvec_claimeq@ --- *
1105 * Arguments: @struct tvec_state *tv@ = test-vector state
1106 * @const struct tvec_regty *ty@ = register type
1107 * @const union tvec_misc *arg@ = register type argument
1108 * @const char *file@, @unsigned @lno@ = calling file and line
1109 * @const char *expr@ = the expression to quote on failure
1111 * Returns: Nonzero if the input and output values of register 0 are
1112 * equal, zero if they differ.
1114 * Use: Check that the input and output values of register 0 are
1115 * equal (according to the register type @ty@). As for
1116 * @tvec_claim@ above, a test case is automatically begun and
1117 * ended if none is already underway. If the values are
1118 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1119 * mismatched values are dumped.
1121 * This function is not expected to be called directly, but
1122 * through type-specific wrapper functions or macros such as
1123 * @TVEC_CLAIMEQ_INT@.
1126 extern int tvec_claimeq(struct tvec_state */
*tv*/
,
1127 const struct tvec_regty */
*ty*/
,
1128 const union tvec_misc */
*arg*/
,
1129 const char */
*file*/
, unsigned /*lno*/,
1130 const char */
*expr*/
);
1132 /*----- Benchmarking ------------------------------------------------------*/
1134 struct tvec_benchenv
{
1135 struct tvec_env _env
; /* benchmarking is an environment */
1136 struct bench_state
**bst
; /* benchmark state anchor or null */
1137 unsigned long niter
; /* iterations done per unit */
1138 int riter
, rbuf
; /* iterations and buffer registers */
1139 const struct tvec_env
*env
; /* subordinate environment */
1142 struct tvec_benchctx
{
1143 const struct tvec_benchenv
*be
; /* environment configuration */
1144 struct bench_state
*bst
; /* benchmark state */
1145 double dflt_target
; /* default time in seconds */
1146 unsigned f
; /* flags */
1147 #define TVBF_SETTRG 1u /* set `@target' */
1148 #define TVBF_SETMASK (TVBF_SETTRG)) /* mask of @TVBF_SET...@ */
1149 void *subctx
; /* subsidiary environment context */
1152 extern struct bench_state
*tvec_benchstate
;
1154 /* --- Environment implementation --- *
1156 * The following special variables are supported.
1158 * * %|@target|% is the (approximate) number of seconds to run the
1161 * Unrecognized variables are passed to the subordinate environment, if there
1162 * is one. Other events are passed through to the subsidiary environment.
1165 extern tvec_envsetupfn tvec_benchsetup
;
1166 extern tvec_envfindvarfn tvec_benchfindvar
;
1167 extern tvec_envbeforefn tvec_benchbefore
;
1168 extern tvec_envrunfn tvec_benchrun
;
1169 extern tvec_envafterfn tvec_benchafter
;
1170 extern tvec_envteardownfn tvec_benchteardown
;
1172 #define TVEC_BENCHENV \
1173 { sizeof(struct tvec_benchctx), \
1175 tvec_benchfindvar, \
1179 tvec_benchteardown }
1180 #define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate
1182 /* --- @tvec_benchreport@ --- *
1184 * Arguments: @const struct gprintf_ops *gops@ = print operations
1185 * @void *go@ = print destination
1186 * @unsigned unit@ = the unit being measured (~TVBU_...@)
1187 * @const struct bench_timing *tm@ = the benchmark result
1191 * Use: Formats a report about the benchmark performance. This
1192 * function is intended to be called on by an output
1193 * @ebench@ function.
1196 extern void tvec_benchreport
1197 (const struct gprintf_ops */
*gops*/
, void */
*go*/
,
1198 unsigned /*unit*/, const struct bench_timing */
*tm*/
);
1200 /*----- Remote execution --------------------------------------------------*/
1202 struct tvec_remotecomms
{
1203 int infd
, outfd
; /* input and output descriptors */
1204 dbuf bout
; /* output buffer */
1205 unsigned char *bin
; /* input buffer */
1206 size_t binoff
, binlen
, binsz
; /* input offset, length, and size */
1207 size_t t
; /* temporary offset */
1208 unsigned f
; /* flags */
1209 #define TVRF_BROKEN 0x0001u /* communications have failed */
1211 #define TVEC_REMOTECOMMS_INIT { -1, -1, DBUF_INIT, 0, 0, 0, 0, 0, 0 }
1213 struct tvec_remotectx
{
1214 struct tvec_state
*tv
; /* test vector state */
1215 struct tvec_remotecomms rc
; /* communication state */
1216 const struct tvec_remoteenv
*re
; /* environment configuration */
1217 void *subctx
; /* subenvironment context */
1218 struct tvec_vardef vd
; /* temporary variable definition */
1219 unsigned ver
; /* protocol version */
1220 pid_t kid
; /* child process id */
1221 int errfd
; /* child stderr descriptor */
1222 lbuf errbuf
; /* child stderr line buffer */
1223 dstr prgwant
, progress
; /* progress: wanted/reported */
1224 unsigned exwant
, exit
; /* exit status wanted/reported */
1225 #define TVRF_RCNMASK 0x0300u /* reconnection behaviour: */
1226 #define TVRCN_DEMAND 0x0000u /* connect on demand */
1227 #define TVRCN_SKIP 0x0100u /* skip unless connected */
1228 #define TVRCN_FORCE 0x0200u /* force reconnection */
1229 #define TVRF_MUFFLE 0x0400u /* muffle child stderr */
1230 #define TVRF_SETEXIT 0x0800u /* set `@exit' */
1231 #define TVRF_SETPRG 0x1000u /* set `@progress' */
1232 #define TVRF_SETRCN 0x2000u /* set `@reconnect' */
1233 #define TVRF_SETMASK (TVRF_SETEXIT | TVRF_SETPRG | TVRF_SETRCN)
1234 /* mask of @TVTF_SET...@ */
1237 typedef int tvec_connectfn(pid_t */
*kid_out*/
, int */
*infd_out*/
,
1238 int */
*outfd_out*/
, int */
*errfd_out*/
,
1239 struct tvec_state */
*tv*/
,
1240 const struct tvec_remoteenv */
*env*/
);
1241 /* A connection function. On entry, @tv@ holds the test-vector state, and
1242 * @env@ is the test group's remote environment structure, which will
1243 * typically really be some subclass of @struct tvec_remoteenv@ containing
1244 * additional parameters for establishing the child process.
1246 * On successful completion, the function stores input and output
1247 * descriptors (which need not be distinct) in @*infd_out@ and
1248 * @*outfd_out@, and returns zero; if it creates a child process, it should
1249 * additionally store the child's process-id in @*kid_out@ and store in
1250 * @*errfd_out@ a descriptor from which the child's error output can be
1251 * read. On error, the function should report an appropriate message via
1252 * @tvec_error@ and return %$-1$%.
1255 struct tvec_remoteenv_slots
{
1256 tvec_connectfn
*connect
; /* connection function */
1257 const struct tvec_env
*env
; /* subsidiary environment */
1258 unsigned dflt_reconn
; /* default reconnection */
1261 struct tvec_remoteenv
{
1262 struct tvec_env _env
;
1263 struct tvec_remoteenv_slots r
;
1266 struct tvec_remotefork_slots
{
1267 const struct tvec_test
*tests
; /* child tests (or null) */
1270 struct tvec_remotefork
{
1271 struct tvec_env _env
;
1272 struct tvec_remoteenv_slots r
;
1273 struct tvec_remotefork_slots f
;
1276 struct tvec_remoteexec_slots
{
1277 const char *const *args
; /* command line to execute */
1280 struct tvec_remoteexec
{
1281 struct tvec_env _env
;
1282 struct tvec_remoteenv_slots r
;
1283 struct tvec_remoteexec_slots x
;
1286 union tvec_remoteenv_subclass_kludge
{
1287 struct tvec_env _env
;
1288 struct tvec_remoteenv renv
;
1289 struct tvec_remotefork fork
;
1290 struct tvec_remoteexec exec
;
1295 * We don't use the conventional encoding returned by the @wait@(2) family of
1296 * system calls because it's too hard for our flags type to decode. Instead,
1297 * we use our own encoding.
1299 * The exit code or signal number ends up in the `value' field in the low 12
1300 * bits; bit 12 is set if the value field holds a signal, and it if holds an
1301 * exit code. Bits 13--15 hold a code which describes the status of a child
1302 * process or connection.
1304 #define TVXF_VALMASK 0x0fffu /* value (exit code or signal) */
1305 #define TVXF_SIG 0x1000u /* value is signal, not exit code */
1306 #define TVXF_CAUSEMASK 0xe000u /* mask for cause bits */
1307 #define TVXST_RUN 0x0000u /* still running */
1308 #define TVXST_EXIT 0x2000u /* child exited */
1309 #define TVXST_KILL 0x4000u /* child killed by signal */
1310 #define TVXST_CONT 0x6000u /* child continued (?) */
1311 #define TVXST_STOP 0x8000u /* child stopped (?) */
1312 #define TVXST_DISCONN 0xa000u /* disconnected */
1313 #define TVXST_UNK 0xc000u /* unknown */
1314 #define TVXST_ERR 0xe000u /* local error prevented diagnosis */
1316 /* Remote environment. */
1317 extern tvec_envsetupfn tvec_remotesetup
;
1318 extern tvec_envfindvarfn tvec_remotefindvar
;
1319 extern tvec_envbeforefn tvec_remotebefore
;
1320 extern tvec_envrunfn tvec_remoterun
;
1321 extern tvec_envafterfn tvec_remoteafter
;
1322 extern tvec_envteardownfn tvec_remoteteardown
;
1323 #define TVEC_REMOTEENV \
1324 { sizeof(struct tvec_remotectx), \
1326 tvec_remotefindvar, \
1327 tvec_remotebefore, \
1330 tvec_remoteteardown }
1332 /* --- @tvec_setprogress@, @tvec_setprogress_v@ --- *
1334 * Arguments: @const char *status@ = progress status token format
1335 * @va_list ap@ = argument tail
1339 * Use: Reports the progress of a test execution to the client.
1341 * The framework makes use of tokens beginning with %|%|%:
1343 * * %|%IDLE|%: during the top-level server code;
1345 * * %|%SETUP|%: during the enclosing environment's @before@
1348 * * %|%RUN|%: during the environment's @run@ function, or the
1349 * test function; and
1351 * * %|%DONE|%: during the enclosing environment's @after@
1354 * The intent is that a test can use the progress token to check
1355 * that a function which is expected to crash does so at the
1356 * correct point, so it's expected that more complex test
1357 * functions and/or environments will set their own progress
1358 * tokens to reflect what's going on.
1361 extern PRINTF_LIKE(1, 2) int tvec_setprogress(const char */
*status*/
, ...);
1362 extern int tvec_setprogress_v(const char */
*status*/
, va_list */
*ap*/
);
1364 /* --- @tvec_remoteserver@ --- *
1366 * Arguments: @int infd@, @int outfd@ = input and output file descriptors
1367 * @const struct tvec_config *config@ = test configuration
1369 * Returns: Suggested exit code.
1371 * Use: Run a test server, reading packets from @infd@ and writing
1372 * responses and notifications to @outfd@, and invoking tests as
1373 * described by @config@.
1375 * This function is not particularly general purpose. It
1376 * expects to `take over' the process, and makes use of private
1380 extern int tvec_remoteserver(int /*infd*/, int /*outfd*/,
1381 const struct tvec_config */
*config*/
);
1383 extern tvec_connectfn tvec_fork
, tvec_exec
;
1385 #define TVEC_REMOTEFORK(subenv, tests) \
1386 TVEC_REMOTEENV, { tvec_fork, subenv }, { tests }
1388 #define TVEC_REMOTEEXEC(subenv, args) \
1389 TVEC_REMOTEENV, { tvec_exec, subenv }, { args }
1391 /*----- Timeouts ----------------------------------------------------------*/
1393 struct tvec_timeoutenv
{
1394 struct tvec_env _env
;
1395 int timer
; /* the timer (@ITIMER_...@) */
1396 double t
; /* time to wait (in seconds) */
1397 const struct tvec_env
*env
; /* subsidiary environment */
1400 struct tvec_timeoutctx
{
1401 const struct tvec_timeoutenv
*te
; /* saved environment description */
1402 int timer
; /* timer code (as overridden) */
1403 double t
; /* time to wait (as overridden) */
1404 unsigned f
; /* flags */
1405 #define TVTF_SETTMO 1u /* set `@timeout' */
1406 #define TVTF_SETTMR 2u /* set `@timer' */
1407 #define TVTF_SETMASK (TVTF_SETTMO | TVTF_SETTMR)
1408 /* mask of @TVTF_SET...@ */
1412 extern tvec_envsetupfn tvec_timeoutsetup
;
1413 extern tvec_envfindvarfn tvec_timeoutfindvar
;
1414 extern tvec_envbeforefn tvec_timeoutbefore
;
1415 extern tvec_envrunfn tvec_timeoutrun
;
1416 extern tvec_envafterfn tvec_timeoutafter
;
1417 extern tvec_envteardownfn tvec_timeoutteardown
;
1418 #define TVEC_TIMEOUTENV \
1419 { sizeof(struct tvec_timeoutctx), \
1420 tvec_timeoutsetup, \
1421 tvec_timeoutfindvar, \
1422 tvec_timeoutbefore, \
1424 tvec_timeoutafter, \
1425 tvec_timeoutteardown }
1426 #define TVEC_TIMEOUTINIT(timer, t) TVEC_TIMEOUTENV, timer, t
1428 /*----- Output functions --------------------------------------------------*/
1430 /* --- @tvec_strlevel@ --- *
1432 * Arguments: @unsigned level@ = level code
1434 * Returns: A human-readable description.
1436 * Use: Converts a level code into something that you can print in a
1440 extern const char *tvec_strlevel(unsigned /*level*/);
1442 /* --- @tvec_report@, @tvec_report_v@ --- *
1444 * Arguments: @struct tvec_state *tv@ = test-vector state
1445 * @const char *msg@, @va_list ap@ = error message
1449 * Use: Report an message with a given severity. Messages with level
1450 * @TVLEV_ERR@ or higher force a nonzero exit code.
1453 extern PRINTF_LIKE(3, 4)
1454 void tvec_report(struct tvec_state */
*tv*/
, unsigned /*level*/,
1455 const char */
*msg*/
, ...);
1456 extern void tvec_report_v(struct tvec_state */
*tv*/
, unsigned /*level*/,
1457 const char */
*msg*/
, va_list */
*ap*/
);
1459 /* --- @tvec_error@, @tvec_notice@ --- *
1461 * Arguments: @struct tvec_state *tv@ = test-vector state
1462 * @const char *msg@, @va_list ap@ = error message
1464 * Returns: The @tvec_error@ function returns %$-1$% as a trivial
1465 * convenience; @tvec_notice@ does not return a value.
1467 * Use: Report an error or a notice. Errors are distinct from test
1468 * failures, and indicate that a problem was encountered which
1469 * compromised the activity of testing. Notices are important
1470 * information which doesn't fit into any other obvious
1474 extern PRINTF_LIKE(2, 3)
1475 int tvec_error(struct tvec_state */
*tv*/
, const char */
*msg*/
, ...);
1476 extern PRINTF_LIKE(2, 3)
1477 void tvec_notice(struct tvec_state */
*tv*/
, const char */
*msg*/
, ...);
1479 /* --- @tvec_humanoutput@ --- *
1481 * Arguments: @FILE *fp@ = output file to write on
1483 * Returns: An output formatter.
1485 * Use: Return an output formatter which writes on @fp@ with the
1486 * expectation that a human will be watching and interpreting
1487 * the output. If @fp@ denotes a terminal, the display shows a
1488 * `scoreboard' indicating the outcome of each test case
1489 * attempted, and may in addition use colour and other
1493 extern struct tvec_output
*tvec_humanoutput(FILE */
*fp*/
);
1495 /* --- @tvec_tapoutput@ --- *
1497 * Arguments: @FILE *fp@ = output file to write on
1499 * Returns: An output formatter.
1501 * Use: Return an output formatter which writes on @fp@ in `TAP'
1502 * (`Test Anything Protocol') format.
1504 * TAP comes from the Perl community, but has spread rather
1505 * further. This driver produces TAP version 14, but pretends
1506 * to be version 13. The driver produces a TAP `test point' --
1507 * i.e., a result reported as `ok' or `not ok' -- for each input
1508 * test group. Failure reports and register dumps are produced
1509 * as diagnostic messages before the final group result. (TAP
1510 * permits structuerd YAML data after the test-point result,
1511 * which could be used to report details, but (a) postponing the
1512 * details until after the report is inconvenient, and (b) there
1513 * is no standardization for the YAML anyway, so in practice
1514 * it's no more useful than the unstructured diagnostics.
1517 extern struct tvec_output
*tvec_tapoutput(FILE */
*fp*/
);
1519 /* --- @tvec_dfltoutput@ --- *
1521 * Arguments: @FILE *fp@ = output file to write on
1523 * Returns: An output formatter.
1525 * Use: Selects and instantiates an output formatter suitable for
1526 * writing on @fp@. The policy is subject to change, but
1527 * currently the `human' output format is selected if @fp@ is
1528 * interactive (i.e., if @isatty(fileno(fp))@ is true), and
1529 * otherwise the `tap' format is used.
1532 extern struct tvec_output
*tvec_dfltout(FILE */
*fp*/
);
1534 /*------ Serialization utilities ------------------------------------------*/
1536 /* Serialization format.
1538 * The `candidate register definitions' are those entries @r@ in the @regs@
1539 * vector whose index @r.i@ is strictly less than @nr@. The `selected
1540 * register definitions' are those candidate register definitions @r@ for
1541 * which the indicated register @rv[r.i]@ has the @TVRF_LIVE@ flag set. The
1542 * serialized output begins with a header bitmap: if there are %$n$%
1543 * candidate register definitions then the header bitmap consists of %$\lceil
1544 * n/8 \rceil$% bytes. Bits are ordered starting from the least significant
1545 * bit of the first byte, end ending at the most significant bit of the final
1546 * byte. The bit corresponding to a candidate register definition is set if
1547 * and only if that register defintion is selected. The header bitmap is
1548 * then followed by the serializations of the selected registers -- i.e., for
1549 * each selected register definition @r@, the serialized value of register
1550 * @rv[r.i]@ -- simply concatenated together, with no padding or alignment.
1553 /* --- @tvec_serialize@ --- *
1555 * Arguments: @const struct tvec_reg *rv@ = vector of registers
1556 * @buf *b@ = buffer to write on
1557 * @const struct tvec_regdef *regs@ = vector of register
1558 * descriptions, terminated by an entry with a null
1560 * @unsigned nr@ = number of entries in the @rv@ vector
1561 * @size_t regsz@ = true size of each element of @rv@
1563 * Returns: Zero on success, %$-1$% on failure.
1565 * Use: Serialize a collection of register values.
1567 * The serialized output is written to the buffer @b@. Failure
1568 * can be caused by running out of buffer space, or a failing
1572 extern int tvec_serialize(const struct tvec_reg */
*rv*/
, buf */
*b*/
,
1573 const struct tvec_regdef */
*regs*/
,
1574 unsigned /*nr*/, size_t /*regsz*/);
1576 /* --- @tvec_deserialize@ --- *
1578 * Arguments: @struct tvec_reg *rv@ = vector of registers
1579 * @buf *b@ = buffer to write on
1580 * @const struct tvec_regdef *regs@ = vector of register
1581 * descriptions, terminated by an entry with a null
1583 * @unsigned nr@ = number of entries in the @rv@ vector
1584 * @size_t regsz@ = true size of each element of @rv@
1586 * Returns: Zero on success, %$-1$% on failure.
1588 * Use: Deserialize a collection of register values.
1590 * The size of the register vector @nr@ and the register
1591 * definitions @regs@ must match those used when producing the
1592 * serialization. For each serialized register value,
1593 * deserialize and store the value into the appropriate register
1594 * slot, and set the @TVRF_LIVE@ flag on the register. See
1595 * @tvec_serialize@ for a description of the format.
1597 * Failure results only from a failing register type handler.
1600 extern int tvec_deserialize(struct tvec_reg */
*rv*/
, buf */
*b*/
,
1601 const struct tvec_regdef */
*regs*/
,
1602 unsigned /*nr*/, size_t /*regsz*/);
1604 /*----- Input utilities ---------------------------------------------------*/
1606 /* These are provided by the core for the benefit of type @parse@ methods,
1607 * and test-environment @set@ functions, which get to read from the test
1608 * input file. The latter are usually best implemented by calling on the
1611 * The two main rules are as follows.
1613 * * Leave the file position at the beginning of the line following
1614 * whatever it was that you read.
1616 * * When you read and consume a newline (which you do at least once, by
1617 * the previous rule), then increment @tv->lno@ to keep track of the
1618 * current line number.
1621 /* --- @tvec_syntax@, @tvec_syntax_v@ --- *
1623 * Arguments: @struct tvec_state *tv@ = test-vector state
1624 * @int ch@ = the character found (in @fgetc@ format)
1625 * @const char *expect@, @va_list ap@ = what was expected
1629 * Use: Report a syntax error quoting @ch@ and @expect@. If @ch@ is
1630 * a newline, then back up so that it can be read again (e.g.,
1631 * by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
1632 * advance the line number).
1635 extern PRINTF_LIKE(3, 4)
1636 int tvec_syntax(struct tvec_state */
*tv*/
, int /*ch*/,
1637 const char */
*expect*/
, ...);
1638 extern int tvec_syntax_v(struct tvec_state */
*tv*/
, int /*ch*/,
1639 const char */
*expect*/
, va_list */
*ap*/
);
1641 /* --- @tvec_unkreg@ --- *
1643 * Arguments: @struct tvec_state *tv@ = test-vector state
1644 * @const char *name@ = register or pseudoregister name
1648 * Use: Reports an error that the register or pseudoregister is
1652 extern int tvec_unkreg(struct tvec_state */
*tv*/
, const char */
*name*/
);
1654 /* --- @tvec_dupreg@ --- *
1656 * Arguments: @struct tvec_state *tv@ = test-vector state
1657 * @const char *name@ = register or pseudoregister name
1661 * Use: Reports an error that the register or pseudoregister has been
1662 * assigned already in the current test.
1665 extern int tvec_dupreg(struct tvec_state */
*tv*/
, const char */
*name*/
);
1667 /* --- @tvec_skipspc@ --- *
1669 * Arguments: @struct tvec_state *tv@ = test-vector state
1673 * Use: Advance over any whitespace characters other than newlines.
1674 * This will stop at `;', end-of-file, or any other kind of
1675 * non-whitespace; and it won't consume a newline.
1678 extern void tvec_skipspc(struct tvec_state */
*tv*/
);
1680 /* --- @tvec_flushtoeol@ --- *
1682 * Arguments: @struct tvec_state *tv@ = test-vector state
1683 * @unsigned f@ = flags (@TVFF_...@)
1685 * Returns: Zero on success, %$-1$% on error.
1687 * Use: Advance to the start of the next line, consuming the
1688 * preceding newline.
1690 * A syntax error is reported if no newline character is found,
1691 * i.e., the file ends in mid-line. A syntax error is also
1692 * reported if material other than whitespace or a comment is
1693 * found before the end of the line end, and @TVFF_ALLOWANY@ is
1694 * not set in @f@. The line number count is updated
1698 #define TVFF_ALLOWANY 1u
1699 extern int tvec_flushtoeol(struct tvec_state */
*tv*/
, unsigned /*f*/);
1701 /* --- @tvec_nexttoken@ --- *
1703 * Arguments: @struct tvec_state *tv@ = test-vector state
1705 * Returns: Zero if there is a next token which can be read; %$-1$% if no
1706 * token is available.
1708 * Use: Advance to the next whitespace-separated token, which may be
1711 * Tokens are separated by non-newline whitespace, comments, and
1712 * newlines followed by whitespace; a newline /not/ followed by
1713 * whitespace instead begins the next assignment, and two
1714 * newlines separated only by whitespace terminate the data for
1717 * If this function returns zero, then the next character in the
1718 * file begins a suitable token which can be read and
1719 * processed. If it returns %$-1$% then there is no such token,
1720 * and the file position is left correctly. The line number
1721 * count is updated appropriately.
1724 extern int tvec_nexttoken(struct tvec_state */
*tv*/
);
1726 /* --- @tvec_readword@, @tvec_readword_v@ --- *
1728 * Arguments: @struct tvec_state *tv@ = test-vector state
1729 * @dstr *d@ = string to append the word to
1730 * @const char *delims@ = additional delimiters to stop at
1731 * @const char *expect@, @va_list ap@ = what was expected
1733 * Returns: Zero on success, %$-1$% on failure.
1735 * Use: A `word' consists of characters other than whitespace, null
1736 * characters, and other than those listed in @delims@;
1737 * furthermore, a word does not begin with a `;'. (If you want
1738 * reading to stop at comments not preceded by whitespace, then
1739 * include `;' in @delims@. This is a common behaviour.)
1741 * If there is no word beginning at the current file position,
1742 * then return %$-1$%; furthermore, if @expect@ is not null,
1743 * then report an appropriate error via @tvec_syntax@.
1745 * Otherwise, the word is accumulated in @d@ and zero is
1746 * returned; if @d@ was not empty at the start of the call, the
1747 * newly read word is separated from the existing material by a
1748 * single space character. Since null bytes are never valid
1749 * word constituents, a null terminator is written to @d@, and
1750 * it is safe to treat the string in @d@ as being null-
1754 extern PRINTF_LIKE(4, 5)
1755 int tvec_readword(struct tvec_state */
*tv*/
, dstr */
*d*/
,
1756 const char */
*delims*/
, const char */
*expect*/
, ...);
1757 extern int tvec_readword_v(struct tvec_state */
*tv*/
, dstr */
*d*/
,
1758 const char */
*delims*/
, const char */
*expect*/
,
1761 /*----- Integer types: signed and unsigned --------------------------------*/
1763 /* Integers may be input in decimal, hex, binary, or octal, following
1764 * approximately usual conventions.
1766 * * Signed integers may be preceded with a `+' or `-' sign.
1768 * * Decimal integers are just a sequence of decimal digits `0' ... `9'.
1770 * * Octal integers are a sequence of digits `0' ... `7', preceded by `0o'
1773 * * Hexadecimal integers are a sequence of digits `0' ... `9', `a'
1774 * ... `f', or `A' ... `F', preceded by `0x' or `0X'.
1776 * * Radix-B integers are a sequence of digits `0' ... `9', `a' ... `f', or
1777 * `A' ... `F', each with value less than B, preceded by `Br' or `BR',
1778 * where 0 < B < 36 is expressed in decimal without any leading `0' or
1779 * internal underscores `_'.
1781 * * A digit sequence may contain internal underscore `_' separators, but
1782 * not before or after all of the digits; and two consecutive `_'
1783 * characters are not permitted.
1786 extern const struct tvec_regty tvty_int
, tvty_uint
;
1788 /* The @arg.p@ slot may be null or a pointer to @struct tvec_irange@ or
1789 * @struct tvec_urange@ as appropriate. The bounds are inclusive; use, e.g.,
1790 * @LONG_MAX@ explicitly if one or the other bound is logically inapplicable.
1792 struct tvec_irange
{ long min
, max
; };
1793 struct tvec_urange
{ unsigned long min
, max
; };
1795 /* Bounds corresponding to common integer types. */
1796 extern const struct tvec_irange
1797 tvrange_schar
, tvrange_short
, tvrange_int
, tvrange_long
,
1798 tvrange_sbyte
, tvrange_i16
, tvrange_i32
;
1799 extern const struct tvec_urange
1800 tvrange_uchar
, tvrange_ushort
, tvrange_uint
, tvrange_ulong
, tvrange_size
,
1801 tvrange_byte
, tvrange_u16
, tvrange_u32
;
1803 /* --- @tvec_claimeq_int@, @TVEC_CLAIMEQ_INT@ --- *
1805 * Arguments: @struct tvec_state *tv@ = test-vector state
1806 * @long i0, i1@ = two signed integers
1807 * @const char *file@, @unsigned @lno@ = calling file and line
1808 * @const char *expr@ = the expression to quote on failure
1810 * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1812 * Use: Check that values of @i0@ and @i1@ are equal. As for
1813 * @tvec_claim@ above, a test case is automatically begun and
1814 * ended if none is already underway. If the values are
1815 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1816 * mismatched values are dumped: @i0@ is printed as the output
1817 * value and @i1@ is printed as the input reference.
1819 * The @TVEC_CLAIM_INT@ macro is similar, only it (a) identifies
1820 * the file and line number of the call site automatically, and
1821 * (b) implicitly quotes the source text of the @i0@ and @i1@
1822 * arguments in the failure message.
1825 extern int tvec_claimeq_int(struct tvec_state */
*tv*/
,
1826 long /*i0*/, long /*i1*/,
1827 const char */
*file*/
, unsigned /*lno*/,
1828 const char */
*expr*/
);
1829 #define TVEC_CLAIMEQ_INT(tv, i0, i1) \
1830 (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1))
1832 /* --- @tvec_claimeq_uint@, @TVEC_CLAIMEQ_UINT@ --- *
1834 * Arguments: @struct tvec_state *tv@ = test-vector state
1835 * @unsigned long u0, u1@ = two unsigned integers
1836 * @const char *file@, @unsigned @lno@ = calling file and line
1837 * @const char *expr@ = the expression to quote on failure
1839 * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1841 * Use: Check that values of @u0@ and @u1@ are equal. As for
1842 * @tvec_claim@ above, a test case is automatically begun and
1843 * ended if none is already underway. If the values are
1844 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1845 * mismatched values are dumped: @u0@ is printed as the output
1846 * value and @u1@ is printed as the input reference.
1848 * The @TVEC_CLAIM_UINT@ macro is similar, only it (a)
1849 * identifies the file and line number of the call site
1850 * automatically, and (b) implicitly quotes the source text of
1851 * the @u0@ and @u1@ arguments in the failure message.
1854 extern int tvec_claimeq_uint(struct tvec_state */
*tv*/
,
1855 unsigned long /*u0*/, unsigned long /*u1*/,
1856 const char */
*file*/
, unsigned /*lno*/,
1857 const char */
*expr*/
);
1858 #define TVEC_CLAIMEQ_UINT(tv, u0, u1) \
1859 (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1))
1861 /*----- Floating-point type -----------------------------------------------*/
1863 /* Floating-point values are either NaN (%|#nan|%, if supported by the
1864 * platform); positive or negative infinity (%|#inf|%, %|+#inf|%, or
1865 * %|#+inf|% (preferring the last), and %|-#inf|% or %|#-inf|% (preferring
1866 * the latter), if supported by the platform); or a number in strtod(3)
1869 * The comparison rules for floating-point numbers are complex: see
1870 * @tvec_claimeqish_float@ for details.
1873 extern const struct tvec_regty tvty_float
;
1875 struct tvec_floatinfo
{
1876 /* Details about acceptable floating-point values. */
1878 unsigned f
; /* flags (@TVFF_...@ bits) */
1879 #define TVFF_NOMIN 1u /* ignore @min@ (allow -inf) */
1880 #define TVFF_NOMAX 2u /* ignore @max@ (allow +inf) */
1881 #define TVFF_NANOK 4u /* permit NaN */
1882 #define TVFF_EQMASK 0xf0 /* how to compare */
1883 #define TVFF_EXACT 0x00 /* must equal exactly */
1884 #define TVFF_ABSDELTA 0x10 /* must be within @delta@ */
1885 #define TVFF_RELDELTA 0x20 /* diff < @delta@ fraction */
1886 double min
, max
; /* smallest/largest value allowed */
1887 double delta
; /* maximum tolerable difference */
1890 /* --- @tvec_claimeqish_float@, @TVEC_CLAIMEQISH_FLOAT@ --- *
1892 * Arguments: @struct tvec_state *tv@ = test-vector state
1893 * @double f0, f1@ = two floating-point numbers
1894 * @unsigned f@ = flags (@TVFF_...@)
1895 * @double delta@ = maximum tolerable difference
1896 * @const char *file@, @unsigned @lno@ = calling file and line
1897 * @const char *expr@ = the expression to quote on failure
1899 * Returns: Nonzero if @f0@ and @u1@ are sufficiently close, otherwise
1902 * Use: Check that values of @f0@ and @f1@ are sufficiently close.
1903 * As for @tvec_claim@ above, a test case is automatically begun
1904 * and ended if none is already underway. If the values are
1905 * too far apart, then @tvec_fail@ is called, quoting @expr@,
1906 * and the mismatched values are dumped: @f0@ is printed as the
1907 * output value and @f1@ is printed as the input reference.
1909 * The details for the comparison are as follows.
1911 * * A NaN value matches any other NaN, and nothing else.
1913 * * An infinity matches another infinity of the same sign,
1916 * * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
1917 * representable number matches only itself: in particular,
1918 * positive and negative zero are considered distinct.
1919 * (This allows tests to check that they land on the correct
1920 * side of branch cuts, for example.)
1922 * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
1923 * %$y$% when %$|x - y| < \delta$%.
1925 * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
1926 * %$y$% when %$|1 - y/x| < \delta$%. (Note that this
1927 * criterion is asymmetric FIXME
1929 * The @TVEC_CLAIM_FLOAT@ macro is similar, only it (a)
1930 * identifies the file and line number of the call site
1931 * automatically, and (b) implicitly quotes the source text of
1932 * the @f0@ and @f1@ arguments (and @delta@) in the failure
1936 extern int tvec_claimeqish_float(struct tvec_state */
*tv*/
,
1937 double /*f0*/, double /*f1*/,
1938 unsigned /*f*/, double /*delta*/,
1939 const char */
*file*/
, unsigned /*lno*/,
1940 const char */
*expr*/
);
1941 #define TVEC_CLAIMEQISH_FLOAT(tv, f0, f1, f, delta) \
1942 (tvec_claimeqish_float(tv, f0, f1, f, delta, , __FILE__, __LINE__, \
1943 #f0 " /= " #f1 " (+/- " #delta ")"))
1945 /* --- @tvec_claimeq_float@, @TVEC_CLAIMEQ_FLOAT@ --- *
1947 * Arguments: @struct tvec_state *tv@ = test-vector state
1948 * @double f0, f1@ = two floating-point numbers
1949 * @const char *file@, @unsigned @lno@ = calling file and line
1950 * @const char *expr@ = the expression to quote on failure
1952 * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero.
1954 * Use: Check that values of @f0@ and @f1@ are identical. The
1955 * function is exactly equivalent to @tvec_claimeqish_float@
1956 * with @f == TVFF_EXACT@; the macro is similarly like
1957 * @TVEC_CLAIMEQISH_FLOAT@ with @f == TVFF_EXACT@, except that
1958 * it doesn't bother to quote a delta.
1961 extern int tvec_claimeq_float(struct tvec_state */
*tv*/
,
1962 double /*f0*/, double /*f1*/,
1963 const char */
*file*/
, unsigned /*lno*/,
1964 const char */
*expr*/
);
1965 #define TVEC_CLAIMEQ_FLOAT(tv, f0, f1) \
1966 (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1))
1968 extern const struct tvec_floatinfo tvflt_finite
, tvflt_nonneg
;
1970 /*----- Durations ---------------------------------------------------------*/
1972 /* A duration measures a time interval in seconds. The input format consists
1973 * of a nonnegative decimal floating-point number in @strtod@ format followed
1974 * by an optional unit specification.
1976 * No @tvec_claimeq_...@ function is provided for durations: use
1977 * @tvec_claimeq_float@.
1980 extern const struct tvec_regty tvty_duration
;
1982 /*----- Enumerated types --------------------------------------------------*/
1984 /* An enumeration describes a set of values of some underlying type, each of
1985 * which has a symbolic name. Values outside of the defined set can occur --
1986 * on output, because of bugs in the tested code, or on input to test
1987 * handling of unexpected values.
1989 * There is a distinct enumerated type for each of the branches of
1990 * @tvec_misc@. In the following, we write @t@ for the type code, which is
1991 * @i@ for signed integer, @u@ for unsigned integer, @f@ for floating-point,
1992 * and @p@ for pointer.
1994 * On input, an enumerated value may be given by name or as a literal value.
1995 * For enumerations based on numeric types, the literal values can be written
1996 * in the same syntax as the underlying values. For enumerations based on
1997 * pointers, the only permitted literal is %|#nil|%, which denotes a null
1998 * pointer. On output, names are preferred (with the underlying value given
2002 #define DEFENUMTY(tag, ty, slot) \
2003 extern const struct tvec_regty tvty_##slot##enum;
2004 TVEC_MISCSLOTS(DEFENUMTY
)
2007 /* A @struct tvec_tassoc@ associates a string tag with a value. */
2008 #define DEFASSOC(tag_, ty, slot) \
2009 struct tvec_##slot##assoc { const char *tag; ty slot; };
2010 TVEC_MISCSLOTS(DEFASSOC
)
2013 #define TVEC_ENDENUM { 0, 0 }
2015 /* Information about an enumerated type. */
2016 #define DEFINFO(tag, ty, slot) \
2017 struct tvec_##slot##enuminfo { \
2018 const char *name; /* type name for diagnostics */ \
2019 const struct tvec_##slot##assoc *av; /* name/value mappings */ \
2020 EXTRA_##tag##_INFOSLOTS /* type-specific extra info */ \
2023 #define EXTRA_INT_INFOSLOTS \
2024 const struct tvec_irange *ir; /* allowed range of raw values */
2026 #define EXTRA_UINT_INFOSLOTS \
2027 const struct tvec_urange *ur; /* allowed range of raw values */
2029 #define EXTRA_FLT_INFOSLOTS \
2030 const struct tvec_floatinfo *fi; /* range and matching policy */
2032 #define EXTRA_PTR_INFOSLOTS /* (nothing) */
2034 TVEC_MISCSLOTS(DEFINFO
)
2036 #undef EXTRA_INT_INFOSLOTS
2037 #undef EXTRA_UINT_INFOSLOTS
2038 #undef EXTRA_FLT_INFOSLOTS
2039 #undef EXTRA_PTR_INFOSLOTS
2043 /* Standard enumerations. */
2044 extern const struct tvec_ienuminfo tvenum_bool
;
2045 extern const struct tvec_ienuminfo tvenum_cmp
;
2047 /* --- @tvec_claimeq_tenum@, @TVEC_CLAIMEQ_TENUM@ --- *
2049 * Arguments: @struct tvec_state *tv@ = test-vector state
2050 * @const struct tvec_typeenuminfo *ei@ = enumeration type info
2051 * @ty t0, t1@ = two values
2052 * @const char *file@, @unsigned @lno@ = calling file and line
2053 * @const char *expr@ = the expression to quote on failure
2055 * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero.
2057 * Use: Check that values of @t0@ and @t1@ are equal. As for
2058 * @tvec_claim@ above, a test case is automatically begun and
2059 * ended if none is already underway. If the values are
2060 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2061 * mismatched values are dumped: @t0@ is printed as the output
2062 * value and @t1@ is printed as the input reference.
2064 * The @TVEC_CLAIM_TENUM@ macro is similar, only it (a)
2065 * identifies the file and line number of the call site
2066 * automatically, and (b) implicitly quotes the source text of
2067 * the @t0@ and @t1@ arguments in the failure message.
2070 #define DECLCLAIM(tag, ty, slot) \
2071 extern int tvec_claimeq_##slot##enum \
2072 (struct tvec_state */*tv*/, \
2073 const struct tvec_##slot##enuminfo */*ei*/, \
2074 ty /*t0*/, ty /*t1*/, \
2075 const char */*file*/, unsigned /*lno*/, const char */*expr*/);
2076 TVEC_MISCSLOTS(DECLCLAIM
)
2078 #define TVEC_CLAIMEQ_IENUM(tv, ei, i0, i1) \
2079 (tvec_claimeq_ienum(tv, ei, i0, i1, \
2080 __FILE__, __LINE__, #i0 " /= " #i1))
2081 #define TVEC_CLAIMEQ_UENUM(tv, ei, u0, u1) \
2082 (tvec_claimeq_uenum(tv, ei, u0, u1, \
2083 __FILE__, __LINE__, #u0 " /= " #u1))
2084 #define TVEC_CLAIMEQ_FENUM(tv, ei, f0, f1) \
2085 (tvec_claimeq_fenum(tv, ei, f0, f1, \
2086 __FILE__, __LINE__, #f0 " /= " #f1))
2087 #define TVEC_CLAIMEQ_PENUM(tv, ei, p0, p1) \
2088 (tvec_claimeq_penum(tv, ei, p0, p1, \
2089 __FILE__, __LINE__, #p0 " /= " #p1))
2091 /*----- Flags type --------------------------------------------------------*/
2093 /* A flags value packs a number of fields into a single nonnegative integer.
2094 * Symbolic names are associated with the possible values of the various
2095 * fields; more precisely, each name is associated with a value and a
2098 * The input syntax is a sequence of items separated by `%|||%' signs. Each
2099 * item may be the symbolic name of a field value, or a literal unsigned
2100 * integer. The masks associated with the given symbolic names must be
2101 * disjoint. The resulting numerical value is simply the bitwise OR of the
2104 * On output, the table of symbolic names and their associated values and
2105 * masks is repeatedly scanned, in order, to find disjoint matches -- i.e.,
2106 * entries whose value matches the target value in the bit positions
2107 * indicated by the mask, and whose mask doesn't overlap with any previously
2108 * found matches; the names are then output, separated by `%|||%'. Any
2109 * remaining nonzero bits not covered by any of the matching masks are output
2110 * as a single literal integer, in hex.
2113 extern const struct tvec_regty tvty_flags
;
2116 /* Definition of a single flag or bitfield value.
2118 * Each named setting comes with a value @v@ and a mask @m@; the mask
2119 * should cover all of the value bits, i.e., @(v&~m) == 0@.
2122 const char *tag
; /* name */
2123 unsigned long m
, v
; /* mask and value */
2126 #define TVEC_ENDFLAGS { 0, 0, 0 }
2128 struct tvec_flaginfo
{
2129 /* Information about a flags type. */
2131 const char *name
; /* type name for diagnostics */
2132 const struct tvec_flag
*fv
; /* name/mask/value mappings */
2133 const struct tvec_urange
*range
; /* permitted range for literals */
2136 /* --- @tvec_claimeq_flags@, @TVEC_CLAIMEQ_FLAGS@ --- *
2138 * Arguments: @struct tvec_state *tv@ = test-vector state
2139 * @const struct tvec_flaginfo *fi@ = flags type info
2140 * @unsigned long f0, f1@ = two values
2141 * @const char *file@, @unsigned @lno@ = calling file and line
2142 * @const char *expr@ = the expression to quote on failure
2144 * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero.
2146 * Use: Check that values of @f0@ and @f1@ are equal. As for
2147 * @tvec_claim@ above, a test case is automatically begun and
2148 * ended if none is already underway. If the values are
2149 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2150 * mismatched values are dumped: @f0@ is printed as the output
2151 * value and @f1@ is printed as the input reference.
2153 * The @TVEC_CLAIM_FLAGS@ macro is similar, only it (a)
2154 * identifies the file and line number of the call site
2155 * automatically, and (b) implicitly quotes the source text of
2156 * the @f0@ and @f1@ arguments in the failure message.
2159 extern int tvec_claimeq_flags(struct tvec_state */
*tv*/
,
2160 const struct tvec_flaginfo */
*fi*/
,
2161 unsigned long /*f0*/, unsigned long /*f1*/,
2162 const char */
*file*/
, unsigned /*lno*/,
2163 const char */
*expr*/
);
2164 #define TVEC_CLAIMEQ_FLAGS(tv, fi, f0, f1) \
2165 (tvec_claimeq_flags(tv, fi, f0, f1, \
2166 __FILE__, __LINE__, #f0 " /= " #f1))
2168 /*----- Character type ----------------------------------------------------*/
2170 /* A character value holds a character, as read by @fgetc@. The special
2171 * @EOF@ value can also be represented.
2173 * On input, a character value can be given by symbolic name, with a leading
2174 * `%|#|%'; or a character or `%|\|%'-escape sequence, optionally in single
2177 * The following escape sequences and character names are recognized.
2179 * * `%|#eof|%' is the special end-of-file marker.
2181 * * `%|#nul|%' is the NUL character, sometimes used to terminate strings.
2183 * * `%|bell|%', `%|bel|%', `%|ding|%', or `%|\a|%' is the BEL character
2184 * used to ring the terminal bell (or do some other thing to attract the
2185 * user's attention).
2187 * * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace character, used to
2188 * move the cursor backwords by one cell.
2190 * * %|#escape|% %|#esc|%, or%|\e|% is the escape character, used to
2191 * introduce special terminal commands.
2193 * * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed character, used to
2194 * separate pages of text.
2196 * * %|#newline|%, %|#linefeed|%, %|#lf|%, %|#nl|%, or %|\n|% is the
2197 * newline character, used to terminate lines of text or advance the
2198 * cursor to the next line (perhaps without returning it to the start of
2201 * * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is the
2202 * carriage-return character, used to return the cursor to the start of
2205 * * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the tab
2206 * character, used to advance the cursor to the next tab stop on the
2209 * * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab character.
2211 * * %|#space|%, %|#spc|% is the space character.
2213 * * %|#delete|%, %|#del|% is the delete character, used to erase the most
2216 * * %|\'|% is the single-quote character.
2218 * * %|\\|% is the backslash character.
2220 * * %|\"|% is the double-quote character.
2222 * * %|\NNN|% or %|\{NNN}|% is the character with code NNN in octal. The
2223 * NNN may be up to three digits long.
2225 * * %|\xNN|% or %|\x{NN}|% is the character with code NNN in hexadecimal.
2228 extern const struct tvec_regty tvty_char
;
2230 /* --- @tvec_claimeq_char@, @TVEC_CLAIMEQ_CHAR@ --- *
2232 * Arguments: @struct tvec_state *tv@ = test-vector state
2233 * @int ch0, ch1@ = two character codes
2234 * @const char *file@, @unsigned @lno@ = calling file and line
2235 * @const char *expr@ = the expression to quote on failure
2237 * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero.
2239 * Use: Check that values of @ch0@ and @ch1@ are equal. As for
2240 * @tvec_claim@ above, a test case is automatically begun and
2241 * ended if none is already underway. If the values are
2242 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2243 * mismatched values are dumped: @ch0@ is printed as the output
2244 * value and @ch1@ is printed as the input reference.
2246 * The @TVEC_CLAIM_CHAR@ macro is similar, only it (a)
2247 * identifies the file and line number of the call site
2248 * automatically, and (b) implicitly quotes the source text of
2249 * the @ch0@ and @ch1@ arguments in the failure message.
2252 extern int tvec_claimeq_char(struct tvec_state */
*tv*/
,
2253 int /*ch0*/, int /*ch1*/,
2254 const char */
*file*/
, unsigned /*lno*/,
2255 const char */
*expr*/
);
2256 #define TVEC_CLAIMEQ_CHAR(tv, c0, c1) \
2257 (tvec_claimeq_char(tv, c0, c1, __FILE__, __LINE__, #c0 " /= " #c1))
2259 /*----- Text and binary string types --------------------------------------*/
2261 /* A string is a sequence of octets. Text and binary strings differ
2262 * primarily in presentation: text strings are shown as raw characters where
2263 * possible; binary strings are shown as hex dumps with an auxiliary text
2266 * The input format for both kinds of strings is basically the same: a
2267 * `compound string', consisting of
2269 * * single-quoted strings, which are interpreted entirely literally, but
2270 * can't contain single quotes or newlines;
2272 * * double-quoted strings, in which `%|\|%'-escapes are interpreted as for
2275 * * character names, marked by an initial `%|#|%' sign;
2277 * * special tokens marked by an initial `%|!|%' sign; or
2279 * * barewords interpreted according to the current coding scheme.
2281 * The special tokens are
2283 * * `%|!bare|%', which causes subsequent sequences of barewords to be
2284 * treated as plain text;
2286 * * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause subsequent
2287 * barewords to be decoded in the requested manner.
2289 * * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%', which includes
2290 * %$n$% copies of the (compound) string.
2292 * The only difference between text and binary strings is that the initial
2293 * coding scheme is %|bare|% for text strings and %|hex|% for binary strings.
2295 * Either kind of string can contain internal nul characters. A trailing nul
2296 * is appended -- beyond the stated input length -- to input strings as a
2297 * convenience to test functions. Test functions may include such a nul
2298 * character on output but this is not checked by the equality test.
2300 * A @struct tvec_urange@ may be supplied as an argument: the length of the
2301 * string (in bytes) will be checked against the permitted range.
2304 extern const struct tvec_regty tvty_text
, tvty_bytes
;
2306 /* --- @tvec_claimeq_text@, @TVEC_CLAIMEQ_TEXT@ --- *
2308 * Arguments: @struct tvec_state *tv@ = test-vector state
2309 * @const char *p0@, @size_t sz0@ = first string with length
2310 * @const char *p1@, @size_t sz1@ = second string with length
2311 * @const char *file@, @unsigned @lno@ = calling file and line
2312 * @const char *expr@ = the expression to quote on failure
2314 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2317 * Use: Check that strings at @p0@ and @p1@ are equal. As for
2318 * @tvec_claim@ above, a test case is automatically begun and
2319 * ended if none is already underway. If the values are
2320 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2321 * mismatched values are dumped: @p0@ is printed as the output
2322 * value and @p1@ is printed as the input reference.
2324 * The @TVEC_CLAIM_TEXT@ macro is similar, only it (a)
2325 * identifies the file and line number of the call site
2326 * automatically, and (b) implicitly quotes the source text of
2327 * the @ch0@ and @ch1@ arguments in the failure message.
2330 extern int tvec_claimeq_text(struct tvec_state */
*tv*/
,
2331 const char */
*p0*/
, size_t /*sz0*/,
2332 const char */
*p1*/
, size_t /*sz1*/,
2333 const char */
*file*/
, unsigned /*lno*/,
2334 const char */
*expr*/
);
2335 #define TVEC_CLAIMEQ_TEXT(tv, p0, sz0, p1, sz1) \
2336 (tvec_claimeq_text(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
2337 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2339 /* --- @tvec_claimeq_textz@, @TVEC_CLAIMEQ_TEXTZ@ --- *
2341 * Arguments: @struct tvec_state *tv@ = test-vector state
2342 * @const char *p0, *p1@ = two strings to compare
2343 * @const char *file@, @unsigned @lno@ = calling file and line
2344 * @const char *expr@ = the expression to quote on failure
2346 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2349 * Use: Check that strings at @p0@ and @p1@ are equal, as for
2350 * @tvec_claimeq_string@, except that the strings are assumed
2351 * null-terminated, so their lengths don't need to be supplied
2352 * explicitly. The macro is similarly like @TVEC_CLAIMEQ_TEXT@.
2355 extern int tvec_claimeq_textz(struct tvec_state */
*tv*/
,
2356 const char */
*p0*/
, const char */
*p1*/
,
2357 const char */
*file*/
, unsigned /*lno*/,
2358 const char */
*expr*/
);
2359 #define TVEC_CLAIMEQ_TEXTZ(tv, p0, p1) \
2360 (tvec_claimeq_textz(tv, p0, p1, __FILE__, __LINE__, #p0 " /= " #p1))
2362 /* --- @tvec_claimeq_bytes@, @TVEC_CLAIMEQ_BYTES@ --- *
2364 * Arguments: @struct tvec_state *tv@ = test-vector state
2365 * @const void *p0@, @size_t sz0@ = first string with length
2366 * @const void *p1@, @size_t sz1@ = second string with length
2367 * @const char *file@, @unsigned @lno@ = calling file and line
2368 * @const char *expr@ = the expression to quote on failure
2370 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
2373 * Use: Check that binary strings at @p0@ and @p1@ are equal. As for
2374 * @tvec_claim@ above, a test case is automatically begun and
2375 * ended if none is already underway. If the values are
2376 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2377 * mismatched values are dumped: @p0@ is printed as the output
2378 * value and @p1@ is printed as the input reference.
2380 * The @TVEC_CLAIM_STRING@ macro is similar, only it (a)
2381 * identifies the file and line number of the call site
2382 * automatically, and (b) implicitly quotes the source text of
2383 * the @ch0@ and @ch1@ arguments in the failure message.
2386 extern int tvec_claimeq_bytes(struct tvec_state */
*tv*/
,
2387 const void */
*p0*/
, size_t /*sz0*/,
2388 const void */
*p1*/
, size_t /*sz1*/,
2389 const char */
*file*/
, unsigned /*lno*/,
2390 const char */
*expr*/
);
2391 #define TVEC_CLAIMEQ_BYTES(tv, p0, sz0, p1, sz1) \
2392 (tvec_claimeq(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
2393 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
2395 /* --- @tvec_alloctext@, @tvec_allocbytes@ --- *
2397 * Arguments: @union tvec_regval *rv@ = register value
2398 * @size_t sz@ = required size
2402 * Use: Allocated space in a text or binary string register. If the
2403 * current register size is sufficient, its buffer is left
2404 * alone; otherwise, the old buffer, if any, is freed and a
2405 * fresh buffer allocated. These functions are not intended to
2406 * be used to adjust a buffer repeatedly, e.g., while building
2407 * output incrementally: (a) they will perform badly, and (b)
2408 * the old buffer contents are simply discarded if reallocation
2409 * is necessary. Instead, use a @dbuf@ or @dstr@.
2411 * The @tvec_alloctext@ function sneakily allocates an extra
2412 * byte for a terminating zero. The @tvec_allocbytes@ function
2416 extern void tvec_alloctext(union tvec_regval */
*rv*/
, size_t /*sz*/);
2417 extern void tvec_allocbytes(union tvec_regval */
*rv*/
, size_t /*sz*/);
2419 /*----- Buffer type -------------------------------------------------------*/
2421 /* Buffer registers are primarily used for benchmarking. Only a buffer's
2422 * size is significant: its contents are ignored on comparison and output,
2423 * and unspecified on input.
2425 * The input is simply the buffer size, as an integer, optionally suffixed
2426 * with a unit `kB', `MB', `GB', `TB', `PB', `EB', `ZB', `YB' (with or
2427 * without the `B') denoting a power of 1024. Units are used on output only
2428 * when the size would be expressed exactly.
2430 * No @claimeq@ functions or macros are provided for buffers because they
2431 * don't seem very useful.
2434 extern const struct tvec_regty tvty_buffer
;
2436 /*----- That's all, folks -------------------------------------------------*/