Commit | Line | Data |
---|---|---|
b64eb60f MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Test vector processing framework | |
4 | * | |
5 | * (c) 2023 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
12 | * mLib is free software: you can redistribute it and/or modify it under | |
13 | * the terms of the GNU Library General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or (at | |
15 | * your option) any later version. | |
16 | * | |
17 | * mLib is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | |
20 | * License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with mLib. If not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
25 | * USA. | |
26 | */ | |
27 | ||
28 | #ifndef MLIB_TVEC_H | |
29 | #define MLIB_TVEC_H | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
c91413e6 MW |
35 | /* Here's the overall flow for a testing session. |
36 | * | |
37 | * @tvec_begin@ | |
38 | * -> output @bsession@ | |
39 | * @tvec_read@ | |
40 | * -> output @bgroup@ | |
41 | * -> env @setup@ | |
42 | * one or more tests | |
43 | * -> type @init@ (input and output) | |
44 | * -> type @parse@ (input) | |
45 | * -> output @btest@ | |
46 | * -> env @before@ | |
47 | * -> @tvec_skipgroup@ | |
48 | * -> output @skipgroup@ | |
49 | * -> env @run@ | |
50 | * -> @tvec_skip@ | |
51 | * -> output @skip@ | |
52 | * -> test @fn@ | |
53 | * -> @tvec_checkregs@ | |
54 | * -> type @eq@ | |
55 | * -> @tvec_fail@ | |
56 | * -> output @fail@ | |
57 | * -> @tvec_mismatch@ | |
58 | * -> output @dumpreg@ | |
59 | * -> type @dump@ | |
c91413e6 | 60 | * -> output @etest@ |
31d0247c | 61 | * -> env @after@ |
c91413e6 MW |
62 | * finally |
63 | * -> output @egroup@ | |
64 | * -> env @teardown@ | |
65 | * | |
66 | * @tvec_adhoc@ | |
67 | * @tvec_begingroup@ | |
68 | * -> output @bgroup@ | |
69 | * -> env @setup@ | |
70 | * @tvec_begintest@ | |
71 | * -> output @btest@ | |
72 | * @tvec_skip@ | |
73 | * -> output @skip@ | |
74 | * @tvec_claimeq@ | |
75 | * -> @tvec_fail@ | |
76 | * -> output @fail@ | |
77 | * -> @tvec_mismatch@ | |
78 | * -> output @dumpreg@ | |
79 | * -> type @dump@ | |
80 | * @tvec_endtest@ | |
81 | * -> output @etest@ | |
82 | * or @tvec_skipgroup@ | |
83 | * -> output @skipgroup@ | |
84 | * @tvec_endgroup@ | |
85 | * -> output @egroup@ | |
86 | * | |
87 | * @tvec_end@ | |
88 | * -> output @esession@ | |
89 | * -> output @destroy@ | |
90 | * | |
91 | * @tvec_benchrun@ | |
92 | * -> type @dump@ (compact style) | |
93 | * -> output @bbench@ | |
94 | * -> subenv @run@ | |
95 | * -> test @fn@ | |
96 | * -> output @ebench@ | |
97 | * -> @tvec_benchreport@ | |
98 | * | |
99 | * The output functions @error@ and @notice@ can be called at arbitrary | |
100 | * times. | |
101 | */ | |
102 | ||
b64eb60f MW |
103 | /*----- Header files ------------------------------------------------------*/ |
104 | ||
105 | #include <stdarg.h> | |
106 | #include <stddef.h> | |
107 | #include <stdio.h> | |
108 | #include <string.h> | |
109 | ||
110 | #ifndef MLIB_BUF_H | |
111 | # include "buf.h" | |
112 | #endif | |
113 | ||
114 | #ifndef MLIB_CONTROL_H | |
115 | # include "control.h" | |
116 | #endif | |
117 | ||
118 | #ifndef MLIB_BUF_H | |
119 | # include "dstr.h" | |
120 | #endif | |
121 | ||
e63124bc MW |
122 | #ifndef MLIB_GPRINTF_H |
123 | # include "gprintf.h" | |
124 | #endif | |
125 | ||
c91413e6 MW |
126 | #ifndef MLIB_LBUF_H |
127 | # include "lbuf.h" | |
128 | #endif | |
129 | ||
b64eb60f MW |
130 | #ifndef MLIB_MACROS_H |
131 | # include "macros.h" | |
132 | #endif | |
133 | ||
134 | /*----- Miscellaneous values ----------------------------------------------*/ | |
135 | ||
136 | /* These are attached to structures which represent extension points, as a | |
137 | * way to pass an opaque parameter to whatever things are hooked onto them. | |
138 | */ | |
139 | ||
140 | #define TVEC_MISCSLOTS(_) \ | |
141 | _(PTR, const void *, p) /* arbitrary pointer */ \ | |
142 | _(INT, long, i) /* signed integer */ \ | |
e63124bc MW |
143 | _(UINT, unsigned long, u) /* signed integer */ \ |
144 | _(FLT, double, f) /* floating point */ | |
b64eb60f MW |
145 | |
146 | union tvec_misc { | |
147 | #define TVEC_DEFSLOT(tag, ty, slot) ty slot; | |
148 | TVEC_MISCSLOTS(TVEC_DEFSLOT) | |
149 | #undef TVEC_DEFSLOT | |
150 | }; | |
151 | enum { | |
152 | #define TVEC_DEFCONST(tag, ty, slot) TVMISC_##tag, | |
153 | TVEC_MISCSLOTS(TVEC_DEFCONST) | |
154 | TVMISC_LIMIT | |
155 | }; | |
156 | ||
157 | /*----- Register values ---------------------------------------------------*/ | |
158 | ||
159 | /* The framework doesn't have a preconceived idea about what's in a register | |
160 | * value: it just allocates them and accesses them through the register type | |
161 | * functions. It doesn't even have a baked-in idea of how big a register | |
162 | * value is: instead, it gets that via the `regsz' slot in `struct | |
163 | * tvec_testinfo'. So, as far as the framework is concerned, it's safe to | |
164 | * add new slots to this union, even if they make the overall union larger. | |
165 | * This can be done by defining the preprocessor macro `TVEC_REGSLOTS' to be | |
166 | * a `union' fragment defining any additional union members. | |
167 | * | |
168 | * This creates a distinction between code which does and doesn't know the | |
169 | * size of a register value. Code which does, which typically means the test | |
170 | * functions, benchmarking setup and teardown functions, and tightly-bound | |
171 | * runner functions, is free to index the register vectors directly. Code | |
172 | * which doesn't, which means the framework core itself and output formatting | |
173 | * machinery, must use the `TVEC_REG' macro (or its more general `TVEC_GREG' | |
174 | * companion) for indexing register vectors. (In principle, register type | |
175 | * handlers also fit into this category, but they have no business peering | |
176 | * into register values other than the one's they're given.) | |
177 | */ | |
178 | ||
179 | union tvec_regval { | |
31d0247c MW |
180 | /* The actual register value. This is what the type handler sees. |
181 | * Additional members can be added by setting `TVEC_REGSLOTS' before | |
182 | * including this file. | |
183 | * | |
184 | * A register value can be /initialized/, which simply means that its | |
185 | * contents represent a valid value according to its type -- the register | |
186 | * can be compared, dumped, serialized, parsed into, etc. You can't do | |
187 | * anything safely to an uninitialized register value other than initialize | |
188 | * it. | |
189 | */ | |
b64eb60f MW |
190 | |
191 | long i; /* signed integer */ | |
192 | unsigned long u; /* unsigned integer */ | |
193 | void *p; /* pointer */ | |
e63124bc | 194 | double f; /* floating point */ |
b64eb60f | 195 | struct { unsigned char *p; size_t sz; } bytes; /* binary string of bytes */ |
c81c35df | 196 | struct { char *p; size_t sz; } text; /* text string */ |
b64eb60f MW |
197 | #ifdef TVEC_REGSLOTS |
198 | TVEC_REGSLOTS | |
199 | #endif | |
200 | }; | |
201 | ||
202 | struct tvec_reg { | |
31d0247c MW |
203 | /* A register. |
204 | * | |
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.) | |
209 | * | |
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. | |
214 | * | |
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. | |
218 | */ | |
b64eb60f MW |
219 | |
220 | unsigned f; /* flags */ | |
221 | #define TVRF_LIVE 1u /* used in current test */ | |
222 | union tvec_regval v; /* register value */ | |
223 | }; | |
224 | ||
225 | struct tvec_regdef { | |
31d0247c MW |
226 | /* A register definition. Register definitions list the registers which |
227 | * are used by a particular test group (see `struct tvec_test' below). | |
228 | * | |
229 | * A vector of register definitions is terminated by a definition whose | |
230 | * `name' slot is null. | |
231 | */ | |
b64eb60f MW |
232 | |
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 */ | |
240 | }; | |
db2bf411 | 241 | #define TVEC_ENDREGS { 0, 0, 0, 0, { 0 } } |
b64eb60f | 242 | |
e63124bc MW |
243 | /* @TVEC_GREG(vec, i, regsz)@ |
244 | * | |
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. | |
249 | * | |
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. | |
254 | */ | |
255 | #define TVEC_GREG(vec, i, regsz) \ | |
256 | ((struct tvec_reg *)((unsigned char *)(vec) + (i)*(regsz))) | |
257 | ||
c91413e6 MW |
258 | /*----- Register types ----------------------------------------------------*/ |
259 | ||
260 | struct tvec_state; /* forward declaration */ | |
261 | ||
262 | struct tvec_regty { | |
263 | /* A register type. */ | |
264 | ||
265 | void (*init)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/); | |
31d0247c MW |
266 | /* Initialize the value in @*rv@. This will be called before any other |
267 | * function acting on the value, including @release@. | |
268 | */ | |
c91413e6 MW |
269 | |
270 | void (*release)(union tvec_regval */*rv*/, | |
271 | const struct tvec_regdef */*rd*/); | |
31d0247c | 272 | /* Release any resources associated with the value in @*rv@. */ |
c91413e6 MW |
273 | |
274 | int (*eq)(const union tvec_regval */*rv0*/, | |
275 | const union tvec_regval */*rv1*/, | |
276 | const struct tvec_regdef */*rd*/); | |
31d0247c MW |
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@. | |
280 | */ | |
c91413e6 MW |
281 | |
282 | int (*tobuf)(buf */*b*/, const union tvec_regval */*rv*/, | |
283 | const struct tvec_regdef */*rd*/); | |
31d0247c MW |
284 | /* Serialize the value @*rv@, writing the result to @b@. Return zero on |
285 | * success, or %$-1$% on error. | |
286 | */ | |
c91413e6 MW |
287 | |
288 | int (*frombuf)(buf */*b*/, union tvec_regval */*rv*/, | |
289 | const struct tvec_regdef */*rd*/); | |
31d0247c MW |
290 | /* Deserialize a value from @b@, storing it in @*rv@. Return zero on |
291 | * success, or %$-1$% on error. | |
292 | */ | |
c91413e6 MW |
293 | |
294 | int (*parse)(union tvec_regval */*rv*/, const struct tvec_regdef */*rd*/, | |
295 | struct tvec_state */*tv*/); | |
31d0247c MW |
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. | |
301 | */ | |
c91413e6 MW |
302 | |
303 | void (*dump)(const union tvec_regval */*rv*/, | |
304 | const struct tvec_regdef */*rd*/, | |
305 | unsigned /*style*/, | |
306 | const struct gprintf_ops */*gops*/, void */*go*/); | |
307 | #define TVSF_COMPACT 1u | |
31d0247c MW |
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 | |
313 | * human reader. | |
314 | */ | |
c91413e6 MW |
315 | }; |
316 | ||
317 | /*----- Test descriptions -------------------------------------------------*/ | |
318 | ||
319 | typedef void tvec_testfn(const struct tvec_reg */*in*/, | |
320 | struct tvec_reg */*out*/, | |
321 | void */*ctx*/); | |
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. | |
327 | * | |
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. | |
331 | * | |
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. | |
336 | */ | |
337 | ||
338 | struct tvec_env; | |
339 | ||
814e42ff MW |
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. | |
344 | */ | |
345 | ||
346 | struct tvec_vardef { | |
347 | size_t regsz; /* (minimum) register size */ | |
348 | tvec_setvarfn *setvar; /* function to set variable */ | |
349 | struct tvec_regdef def; /* register definition */ | |
350 | }; | |
351 | ||
c91413e6 MW |
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 | |
31d0247c MW |
358 | * @tvec_skipgroup@ with a suitable excuse. The @set@, @after@, and |
359 | * @teardown@ entry points will still be called, but @before@ and @run@ | |
360 | * will not. | |
c91413e6 MW |
361 | */ |
362 | ||
814e42ff MW |
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. | |
c91413e6 MW |
371 | */ |
372 | ||
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 | |
814e42ff MW |
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@ | |
379 | * flag if necessary. | |
c91413e6 MW |
380 | */ |
381 | ||
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. | |
390 | */ | |
391 | ||
392 | typedef void tvec_envafterfn(struct tvec_state */*tv*/, void */*ctx*/); | |
393 | /* Called after running or skipping a test. Typical actions involve | |
31d0247c | 394 | * resetting whatever things were established by @set@. This function |
814e42ff MW |
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. | |
c91413e6 MW |
398 | */ |
399 | ||
400 | typedef void tvec_envteardownfn(struct tvec_state */*tv*/, void */*ctx*/); | |
401 | /* Tear down the environment: called at the end of a test group. */ | |
402 | ||
403 | ||
404 | struct tvec_env { | |
405 | /* A test environment sets things up for and arranges to run the test. | |
406 | * | |
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. | |
411 | */ | |
412 | ||
413 | size_t ctxsz; /* environment context size */ | |
414 | ||
415 | tvec_envsetupfn *setup; /* setup for group */ | |
814e42ff | 416 | tvec_envfindvarfn *findvar; /* find variable */ |
c91413e6 MW |
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 */ | |
421 | }; | |
422 | ||
423 | struct tvec_test { | |
424 | /* A test description. */ | |
425 | ||
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 */ | |
430 | }; | |
431 | #define TVEC_ENDTESTS { 0, 0, 0, 0 } | |
432 | ||
433 | enum { | |
434 | /* Register output dispositions. */ | |
435 | ||
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) */ | |
442 | }; | |
443 | ||
b64eb60f MW |
444 | /*----- Test state --------------------------------------------------------*/ |
445 | ||
3efcfd2d MW |
446 | enum { |
447 | /* Possible test outcomes. */ | |
448 | ||
449 | TVOUT_LOSE, /* test failed */ | |
450 | TVOUT_SKIP, /* test skipped */ | |
451 | TVOUT_WIN, /* test passed */ | |
20ba6b0b | 452 | TVOUT_XFAIL, /* test passed, but shouldn't have */ |
3efcfd2d MW |
453 | TVOUT_LIMIT /* (number of possible outcomes) */ |
454 | }; | |
b64eb60f MW |
455 | |
456 | struct tvec_state { | |
e63124bc MW |
457 | /* The primary state structure for the test vector machinery. */ |
458 | ||
b64eb60f | 459 | unsigned f; /* flags */ |
20ba6b0b MW |
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_...@) */ | |
3efcfd2d | 465 | #define TVSF_OUTSHIFT 4 /* shift applied to outcome */ |
20ba6b0b | 466 | #define TVSF_XFAIL 0x0100u /* test expected to fail */ |
31d0247c | 467 | #define TVSF_MUFFLE 0x0200u /* muffle errors */ |
e63124bc MW |
468 | |
469 | /* Registers. Available to execution environments. */ | |
b64eb60f MW |
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 */ | |
e63124bc MW |
473 | |
474 | /* Test groups state. Available to output formatters. */ | |
b64eb60f | 475 | const struct tvec_test *tests, *test; /* all tests and current test */ |
e63124bc MW |
476 | |
477 | /* Test scoreboard. Available to output formatters. */ | |
b64eb60f | 478 | unsigned curr[TVOUT_LIMIT], all[TVOUT_LIMIT], grps[TVOUT_LIMIT]; |
e63124bc MW |
479 | |
480 | /* Output machinery. */ | |
b64eb60f | 481 | struct tvec_output *output; /* output formatter */ |
e63124bc MW |
482 | |
483 | /* Input machinery. Available to type parsers. */ | |
b64eb60f MW |
484 | const char *infile; unsigned lno, test_lno; /* input file name, line */ |
485 | FILE *fp; /* input file stream */ | |
486 | }; | |
487 | ||
e63124bc MW |
488 | /* @TVEC_REG(tv, vec, i)@ |
489 | * | |
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. | |
493 | */ | |
b64eb60f MW |
494 | #define TVEC_REG(tv, vec, i) TVEC_GREG((tv)->vec, (i), (tv)->regsz) |
495 | ||
67b5031e MW |
496 | struct tvec_config { |
497 | /* An overall test configuration. */ | |
498 | ||
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 */ | |
502 | }; | |
503 | ||
504 | /*----- Output formatting -------------------------------------------------*/ | |
505 | ||
506 | struct tvec_output { | |
507 | /* An output formatter. */ | |
508 | const struct tvec_outops *ops; /* pointer to operations */ | |
509 | }; | |
510 | ||
511 | /* Benchmarking details. */ | |
512 | enum { | |
513 | TVBU_OP, /* counting operations of some kind */ | |
c91413e6 MW |
514 | TVBU_BYTE, /* counting bytes (@rbuf >= 0@) */ |
515 | TVBU_LIMIT /* (number of units) */ | |
67b5031e MW |
516 | }; |
517 | struct bench_timing; /* forward declaration */ | |
518 | ||
519 | struct tvec_outops { | |
520 | /* Output operations. */ | |
521 | ||
522 | void (*bsession)(struct tvec_output */*o*/, struct tvec_state */*tv*/); | |
31d0247c MW |
523 | /* Begin a test session. The output driver will probably want to |
524 | * save @tv@, because this isn't provided to any other methods. | |
525 | */ | |
67b5031e MW |
526 | |
527 | int (*esession)(struct tvec_output */*o*/); | |
31d0247c | 528 | /* End a session, and return the suggested exit code. */ |
67b5031e MW |
529 | |
530 | void (*bgroup)(struct tvec_output */*o*/); | |
31d0247c | 531 | /* Begin a test group. The test group description is @tv->test@. */ |
67b5031e MW |
532 | |
533 | void (*skipgroup)(struct tvec_output */*o*/, | |
534 | const char */*excuse*/, va_list */*ap*/); | |
31d0247c MW |
535 | /* The group is being skipped; @excuse@ may be null or a format |
536 | * string explaining why. The @egroup@ method will not be called | |
537 | * separately. | |
538 | */ | |
67b5031e MW |
539 | |
540 | void (*egroup)(struct tvec_output */*o*/); | |
31d0247c MW |
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. | |
544 | */ | |
67b5031e MW |
545 | |
546 | void (*btest)(struct tvec_output */*o*/); | |
31d0247c | 547 | /* Begin a test case. */ |
67b5031e MW |
548 | |
549 | void (*skip)(struct tvec_output */*o*/, | |
550 | const char */*excuse*/, va_list */*ap*/); | |
31d0247c MW |
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. | |
555 | */ | |
67b5031e MW |
556 | |
557 | void (*fail)(struct tvec_output */*o*/, | |
558 | const char */*detail*/, va_list */*ap*/); | |
31d0247c MW |
559 | /* The test case failed. |
560 | * | |
561 | * The output driver should preferably report the filename (@infile@) and | |
562 | * line number (@test_lno@, not @lno@) for the failing test. | |
563 | * | |
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. | |
568 | * | |
569 | * A single test case can fail multiple times! | |
570 | */ | |
67b5031e MW |
571 | |
572 | void (*dumpreg)(struct tvec_output */*o*/, | |
573 | unsigned /*disp*/, const union tvec_regval */*rv*/, | |
574 | const struct tvec_regdef */*rd*/); | |
31d0247c MW |
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. | |
582 | */ | |
67b5031e MW |
583 | |
584 | void (*etest)(struct tvec_output */*o*/, unsigned /*outcome*/); | |
31d0247c MW |
585 | /* The test case concluded with the given @outcome@ (one of the |
586 | * @TVOUT_...@ codes. | |
587 | */ | |
67b5031e MW |
588 | |
589 | void (*bbench)(struct tvec_output */*o*/, | |
590 | const char */*ident*/, unsigned /*unit*/); | |
31d0247c MW |
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. | |
597 | */ | |
67b5031e MW |
598 | |
599 | void (*ebench)(struct tvec_output */*o*/, | |
600 | const char */*ident*/, unsigned /*unit*/, | |
601 | const struct bench_timing */*tm*/); | |
31d0247c MW |
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. | |
606 | */ | |
67b5031e | 607 | |
c91413e6 | 608 | void (*report)(struct tvec_output */*o*/, unsigned /*level*/, |
67b5031e | 609 | const char */*msg*/, va_list */*ap*/); |
31d0247c MW |
610 | /* Report a message. The driver should ideally report the filename |
611 | * (@infile@) and line number (@lno@) prompting the error. | |
612 | */ | |
67b5031e MW |
613 | |
614 | void (*destroy)(struct tvec_output */*o*/); | |
31d0247c | 615 | /* Release any resources acquired by the driver. */ |
67b5031e MW |
616 | }; |
617 | ||
31d0247c MW |
618 | #define TVEC_LEVELS(_) \ |
619 | _(NOTE, "notice", 4) \ | |
620 | _(ERR, "ERROR", 8) | |
621 | ||
e63124bc | 622 | enum { |
31d0247c MW |
623 | #define TVEC_DEFLEVEL(tag, name, val) TVLEV_##tag = val, |
624 | TVEC_LEVELS(TVEC_DEFLEVEL) | |
625 | #undef TVEC_DEFLEVEL | |
626 | TVLEV_LIMIT | |
67b5031e MW |
627 | }; |
628 | ||
629 | /*----- Session lifecycle -------------------------------------------------*/ | |
630 | ||
67b5031e MW |
631 | /* --- @tvec_begin@ --- * |
632 | * | |
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 | |
636 | * | |
637 | * Returns: --- | |
638 | * | |
639 | * Use: Initialize a state structure ready to do some testing. | |
640 | */ | |
641 | ||
642 | extern void tvec_begin(struct tvec_state */*tv_out*/, | |
643 | const struct tvec_config */*config*/, | |
644 | struct tvec_output */*o*/); | |
645 | ||
646 | /* --- @tvec_end@ --- * | |
647 | * | |
648 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
649 | * | |
650 | * Returns: A proposed exit code. | |
651 | * | |
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.) | |
655 | */ | |
656 | ||
657 | extern int tvec_end(struct tvec_state */*tv*/); | |
658 | ||
659 | /* --- @tvec_read@ --- * | |
660 | * | |
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 | |
664 | * | |
31d0247c | 665 | * Returns: Zero on success, %$-1$% on error. |
67b5031e MW |
666 | * |
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. | |
671 | */ | |
672 | ||
673 | extern int tvec_read(struct tvec_state */*tv*/, | |
674 | const char */*infile*/, FILE */*fp*/); | |
675 | ||
676 | /*----- Command-line interface --------------------------------------------*/ | |
677 | ||
678 | extern const struct tvec_config tvec_adhocconfig; | |
20ba6b0b MW |
679 | /* A special @struct tvec_config@ to use for programs which perform ad-hoc |
680 | * testing. | |
681 | */ | |
67b5031e MW |
682 | |
683 | /* --- @tvec_parseargs@ --- * | |
684 | * | |
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 | |
690 | * | |
691 | * Returns: --- | |
692 | * | |
693 | * Use: Parse arguments and set up the test vector state @*tv_out@. | |
694 | * If errors occur, print messages to standard error and exit | |
695 | * with status 2. | |
696 | */ | |
697 | ||
698 | extern void tvec_parseargs(int /*argc*/, char */*argv*/[], | |
699 | struct tvec_state */*tv_out*/, | |
700 | int */*argpos_out*/, | |
701 | const struct tvec_config */*config*/); | |
702 | ||
703 | /* --- @tvec_readstdin@, @tvec_readfile@, @tvec_readarg@ --- * | |
704 | * | |
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 | |
708 | * | |
31d0247c | 709 | * Returns: Zero on success, %$-1$% on error. |
67b5031e MW |
710 | * |
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. | |
714 | */ | |
715 | ||
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*/); | |
719 | ||
720 | /* --- @tvec_readdflt@ --- * | |
721 | * | |
722 | * Arguments: @struct tvec_state *tv@ = test vector state | |
723 | * @const char *dflt@ = defsault filename or null | |
724 | * | |
31d0247c | 725 | * Returns: Zero on success, %$-1$% on error. |
67b5031e MW |
726 | * |
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. | |
732 | */ | |
733 | ||
734 | extern int tvec_readdflt(struct tvec_state */*tv*/, const char */*file*/); | |
735 | ||
736 | /* --- @tvec_readargs@ --- * | |
737 | * | |
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 | |
743 | * | |
31d0247c | 744 | * Returns: Zero on success, %$-1$% on error. |
67b5031e MW |
745 | * |
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 | |
749 | * @tvec_readdflt@. | |
750 | */ | |
751 | ||
752 | extern int tvec_readargs(int /*argc*/, char */*argv*/[], | |
753 | struct tvec_state */*tv*/, | |
754 | int */*argpos_inout*/, const char */*dflt*/); | |
755 | ||
756 | /* --- @tvec_main@ --- * | |
757 | * | |
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 | |
762 | * | |
763 | * Returns: Exit code. | |
764 | * | |
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. | |
769 | */ | |
770 | ||
771 | extern int tvec_main(int /*argc*/, char */*argv*/[], | |
772 | const struct tvec_config */*config*/, | |
773 | const char */*dflt*/); | |
774 | ||
775 | /*----- Test processing ---------------------------------------------------*/ | |
776 | ||
777 | /* --- @tvec_skipgroup@, @tvec_skipgroup_v@ --- * | |
778 | * | |
779 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
780 | * @const char *excuse@, @va_list *ap@ = reason why skipped | |
781 | * | |
782 | * Returns: --- | |
783 | * | |
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. | |
787 | */ | |
788 | ||
31d0247c MW |
789 | extern PRINTF_LIKE(2, 3) |
790 | void tvec_skipgroup(struct tvec_state */*tv*/, | |
791 | const char */*excuse*/, ...); | |
67b5031e MW |
792 | extern void tvec_skipgroup_v(struct tvec_state */*tv*/, |
793 | const char */*excuse*/, va_list */*ap*/); | |
794 | ||
795 | /* --- @tvec_skip@, @tvec_skip_v@ --- * | |
796 | * | |
797 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
798 | * @const char *excuse@, @va_list *ap@ = reason why test skipped | |
799 | * | |
800 | * Returns: --- | |
801 | * | |
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. | |
805 | */ | |
806 | ||
31d0247c MW |
807 | extern PRINTF_LIKE(2, 3) |
808 | void tvec_skip(struct tvec_state */*tv*/, const char */*excuse*/, ...); | |
67b5031e MW |
809 | extern void tvec_skip_v(struct tvec_state */*tv*/, |
810 | const char */*excuse*/, va_list */*ap*/); | |
811 | ||
e63124bc MW |
812 | /* --- @tvec_fail@, @tvec_fail_v@ --- * |
813 | * | |
814 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
3efcfd2d | 815 | * @const char *detail@, @va_list *ap@ = description of test |
e63124bc MW |
816 | * |
817 | * Returns: --- | |
818 | * | |
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. | |
827 | */ | |
828 | ||
31d0247c MW |
829 | extern PRINTF_LIKE(2, 3) |
830 | void tvec_fail(struct tvec_state */*tv*/, const char */*detail*/, ...); | |
e63124bc MW |
831 | extern void tvec_fail_v(struct tvec_state */*tv*/, |
832 | const char */*detail*/, va_list */*ap*/); | |
833 | ||
834 | /* --- @tvec_dumpreg@ --- * | |
835 | * | |
836 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
837 | * @unsigned disp@ = the register disposition (@TVRD_...@) | |
c91413e6 | 838 | * @const union tvec_regval *tv@ = register value, or null |
e63124bc MW |
839 | * @const struct tvec_regdef *rd@ = register definition |
840 | * | |
841 | * Returns: --- | |
842 | * | |
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. | |
846 | * | |
847 | * Usually @tvec_mismatch@ is much more convenient. Low-level | |
848 | * access is required for reporting `virtual' registers | |
849 | * corresponding to test environment settings. | |
850 | */ | |
851 | ||
852 | extern void tvec_dumpreg(struct tvec_state */*tv*/, | |
853 | unsigned /*disp*/, const union tvec_regval */*rv*/, | |
854 | const struct tvec_regdef */*rd*/); | |
855 | ||
31d0247c MW |
856 | /* --- @tvec_initregs@, @tvec_releaseregs@ --- * |
857 | * | |
858 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
859 | * | |
860 | * Returns: --- | |
861 | * | |
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 | |
865 | * live. | |
866 | */ | |
867 | ||
868 | extern void tvec_initregs(struct tvec_state */*tv*/); | |
869 | extern void tvec_releaseregs(struct tvec_state */*tv*/); | |
870 | ||
871 | /* --- @tvec_resetoutputs@ --- * | |
872 | * | |
873 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
874 | * | |
875 | * Returns: --- | |
876 | * | |
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. | |
882 | */ | |
883 | ||
884 | extern void tvec_resetoutputs(struct tvec_state */*tv*/); | |
885 | ||
886 | /* --- @tvec_checkregs@ --- * | |
887 | * | |
888 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
889 | * | |
890 | * Returns: Zero on success, %$-1$% on mismatch. | |
891 | * | |
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. | |
897 | * | |
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. | |
901 | */ | |
902 | ||
903 | extern int tvec_checkregs(struct tvec_state */*tv*/); | |
904 | ||
e63124bc MW |
905 | /* --- @tvec_mismatch@ --- * |
906 | * | |
907 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
908 | * @unsigned f@ = flags (@TVMF_...@) | |
909 | * | |
910 | * Returns: --- | |
911 | * | |
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. | |
917 | */ | |
918 | ||
919 | #define TVMF_IN 1u | |
920 | #define TVMF_OUT 2u | |
921 | extern void tvec_mismatch(struct tvec_state */*tv*/, unsigned /*f*/); | |
922 | ||
923 | /* --- @tvec_check@, @tvec_check_v@ --- * | |
924 | * | |
925 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
3efcfd2d | 926 | * @const char *detail@, @va_list *ap@ = description of test |
e63124bc MW |
927 | * |
928 | * Returns: --- | |
929 | * | |
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 | |
933 | * obvious way. | |
934 | */ | |
935 | ||
31d0247c MW |
936 | extern PRINTF_LIKE(2, 3) |
937 | void tvec_check(struct tvec_state */*tv*/, const char */*detail*/, ...); | |
e63124bc MW |
938 | extern void tvec_check_v(struct tvec_state */*tv*/, |
939 | const char */*detail*/, va_list */*ap*/); | |
b64eb60f | 940 | |
67b5031e | 941 | /*----- Ad-hoc testing ----------------------------------------------------*/ |
b64eb60f | 942 | |
67b5031e | 943 | /* --- @tvec_adhoc@ --- * |
c5e0e403 | 944 | * |
67b5031e MW |
945 | * Arguments: @struct tvec_state *tv@ = test-vector state |
946 | * @struct tvec_test *t@ = space for a test definition | |
c5e0e403 MW |
947 | * |
948 | * Returns: --- | |
949 | * | |
67b5031e MW |
950 | * Use: Begin ad-hoc testing, i.e., without reading a file of |
951 | * test-vector data. | |
952 | * | |
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. | |
c5e0e403 | 957 | */ |
b64eb60f | 958 | |
67b5031e | 959 | extern void tvec_adhoc(struct tvec_state */*tv*/, struct tvec_test */*t*/); |
b64eb60f | 960 | |
67b5031e | 961 | /* --- @tvec_begingroup@, @TVEC_BEGINGROUP@ --- * |
c5e0e403 MW |
962 | * |
963 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
67b5031e MW |
964 | * @const char *name@ = name for this test group |
965 | * @const char *file@, @unsigned @lno@ = calling file and line | |
c5e0e403 | 966 | * |
67b5031e | 967 | * Returns: --- |
c5e0e403 | 968 | * |
67b5031e MW |
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. | |
c5e0e403 | 973 | */ |
b64eb60f | 974 | |
67b5031e MW |
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) | |
b64eb60f | 979 | |
67b5031e | 980 | /* --- @tvec_endgroup@ --- * |
c5e0e403 MW |
981 | * |
982 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
c5e0e403 | 983 | * |
67b5031e | 984 | * Returns: --- |
c5e0e403 | 985 | * |
67b5031e MW |
986 | * Use: End an ad-hoc test group. The statistics are updated and the |
987 | * outcome is reported to the output formatter. | |
3efcfd2d MW |
988 | */ |
989 | ||
990 | extern void tvec_endgroup(struct tvec_state */*tv*/); | |
991 | ||
992 | /* --- @TVEC_TESTGROUP@, @TVEC_TESTGROUP_TAG@ --- * | |
993 | * | |
994 | * Arguments: @tag@ = label-disambiguation tag | |
995 | * @const struct tvec_state *tv = test-vector state | |
996 | * @const char *name@ = test-group name | |
997 | * | |
998 | * Returns: --- | |
999 | * | |
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. | |
1006 | */ | |
1007 | ||
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) | |
1015 | ||
1016 | /* --- @tvec_begintest@, @TVEC_BEGINTEST@ --- * | |
1017 | * | |
1018 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1019 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1020 | * | |
1021 | * Returns: --- | |
1022 | * | |
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. | |
1027 | */ | |
1028 | ||
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) | |
1033 | ||
31d0247c | 1034 | /* --- @tvec_endtest@ --- * |
3efcfd2d MW |
1035 | * |
1036 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1037 | * | |
1038 | * Returns: --- | |
1039 | * | |
31d0247c | 1040 | * Use: End an ad-hoc test case, The statistics are updated and the |
3efcfd2d MW |
1041 | * outcome is reported to the output formatter. |
1042 | */ | |
1043 | ||
1044 | extern void tvec_endtest(struct tvec_state */*tv*/); | |
1045 | ||
1046 | /* --- @TVEC_TEST@, @TVEC_TEST_TAG@ --- * | |
1047 | * | |
1048 | * Arguments: @tag@ = label-disambiguation tag | |
1049 | * @struct tvec_test *t@ = space for a test definition | |
1050 | * | |
1051 | * Returns: --- | |
1052 | * | |
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. | |
1058 | */ | |
1059 | ||
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) | |
1067 | ||
1068 | /* --- @tvec_claim@, @tvec_claim_v@, @TVEC_CLAIM@ --- * | |
1069 | * | |
1070 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1071 | * @int ok@ = a flag | |
1072 | * @const char *file@, @unsigned @lno@ = calling file and line | |
1073 | * @const char *msg@, @va_list *ap@ = message to report on | |
1074 | * failure | |
1075 | * | |
1076 | * Returns: The value @ok@. | |
1077 | * | |
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@. | |
1086 | * | |
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. | |
1091 | */ | |
1092 | ||
31d0247c MW |
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*/, ...); | |
3efcfd2d MW |
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)) | |
1102 | ||
1103 | /* --- @tvec_claimeq@ --- * | |
1104 | * | |
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 | |
1110 | * | |
1111 | * Returns: Nonzero if the input and output values of register 0 are | |
1112 | * equal, zero if they differ. | |
1113 | * | |
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. | |
1120 | * | |
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@. | |
1124 | */ | |
1125 | ||
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*/); | |
1131 | ||
67b5031e | 1132 | /*----- Benchmarking ------------------------------------------------------*/ |
b64eb60f | 1133 | |
c91413e6 | 1134 | struct tvec_benchenv { |
67b5031e MW |
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 */ | |
b64eb60f MW |
1140 | }; |
1141 | ||
67b5031e | 1142 | struct tvec_benchctx { |
c91413e6 MW |
1143 | const struct tvec_benchenv *be; /* environment configuration */ |
1144 | struct bench_state *bst; /* benchmark state */ | |
1145 | double dflt_target; /* default time in seconds */ | |
31d0247c MW |
1146 | unsigned f; /* flags */ |
1147 | #define TVBF_SETTRG 1u /* set `@target' */ | |
1148 | #define TVBF_SETMASK (TVBF_SETTRG)) /* mask of @TVBF_SET...@ */ | |
c91413e6 | 1149 | void *subctx; /* subsidiary environment context */ |
3efcfd2d | 1150 | }; |
c5e0e403 | 1151 | |
67b5031e | 1152 | extern struct bench_state *tvec_benchstate; |
3efcfd2d | 1153 | |
c91413e6 | 1154 | /* --- Environment implementation --- * |
67b5031e | 1155 | * |
c91413e6 | 1156 | * The following special variables are supported. |
67b5031e | 1157 | * |
c91413e6 MW |
1158 | * * %|@target|% is the (approximate) number of seconds to run the |
1159 | * benchmark. | |
67b5031e | 1160 | * |
c91413e6 MW |
1161 | * Unrecognized variables are passed to the subordinate environment, if there |
1162 | * is one. Other events are passed through to the subsidiary environment. | |
67b5031e | 1163 | */ |
b64eb60f | 1164 | |
c91413e6 | 1165 | extern tvec_envsetupfn tvec_benchsetup; |
814e42ff | 1166 | extern tvec_envfindvarfn tvec_benchfindvar; |
c91413e6 MW |
1167 | extern tvec_envbeforefn tvec_benchbefore; |
1168 | extern tvec_envrunfn tvec_benchrun; | |
1169 | extern tvec_envafterfn tvec_benchafter; | |
1170 | extern tvec_envteardownfn tvec_benchteardown; | |
b64eb60f | 1171 | |
c91413e6 MW |
1172 | #define TVEC_BENCHENV \ |
1173 | { sizeof(struct tvec_benchctx), \ | |
1174 | tvec_benchsetup, \ | |
814e42ff | 1175 | tvec_benchfindvar, \ |
c91413e6 MW |
1176 | tvec_benchbefore, \ |
1177 | tvec_benchrun, \ | |
1178 | tvec_benchafter, \ | |
1179 | tvec_benchteardown } | |
1180 | #define TVEC_BENCHINIT TVEC_BENCHENV, &tvec_benchstate | |
3efcfd2d | 1181 | |
67b5031e MW |
1182 | /* --- @tvec_benchreport@ --- * |
1183 | * | |
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 | |
1188 | * | |
1189 | * Returns: --- | |
1190 | * | |
1191 | * Use: Formats a report about the benchmark performance. This | |
1192 | * function is intended to be called on by an output | |
1193 | * @ebench@ function. | |
1194 | */ | |
3efcfd2d | 1195 | |
67b5031e MW |
1196 | extern void tvec_benchreport |
1197 | (const struct gprintf_ops */*gops*/, void */*go*/, | |
1198 | unsigned /*unit*/, const struct bench_timing */*tm*/); | |
b64eb60f | 1199 | |
c91413e6 MW |
1200 | /*----- Remote execution --------------------------------------------------*/ |
1201 | ||
1202 | struct tvec_remotecomms { | |
1203 | int infd, outfd; /* input and output descriptors */ | |
31d0247c MW |
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 */ | |
c91413e6 MW |
1208 | unsigned f; /* flags */ |
1209 | #define TVRF_BROKEN 0x0001u /* communications have failed */ | |
c91413e6 | 1210 | }; |
31d0247c | 1211 | #define TVEC_REMOTECOMMS_INIT { -1, -1, DBUF_INIT, 0, 0, 0, 0, 0, 0 } |
c91413e6 MW |
1212 | |
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 */ | |
814e42ff MW |
1217 | void *subctx; /* subenvironment context */ |
1218 | struct tvec_vardef vd; /* temporary variable definition */ | |
c91413e6 MW |
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: */ | |
814e42ff MW |
1226 | #define TVRCN_DEMAND 0x0000u /* connect on demand */ |
1227 | #define TVRCN_SKIP 0x0100u /* skip unless connected */ | |
c91413e6 MW |
1228 | #define TVRCN_FORCE 0x0200u /* force reconnection */ |
1229 | #define TVRF_MUFFLE 0x0400u /* muffle child stderr */ | |
31d0247c MW |
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...@ */ | |
c91413e6 MW |
1235 | }; |
1236 | ||
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*/); | |
c81c35df MW |
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. | |
1245 | * | |
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$%. | |
1253 | */ | |
c91413e6 MW |
1254 | |
1255 | struct tvec_remoteenv_slots { | |
1256 | tvec_connectfn *connect; /* connection function */ | |
1257 | const struct tvec_env *env; /* subsidiary environment */ | |
814e42ff | 1258 | unsigned dflt_reconn; /* default reconnection */ |
c91413e6 MW |
1259 | }; |
1260 | ||
1261 | struct tvec_remoteenv { | |
1262 | struct tvec_env _env; | |
1263 | struct tvec_remoteenv_slots r; | |
1264 | }; | |
1265 | ||
1266 | struct tvec_remotefork_slots { | |
1267 | const struct tvec_test *tests; /* child tests (or null) */ | |
1268 | }; | |
1269 | ||
1270 | struct tvec_remotefork { | |
1271 | struct tvec_env _env; | |
1272 | struct tvec_remoteenv_slots r; | |
1273 | struct tvec_remotefork_slots f; | |
1274 | }; | |
1275 | ||
1276 | struct tvec_remoteexec_slots { | |
1277 | const char *const *args; /* command line to execute */ | |
1278 | }; | |
1279 | ||
1280 | struct tvec_remoteexec { | |
1281 | struct tvec_env _env; | |
1282 | struct tvec_remoteenv_slots r; | |
1283 | struct tvec_remoteexec_slots x; | |
1284 | }; | |
1285 | ||
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; | |
1291 | }; | |
1292 | ||
c81c35df MW |
1293 | /* Exit status. |
1294 | * | |
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. | |
1298 | * | |
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. | |
1303 | */ | |
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 */ | |
1315 | ||
1316 | /* Remote environment. */ | |
c91413e6 | 1317 | extern tvec_envsetupfn tvec_remotesetup; |
814e42ff MW |
1318 | extern tvec_envfindvarfn tvec_remotefindvar; |
1319 | extern tvec_envbeforefn tvec_remotebefore; | |
c91413e6 MW |
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), \ | |
1325 | tvec_remotesetup, \ | |
814e42ff MW |
1326 | tvec_remotefindvar, \ |
1327 | tvec_remotebefore, \ | |
c91413e6 MW |
1328 | tvec_remoterun, \ |
1329 | tvec_remoteafter, \ | |
1330 | tvec_remoteteardown } | |
1331 | ||
c81c35df MW |
1332 | /* --- @tvec_setprogress@, @tvec_setprogress_v@ --- * |
1333 | * | |
1334 | * Arguments: @const char *status@ = progress status token format | |
1335 | * @va_list ap@ = argument tail | |
1336 | * | |
1337 | * Returns: --- | |
1338 | * | |
1339 | * Use: Reports the progress of a test execution to the client. | |
1340 | * | |
1341 | * The framework makes use of tokens beginning with %|%|%: | |
1342 | * | |
1343 | * * %|%IDLE|%: during the top-level server code; | |
1344 | * | |
1345 | * * %|%SETUP|%: during the enclosing environment's @before@ | |
1346 | * function; | |
1347 | * | |
1348 | * * %|%RUN|%: during the environment's @run@ function, or the | |
1349 | * test function; and | |
1350 | * | |
1351 | * * %|%DONE|%: during the enclosing environment's @after@ | |
1352 | * function. | |
1353 | * | |
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. | |
1359 | */ | |
1360 | ||
31d0247c MW |
1361 | extern PRINTF_LIKE(1, 2) int tvec_setprogress(const char */*status*/, ...); |
1362 | extern int tvec_setprogress_v(const char */*status*/, va_list */*ap*/); | |
c91413e6 | 1363 | |
c81c35df MW |
1364 | /* --- @tvec_remoteserver@ --- * |
1365 | * | |
1366 | * Arguments: @int infd@, @int outfd@ = input and output file descriptors | |
1367 | * @const struct tvec_config *config@ = test configuration | |
1368 | * | |
1369 | * Returns: Suggested exit code. | |
1370 | * | |
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@. | |
1374 | * | |
1375 | * This function is not particularly general purpose. It | |
1376 | * expects to `take over' the process, and makes use of private | |
1377 | * global variables. | |
1378 | */ | |
1379 | ||
c91413e6 MW |
1380 | extern int tvec_remoteserver(int /*infd*/, int /*outfd*/, |
1381 | const struct tvec_config */*config*/); | |
1382 | ||
1383 | extern tvec_connectfn tvec_fork, tvec_exec; | |
1384 | ||
31d0247c | 1385 | #define TVEC_REMOTEFORK(subenv, tests) \ |
c91413e6 MW |
1386 | TVEC_REMOTEENV, { tvec_fork, subenv }, { tests } |
1387 | ||
1388 | #define TVEC_REMOTEEXEC(subenv, args) \ | |
1389 | TVEC_REMOTEENV, { tvec_exec, subenv }, { args } | |
1390 | ||
31d0247c MW |
1391 | /*----- Timeouts ----------------------------------------------------------*/ |
1392 | ||
1393 | struct tvec_timeoutenv { | |
1394 | struct tvec_env _env; | |
814e42ff | 1395 | int timer; /* the timer (@ITIMER_...@) */ |
c81c35df MW |
1396 | double t; /* time to wait (in seconds) */ |
1397 | const struct tvec_env *env; /* subsidiary environment */ | |
31d0247c MW |
1398 | }; |
1399 | ||
1400 | struct tvec_timeoutctx { | |
c81c35df | 1401 | const struct tvec_timeoutenv *te; /* saved environment description */ |
814e42ff | 1402 | int timer; /* timer code (as overridden) */ |
c81c35df | 1403 | double t; /* time to wait (as overridden) */ |
31d0247c MW |
1404 | unsigned f; /* flags */ |
1405 | #define TVTF_SETTMO 1u /* set `@timeout' */ | |
814e42ff MW |
1406 | #define TVTF_SETTMR 2u /* set `@timer' */ |
1407 | #define TVTF_SETMASK (TVTF_SETTMO | TVTF_SETTMR) | |
1408 | /* mask of @TVTF_SET...@ */ | |
31d0247c MW |
1409 | void *subctx; |
1410 | }; | |
1411 | ||
1412 | extern tvec_envsetupfn tvec_timeoutsetup; | |
814e42ff | 1413 | extern tvec_envfindvarfn tvec_timeoutfindvar; |
31d0247c MW |
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, \ | |
814e42ff | 1421 | tvec_timeoutfindvar, \ |
31d0247c MW |
1422 | tvec_timeoutbefore, \ |
1423 | tvec_timeoutrun, \ | |
1424 | tvec_timeoutafter, \ | |
1425 | tvec_timeoutteardown } | |
1426 | #define TVEC_TIMEOUTINIT(timer, t) TVEC_TIMEOUTENV, timer, t | |
1427 | ||
67b5031e | 1428 | /*----- Output functions --------------------------------------------------*/ |
b64eb60f | 1429 | |
31d0247c MW |
1430 | /* --- @tvec_strlevel@ --- * |
1431 | * | |
1432 | * Arguments: @unsigned level@ = level code | |
1433 | * | |
1434 | * Returns: A human-readable description. | |
1435 | * | |
1436 | * Use: Converts a level code into something that you can print in a | |
1437 | * message. | |
1438 | */ | |
1439 | ||
1440 | extern const char *tvec_strlevel(unsigned /*level*/); | |
1441 | ||
c91413e6 | 1442 | /* --- @tvec_report@, @tvec_report_v@ --- * |
3efcfd2d MW |
1443 | * |
1444 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1445 | * @const char *msg@, @va_list ap@ = error message | |
1446 | * | |
c91413e6 | 1447 | * Returns: --- |
3efcfd2d | 1448 | * |
c91413e6 MW |
1449 | * Use: Report an message with a given severity. Messages with level |
1450 | * @TVLEV_ERR@ or higher force a nonzero exit code. | |
3efcfd2d MW |
1451 | */ |
1452 | ||
31d0247c MW |
1453 | extern PRINTF_LIKE(3, 4) |
1454 | void tvec_report(struct tvec_state */*tv*/, unsigned /*level*/, | |
1455 | const char */*msg*/, ...); | |
c91413e6 MW |
1456 | extern void tvec_report_v(struct tvec_state */*tv*/, unsigned /*level*/, |
1457 | const char */*msg*/, va_list */*ap*/); | |
b64eb60f | 1458 | |
c91413e6 | 1459 | /* --- @tvec_error@, @tvec_notice@ --- * |
3efcfd2d MW |
1460 | * |
1461 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
c91413e6 | 1462 | * @const char *msg@, @va_list ap@ = error message |
3efcfd2d | 1463 | * |
31d0247c | 1464 | * Returns: The @tvec_error@ function returns %$-1$% as a trivial |
c91413e6 | 1465 | * convenience; @tvec_notice@ does not return a value. |
3efcfd2d | 1466 | * |
c91413e6 MW |
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 | |
1471 | * category. | |
3efcfd2d MW |
1472 | */ |
1473 | ||
31d0247c MW |
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*/, ...); | |
b64eb60f | 1478 | |
3efcfd2d MW |
1479 | /* --- @tvec_humanoutput@ --- * |
1480 | * | |
1481 | * Arguments: @FILE *fp@ = output file to write on | |
1482 | * | |
1483 | * Returns: An output formatter. | |
1484 | * | |
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 | |
1490 | * highlighting. | |
1491 | */ | |
1492 | ||
e63124bc | 1493 | extern struct tvec_output *tvec_humanoutput(FILE */*fp*/); |
3efcfd2d MW |
1494 | |
1495 | /* --- @tvec_tapoutput@ --- * | |
1496 | * | |
1497 | * Arguments: @FILE *fp@ = output file to write on | |
1498 | * | |
1499 | * Returns: An output formatter. | |
1500 | * | |
1501 | * Use: Return an output formatter which writes on @fp@ in `TAP' | |
1502 | * (`Test Anything Protocol') format. | |
1503 | * | |
1504 | * TAP comes from the Perl community, but has spread rather | |
ce896b0b MW |
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. | |
3efcfd2d MW |
1515 | */ |
1516 | ||
e63124bc | 1517 | extern struct tvec_output *tvec_tapoutput(FILE */*fp*/); |
3efcfd2d MW |
1518 | |
1519 | /* --- @tvec_dfltoutput@ --- * | |
1520 | * | |
1521 | * Arguments: @FILE *fp@ = output file to write on | |
1522 | * | |
1523 | * Returns: An output formatter. | |
1524 | * | |
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. | |
1530 | */ | |
1531 | ||
e63124bc | 1532 | extern struct tvec_output *tvec_dfltout(FILE */*fp*/); |
b64eb60f | 1533 | |
67b5031e | 1534 | /*------ Serialization utilities ------------------------------------------*/ |
b64eb60f | 1535 | |
c91413e6 MW |
1536 | /* Serialization format. |
1537 | * | |
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. | |
1551 | */ | |
1552 | ||
67b5031e MW |
1553 | /* --- @tvec_serialize@ --- * |
1554 | * | |
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 | |
1559 | * @name@ slot | |
1560 | * @unsigned nr@ = number of entries in the @rv@ vector | |
1561 | * @size_t regsz@ = true size of each element of @rv@ | |
1562 | * | |
31d0247c | 1563 | * Returns: Zero on success, %$-1$% on failure. |
67b5031e MW |
1564 | * |
1565 | * Use: Serialize a collection of register values. | |
1566 | * | |
67b5031e MW |
1567 | * The serialized output is written to the buffer @b@. Failure |
1568 | * can be caused by running out of buffer space, or a failing | |
1569 | * type handler. | |
1570 | */ | |
3efcfd2d | 1571 | |
67b5031e MW |
1572 | extern int tvec_serialize(const struct tvec_reg */*rv*/, buf */*b*/, |
1573 | const struct tvec_regdef */*regs*/, | |
1574 | unsigned /*nr*/, size_t /*regsz*/); | |
3efcfd2d | 1575 | |
67b5031e MW |
1576 | /* --- @tvec_deserialize@ --- * |
1577 | * | |
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 | |
1582 | * @name@ slot | |
1583 | * @unsigned nr@ = number of entries in the @rv@ vector | |
1584 | * @size_t regsz@ = true size of each element of @rv@ | |
1585 | * | |
31d0247c | 1586 | * Returns: Zero on success, %$-1$% on failure. |
67b5031e MW |
1587 | * |
1588 | * Use: Deserialize a collection of register values. | |
1589 | * | |
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. | |
1596 | * | |
c91413e6 | 1597 | * Failure results only from a failing register type handler. |
67b5031e | 1598 | */ |
3efcfd2d | 1599 | |
67b5031e MW |
1600 | extern int tvec_deserialize(struct tvec_reg */*rv*/, buf */*b*/, |
1601 | const struct tvec_regdef */*regs*/, | |
1602 | unsigned /*nr*/, size_t /*regsz*/); | |
3efcfd2d | 1603 | |
67b5031e MW |
1604 | /*----- Input utilities ---------------------------------------------------*/ |
1605 | ||
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 | |
1609 | * former. | |
1610 | * | |
1611 | * The two main rules are as follows. | |
1612 | * | |
1613 | * * Leave the file position at the beginning of the line following | |
1614 | * whatever it was that you read. | |
1615 | * | |
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. | |
1619 | */ | |
1620 | ||
67b5031e MW |
1621 | /* --- @tvec_syntax@, @tvec_syntax_v@ --- * |
1622 | * | |
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 | |
1626 | * | |
31d0247c | 1627 | * Returns: %$-1$%. |
67b5031e MW |
1628 | * |
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). | |
1633 | */ | |
1634 | ||
31d0247c MW |
1635 | extern PRINTF_LIKE(3, 4) |
1636 | int tvec_syntax(struct tvec_state */*tv*/, int /*ch*/, | |
1637 | const char */*expect*/, ...); | |
67b5031e MW |
1638 | extern int tvec_syntax_v(struct tvec_state */*tv*/, int /*ch*/, |
1639 | const char */*expect*/, va_list */*ap*/); | |
1640 | ||
814e42ff MW |
1641 | /* --- @tvec_unkreg@ --- * |
1642 | * | |
1643 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1644 | * @const char *name@ = register or pseudoregister name | |
1645 | * | |
1646 | * Returns: %$-1$%. | |
1647 | * | |
1648 | * Use: Reports an error that the register or pseudoregister is | |
1649 | * unrecognized. | |
1650 | */ | |
1651 | ||
1652 | extern int tvec_unkreg(struct tvec_state */*tv*/, const char */*name*/); | |
1653 | ||
31d0247c MW |
1654 | /* --- @tvec_dupreg@ --- * |
1655 | * | |
1656 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1657 | * @const char *name@ = register or pseudoregister name | |
1658 | * | |
1659 | * Returns: %$-1$%. | |
1660 | * | |
1661 | * Use: Reports an error that the register or pseudoregister has been | |
1662 | * assigned already in the current test. | |
1663 | */ | |
1664 | ||
1665 | extern int tvec_dupreg(struct tvec_state */*tv*/, const char */*name*/); | |
1666 | ||
1667 | /* --- @tvec_skipspc@ --- * | |
1668 | * | |
1669 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1670 | * | |
1671 | * Returns: --- | |
1672 | * | |
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. | |
1676 | */ | |
1677 | ||
1678 | extern void tvec_skipspc(struct tvec_state */*tv*/); | |
1679 | ||
67b5031e MW |
1680 | /* --- @tvec_flushtoeol@ --- * |
1681 | * | |
1682 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1683 | * @unsigned f@ = flags (@TVFF_...@) | |
1684 | * | |
31d0247c | 1685 | * Returns: Zero on success, %$-1$% on error. |
67b5031e MW |
1686 | * |
1687 | * Use: Advance to the start of the next line, consuming the | |
1688 | * preceding newline. | |
1689 | * | |
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 | |
1695 | * appropriately. | |
1696 | */ | |
1697 | ||
1698 | #define TVFF_ALLOWANY 1u | |
1699 | extern int tvec_flushtoeol(struct tvec_state */*tv*/, unsigned /*f*/); | |
1700 | ||
1701 | /* --- @tvec_nexttoken@ --- * | |
1702 | * | |
1703 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
1704 | * | |
31d0247c | 1705 | * Returns: Zero if there is a next token which can be read; %$-1$% if no |
67b5031e MW |
1706 | * token is available. |
1707 | * | |
1708 | * Use: Advance to the next whitespace-separated token, which may be | |
1709 | * on the next line. | |
1710 | * | |
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 | |
1715 | * a test. | |
1716 | * | |
1717 | * If this function returns zero, then the next character in the | |
1718 | * file begins a suitable token which can be read and | |
31d0247c | 1719 | * processed. If it returns %$-1$% then there is no such token, |
67b5031e MW |
1720 | * and the file position is left correctly. The line number |
1721 | * count is updated appropriately. | |
1722 | */ | |
3efcfd2d | 1723 | |
67b5031e | 1724 | extern int tvec_nexttoken(struct tvec_state */*tv*/); |
3efcfd2d | 1725 | |
31d0247c | 1726 | /* --- @tvec_readword@, @tvec_readword_v@ --- * |
67b5031e MW |
1727 | * |
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 | |
1732 | * | |
31d0247c | 1733 | * Returns: Zero on success, %$-1$% on failure. |
67b5031e MW |
1734 | * |
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.) | |
1740 | * | |
1741 | * If there is no word beginning at the current file position, | |
31d0247c MW |
1742 | * then return %$-1$%; furthermore, if @expect@ is not null, |
1743 | * then report an appropriate error via @tvec_syntax@. | |
67b5031e MW |
1744 | * |
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- | |
1751 | * terminated. | |
1752 | */ | |
3efcfd2d | 1753 | |
31d0247c MW |
1754 | extern PRINTF_LIKE(4, 5) |
1755 | int tvec_readword(struct tvec_state */*tv*/, dstr */*d*/, | |
1756 | const char */*delims*/, const char */*expect*/, ...); | |
67b5031e MW |
1757 | extern int tvec_readword_v(struct tvec_state */*tv*/, dstr */*d*/, |
1758 | const char */*delims*/, const char */*expect*/, | |
1759 | va_list */*ap*/); | |
b64eb60f | 1760 | |
67b5031e | 1761 | /*----- Integer types: signed and unsigned --------------------------------*/ |
3efcfd2d MW |
1762 | |
1763 | /* Integers may be input in decimal, hex, binary, or octal, following | |
1764 | * approximately usual conventions. | |
1765 | * | |
1766 | * * Signed integers may be preceded with a `+' or `-' sign. | |
1767 | * | |
1768 | * * Decimal integers are just a sequence of decimal digits `0' ... `9'. | |
1769 | * | |
1770 | * * Octal integers are a sequence of digits `0' ... `7', preceded by `0o' | |
1771 | * or `0O'. | |
1772 | * | |
1773 | * * Hexadecimal integers are a sequence of digits `0' ... `9', `a' | |
1774 | * ... `f', or `A' ... `F', preceded by `0x' or `0X'. | |
1775 | * | |
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 `_'. | |
1780 | * | |
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. | |
1784 | */ | |
1785 | ||
b64eb60f | 1786 | extern const struct tvec_regty tvty_int, tvty_uint; |
3efcfd2d MW |
1787 | |
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. | |
1791 | */ | |
b64eb60f MW |
1792 | struct tvec_irange { long min, max; }; |
1793 | struct tvec_urange { unsigned long min, max; }; | |
1794 | ||
3efcfd2d | 1795 | /* Bounds corresponding to common integer types. */ |
b64eb60f MW |
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; | |
3efcfd2d | 1802 | |
67b5031e | 1803 | /* --- @tvec_claimeq_int@, @TVEC_CLAIMEQ_INT@ --- * |
3efcfd2d MW |
1804 | * |
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 | |
1809 | * | |
1810 | * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero. | |
1811 | * | |
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. | |
1818 | * | |
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. | |
1823 | */ | |
b64eb60f MW |
1824 | |
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*/); | |
3efcfd2d MW |
1829 | #define TVEC_CLAIMEQ_INT(tv, i0, i1) \ |
1830 | (tvec_claimeq_int(tv, i0, i1, __FILE__, __LINE__, #i0 " /= " #i1)) | |
1831 | ||
1832 | /* --- @tvec_claimeq_uint@, @TVEC_CLAIMEQ_UINT@ --- * | |
1833 | * | |
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 | |
1838 | * | |
1839 | * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero. | |
1840 | * | |
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. | |
1847 | * | |
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. | |
1852 | */ | |
1853 | ||
b64eb60f MW |
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*/); | |
b64eb60f MW |
1858 | #define TVEC_CLAIMEQ_UINT(tv, u0, u1) \ |
1859 | (tvec_claimeq_uint(tv, u0, u1, __FILE__, __LINE__, #u0 " /= " #u1)) | |
1860 | ||
3efcfd2d MW |
1861 | /*----- Floating-point type -----------------------------------------------*/ |
1862 | ||
c81c35df MW |
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) | |
1867 | * syntax. | |
3efcfd2d MW |
1868 | * |
1869 | * The comparison rules for floating-point numbers are complex: see | |
1870 | * @tvec_claimeqish_float@ for details. | |
1871 | */ | |
1872 | ||
e63124bc | 1873 | extern const struct tvec_regty tvty_float; |
3efcfd2d | 1874 | |
e63124bc | 1875 | struct tvec_floatinfo { |
3efcfd2d MW |
1876 | /* Details about acceptable floating-point values. */ |
1877 | ||
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 */ | |
e63124bc MW |
1888 | }; |
1889 | ||
3efcfd2d MW |
1890 | /* --- @tvec_claimeqish_float@, @TVEC_CLAIMEQISH_FLOAT@ --- * |
1891 | * | |
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 | |
1898 | * | |
1899 | * Returns: Nonzero if @f0@ and @u1@ are sufficiently close, otherwise | |
1900 | * zero. | |
1901 | * | |
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. | |
1908 | * | |
1909 | * The details for the comparison are as follows. | |
1910 | * | |
1911 | * * A NaN value matches any other NaN, and nothing else. | |
1912 | * | |
1913 | * * An infinity matches another infinity of the same sign, | |
1914 | * and nothing else. | |
1915 | * | |
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.) | |
1921 | * | |
1922 | * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches | |
1923 | * %$y$% when %$|x - y| < \delta$%. | |
1924 | * | |
1925 | * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches | |
1926 | * %$y$% when %$|1 - y/x| < \delta$%. (Note that this | |
db2bf411 | 1927 | * criterion is asymmetric FIXME |
3efcfd2d MW |
1928 | * |
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 | |
1933 | * message. | |
1934 | */ | |
1935 | ||
e63124bc MW |
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*/); | |
3efcfd2d MW |
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 ")")) | |
1944 | ||
1945 | /* --- @tvec_claimeq_float@, @TVEC_CLAIMEQ_FLOAT@ --- * | |
1946 | * | |
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 | |
1951 | * | |
1952 | * Returns: Nonzero if @f0@ and @u1@ are identical, otherwise zero. | |
1953 | * | |
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. | |
1959 | */ | |
1960 | ||
e63124bc MW |
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*/); | |
e63124bc MW |
1965 | #define TVEC_CLAIMEQ_FLOAT(tv, f0, f1) \ |
1966 | (tvec_claimeq_float(tv, f0, f1, __FILE__, __LINE__, #f0 " /= " #f1)) | |
1967 | ||
31d0247c MW |
1968 | extern const struct tvec_floatinfo tvflt_finite, tvflt_nonneg; |
1969 | ||
814e42ff MW |
1970 | /*----- Durations ---------------------------------------------------------*/ |
1971 | ||
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. | |
1975 | * | |
1976 | * No @tvec_claimeq_...@ function is provided for durations: use | |
1977 | * @tvec_claimeq_float@. | |
1978 | */ | |
1979 | ||
1980 | extern const struct tvec_regty tvty_duration; | |
1981 | ||
3efcfd2d MW |
1982 | /*----- Enumerated types --------------------------------------------------*/ |
1983 | ||
67b5031e MW |
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. | |
1988 | * | |
1989 | * There is a distinct enumerated type for each of the branches of | |
3efcfd2d MW |
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. | |
67b5031e MW |
1993 | * |
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 | |
c81c35df | 1997 | * pointers, the only permitted literal is %|#nil|%, which denotes a null |
67b5031e MW |
1998 | * pointer. On output, names are preferred (with the underlying value given |
1999 | * in a comment). | |
3efcfd2d | 2000 | */ |
b64eb60f | 2001 | |
3efcfd2d MW |
2002 | #define DEFENUMTY(tag, ty, slot) \ |
2003 | extern const struct tvec_regty tvty_##slot##enum; | |
2004 | TVEC_MISCSLOTS(DEFENUMTY) | |
2005 | #undef DEFENUMTY | |
2006 | ||
2007 | /* A @struct tvec_tassoc@ associates a string tag with a value. */ | |
b64eb60f MW |
2008 | #define DEFASSOC(tag_, ty, slot) \ |
2009 | struct tvec_##slot##assoc { const char *tag; ty slot; }; | |
2010 | TVEC_MISCSLOTS(DEFASSOC) | |
2011 | #undef DEFASSOC | |
2012 | ||
67b5031e MW |
2013 | #define TVEC_ENDENUM { 0, 0 } |
2014 | ||
3efcfd2d MW |
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 */ \ | |
2021 | }; | |
2022 | ||
2023 | #define EXTRA_INT_INFOSLOTS \ | |
2024 | const struct tvec_irange *ir; /* allowed range of raw values */ | |
2025 | ||
2026 | #define EXTRA_UINT_INFOSLOTS \ | |
2027 | const struct tvec_urange *ur; /* allowed range of raw values */ | |
2028 | ||
2029 | #define EXTRA_FLT_INFOSLOTS \ | |
2030 | const struct tvec_floatinfo *fi; /* range and matching policy */ | |
2031 | ||
2032 | #define EXTRA_PTR_INFOSLOTS /* (nothing) */ | |
b64eb60f | 2033 | |
3efcfd2d MW |
2034 | TVEC_MISCSLOTS(DEFINFO) |
2035 | ||
2036 | #undef EXTRA_INT_INFOSLOTS | |
2037 | #undef EXTRA_UINT_INFOSLOTS | |
2038 | #undef EXTRA_FLT_INFOSLOTS | |
2039 | #undef EXTRA_PTR_INFOSLOTS | |
2040 | ||
2041 | #undef DEFINFO | |
2042 | ||
2043 | /* Standard enumerations. */ | |
20ba6b0b MW |
2044 | extern const struct tvec_ienuminfo tvenum_bool; |
2045 | extern const struct tvec_ienuminfo tvenum_cmp; | |
e63124bc | 2046 | |
3efcfd2d MW |
2047 | /* --- @tvec_claimeq_tenum@, @TVEC_CLAIMEQ_TENUM@ --- * |
2048 | * | |
2049 | * Arguments: @struct tvec_state *tv@ = test-vector state | |
67b5031e | 2050 | * @const struct tvec_typeenuminfo *ei@ = enumeration type info |
3efcfd2d MW |
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 | |
2054 | * | |
2055 | * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero. | |
2056 | * | |
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 | |
67b5031e MW |
2061 | * mismatched values are dumped: @t0@ is printed as the output |
2062 | * value and @t1@ is printed as the input reference. | |
3efcfd2d MW |
2063 | * |
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 | |
67b5031e | 2067 | * the @t0@ and @t1@ arguments in the failure message. |
3efcfd2d MW |
2068 | */ |
2069 | ||
b64eb60f MW |
2070 | #define DECLCLAIM(tag, ty, slot) \ |
2071 | extern int tvec_claimeq_##slot##enum \ | |
2072 | (struct tvec_state */*tv*/, \ | |
e63124bc | 2073 | const struct tvec_##slot##enuminfo */*ei*/, \ |
3efcfd2d | 2074 | ty /*t0*/, ty /*t1*/, \ |
b64eb60f MW |
2075 | const char */*file*/, unsigned /*lno*/, const char */*expr*/); |
2076 | TVEC_MISCSLOTS(DECLCLAIM) | |
2077 | #undef DECLCLAIM | |
3efcfd2d MW |
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)) | |
2090 | ||
67b5031e MW |
2091 | /*----- Flags type --------------------------------------------------------*/ |
2092 | ||
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 | |
2096 | * covering bitmask. | |
2097 | * | |
c81c35df MW |
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 | |
2102 | * given values. | |
67b5031e MW |
2103 | * |
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 | |
c81c35df MW |
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. | |
67b5031e | 2111 | */ |
b64eb60f MW |
2112 | |
2113 | extern const struct tvec_regty tvty_flags; | |
3efcfd2d MW |
2114 | |
2115 | struct tvec_flag { | |
2116 | /* Definition of a single flag or bitfield value. | |
2117 | * | |
2118 | * Each named setting comes with a value @v@ and a mask @m@; the mask | |
67b5031e | 2119 | * should cover all of the value bits, i.e., @(v&~m) == 0@. |
3efcfd2d MW |
2120 | */ |
2121 | ||
2122 | const char *tag; /* name */ | |
2123 | unsigned long m, v; /* mask and value */ | |
2124 | }; | |
2125 | ||
67b5031e MW |
2126 | #define TVEC_ENDFLAGS { 0, 0, 0 } |
2127 | ||
b64eb60f | 2128 | struct tvec_flaginfo { |
67b5031e MW |
2129 | /* Information about a flags type. */ |
2130 | ||
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 */ | |
b64eb60f MW |
2134 | }; |
2135 | ||
67b5031e MW |
2136 | /* --- @tvec_claimeq_flags@, @TVEC_CLAIMEQ_FLAGS@ --- * |
2137 | * | |
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 | |
2143 | * | |
2144 | * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero. | |
2145 | * | |
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. | |
2152 | * | |
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. | |
2157 | */ | |
2158 | ||
b64eb60f MW |
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)) | |
2167 | ||
67b5031e MW |
2168 | /*----- Character type ----------------------------------------------------*/ |
2169 | ||
2170 | /* A character value holds a character, as read by @fgetc@. The special | |
2171 | * @EOF@ value can also be represented. | |
2172 | * | |
c81c35df MW |
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 | |
2175 | * quotes. | |
67b5031e MW |
2176 | * |
2177 | * The following escape sequences and character names are recognized. | |
2178 | * | |
2179 | * * `%|#eof|%' is the special end-of-file marker. | |
2180 | * | |
2181 | * * `%|#nul|%' is the NUL character, sometimes used to terminate strings. | |
2182 | * | |
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). | |
2186 | * | |
2187 | * * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace character, used to | |
2188 | * move the cursor backwords by one cell. | |
2189 | * | |
2190 | * * %|#escape|% %|#esc|%, or%|\e|% is the escape character, used to | |
2191 | * introduce special terminal commands. | |
2192 | * | |
2193 | * * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed character, used to | |
2194 | * separate pages of text. | |
2195 | * | |
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 | |
2199 | * the line). | |
2200 | * | |
2201 | * * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is the | |
2202 | * carriage-return character, used to return the cursor to the start of | |
2203 | * the line. | |
2204 | * | |
2205 | * * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the tab | |
2206 | * character, used to advance the cursor to the next tab stop on the | |
2207 | * current line. | |
2208 | * | |
2209 | * * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab character. | |
2210 | * | |
2211 | * * %|#space|%, %|#spc|% is the space character. | |
2212 | * | |
2213 | * * %|#delete|%, %|#del|% is the delete character, used to erase the most | |
2214 | * recent character. | |
2215 | * | |
2216 | * * %|\'|% is the single-quote character. | |
2217 | * | |
2218 | * * %|\\|% is the backslash character. | |
2219 | * | |
2220 | * * %|\"|% is the double-quote character. | |
2221 | * | |
2222 | * * %|\NNN|% or %|\{NNN}|% is the character with code NNN in octal. The | |
2223 | * NNN may be up to three digits long. | |
2224 | * | |
2225 | * * %|\xNN|% or %|\x{NN}|% is the character with code NNN in hexadecimal. | |
2226 | */ | |
2227 | ||
e63124bc | 2228 | extern const struct tvec_regty tvty_char; |
67b5031e MW |
2229 | |
2230 | /* --- @tvec_claimeq_char@, @TVEC_CLAIMEQ_CHAR@ --- * | |
2231 | * | |
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 | |
2236 | * | |
2237 | * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero. | |
2238 | * | |
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. | |
2245 | * | |
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. | |
2250 | */ | |
2251 | ||
e63124bc MW |
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)) | |
2258 | ||
67b5031e MW |
2259 | /*----- Text and binary string types --------------------------------------*/ |
2260 | ||
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 | |
2264 | * display. | |
2265 | * | |
2266 | * The input format for both kinds of strings is basically the same: a | |
2267 | * `compound string', consisting of | |
2268 | * | |
2269 | * * single-quoted strings, which are interpreted entirely literally, but | |
2270 | * can't contain single quotes or newlines; | |
2271 | * | |
2272 | * * double-quoted strings, in which `%|\|%'-escapes are interpreted as for | |
2273 | * characters; | |
2274 | * | |
2275 | * * character names, marked by an initial `%|#|%' sign; | |
2276 | * | |
2277 | * * special tokens marked by an initial `%|!|%' sign; or | |
2278 | * | |
2279 | * * barewords interpreted according to the current coding scheme. | |
2280 | * | |
2281 | * The special tokens are | |
2282 | * | |
2283 | * * `%|!bare|%', which causes subsequent sequences of barewords to be | |
2284 | * treated as plain text; | |
2285 | * | |
2286 | * * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause subsequent | |
2287 | * barewords to be decoded in the requested manner. | |
2288 | * | |
2289 | * * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%', which includes | |
2290 | * %$n$% copies of the (compound) string. | |
2291 | * | |
c81c35df MW |
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. | |
2294 | * | |
67b5031e MW |
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. | |
2299 | * | |
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. | |
2302 | */ | |
2303 | ||
c81c35df | 2304 | extern const struct tvec_regty tvty_text, tvty_bytes; |
b64eb60f | 2305 | |
c81c35df | 2306 | /* --- @tvec_claimeq_text@, @TVEC_CLAIMEQ_TEXT@ --- * |
67b5031e MW |
2307 | * |
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 | |
2313 | * | |
2314 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2315 | * zero. | |
2316 | * | |
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. | |
2323 | * | |
c81c35df | 2324 | * The @TVEC_CLAIM_TEXT@ macro is similar, only it (a) |
67b5031e MW |
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. | |
2328 | */ | |
2329 | ||
c81c35df MW |
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 "]")) | |
67b5031e | 2338 | |
c81c35df | 2339 | /* --- @tvec_claimeq_textz@, @TVEC_CLAIMEQ_TEXTZ@ --- * |
67b5031e MW |
2340 | * |
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 | |
2345 | * | |
2346 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2347 | * zero. | |
2348 | * | |
2349 | * Use: Check that strings at @p0@ and @p1@ are equal, as for | |
2350 | * @tvec_claimeq_string@, except that the strings are assumed | |
2351 | * null-terminated, so their lengths don't need to be supplied | |
c81c35df | 2352 | * explicitly. The macro is similarly like @TVEC_CLAIMEQ_TEXT@. |
67b5031e MW |
2353 | */ |
2354 | ||
c81c35df MW |
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)) | |
67b5031e MW |
2361 | |
2362 | /* --- @tvec_claimeq_bytes@, @TVEC_CLAIMEQ_BYTES@ --- * | |
2363 | * | |
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 | |
2369 | * | |
2370 | * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise | |
2371 | * zero. | |
2372 | * | |
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. | |
2379 | * | |
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. | |
2384 | */ | |
2385 | ||
b64eb60f MW |
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*/); | |
b64eb60f MW |
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 "]")) | |
2394 | ||
c81c35df | 2395 | /* --- @tvec_alloctext@, @tvec_allocbytes@ --- * |
67b5031e MW |
2396 | * |
2397 | * Arguments: @union tvec_regval *rv@ = register value | |
2398 | * @size_t sz@ = required size | |
2399 | * | |
2400 | * Returns: --- | |
2401 | * | |
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@. | |
2410 | * | |
c81c35df | 2411 | * The @tvec_alloctext@ function sneakily allocates an extra |
67b5031e MW |
2412 | * byte for a terminating zero. The @tvec_allocbytes@ function |
2413 | * doesn't do this. | |
2414 | */ | |
b64eb60f | 2415 | |
c81c35df | 2416 | extern void tvec_alloctext(union tvec_regval */*rv*/, size_t /*sz*/); |
b64eb60f MW |
2417 | extern void tvec_allocbytes(union tvec_regval */*rv*/, size_t /*sz*/); |
2418 | ||
67b5031e | 2419 | /*----- Buffer type -------------------------------------------------------*/ |
c5e0e403 | 2420 | |
67b5031e MW |
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. | |
2424 | * | |
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. | |
c81c35df MW |
2429 | * |
2430 | * No @claimeq@ functions or macros are provided for buffers because they | |
2431 | * don't seem very useful. | |
67b5031e | 2432 | */ |
c5e0e403 | 2433 | |
67b5031e | 2434 | extern const struct tvec_regty tvty_buffer; |
c5e0e403 | 2435 | |
b64eb60f MW |
2436 | /*----- That's all, folks -------------------------------------------------*/ |
2437 | ||
2438 | #ifdef __cplusplus | |
2439 | } | |
2440 | #endif | |
2441 | ||
2442 | #endif |