chiark / gitweb /
@@@ more mess
[mLib] / test / tvec.h
CommitLineData
b64eb60f
MW
1/* -*-c-*-
2 *
3 * Test vector processing framework
4 *
5 * (c) 2023 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 * USA.
26 */
27
28#ifndef MLIB_TVEC_H
29#define MLIB_TVEC_H
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
37#include <stdarg.h>
38#include <stddef.h>
39#include <stdio.h>
40#include <string.h>
41
42#ifndef MLIB_BUF_H
43# include "buf.h"
44#endif
45
46#ifndef MLIB_CONTROL_H
47# include "control.h"
48#endif
49
50#ifndef MLIB_BUF_H
51# include "dstr.h"
52#endif
53
e63124bc
MW
54#ifndef MLIB_GPRINTF_H
55# include "gprintf.h"
56#endif
57
b64eb60f
MW
58#ifndef MLIB_MACROS_H
59# include "macros.h"
60#endif
61
62/*----- Miscellaneous values ----------------------------------------------*/
63
64/* These are attached to structures which represent extension points, as a
65 * way to pass an opaque parameter to whatever things are hooked onto them.
66 */
67
68#define TVEC_MISCSLOTS(_) \
69 _(PTR, const void *, p) /* arbitrary pointer */ \
70 _(INT, long, i) /* signed integer */ \
e63124bc
MW
71 _(UINT, unsigned long, u) /* signed integer */ \
72 _(FLT, double, f) /* floating point */
b64eb60f
MW
73
74union tvec_misc {
75#define TVEC_DEFSLOT(tag, ty, slot) ty slot;
76 TVEC_MISCSLOTS(TVEC_DEFSLOT)
77#undef TVEC_DEFSLOT
78};
79enum {
80#define TVEC_DEFCONST(tag, ty, slot) TVMISC_##tag,
81 TVEC_MISCSLOTS(TVEC_DEFCONST)
82 TVMISC_LIMIT
83};
84
85/*----- Register values ---------------------------------------------------*/
86
87/* The framework doesn't have a preconceived idea about what's in a register
88 * value: it just allocates them and accesses them through the register type
89 * functions. It doesn't even have a baked-in idea of how big a register
90 * value is: instead, it gets that via the `regsz' slot in `struct
91 * tvec_testinfo'. So, as far as the framework is concerned, it's safe to
92 * add new slots to this union, even if they make the overall union larger.
93 * This can be done by defining the preprocessor macro `TVEC_REGSLOTS' to be
94 * a `union' fragment defining any additional union members.
95 *
96 * This creates a distinction between code which does and doesn't know the
97 * size of a register value. Code which does, which typically means the test
98 * functions, benchmarking setup and teardown functions, and tightly-bound
99 * runner functions, is free to index the register vectors directly. Code
100 * which doesn't, which means the framework core itself and output formatting
101 * machinery, must use the `TVEC_REG' macro (or its more general `TVEC_GREG'
102 * companion) for indexing register vectors. (In principle, register type
103 * handlers also fit into this category, but they have no business peering
104 * into register values other than the one's they're given.)
105 */
106
107union tvec_regval {
108 /* The actual register value. This is what the type handler sees.
109 * Additional members can be added by setting `TVEC_REGSLOTS' before
110 * including this file.
111 *
112 * A register value can be /initialized/, which simply means that its
113 * contents represent a valid value according to its type -- the
114 * register can be compared, dumped, serialized, parsed into, etc.
115 * You can't do anything safely to an uninitialized register value
116 * other than initialize it.
117 */
118
119 long i; /* signed integer */
120 unsigned long u; /* unsigned integer */
121 void *p; /* pointer */
e63124bc 122 double f; /* floating point */
b64eb60f
MW
123 struct { unsigned char *p; size_t sz; } bytes; /* binary string of bytes */
124 struct { char *p; size_t sz; } str; /* text string */
125#ifdef TVEC_REGSLOTS
126 TVEC_REGSLOTS
127#endif
128};
129
130struct tvec_reg {
131 /* A register.
132 *
133 * Note that all of the registers listed as being used by a
134 * particular test group are initialized at all times[1] while that
135 * test group is being processed. (The other register slots don't
136 * even have types associated with them, so there's nothing useful we
137 * could do with them.)
138 *
139 * The `TVRF_LIVE' flag indicates that the register was assigned a
140 * value by the test vector file: it's the right thing to use to
141 * check whether an optional register is actually present. Even
142 * `dead' registers are still initialized, though.
143 *
144 * [1] This isn't quite true. Between individual tests, the
145 * registers are released and reinitialized in order to reset
146 * them to known values ready for the next test. But you won't
147 * see them at this point.
148 */
149
150 unsigned f; /* flags */
151#define TVRF_LIVE 1u /* used in current test */
152 union tvec_regval v; /* register value */
153};
154
155struct tvec_regdef {
156 /* A register definition. Register definitions list the registers
157 * which are used by a particular test group (see `struct tvec_test'
158 * below).
159 *
160 * A vector of register definitions is terminated by a definition
161 * whose `name' slot is null.
162 */
163
164 const char *name; /* register name (for input files) */
165 unsigned i; /* register index */
166 const struct tvec_regty *ty; /* register type descriptor */
167 unsigned f; /* flags */
168#define TVRF_OPT 1u /* optional register */
169#define TVRF_ID 2u /* part of test identity */
170 union tvec_misc arg; /* extra detail for the type */
171};
172
e63124bc
MW
173/* @TVEC_GREG(vec, i, regsz)@
174 *
175 * If @vec@ is a data pointer which happens to contain the address of a
176 * vector of @struct tvec_reg@ objects, @i@ is an integer, and @regsz@ is the
177 * size of a @struct tvec_reg@, then this evaluates to the address of the
178 * @i@th element of the vector.
179 *
180 * This is the general tool you need for accessing register vectors when you
181 * don't have absolute knowledge of the size of a @union tvec_regval@.
182 * Usually you want to access one of the register vectors in a @struct
183 * tvec_state@, and @TVEC_REG@ will be more convenient.
184 */
185#define TVEC_GREG(vec, i, regsz) \
186 ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz)))
187
188/*------ Serialization utilities ------------------------------------------*/
189
190/* --- @tvec_serialize@ --- *
191 *
192 * Arguments: @const struct tvec_reg *rv@ = vector of registers
193 * @buf *b@ = buffer to write on
194 * @const struct tvec_regdef *regs@ = vector of register
195 * descriptions, terminated by an entry with a null
196 * @name@ slot
197 * @unsigned nr@ = number of entries in the @rv@ vector
198 * @size_t regsz@ = true size of each element of @rv@
199 *
200 * Returns: Zero on success, @-1@ on failure.
201 *
202 * Use: Serialize a collection of register values.
203 *
204 * The `candidate register definitions' are those entries @r@ in
205 * the @regs@ vector whose index @r.i@ is strictly less than
206 * @nr@. The `selected register definitions' are those
207 * candidate register definitions @r@ for which the indicated
208 * register @rv[r.i]@ has the @TVRF_LIVE@ flag set. The
209 * serialized output begins with a header bitmap: if there are
210 * %$n$% candidate register definitions then the header bitmap
211 * consists of %$\lceil n/8 \rceil$% bytes. Bits are ordered
212 * starting from the least significant bit of the first byte,
213 * end ending at the most significant bit of the final byte.
214 * The bit corresponding to a candidate register definition is
215 * set if and only if that register defintion is selected. The
216 * header bitmap is then followed by the serializations of the
217 * selected registers -- i.e., for each selected register
218 * definition @r@, the serialized value of register @rv[r.i]@ --
219 * simply concatenated together, with no padding or alignment.
220 *
221 * The serialized output is written to the buffer @b@. Failure
222 * can be caused by running out of buffer space, or a failing
223 * type handler.
224 */
225
226extern int tvec_serialize(const struct tvec_reg */*rv*/, buf */*b*/,
b64eb60f 227 const struct tvec_regdef */*regs*/,
e63124bc 228 unsigned /*nr*/, size_t /*regsz*/);
b64eb60f 229
e63124bc
MW
230/* --- @tvec_deserialize@ --- *
231 *
232 * Arguments: @struct tvec_reg *rv@ = vector of registers
233 * @buf *b@ = buffer to write on
234 * @const struct tvec_regdef *regs@ = vector of register
235 * descriptions, terminated by an entry with a null
236 * @name@ slot
237 * @unsigned nr@ = number of entries in the @rv@ vector
238 * @size_t regsz@ = true size of each element of @rv@
239 *
240 * Returns: Zero on success, @-1@ on failure.
241 *
242 * Use: Deserialize a collection of register values.
243 *
244 * The size of the register vector @nr@ and the register
245 * definitions @regs@ must match those used when producing the
246 * serialization. For each serialized register value,
247 * deserialize and store the value into the appropriate register
248 * slot, and set the @TVRF_LIVE@ flag on the register. See
249 * @tvec_serialize@ for a description of the format.
250 *
251 * On successful completion, store the address of the first byte
252 * after the serialized data in @*end_out@ if @end_out@ is not
253 * null. Failure results only from a failing register type
254 * handler.
255 */
256
257extern int tvec_deserialize(struct tvec_reg */*rv*/, buf */*b*/,
b64eb60f 258 const struct tvec_regdef */*regs*/,
e63124bc 259 unsigned /*nr*/, size_t /*regsz*/);
b64eb60f
MW
260
261/*----- Test state --------------------------------------------------------*/
262
e63124bc 263/* Possible test outcomes. */
b64eb60f
MW
264enum { TVOUT_LOSE, TVOUT_SKIP, TVOUT_WIN, TVOUT_LIMIT };
265
266struct tvec_state {
e63124bc
MW
267 /* The primary state structure for the test vector machinery. */
268
b64eb60f
MW
269 unsigned f; /* flags */
270#define TVSF_SKIP 1u /* skip this test group */
271#define TVSF_OPEN 2u /* test is open */
272#define TVSF_ACTIVE 4u /* test is active */
882a39c1 273#define TVSF_ERROR 8u /* an error occurred */
b64eb60f
MW
274#define TVSF_OUTMASK 0xf0 /* test outcome */
275#define TVSF_OUTSHIFT 4
e63124bc
MW
276
277 /* Registers. Available to execution environments. */
b64eb60f
MW
278 unsigned nrout, nreg; /* number of output/total registers */
279 size_t regsz; /* size of register entry */
280 struct tvec_reg *in, *out; /* register vectors */
e63124bc
MW
281
282 /* Test groups state. Available to output formatters. */
b64eb60f 283 const struct tvec_test *tests, *test; /* all tests and current test */
e63124bc
MW
284
285 /* Test scoreboard. Available to output formatters. */
b64eb60f 286 unsigned curr[TVOUT_LIMIT], all[TVOUT_LIMIT], grps[TVOUT_LIMIT];
e63124bc
MW
287
288 /* Output machinery. */
b64eb60f 289 struct tvec_output *output; /* output formatter */
e63124bc
MW
290
291 /* Input machinery. Available to type parsers. */
b64eb60f
MW
292 const char *infile; unsigned lno, test_lno; /* input file name, line */
293 FILE *fp; /* input file stream */
294};
295
e63124bc
MW
296/* @TVEC_REG(tv, vec, i)@
297 *
298 * If @tv@ is a pointer to a @struct tvec_state@, @vec@ is either @in@ or
299 * @out@, and @i@ is an integer, then this evaluates to the address of the
300 * @i@th register in the selected vector.
301 */
b64eb60f
MW
302#define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->regsz)
303
304/*----- Test descriptions -------------------------------------------------*/
305
b64eb60f
MW
306typedef void tvec_testfn(const struct tvec_reg */*in*/,
307 struct tvec_reg */*out*/,
308 void */*ctx*/);
e63124bc
MW
309 /* A test function. It should read inputs from @in@ and write outputs to
310 * @out@. The @TVRF_LIVE@ is set on inputs which are actually present, and
311 * on outputs which are wanted to test. A test function can set additional
312 * `gratuitous outputs' by setting @TVRF_LIVE@ on them; clearing
313 * @TVRF_LIVE@ on a wanted output causes a mismatch.
314 *
315 * A test function may be called zero or more times by the environment. In
316 * particular, it may be called multiple times, though usually by prior
317 * arrangement with the environment.
318 *
319 * The @ctx@ is supplied by the environment's @run@ function (see below).
320 * The default environment calls the test function once, with a null
321 * @ctx@. There is no expectation that the environment's context has
322 * anything to do with the test function's context.
323 */
324
325struct tvec_env {
326 /* A test environment sets things up for and arranges to run the test.
327 *
328 * The caller is responsible for allocating storage for the environment's
329 * context, based on the @ctxsz@ slot, and freeing it later; this space is
330 * passed in as the @ctx@ parameter to the remaining functions; if @ctxsz@
331 * is zero then @ctx@ is null.
332 */
333
334 size_t ctxsz; /* environment context size */
335
336 int (*setup)(struct tvec_state */*tv*/, const struct tvec_env */*env*/,
337 void */*pctx*/, void */*ctx*/);
338 /* Initialize the context; called at the start of a test group. Return
339 * zero on success, or @-1@ on failure. If setup fails, the context is
340 * freed, and the test group is skipped.
341 */
342
343 int (*set)(struct tvec_state */*tv*/, const char */*var*/,
344 const struct tvec_env */*env*/, void */*ctx*/);
345 /* Called when the parser finds a %|@var|%' setting to parse and store
346 * the value. If @setup@ failed, this is still called (so as to skip the
347 * value), but @ctx@ is null.
348 */
349
350 int (*before)(struct tvec_state */*tv*/, void */*ctx*/);
351 /* Called prior to running a test. This is the right place to act on any
352 * `%|@var|%' settings. Return zero on success or @-1@ on failure (which
353 * causes the test to be skipped). This function is never called if the
354 * test group is skipped.
355 */
356
357 void (*run)(struct tvec_state */*tv*/, tvec_testfn */*fn*/, void */*ctx*/);
358 /* Run the test. It should either call @tvec_skip@, or run @fn@ one or
359 * more times. In the latter case, it is responsible for checking the
360 * outputs, and calling @tvec_fail@ as necessary; @tvec_checkregs@ will
361 * check the register values against the supplied test vector, while
362 * @tvec_check@ does pretty much everything necessary. This function is
363 * never called if the test group is skipped.
364 */
365
366 void (*after)(struct tvec_state */*tv*/, void */*ctx*/);
367 /* Called after running or skipping a test. Typical actions involve
368 * resetting whatever things were established by @set@. This function is
369 * never called if the test group is skipped.
370 */
371
372 void (*teardown)(struct tvec_state */*tv*/, void */*ctx*/);
373 /* Tear down the environment: called at the end of a test group. If the
374 * setup failed, then this function is still called, with a null @ctx@.
375 */
376};
b64eb60f
MW
377
378struct tvec_test {
e63124bc
MW
379 /* A test description. */
380
b64eb60f
MW
381 const char *name; /* name of the test */
382 const struct tvec_regdef *regs; /* descriptions of the registers */
e63124bc 383 const struct tvec_env *env; /* environment to run test in */
b64eb60f 384 tvec_testfn *fn; /* test function */
b64eb60f
MW
385};
386
e63124bc
MW
387
388enum {
389 /* Register output dispositions. */
390
391 TVRD_INPUT, /* input-only register */
392 TVRD_OUTPUT, /* output-only (input is dead) */
393 TVRD_MATCH, /* matching (equal) registers */
394 TVRD_FOUND, /* mismatching output register */
395 TVRD_EXPECT /* mismatching input register */
396};
397
398/* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- *
399 *
400 * Arguments: @struct tvec_state *tv@ = test-vector state
401 * @const char *excuse@, @va_list ap@ = reason why group skipped
402 *
403 * Returns: ---
404 *
405 * Use: Skip the current group. This should only be called from a
406 * test environment @setup@ function; a similar effect occurs if
407 * the @setup@ function fails.
408 */
409
b64eb60f 410extern void PRINTF_LIKE(2, 3)
e63124bc
MW
411 tvec_skipgroup(struct tvec_state */*tv*/, const char */*excuse*/, ...);
412extern void tvec_skipgroup_v(struct tvec_state */*tv*/,
413 const char */*excuse*/, va_list */*ap*/);
b64eb60f 414
e63124bc
MW
415/* --- @tvec_skip@, @tvec_skip_v@ --- *
416 *
417 * Arguments: @struct tvec_state *tv@ = test-vector state
418 * @const char *excuse@, @va_list ap@ = reason why test skipped
419 *
420 * Returns: ---
421 *
422 * Use: Skip the current test. This should only be called from a
423 * test environment @run@ function; a similar effect occurs if
424 * the @before@ function fails.
425 */
b64eb60f 426
e63124bc
MW
427extern void PRINTF_LIKE(2, 3)
428 tvec_skip(struct tvec_state */*tv*/, const char */*excuse*/, ...);
429extern void tvec_skip_v(struct tvec_state */*tv*/,
430 const char */*excuse*/, va_list */*ap*/);
b64eb60f 431
e63124bc
MW
432/* --- @tvec_resetoutputs@ --- *
433 *
434 * Arguments: @struct tvec_state *tv@ = test-vector state
435 *
436 * Returns: ---
437 *
438 * Use: Reset (releases and reinitializes) the output registers in
439 * the test state. This is mostly of use to test environment
440 * @run@ functions, between invocations of the test function.
441 */
b64eb60f 442
e63124bc 443extern void tvec_resetoutputs(struct tvec_state */*tv*/);
b64eb60f 444
e63124bc
MW
445/* --- @tvec_checkregs@ --- *
446 *
447 * Arguments: @struct tvec_state *tv@ = test-vector state
448 *
449 * Returns: Zero on success, @-1@ on mismatch.
450 *
451 * Use: Compare the active output registers (according to the current
452 * test group definition) with the corresponding input register
453 * values. A mismatch occurs if the two values differ
454 * (according to the register type's @eq@ method), or if the
455 * input is live but the output is dead.
456 *
457 * This function only checks for a mismatch and returns the
458 * result; it takes no other action. In particular, it doesn't
459 * report a failure, or dump register values.
460 */
b64eb60f 461
e63124bc
MW
462extern int tvec_checkregs(struct tvec_state */*tv*/);
463
464/* --- @tvec_fail@, @tvec_fail_v@ --- *
465 *
466 * Arguments: @struct tvec_state *tv@ = test-vector state
467 * @const char *detail@, @va_list ap@ = description of test
468 *
469 * Returns: ---
470 *
471 * Use: Report the current test as a failure. This function can be
472 * called multiple times for a single test, e.g., if the test
473 * environment's @run@ function invokes the test function
474 * repeatedly; but a single test that fails repeatedly still
475 * only counts as a single failure in the statistics. The
476 * @detail@ string and its format parameters can be used to
477 * distinguish which of several invocations failed; it can
478 * safely be left null if the test function is run only once.
479 */
480
481extern void PRINTF_LIKE(2, 3)
482 tvec_fail(struct tvec_state */*tv*/, const char */*detail*/, ...);
483extern void tvec_fail_v(struct tvec_state */*tv*/,
484 const char */*detail*/, va_list */*ap*/);
485
486/* --- @tvec_dumpreg@ --- *
487 *
488 * Arguments: @struct tvec_state *tv@ = test-vector state
489 * @unsigned disp@ = the register disposition (@TVRD_...@)
490 * @const union tvec_regval *tv@ = register value
491 * @const struct tvec_regdef *rd@ = register definition
492 *
493 * Returns: ---
494 *
495 * Use: Dump a register value to the output. This is the lowest-
496 * level function for dumping registers, and calls the output
497 * formatter directly.
498 *
499 * Usually @tvec_mismatch@ is much more convenient. Low-level
500 * access is required for reporting `virtual' registers
501 * corresponding to test environment settings.
502 */
503
504extern void tvec_dumpreg(struct tvec_state */*tv*/,
505 unsigned /*disp*/, const union tvec_regval */*rv*/,
506 const struct tvec_regdef */*rd*/);
507
508/* --- @tvec_mismatch@ --- *
509 *
510 * Arguments: @struct tvec_state *tv@ = test-vector state
511 * @unsigned f@ = flags (@TVMF_...@)
512 *
513 * Returns: ---
514 *
515 * Use: Dumps registers suitably to report a mismatch. The flag bits
516 * @TVMF_IN@ and @TVF_OUT@ select input-only and output
517 * registers. If both are reset then nothing happens.
518 * Suppressing the output registers may be useful, e.g., if the
519 * test function crashed rather than returning outputs.
520 */
521
522#define TVMF_IN 1u
523#define TVMF_OUT 2u
524extern void tvec_mismatch(struct tvec_state */*tv*/, unsigned /*f*/);
525
526/* --- @tvec_check@, @tvec_check_v@ --- *
527 *
528 * Arguments: @struct tvec_state *tv@ = test-vector state
529 * @const char *detail@, @va_list ap@ = description of test
530 *
531 * Returns: ---
532 *
533 * Use: Check the register values, reporting a failure and dumping
534 * the registers in the event of a mismatch. This just wraps up
535 * @tvec_checkregs@, @tvec_fail@ and @tvec_mismatch@ in the
536 * obvious way.
537 */
538
539extern void PRINTF_LIKE(2, 3)
540 tvec_check(struct tvec_state */*tv*/, const char */*detail*/, ...);
541extern void tvec_check_v(struct tvec_state */*tv*/,
542 const char */*detail*/, va_list */*ap*/);
b64eb60f
MW
543
544/*----- Session lifecycle -------------------------------------------------*/
545
c5e0e403
MW
546struct tvec_config {
547 /* An overall test configuration. */
e63124bc 548
c5e0e403
MW
549 const struct tvec_test *tests; /* the tests to be performed */
550 unsigned nrout, nreg; /* number of output/total regs */
551 size_t regsz; /* size of a register */
b64eb60f
MW
552};
553
c5e0e403
MW
554/* --- @tvec_begin@ --- *
555 *
556 * Arguments: @struct tvec_state *tv_out@ = state structure to fill in
557 * @const struct tvec_config *config@ = test configuration
558 * @struct tvec_output *o@ = output driver
559 *
560 * Returns: ---
561 *
562 * Use: Initialize a state structure ready to do some testing.
563 */
b64eb60f 564
c5e0e403
MW
565extern void tvec_begin(struct tvec_state */*tv_out*/,
566 const struct tvec_config */*config*/,
567 struct tvec_output */*o*/);
b64eb60f 568
c5e0e403
MW
569/* --- @tvec_end@ --- *
570 *
571 * Arguments: @struct tvec_state *tv@ = test-vector state
572 *
573 * Returns: A proposed exit code.
574 *
575 * Use: Conclude testing and suggests an exit code to be returned to
576 * the calling program. (The exit code comes from the output
577 * driver's @esession@ method.)
578 */
b64eb60f 579
c5e0e403 580extern int tvec_end(struct tvec_state */*tv*/);
b64eb60f 581
c5e0e403
MW
582/* --- @tvec_read@ --- *
583 *
584 * Arguments: @struct tvec_state *tv@ = test-vector state
585 * @const char *infile@ = the name of the input file
586 * @FILE *fp@ = stream to read from
587 *
588 * Returns: Zero on success, @-1@ on error.
589 *
590 * Use: Read test vector data from @fp@ and exercise test functions.
591 * THe return code doesn't indicate test failures: it's only
592 * concerned with whether there were problems with the input
593 * file or with actually running the tests.
594 */
b64eb60f 595
c5e0e403
MW
596extern int tvec_read(struct tvec_state */*tv*/,
597 const char */*infile*/, FILE */*fp*/);
b64eb60f 598
c5e0e403 599/*----- Input utilities ---------------------------------------------------*/
b64eb60f 600
c5e0e403
MW
601/* These are provided by the core for the benefit of type @parse@ methods,
602 * and test-environment @set@ functions, which get to read from the test
603 * input file. The latter are usually best implemented by calling on the
604 * former.
605 *
606 * The two main rules are as follows.
607 *
608 * * Leave the file position at the beginning of the line following
609 * whatever it was that you read.
610 *
611 * * When you read and consume a newline (which you do at least once, by
612 * the previous rule), then increment @tv->lno@ to keep track of the
613 * current line number.
614 */
b64eb60f 615
c5e0e403
MW
616/* --- @tvec_skipspc@ --- *
617 *
618 * Arguments: @struct tvec_state *tv@ = test-vector state
619 *
620 * Returns: ---
621 *
622 * Use: Advance over any whitespace characters other than newlines.
623 * This will stop at `;', end-of-file, or any other kind of
624 * non-whitespace; and it won't consume a newline.
625 */
b64eb60f 626
c5e0e403 627extern void tvec_skipspc(struct tvec_state */*tv*/);
b64eb60f 628
c5e0e403
MW
629/* --- @tvec_syntax@, @tvec_syntax_v@ --- *
630 *
631 * Arguments: @struct tvec_state *tv@ = test-vector state
632 * @int ch@ = the character found (in @fgetc@ format)
633 * @const char *expect@, @va_list ap@ = what was expected
634 *
635 * Returns: @-1@
636 *
637 * Use: Report a syntax error quoting @ch@ and @expect@. If @ch@ is
638 * a newline, then back up so that it can be read again (e.g.,
639 * by @tvec_flushtoeol@ or @tvec_nexttoken@, which will also
640 * advance the line number).
641 */
b64eb60f 642
c5e0e403
MW
643extern int PRINTF_LIKE(3, 4)
644 tvec_syntax(struct tvec_state */*tv*/, int /*ch*/,
645 const char */*expect*/, ...);
646extern int tvec_syntax_v(struct tvec_state */*tv*/, int /*ch*/,
647 const char */*expect*/, va_list */*ap*/);
b64eb60f 648
c5e0e403
MW
649/* --- @tvec_flushtoeol@ --- *
650 *
651 * Arguments: @struct tvec_state *tv@ = test-vector state
652 * @unsigned f@ = flags (@TVFF_...@)
653 *
654 * Returns: Zero on success, @-1@ on error.
655 *
656 * Use: Advance to the start of the next line, consuming the
657 * preceding newline.
658 *
659 * A syntax error is reported if no newline character is found,
660 * i.e., the file ends in mid-line. A syntax error is also
661 * reported if material other than whitespace or a comment is
662 * found before the end of the line end, and @TVFF_ALLOWANY@ is
663 * not set in @f@. The line number count is updated
664 * appropriately.
665 */
b64eb60f 666
c5e0e403
MW
667#define TVFF_ALLOWANY 1u
668extern int tvec_flushtoeol(struct tvec_state */*tv*/, unsigned /*f*/);
b64eb60f 669
c5e0e403
MW
670/* --- @tvec_nexttoken@ --- *
671 *
672 * Arguments: @struct tvec_state *tv@ = test-vector state
673 *
674 * Returns: Zero if there is a next token which can be read; @-1@ if no
675 * token is available.
676 *
677 * Use: Advance to the next whitespace-separated token, which may be
678 * on the next line.
679 *
680 * Tokens are separated by non-newline whitespace, comments, and
681 * newlines followed by whitespace; a newline /not/ followed by
682 * whitespace instead begins the next assignment, and two
683 * newlines separated only by whitespace terminate the data for
684 * a test.
685 *
686 * If this function returns zero, then the next character in the
687 * file begins a suitable token which can be read and
688 * processed. If it returns @-1@ then there is no such token,
689 * and the file position is left correctly. The line number
690 * count is updated appropriately.
691 */
b64eb60f 692
c5e0e403 693extern int tvec_nexttoken(struct tvec_state */*tv*/);
b64eb60f 694
c5e0e403
MW
695/* --- @tvec_readword@ --- *
696 *
697 * Arguments: @struct tvec_state *tv@ = test-vector state
698 * @dstr *d@ = string to append the word to
699 * @const char *delims@ = additional delimiters to stop at
700 * @const char *expect@, @va_list ap@ = what was expected
701 *
702 * Returns: Zero on success, @-1@ on failure.
703 *
704 * Use: A `word' consists of characters other than whitespace, null
705 * characters, and other than those listed in @delims@;
706 * furthermore, a word does not begin with a `;'. (If you want
707 * reading to stop at comments not preceded by whitespace, then
708 * include `;' in @delims@. This is a common behaviour.)
709 *
710 * If there is no word beginning at the current file position,
711 * then return @-1@; furthermore, if @expect@ is not null, then
712 * report an appropriate error via @tvec_syntax@.
713 *
714 * Otherwise, the word is accumulated in @d@ and zero is
715 * returned; if @d@ was not empty at the start of the call, the
716 * newly read word is separated from the existing material by a
717 * single space character. Since null bytes are never valid
718 * word constituents, a null terminator is written to @d@, and
719 * it is safe to treat the string in @d@ as being null-
720 * terminated.
721 */
b64eb60f 722
c5e0e403
MW
723extern int PRINTF_LIKE(4, 5)
724 tvec_readword(struct tvec_state */*tv*/, dstr */*d*/,
725 const char */*delims*/, const char */*expect*/, ...);
726extern int tvec_readword_v(struct tvec_state */*tv*/, dstr */*d*/,
727 const char */*delims*/, const char */*expect*/,
728 va_list */*ap*/);
b64eb60f
MW
729
730/*----- Output formatting -------------------------------------------------*/
731
732struct tvec_output {
733 const struct tvec_outops *ops;
734 struct tvec_state *tv;
735};
736
e63124bc 737enum { TVBU_OP, TVBU_BYTE };
b64eb60f 738
c5e0e403
MW
739struct bench_timing;
740
b64eb60f
MW
741struct tvec_outops {
742 void (*error)(struct tvec_output */*o*/,
743 const char */*msg*/, va_list */*ap*/);
744 void (*notice)(struct tvec_output */*o*/,
745 const char */*msg*/, va_list */*ap*/);
b64eb60f
MW
746
747 void (*bsession)(struct tvec_output */*o*/);
748 int (*esession)(struct tvec_output */*o*/);
749
750 void (*bgroup)(struct tvec_output */*o*/);
751 void (*egroup)(struct tvec_output */*o*/, unsigned /*outcome*/);
752 void (*skipgroup)(struct tvec_output */*o*/,
753 const char */*excuse*/, va_list */*ap*/);
754
755 void (*btest)(struct tvec_output */*o*/);
756 void (*skip)(struct tvec_output */*o*/,
757 const char */*excuse*/, va_list */*ap*/);
758 void (*fail)(struct tvec_output */*o*/,
759 const char */*detail*/, va_list */*ap*/);
e63124bc
MW
760 void (*dumpreg)(struct tvec_output */*o*/,
761 unsigned /*disp*/, const union tvec_regval */*rv*/,
762 const struct tvec_regdef */*rd*/);
b64eb60f
MW
763 void (*etest)(struct tvec_output */*o*/, unsigned /*outcome*/);
764
e63124bc
MW
765 void (*bbench)(struct tvec_output */*o*/,
766 const char */*ident*/, unsigned /*unit*/);
b64eb60f 767 void (*ebench)(struct tvec_output */*o*/,
e63124bc 768 const char */*ident*/, unsigned /*unit*/,
b64eb60f
MW
769 const struct bench_timing */*tm*/);
770
771 void (*destroy)(struct tvec_output */*o*/);
772};
773
882a39c1 774extern int PRINTF_LIKE(2, 3)
b64eb60f 775 tvec_error(struct tvec_state */*tv*/, const char */*msg*/, ...);
882a39c1
MW
776extern int tvec_error_v(struct tvec_state */*tv*/,
777 const char */*msg*/, va_list */*ap*/);
b64eb60f
MW
778
779extern void PRINTF_LIKE(2, 3)
780 tvec_notice(struct tvec_state */*tv*/, const char */*msg*/, ...);
781extern void tvec_notice_v(struct tvec_state */*tv*/,
782 const char */*msg*/, va_list */*ap*/);
783
e63124bc
MW
784extern struct tvec_output *tvec_humanoutput(FILE */*fp*/);
785extern struct tvec_output *tvec_tapoutput(FILE */*fp*/);
786extern struct tvec_output *tvec_dfltout(FILE */*fp*/);
b64eb60f 787
b64eb60f
MW
788/*----- Register types ----------------------------------------------------*/
789
790struct tvec_regty {
791 void (*init)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/);
792 void (*release)(union tvec_regval */*rv*/,
793 const struct tvec_regdef */*rd*/);
794 int (*eq)(const union tvec_regval */*rv0*/,
795 const union tvec_regval */*rv1*/,
796 const struct tvec_regdef */*rd*/);
b64eb60f
MW
797 int (*tobuf)(buf */*b*/, const union tvec_regval */*rv*/,
798 const struct tvec_regdef */*rd*/);
799 int (*frombuf)(buf */*b*/, union tvec_regval */*rv*/,
800 const struct tvec_regdef */*rd*/);
882a39c1
MW
801 int (*parse)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/,
802 struct tvec_state */*tv*/);
b64eb60f
MW
803 void (*dump)(const union tvec_regval */*rv*/,
804 const struct tvec_regdef */*rd*/,
e63124bc
MW
805 unsigned /*style*/,
806 const struct gprintf_ops */*gops*/, void */*go*/);
b64eb60f
MW
807#define TVSF_COMPACT 1u
808};
809
810extern const struct tvec_regty tvty_int, tvty_uint;
811struct tvec_irange { long min, max; };
812struct tvec_urange { unsigned long min, max; };
813
814extern const struct tvec_irange
815 tvrange_schar, tvrange_short, tvrange_int, tvrange_long,
816 tvrange_sbyte, tvrange_i16, tvrange_i32;
817extern const struct tvec_urange
818 tvrange_uchar, tvrange_ushort, tvrange_uint, tvrange_ulong, tvrange_size,
819 tvrange_byte, tvrange_u16, tvrange_u32;
e63124bc
MW
820extern const struct tvec_frange
821 tvrange_float, tvrange_double;
b64eb60f
MW
822
823extern int tvec_claimeq_int(struct tvec_state */*tv*/,
824 long /*i0*/, long /*i1*/,
825 const char */*file*/, unsigned /*lno*/,
826 const char */*expr*/);
827extern int tvec_claimeq_uint(struct tvec_state */*tv*/,
828 unsigned long /*u0*/, unsigned long /*u1*/,
829 const char */*file*/, unsigned /*lno*/,
830 const char */*expr*/);
831#define TVEC_CLAIMEQ_INT(tv, i0, i1) \
832 (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1))
833#define TVEC_CLAIMEQ_UINT(tv, u0, u1) \
834 (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1))
835
e63124bc
MW
836extern const struct tvec_regty tvty_float;
837struct tvec_floatinfo {
838 unsigned f;
839#define TVFF_NOMIN 1u
840#define TVFF_NOMAX 2u
841#define TVFF_NANOK 4u
842#define TVFF_EXACT 0u
843#define TVFF_ABSDELTA 0x10
844#define TVFF_RELDELTA 0x20
845#define TVFF_EQMASK 0xf0
846 double min, max;
847 double delta;
848};
849
850extern int tvec_claimeqish_float(struct tvec_state */*tv*/,
851 double /*f0*/, double /*f1*/,
852 unsigned /*f*/, double /*delta*/,
853 const char */*file*/, unsigned /*lno*/,
854 const char */*expr*/);
855extern int tvec_claimeq_float(struct tvec_state */*tv*/,
856 double /*f0*/, double /*f1*/,
857 const char */*file*/, unsigned /*lno*/,
858 const char */*expr*/);
859#define TVEC_CLAIMEQISH_FLOAT(tv, f0, f1, f, delta) \
860 (tvec_claimeqish_float(tv, f0, f1, f, delta, , __FILE__, __LINE__, \
861 #f0 " /= " #f1 " (+/- " #delta ")"))
862#define TVEC_CLAIMEQ_FLOAT(tv, f0, f1) \
863 (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1))
864
b64eb60f
MW
865extern const struct tvec_regty tvty_enum;
866
867#define DEFASSOC(tag_, ty, slot) \
868 struct tvec_##slot##assoc { const char *tag; ty slot; };
869TVEC_MISCSLOTS(DEFASSOC)
870#undef DEFASSOC
871
e63124bc
MW
872struct tvec_enuminfo { const char *name; unsigned mv; /* ... */ };
873struct tvec_ienuminfo {
874 struct tvec_enuminfo _ei;
875 const struct tvec_iassoc *av;
876 const struct tvec_irange *ir;
877};
878struct tvec_uenuminfo {
879 struct tvec_enuminfo _ei;
880 const struct tvec_uassoc *av;
881 const struct tvec_urange *ur;
882};
883struct tvec_fenuminfo {
884 struct tvec_enuminfo _ei;
885 const struct tvec_fassoc *av;
886 const struct tvec_floatinfo *fi;
887};
888struct tvec_penuminfo {
889 struct tvec_enuminfo _ei;
890 const struct tvec_passoc *av;
b64eb60f
MW
891};
892
e63124bc
MW
893const struct tvec_ienuminfo tvenum_bool;
894
b64eb60f
MW
895#define DECLCLAIM(tag, ty, slot) \
896 extern int tvec_claimeq_##slot##enum \
897 (struct tvec_state */*tv*/, \
e63124bc
MW
898 const struct tvec_##slot##enuminfo */*ei*/, \
899 ty /*e0*/, ty /*e1*/, \
b64eb60f
MW
900 const char */*file*/, unsigned /*lno*/, const char */*expr*/);
901TVEC_MISCSLOTS(DECLCLAIM)
902#undef DECLCLAIM
903#define TVEC_CLAIMEQ_IENUM(tv, ei, e0, e1) \
904 (tvec_claimeq_ienum(tv, ei, e0, e1, \
905 __FILE__, __LINE__, #e0 " /= " #e1))
906#define TVEC_CLAIMEQ_UENUM(tv, ei, e0, e1) \
907 (tvec_claimeq_uenum(tv, ei, e0, e1, \
908 __FILE__, __LINE__, #e0 " /= " #e1))
e63124bc
MW
909#define TVEC_CLAIMEQ_FENUM(tv, ei, e0, e1) \
910 (tvec_claimeq_fenum(tv, ei, e0, e1, \
911 __FILE__, __LINE__, #e0 " /= " #e1))
b64eb60f
MW
912#define TVEC_CLAIMEQ_PENUM(tv, ei, e0, e1) \
913 (tvec_claimeq_penum(tv, ei, e0, e1, \
914 __FILE__, __LINE__, #e0 " /= " #e1))
915
916extern const struct tvec_regty tvty_flags;
917struct tvec_flag { const char *tag; unsigned long m, v; };
918struct tvec_flaginfo {
919 const char *name;
920 const struct tvec_flag *fv;
921 const struct tvec_urange *range;
922};
923
924extern int tvec_claimeq_flags(struct tvec_state */*tv*/,
925 const struct tvec_flaginfo */*fi*/,
926 unsigned long /*f0*/, unsigned long /*f1*/,
927 const char */*file*/, unsigned /*lno*/,
928 const char */*expr*/);
929#define TVEC_CLAIMEQ_FLAGS(tv, fi, f0, f1) \
930 (tvec_claimeq_flags(tv, fi, f0, f1, \
931 __FILE__, __LINE__, #f0 " /= " #f1))
932
e63124bc
MW
933extern const struct tvec_regty tvty_char;
934extern int tvec_claimeq_char(struct tvec_state */*tv*/,
935 int /*ch0*/, int /*ch1*/,
936 const char */*file*/, unsigned /*lno*/,
937 const char */*expr*/);
938#define TVEC_CLAIMEQ_CHAR(tv, c0, c1) \
939 (tvec_claimeq_char(tv, c0, c1, __FILE__, __LINE__, #c0 " /= " #c1))
940
b64eb60f
MW
941extern const struct tvec_regty tvty_string, tvty_bytes;
942
943extern int tvec_claimeq_string(struct tvec_state */*tv*/,
944 const char */*p0*/, size_t /*sz0*/,
945 const char */*p1*/, size_t /*sz1*/,
946 const char */*file*/, unsigned /*lno*/,
947 const char */*expr*/);
948extern int tvec_claimeq_strz(struct tvec_state */*tv*/,
949 const char */*p0*/, const char */*p1*/,
950 const char */*file*/, unsigned /*lno*/,
951 const char */*expr*/);
952extern int tvec_claimeq_bytes(struct tvec_state */*tv*/,
953 const void */*p0*/, size_t /*sz0*/,
954 const void */*p1*/, size_t /*sz1*/,
955 const char */*file*/, unsigned /*lno*/,
956 const char */*expr*/);
b64eb60f
MW
957#define TVEC_CLAIMEQ_STRING(tv, p0, sz0, p1, sz1) \
958 (tvec_claimeq_string(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
959 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
960#define TVEC_CLAIMEQ_STRZ(tv, p0, p1) \
961 (tvec_claimeq_strz(tv, p0, p1, __FILE__, __LINE__, #p0 " /= " #p1))
962#define TVEC_CLAIMEQ_BYTES(tv, p0, sz0, p1, sz1) \
963 (tvec_claimeq(tv, p0, sz0, p1, sz1, __FILE__, __LINE__, \
964 #p0 "[" #sz0 "] /= " #p1 "[" #sz1 "]"))
965
966extern const struct tvec_regty tvty_buffer;
967
968extern void tvec_allocstring(union tvec_regval */*rv*/, size_t /*sz*/);
969extern void tvec_allocbytes(union tvec_regval */*rv*/, size_t /*sz*/);
970
c5e0e403
MW
971/*----- Ad-hoc testing ----------------------------------------------------*/
972
973extern void tvec_adhoc(struct tvec_state */*tv*/, struct tvec_test */*t*/);
974
975extern void tvec_begingroup(struct tvec_state */*tv*/, const char */*name*/,
976 const char */*file*/, unsigned /*lno*/);
977extern void tvec_reportgroup(struct tvec_state */*tv*/);
978extern void tvec_endgroup(struct tvec_state */*tv*/);
979
980#define TVEC_BEGINGROUP(tv, name) \
981 do tvec_begingroup(tv, name, __FILE__, __LINE__); while (0)
982
983#define TVEC_TESTGROUP(tag, tv, name) \
984 MC_WRAP(tag##__around, \
985 { TVEC_BEGINGROUP(tv, name); }, \
986 { tvec_endgroup(tv); }, \
987 { if (!((tv)->f&TVSF_SKIP)) tvec_skipgroup(tv, 0); \
988 tvec_endgroup(tv); })
989
990extern void tvec_begintest(struct tvec_state */*tv*/,
991 const char */*file*/, unsigned /*lno*/);
992extern void tvec_endtest(struct tvec_state */*tv*/);
993
994#define TVEC_BEGINTEST(tv) \
995 do tvec_begintest(tv, __FILE__, __LINE__); while (0)
996
997#define TVEC_TEST(tag, tv) \
998 MC_WRAP(tag##__around, \
999 { TVEC_BEGINTEST(tv); }, \
1000 { tvec_endtest(tv); }, \
1001 { if ((tv)->f&TVSF_ACTIVE) tvec_skipgroup((tv), 0); \
1002 tvec_endtest(tv); })
1003
1004extern int PRINTF_LIKE(5, 6)
1005 tvec_claim(struct tvec_state */*tv*/, int /*ok*/,
1006 const char */*file*/, unsigned /*lno*/,
1007 const char */*expr*/, ...);
1008
1009#define TVEC_CLAIM(tv, cond) \
1010 (tvec_claim(tv, !!(cond), __FILE__, __LINE__, #cond " untrue"))
1011
1012extern int tvec_claimeq(struct tvec_state */*tv*/,
1013 const struct tvec_regty */*ty*/,
1014 const union tvec_misc */*arg*/,
1015 const char */*file*/, unsigned /*lno*/,
1016 const char */*expr*/);
1017
1018/*----- Benchmarking ------------------------------------------------------*/
1019
1020struct tvec_bench {
1021 struct tvec_env _env; /* benchmarking is an environment */
1022 struct bench_state **bst; /* benchmark state anchor or null */
1023 unsigned long niter; /* iterations done per unit */
1024 int riter, rbuf; /* iterations and buffer registers */
1025 const struct tvec_env *env; /* environment (per test, not grp) */
1026};
1027#define TVEC_BENCHENV \
1028 { sizeof(struct tvec_benchctx), \
1029 tvec_benchsetup, \
1030 tvec_benchset, \
1031 tvec_benchbefore, \
1032 tvec_benchrun, \
1033 tvec_benchafter, \
1034 tvec_benchteardown }
1035#define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate
1036
1037struct tvec_benchctx {
1038 const struct tvec_bench *b;
1039 struct bench_state *bst;
1040 double dflt_target;
1041 void *subctx;
1042};
1043
1044extern struct bench_state *tvec_benchstate;
1045
1046extern int tvec_benchsetup(struct tvec_state */*tv*/,
1047 const struct tvec_env */*env*/,
1048 void */*pctx*/, void */*ctx*/);
1049extern int tvec_benchset(struct tvec_state */*tv*/, const char */*var*/,
1050 const struct tvec_env */*env*/, void */*ctx*/);
1051extern int tvec_benchbefore(struct tvec_state */*tv*/, void */*ctx*/);
1052extern void tvec_benchrun(struct tvec_state */*tv*/,
1053 tvec_testfn */*fn*/, void */*ctx*/);
1054extern void tvec_benchafter(struct tvec_state */*tv*/, void */*ctx*/);
1055extern void tvec_benchteardown(struct tvec_state */*tv*/, void */*ctx*/);
1056
1057extern void tvec_benchreport
1058 (const struct gprintf_ops */*gops*/, void */*go*/,
1059 unsigned /*unit*/, const struct bench_timing */*tm*/);
1060
1061/*----- Command-line interface --------------------------------------------*/
1062
1063extern const struct tvec_config tvec_adhocconfig;
1064
1065extern void tvec_parseargs(int /*argc*/, char */*argv*/[],
1066 struct tvec_state */*tv_out*/,
1067 int */*argpos_out*/,
1068 const struct tvec_config */*config*/);
1069
1070extern int tvec_readstdin(struct tvec_state */*tv*/);
1071extern int tvec_readfile(struct tvec_state */*tv*/, const char */*file*/);
1072extern int tvec_readdflt(struct tvec_state */*tv*/, const char */*file*/);
1073extern int tvec_readarg(struct tvec_state */*tv*/, const char */*arg*/);
1074
1075extern int tvec_readargs(int /*argc*/, char */*argv*/[],
1076 struct tvec_state */*tv*/,
1077 int */*argpos_inout*/, const char */*dflt*/);
1078
1079extern int tvec_main(int /*argc*/, char */*argv*/[],
1080 const struct tvec_config */*config*/,
1081 const char */*dflt*/);
1082
b64eb60f
MW
1083/*----- That's all, folks -------------------------------------------------*/
1084
1085#ifdef __cplusplus
1086 }
1087#endif
1088
1089#endif