3 * Test the test-vector framework
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
31 #include "tvec-adhoc.h"
32 #include "tvec-remote.h"
33 #include "tvec-timeout.h"
34 #include "tvec-types.h"
36 #include <sys/select.h>
41 /*----- Register definitions ----------------------------------------------*/
43 static const struct tvec_iassoc ienum_assocs[] = {
50 static const struct tvec_uassoc uenum_assocs[] = {
57 static const struct tvec_fassoc fenum_assocs[] = {
58 { "e", 2.718281828459045 },
59 { "pi", 3.141592653589793 },
60 { "tau", 6.283185307179586 },
64 static const struct tvec_passoc penum_assocs[] = {
65 { "alice", &uenum_assocs[0] },
66 { "bob", &uenum_assocs[1] },
67 { "carol", &uenum_assocs[2] },
71 #if __STDC_VERSION__ >= 199901
77 static const struct tvec_floatinfo fenum_fltinfo =
78 { TVFF_ABSDELTA, -10, +10, 1e-3 };
80 #define DEFENUM(tag, ty, slot) \
81 static const struct tvec_##slot##enuminfo slot##enum_info = \
82 { slot##enum_NAME, slot##enum_assocs slot##enum_ARGS };
83 #define ienum_NAME "order"
84 #define ienum_ARGS , &tvrange_i16
85 #define uenum_NAME "fruit"
86 #define uenum_ARGS , &tvrange_u16
87 #define fenum_NAME "const"
88 #define fenum_ARGS , &fenum_fltinfo
89 #define penum_NAME "actor"
91 TVEC_MISCSLOTS(DEFENUM)
94 static const struct tvec_flag attr_flags[] = {
95 { "black-fg", 0x07, 0x00 },
96 { "blue-fg", 0x07, 0x01 },
97 { "red-fg", 0x07, 0x02 },
98 { "magenta-fg", 0x07, 0x03 },
99 { "green-fg", 0x07, 0x04 },
100 { "cyan-fg", 0x07, 0x05 },
101 { "yellow-fg", 0x07, 0x06 },
102 { "white-fg", 0x07, 0x07 },
104 { "black-bg", 0x38, 0x00 },
105 { "blue-bg", 0x38, 0x08 },
106 { "red-bg", 0x38, 0x10 },
107 { "magenta-bg", 0x38, 0x18 },
108 { "green-bg", 0x38, 0x20 },
109 { "cyan-bg", 0x38, 0x28 },
110 { "yellow-bg", 0x38, 0x30 },
111 { "white-bg", 0x38, 0x38 },
113 { "normal", 0xc0, 0x00 },
114 { "bright", 0x40, 0x40 },
115 { "flash", 0x80, 0x80 },
120 static const struct tvec_flaginfo attr_info =
121 { "attr", attr_flags, &tvrange_u16 };
123 static const struct tvec_floatinfo fltish_info =
124 { TVFF_RELDELTA, -1.0, +1.0, 1e-6 };
126 static const struct tvec_urange range_32 = { 0, 31 };
128 #define TYPEREGS(_) \
129 _(int, RI, int, p, &tvrange_i16) \
130 _(uint, RU, uint, p, &tvrange_u16) \
131 _(size, RSZ, size, p, 0) \
132 _(float, RFP, float, p, 0) \
133 _(fltish, RFISH, float, p, &fltish_info) \
134 _(dur, RDUR, duration, p, 0) \
135 _(char, RCH, char, p, 0) \
136 _(ienum, RIE, ienum, p, &ienum_info) \
137 _(uenum, RUE, uenum, p, &uenum_info) \
138 _(fenum, RFE, fenum, p, &fenum_info) \
139 _(penum, RPE, penum, p, &penum_info) \
140 _(flags, RF, flags, p, &attr_info) \
141 _(text, RTXT, text, p, &range_32) \
142 _(bytes, RBY, bytes, p, &tvrange_byte) \
143 _(buffer, RBUF, buffer, p, 0)
146 /* Output registers, one for each register type. */
147 #define DEFREG(name, i, ty, argslot, argval) i,
153 RRC = NTY, /* return code from deserialize */
154 RSEROUT, /* serialized output */
158 /* Alternative outputs. */
159 RVOUT = 0, /* output/copy value */
160 RLEFT, /* size data remaining in input */
162 /* Additional inputs. */
163 RSAB = NROUT, /* which register to sabotage */
164 RV, /* input value */
165 RSER, /* serialized input */
170 /*----- Common execution environment --------------------------------------*/
172 struct test_context {
173 struct tvec_state *tv;
178 static void common_setup(struct tvec_state *tv,
179 const struct tvec_env *env, void *pctx, void *ctx)
181 struct test_context *tctx = ctx;
187 static int common_setvar(struct tvec_state *tv, const char *var,
188 const union tvec_regval *rv, void *ctx)
190 struct test_context *tctx = ctx;
192 if (STRCMP(var, ==, "@show")) {
193 if (rv->i) tctx->f |= SF_SHOW;
194 } else assert(!"unknown var");
198 static const struct tvec_vardef show_var =
199 { sizeof(struct tvec_reg), common_setvar,
200 { "@show", &tvty_ienum, -1, 0, { &tvenum_bool } } };
202 static const struct tvec_vardef *common_findvar
203 (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx)
205 if (STRCMP(var, ==, "@show")) { *ctx_out = ctx; return (&show_var); }
209 static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
211 struct test_context *tctx = ctx;
212 unsigned f = tctx->f;
214 fn(tv->in, tv->out, tctx);
215 if (tvec_checkregs(tv)) { tvec_fail(tv, 0); f |= SF_SHOW; }
216 if (f&SF_SHOW) tvec_mismatch(tv, TVMF_IN | TVMF_OUT);
219 static void common_after(struct tvec_state *tv, void *ctx)
220 { struct test_context *tctx = ctx; tctx->f = 0; }
222 static const struct tvec_env common_env = {
223 sizeof(struct test_context),
224 common_setup, common_findvar,
225 0, common_run, common_after,
229 /*----- Single-type copy tests --------------------------------------------*/
231 #define COPYREG(name, i, ty, argslot, argval) \
232 static DSGINIT(const) struct tvec_regdef name##_copyregs[] = { \
233 { #name, &tvty_##ty, RVOUT, 0, DSGINIT({ .argslot = argval }) }, \
239 static void test_copy_simple
240 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
243 static void test_copy_text
244 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
246 tvec_alloctext(&out->v, in->v.text.sz);
247 memcpy(out->v.text.p, in->v.text.p, in->v.text.sz);
250 static void test_copy_bytes
251 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
253 tvec_allocbytes(&out->v, in->v.bytes.sz);
254 memcpy(out->v.bytes.p, in->v.bytes.p, in->v.bytes.sz);
257 static void test_copy_buffer
258 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
259 { tvec_initbuffer(&out->v, &in->v, in->v.buf.sz); }
261 #define test_copy_int test_copy_simple
262 #define test_copy_uint test_copy_simple
263 #define test_copy_size test_copy_simple
264 #define test_copy_ienum test_copy_simple
265 #define test_copy_uenum test_copy_simple
266 #define test_copy_fenum test_copy_simple
267 #define test_copy_penum test_copy_simple
268 #define test_copy_char test_copy_simple
269 #define test_copy_flags test_copy_simple
270 #define test_copy_float test_copy_simple
271 #define test_copy_fltish test_copy_simple
272 #define test_copy_dur test_copy_simple
274 #define COPYTEST(name, i, ty, argslot, argval) \
275 static const struct tvec_test copy_##name##_test = \
276 { "copy-" #name, name##_copyregs, &common_env, test_copy_##name };
280 /*----- Single-type serialization tests -----------------------------------*/
282 #define SERREG(name, i, ty, argslot, argval) \
283 static DSGINIT(const) struct tvec_regdef name##_serregs[] = { \
284 { #name, &tvty_##ty, RV, 0, \
285 DSGINIT({ .argslot = argval }) }, \
286 { "buf", &tvty_bytes, RSEROUT, 0 }, \
287 { "rc", &tvty_int, RRC, TVRF_OPT, \
288 { &tvrange_int } }, \
294 static void setup_regdef(struct tvec_regdef *rd, unsigned i,
295 struct tvec_state *tv)
297 const struct tvec_regdef *r;
299 for (r = tv->test->regs; r->name; r++) if (r->i == i) goto found;
300 tvec_error(tv, "internel: register definition not found"); exit(2);
302 rd[0] = *r; rd[1].name = 0;
305 static void before_single_serialize(struct tvec_state *tv, void *ctx)
307 if (!(tv->in[RRC].f&TVRF_LIVE)) {
308 tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
309 tv->out[RRC].f |= TVRF_LIVE;
313 static void test_single_serialize
314 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
316 struct test_context *tctx = ctx;
317 struct tvec_state *tv = tctx->tv;
318 struct tvec_regdef rd[2];
322 setup_regdef(rd, RV, tv);
323 rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd,
324 0, 0, NREG, sizeof(struct tvec_reg));
327 out[RSEROUT].f &= ~TVRF_LIVE;
329 tvec_allocbytes(&out[RSEROUT].v, BLEN(DBUF_BUF(&b)));
330 memcpy(out[RSEROUT].v.bytes.p, BBASE(DBUF_BUF(&b)), BLEN(DBUF_BUF(&b)));
334 static const struct tvec_env single_serialize_env = {
335 sizeof(struct test_context),
336 common_setup, common_findvar,
337 before_single_serialize, common_run, common_after,
340 #define SERTEST(name, i, ty, argslot, argval) \
341 static const struct tvec_test serialize_##name##_test = \
342 { "serialize-" #name, name##_serregs, \
343 &single_serialize_env, test_single_serialize };
347 #define DESERREG(name, i, ty, argslot, argval) \
348 static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \
349 { "buf", &tvty_bytes, RSER, 0 }, \
350 { #name, &tvty_##ty, RVOUT, 0, \
351 DSGINIT({ .argslot = argval }) }, \
352 { "left", &tvty_uint, RLEFT, TVRF_OPT, \
353 { &tvrange_size } }, \
354 { "rc", &tvty_int, RRC, TVRF_OPT, \
355 { &tvrange_int } }, \
361 static void before_single_deserialize(struct tvec_state *tv, void *ctx)
363 if (!(tv->in[RRC].f&TVRF_LIVE)) {
364 tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
365 tv->out[RRC].f |= TVRF_LIVE;
367 if (!(tv->in[RLEFT].f&TVRF_LIVE)) {
368 tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
369 tv->out[RLEFT].f |= TVRF_LIVE;
373 static void test_single_deserialize
374 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
376 struct test_context *tctx = ctx;
377 struct tvec_state *tv = tctx->tv;
378 struct tvec_regdef rd[2];
382 setup_regdef(rd, RV, tv);
383 buf_init(&b, in[RSER].v.bytes.p, in[RSER].v.bytes.sz);
384 rc = tvec_deserialize(tv->out, &b, rd,
385 0, 0, NREG, sizeof(struct tvec_reg));
387 if (rc) out[RVOUT].f &= ~TVRF_LIVE;
390 static const struct tvec_env single_deserialize_env = {
391 sizeof(struct test_context),
392 common_setup, common_findvar,
393 before_single_deserialize, common_run, common_after,
396 #define DESERTEST(name, i, ty, argslot, argval) \
397 static const struct tvec_test deserialize_##name##_test = \
398 { "deserialize-" #name, name##_deserregs, \
399 &single_deserialize_env, test_single_deserialize };
403 /*----- Multi-type serialization test -------------------------------------*/
405 static const struct tvec_iassoc reg_assocs[] = {
407 #define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
412 static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
414 static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
415 #define DEFREG(name, i, ty, argslot, argval) \
416 { #name, &tvty_##ty, i, TVRF_OPT, \
417 DSGINIT({ .argslot = argval }) },
421 { "rc", &tvty_int, RRC, TVRF_OPT, { &tvrange_int } },
422 { "serialized", &tvty_bytes, RSEROUT, TVRF_OPT },
423 { "sabotage", &tvty_ienum, RSAB, TVRF_OPT, { ®_enum } },
428 static void before_multi_serialize(struct tvec_state *tv, void *ctx)
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;
436 static void test_multi_serialize
437 (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
439 struct test_context *tctx = ctx;
440 struct tvec_state *tv = tctx->tv;
441 const struct tvec_regdef *rd;
442 union tvec_regval *rv;
445 if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs,
446 0, 0, NTY, sizeof(struct tvec_reg)))
447 { out[RRC].v.i = -1; goto end; }
448 tvec_allocbytes(&out[RSEROUT].v, DBLEN(&b));
449 memcpy(out[RSEROUT].v.bytes.p, DBBASE(&b), DBLEN(&b));
450 out[RSEROUT].f |= TVRF_LIVE;
451 buf_flip(DBUF_BUF(&b));
453 if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs,
454 0, 0, NTY, sizeof(struct tvec_reg)))
455 { out[RRC].v.i = -2; goto end; }
457 { out[RRC].v.i = -3; goto end; }
459 if ((in[RSAB].f&TVRF_LIVE) && in[RSAB].v.i >= 0) {
460 rd = &tv->test->regs[in[RSAB].v.i]; rv = &out[in[RSAB].v.i].v;
461 if (rd->ty == &tvty_int || rd->ty == &tvty_ienum)
463 else if (rd->ty == &tvty_uint ||
464 rd->ty == &tvty_flags || rd->ty == &tvty_uenum)
466 else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) {
467 if (rv->f == rv->f) rv->f = -rv->f;
469 } else if (rd->ty == &tvty_penum)
472 : UNCONST(void, ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p);
473 else if (rd->ty == &tvty_text)
474 { if (rv->text.sz) rv->text.p[0] ^= 1; }
475 else if (rd->ty == &tvty_bytes)
476 { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; }
484 static const struct tvec_env multi_serialize_env = {
485 sizeof(struct test_context),
486 common_setup, common_findvar,
487 before_multi_serialize, common_run, common_after,
490 static const struct tvec_test multi_serialize_test =
491 { "multi", multi_serialize_regs, &multi_serialize_env,
492 test_multi_serialize };
494 /*----- Crash test --------------------------------------------------------*/
496 static const struct tvec_regdef crash_regs[] = {
497 { "crash", &tvty_ienum, RSAB, 0, { &tvenum_bool } },
498 { "x", &tvty_uint, RV, 0, { &tvrange_uint } },
499 { "z", &tvty_uint, RVOUT, 0, { &tvrange_uint } },
503 static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
506 out[RVOUT].v.u = in[RV].v.u;
507 if (in[RSAB].v.i) abort();
510 static const struct tvec_remotefork crash_env =
511 { TVEC_REMOTEFORK(0, 0) };
512 static const struct tvec_test crash_test =
513 { "crash", crash_regs, &crash_env._env, test_crash };
515 /*----- Sleep test --------------------------------------------------------*/
517 static const struct tvec_regdef sleep_regs[] = {
518 { "time", &tvty_duration, RV, 0, { &tvflt_nonneg } },
519 { "z", &tvty_float, RVOUT, 0, { &tvflt_nonneg } },
523 static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
526 struct timeval now, when, tv;
529 rc = gettimeofday(&now, 0); assert(!rc);
530 tv.tv_sec = in[RV].v.f; tv.tv_usec = 1e6*(in[RV].v.f - tv.tv_sec);
532 TV_ADD(&when, &now, &tv);
534 rc = select(0, 0, 0, 0, &tv); assert(!rc);
535 rc = gettimeofday(&now, 0); assert(!rc);
536 if (TV_CMP(&now, >=, &when)) break;
537 TV_SUB(&tv, &when, &now);
539 out[RVOUT].v.f = in[RV].v.f;
542 static const struct tvec_timeoutenv sleep_subenv =
543 { TVEC_TIMEOUTINIT(ITIMER_REAL, 0.25) };
544 static const struct tvec_remotefork sleep_env =
545 { TVEC_REMOTEFORK(&sleep_subenv._env, 0) };
546 static const struct tvec_test sleep_test =
547 { "sleep", sleep_regs, &sleep_env._env, test_sleep };
549 /*----- Front end ---------------------------------------------------------*/
551 static const struct tvec_test *const tests[] = {
552 &multi_serialize_test,
556 #define DEFSINGLE(name, i, ty, argslot, argval) \
557 ©_##name##_test, &serialize_##name##_test, &deserialize_##name##_test,
564 static const struct tvec_config testconfig = {
566 NROUT, NREG, sizeof(struct tvec_reg)
569 int main(int argc, char *argv[])
571 #if __STDC_VERSION__ < 199901
572 # define POKE(name, i, ty, argslot, argval) \
573 multi_serialize_regs[i].arg.argslot = argval; \
574 name##_copyregs->arg.argslot = argval; \
575 name##_serregs->arg.argslot = argval; \
576 name##_deserregs->arg.argslot = argval;
580 return (tvec_main(argc, argv, &testconfig, 0));
583 /*----- That's all, folks -------------------------------------------------*/