chiark / gitweb /
@@@ remote works?
[mLib] / test / t / tvec-test.c
CommitLineData
b64eb60f
MW
1/* -*-c-*-
2 *
3 * Test the test-vector 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/*----- Header files ------------------------------------------------------*/
29
30#include "tvec.h"
31
32/*----- Register definitions ----------------------------------------------*/
33
b64eb60f
MW
34static const struct tvec_iassoc ienum_assocs[] = {
35 { "less", -1 },
36 { "equal", 0 },
37 { "greater", +1 },
38 { 0 }
39};
40
41static const struct tvec_uassoc uenum_assocs[] = {
42 { "apple", 0 },
43 { "banana", 1 },
44 { "clementine", 2 },
45 { 0 }
46};
47
e63124bc
MW
48static const struct tvec_fassoc fenum_assocs[] = {
49 { "e", 2.718281828459045 },
50 { "pi", 3.141592653589793 },
51 { "tau", 6.283185307179586 },
52 { 0 }
53};
54
b64eb60f
MW
55static const struct tvec_passoc penum_assocs[] = {
56 { "alice", &uenum_assocs[0] },
57 { "bob", &uenum_assocs[1] },
58 { "carol", &uenum_assocs[2] },
59 { 0 }
60};
61
e63124bc 62#if __STDC_VERSION__x >= 199901
b64eb60f
MW
63# define DSGINIT(x) x
64#else
65# define DSGINIT(x)
66#endif
67
e63124bc
MW
68static const struct tvec_floatinfo fenum_fltinfo =
69 { TVFF_ABSDELTA, -10, +10, 1e-3 };
70
71#define DEFENUM(tag, ty, slot) \
72 static const struct tvec_##slot##enuminfo slot##enum_info = \
3efcfd2d 73 { slot##enum_NAME, slot##enum_assocs slot##enum_ARGS };
e63124bc
MW
74#define ienum_NAME "order"
75#define ienum_ARGS , &tvrange_i16
76#define uenum_NAME "fruit"
77#define uenum_ARGS , &tvrange_u16
78#define fenum_NAME "const"
79#define fenum_ARGS , &fenum_fltinfo
80#define penum_NAME "actor"
81#define penum_ARGS
82TVEC_MISCSLOTS(DEFENUM)
83#undef DEFENUM
b64eb60f
MW
84
85static const struct tvec_flag attr_flags[] = {
86 { "black-fg", 0x07, 0x00 },
87 { "blue-fg", 0x07, 0x01 },
88 { "red-fg", 0x07, 0x02 },
89 { "magenta-fg", 0x07, 0x03 },
90 { "green-fg", 0x07, 0x04 },
91 { "cyan-fg", 0x07, 0x05 },
92 { "yellow-fg", 0x07, 0x06 },
93 { "white-fg", 0x07, 0x07 },
94
95 { "black-bg", 0x38, 0x00 },
96 { "blue-bg", 0x38, 0x08 },
97 { "red-bg", 0x38, 0x10 },
98 { "magenta-bg", 0x38, 0x18 },
99 { "green-bg", 0x38, 0x20 },
100 { "cyan-bg", 0x38, 0x28 },
101 { "yellow-bg", 0x38, 0x30 },
102 { "white-bg", 0x38, 0x38 },
103
104 { "normal", 0xc0, 0x00 },
105 { "bright", 0x40, 0x40 },
106 { "flash", 0x80, 0x80 },
107
108 { 0 }
109};
110
111static const struct tvec_flaginfo attr_info =
112 { "attr", attr_flags, &tvrange_u16 };
113
e63124bc
MW
114static const struct tvec_floatinfo fltish_info =
115 { TVFF_RELDELTA, -1.0, +1.0, 1e-6 };
116
b64eb60f
MW
117static const struct tvec_urange range_32 = { 0, 31 };
118
119#define TYPEREGS(_) \
120 _(int, RI, int, p, &tvrange_i16) \
121 _(uint, RU, uint, p, &tvrange_u16) \
e63124bc
MW
122 _(float, RFP, float, p, 0) \
123 _(fltish, RFISH, float, p, &fltish_info) \
124 _(char, RCH, char, p, 0) \
3efcfd2d
MW
125 _(ienum, RIE, ienum, p, &ienum_info) \
126 _(uenum, RUE, uenum, p, &uenum_info) \
127 _(fenum, RFE, fenum, p, &fenum_info) \
128 _(penum, RPE, penum, p, &penum_info) \
b64eb60f
MW
129 _(flags, RF, flags, p, &attr_info) \
130 _(string, RSTR, string, p, &range_32) \
131 _(bytes, RBY, bytes, p, &tvrange_byte) \
67b5031e 132 _(buffer, RBUF, buffer, p, 0)
b64eb60f 133
e63124bc
MW
134enum {
135 /* Output registers, one for each register type. */
136#define DEFREG(name, i, ty, argslot, argval) i,
137 TYPEREGS(DEFREG)
138#undef DEFREG
db2bf411 139 NTY,
e63124bc 140
db2bf411
MW
141 /* Outputs. */
142 RRC = NTY, /* return code from deserialize */
143 RSEROUT, /* serialized output */
e63124bc
MW
144
145 NROUT,
146
db2bf411
MW
147 /* Alternative outputs. */
148 RVOUT = 0, /* output/copy value */
149 RLEFT, /* size data remaining in input */
e63124bc 150
db2bf411
MW
151 /* Additional inputs. */
152 RSAB = NROUT, /* which register to sabotage */
153 RV, /* input value */
154 RSER, /* serialized input */
e63124bc 155
db2bf411 156 NREG
e63124bc
MW
157};
158
db2bf411 159/*----- Common execution environment --------------------------------------*/
b64eb60f
MW
160
161struct test_context {
162 struct tvec_state *tv;
db2bf411
MW
163 unsigned f;
164#define SF_SHOW 1u
b64eb60f
MW
165};
166
c91413e6 167static void common_setup(struct tvec_state *tv,
e63124bc 168 const struct tvec_env *env, void *pctx, void *ctx)
db2bf411
MW
169{
170 struct test_context *tctx = ctx;
171
172 tctx->tv = tv;
173 tctx->f = 0;
db2bf411
MW
174}
175
176static int common_set(struct tvec_state *tv, const char *name,
177 const struct tvec_env *env, void *ctx)
178{
179 struct test_context *tctx = ctx;
180 union tvec_regval rv;
181 static const struct tvec_regdef rd =
182 { "@show", -1, &tvty_ienum, 0, { &tvenum_bool } };
183
184 if (STRCMP(name, ==, "@show")) {
185 if (tvty_ienum.parse(&rv, &rd, tv)) return (-1);
186 if (tctx) {
187 if (rv.i) tctx->f |= SF_SHOW;
188 else tctx->f &= ~SF_SHOW;
189 }
190 return (1);
191 } else
192 return (0);
193}
194
195static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
196{
197 struct test_context *tctx = ctx;
198 unsigned f = tctx->f;
199
200 fn(tv->in, tv->out, tctx);
201 if (tvec_checkregs(tv)) { tvec_fail(tv, 0); f |= SF_SHOW; }
202 if (f&SF_SHOW) tvec_mismatch(tv, TVMF_IN | TVMF_OUT);
203}
204
205static void common_after(struct tvec_state *tv, void *ctx)
206 { struct test_context *tctx = ctx; tctx->f = 0; }
b64eb60f 207
db2bf411
MW
208static const struct tvec_env common_testenv = {
209 sizeof(struct test_context),
210 common_setup, common_set,
211 0, common_run, common_after,
212 0
213};
214
215/*----- Single-type copy tests --------------------------------------------*/
216
217static void test_copy_simple
218 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
219 { out->v = in->v; }
220
221static void test_copy_string
222 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
223{
224 tvec_allocstring(&out->v, in->v.str.sz);
225 memcpy(out->v.str.p, in->v.str.p, in->v.str.sz);
226}
227
228static void test_copy_bytes
229 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
230{
231 tvec_allocstring(&out->v, in->v.str.sz);
232 memcpy(out->v.str.p, in->v.str.p, in->v.str.sz);
233}
234
235#define test_copy_int test_copy_simple
236#define test_copy_uint test_copy_simple
237#define test_copy_ienum test_copy_simple
238#define test_copy_uenum test_copy_simple
239#define test_copy_fenum test_copy_simple
240#define test_copy_penum test_copy_simple
241#define test_copy_char test_copy_simple
242#define test_copy_flags test_copy_simple
243#define test_copy_float test_copy_simple
244#define test_copy_fltish test_copy_simple
245#define test_copy_buffer test_copy_bytes
246
247#define COPYREG(name, i, ty, argslot, argval) \
248 static DSGINIT(const) struct tvec_regdef name##_copyregs[] = { \
249 { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
250 { 0 } \
251 };
252TYPEREGS(COPYREG)
253#undef COPYREG
254
255/*----- Single-type serialization tests -----------------------------------*/
256
257static void setup_regdef(struct tvec_regdef *rd, unsigned i,
258 struct tvec_state *tv)
259{
260 const struct tvec_regdef *r;
261
262 for (r = tv->test->regs; r->name; r++) if (r->i == i) goto found;
263 tvec_error(tv, "internel: register definition not found"); exit(2);
264found:
265 rd[0] = *r; rd[1].name = 0;
266}
267
268static void test_single_serialize
269 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
270{
271 struct test_context *tctx = ctx;
272 struct tvec_state *tv = tctx->tv;
273 struct tvec_regdef rd[2];
274 dbuf b = DBUF_INIT;
275 int rc;
276
277 setup_regdef(rd, RV, tv);
278 rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd, NREG,
279 sizeof(struct tvec_reg));
280 out[RRC].v.i = rc;
281 if (rc)
282 out[RSEROUT].f &= ~TVRF_LIVE;
283 else {
284 tvec_allocbytes(&out[RSEROUT].v, BLEN(DBUF_BUF(&b)));
285 memcpy(out[RSEROUT].v.bytes.p, BBASE(DBUF_BUF(&b)), BLEN(DBUF_BUF(&b)));
286 }
287}
288
289static void test_single_deserialize
290 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
291{
292 struct test_context *tctx = ctx;
293 struct tvec_state *tv = tctx->tv;
294 struct tvec_regdef rd[2];
295 buf b;
296 int rc;
297
298 setup_regdef(rd, RV, tv);
299 buf_init(&b, in[RSER].v.bytes.p, in[RSER].v.bytes.sz);
300 rc = tvec_deserialize(tv->out, &b, rd, NREG, sizeof(struct tvec_reg));
301 out[RRC].v.i = rc;
302 if (rc) out[RVOUT].f &= ~TVRF_LIVE;
303}
304
305#define SERREG(name, i, ty, argslot, argval) \
306 static DSGINIT(const) struct tvec_regdef name##_serregs[] = { \
307 { #name, RV, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
308 { "buf", RSEROUT, &tvty_bytes }, \
309 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \
310 TVEC_ENDREGS \
311 }; \
312 static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \
313 { "buf", RSER, &tvty_bytes }, \
314 { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
315 { "left", RLEFT, &tvty_uint, TVRF_OPT, { &tvrange_size } }, \
316 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \
317 TVEC_ENDREGS \
318 };
319TYPEREGS(SERREG)
320#undef SERREG
321
c91413e6 322static void before_single_serialize(struct tvec_state *tv, void *ctx)
e63124bc 323{
b64eb60f 324 if (!(tv->in[RRC].f&TVRF_LIVE)) {
db2bf411 325 tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
e63124bc 326 tv->out[RRC].f |= TVRF_LIVE;
b64eb60f 327 }
b64eb60f
MW
328}
329
c91413e6 330static void before_single_deserialize(struct tvec_state *tv, void *ctx)
db2bf411
MW
331{
332 if (!(tv->in[RRC].f&TVRF_LIVE)) {
333 tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
334 tv->out[RRC].f |= TVRF_LIVE;
335 }
336 if (!(tv->in[RLEFT].f&TVRF_LIVE)) {
337 tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
338 tv->out[RLEFT].f |= TVRF_LIVE;
339 }
db2bf411
MW
340}
341
342static const struct tvec_env single_serialize_testenv = {
343 sizeof(struct test_context),
344 common_setup, common_set,
345 before_single_serialize, common_run, common_after,
346 0
347}, single_deserialize_testenv = {
348 sizeof(struct test_context),
349 common_setup, common_set,
350 before_single_deserialize, common_run, common_after,
351 0
352};
353
354/*----- Multi-type serialization test -------------------------------------*/
355
356static void test_multi_serialize
b64eb60f
MW
357 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
358{
359 struct test_context *tctx = ctx;
360 struct tvec_state *tv = tctx->tv;
361 const struct tvec_regdef *rd;
362 union tvec_regval *rv;
e63124bc 363 dbuf b = DBUF_INIT;
b64eb60f 364
e63124bc 365 if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs,
db2bf411
MW
366 NTY, sizeof(struct tvec_reg)))
367 { out[RRC].v.i = -1; goto end; }
368 tvec_allocbytes(&out[RSEROUT].v, DBLEN(&b));
369 memcpy(out[RSEROUT].v.bytes.p, DBBASE(&b), DBLEN(&b));
370 out[RSEROUT].f |= TVRF_LIVE;
e63124bc 371 buf_flip(DBUF_BUF(&b));
b64eb60f 372
e63124bc 373 if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs,
db2bf411 374 NTY, sizeof(struct tvec_reg)))
e63124bc
MW
375 { out[RRC].v.i = -2; goto end; }
376 if (BLEFT(&b._b))
377 { out[RRC].v.i = -3; goto end; }
b64eb60f 378
3efcfd2d
MW
379 if ((in[RSAB].f&TVRF_LIVE) && in[RSAB].v.i >= 0) {
380 rd = &tv->test->regs[in[RSAB].v.i]; rv = &out[in[RSAB].v.i].v;
381 if (rd->ty == &tvty_int || rd->ty == &tvty_ienum)
382 rv->i ^= 1;
383 else if (rd->ty == &tvty_uint ||
384 rd->ty == &tvty_flags || rd->ty == &tvty_uenum)
385 rv->u ^= 1;
386 else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) {
387 if (rv->f == rv->f) rv->f = -rv->f;
388 else rv->f = 1.0;
389 } else if (rd->ty == &tvty_penum)
390 rv->p = rv->p
391 ? 0
392 : (/*unconst*/ void *)
393 ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p;
394 else if (rd->ty == &tvty_string)
395 { if (rv->str.sz) rv->str.p[0] ^= 1; }
396 else if (rd->ty == &tvty_bytes)
397 { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; }
b64eb60f
MW
398 }
399
400 out[RRC].v.i = 0;
e63124bc
MW
401end:
402 dbuf_destroy(&b);
b64eb60f
MW
403}
404
db2bf411 405static const struct tvec_iassoc reg_assocs[] = {
3efcfd2d
MW
406 { "none", -1 },
407#define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
408 TYPEREGS(DEFASSOC)
409#undef DEFASSOC
410 { 0, 0 }
411};
db2bf411 412static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
3efcfd2d 413
db2bf411 414static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
b64eb60f
MW
415#define DEFREG(name, i, ty, argslot, argval) \
416 { #name, i, &tvty_##ty, TVRF_OPT, \
417 DSGINIT({ .argslot = argval }) },
418 TYPEREGS(DEFREG)
419#undef DEFREG
db2bf411 420
b64eb60f 421 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } },
db2bf411
MW
422 { "serialized", RSEROUT, &tvty_bytes, TVRF_OPT },
423 { "sabotage", RSAB, &tvty_ienum, TVRF_OPT, { &reg_enum } },
b64eb60f 424
db2bf411 425 TVEC_ENDREGS
b64eb60f
MW
426};
427
c91413e6 428static void before_multi_serialize(struct tvec_state *tv, void *ctx)
b64eb60f 429{
db2bf411
MW
430 if (!(tv->in[RRC].f&TVRF_LIVE)) {
431 tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
432 tv->out[RRC].f |= TVRF_LIVE;
433 }
b64eb60f
MW
434}
435
db2bf411
MW
436static const struct tvec_env multi_serialize_testenv = {
437 sizeof(struct test_context),
438 common_setup, common_set,
439 before_multi_serialize, common_run, common_after,
440 0
e63124bc
MW
441};
442
c91413e6
MW
443/*----- Crash test --------------------------------------------------------*/
444
445static void test_crash(const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
446{
447 out[RVOUT].v.u = in[RV].v.u;
448 if (in[RSAB].v.i) abort();
449}
450
451static const struct tvec_remotefork crash_testenv =
452 { TVEC_REMOTEFORK(0, 0) };
453
454static const struct tvec_regdef crash_regs[] = {
455 { "crash", RSAB, &tvty_ienum, 0, { &tvenum_bool } },
456 { "x", RV, &tvty_uint, 0, { &tvrange_uint } },
457 { "z", RVOUT, &tvty_uint, 0, { &tvrange_uint } },
458 TVEC_ENDREGS
459};
460
b64eb60f
MW
461/*----- Front end ---------------------------------------------------------*/
462
463static const struct tvec_test tests[] = {
db2bf411
MW
464 { "multi", multi_serialize_regs, &multi_serialize_testenv,
465 test_multi_serialize },
466
467#define DEFSINGLE(name, i, ty, argslot, argval) \
468 { "copy-" #name, name##_copyregs, &common_testenv, test_copy_##name }, \
469 { "serialize-" #name, name##_serregs, &single_serialize_testenv, \
470 test_single_serialize }, \
471 { "deserialize-" #name, name##_deserregs, &single_deserialize_testenv, \
472 test_single_deserialize },
473 TYPEREGS(DEFSINGLE)
474#undef DEFSINGLE
475
c91413e6
MW
476 { "crash", crash_regs, &crash_testenv._env, test_crash } ,
477
db2bf411 478 TVEC_ENDTESTS
b64eb60f
MW
479};
480
c5e0e403 481static const struct tvec_config testconfig = {
b64eb60f
MW
482 tests,
483 NROUT, NREG, sizeof(struct tvec_reg)
484};
485
486int main(int argc, char *argv[])
487{
e63124bc 488#if __STDC_VERSION__x < 199901
b64eb60f 489# define POKE(name, i, ty, argslot, argval) \
db2bf411
MW
490 multi_serialize_regs[i].arg.argslot = argval; \
491 name##_copyregs->arg.argslot = argval; \
492 name##_serregs->arg.argslot = argval; \
493 name##_deserregs->arg.argslot = argval;
b64eb60f
MW
494 TYPEREGS(POKE)
495# undef POKE
496#endif
c5e0e403 497 return (tvec_main(argc, argv, &testconfig, 0));
b64eb60f
MW
498}
499
500/*----- That's all, folks -------------------------------------------------*/