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 ------------------------------------------------------*/
33 #include <sys/select.h>
36 /*----- Register definitions ----------------------------------------------*/
38 static const struct tvec_iassoc ienum_assocs
[] = {
45 static const struct tvec_uassoc uenum_assocs
[] = {
52 static const struct tvec_fassoc fenum_assocs
[] = {
53 { "e", 2.718281828459045 },
54 { "pi", 3.141592653589793 },
55 { "tau", 6.283185307179586 },
59 static const struct tvec_passoc penum_assocs
[] = {
60 { "alice", &uenum_assocs
[0] },
61 { "bob", &uenum_assocs
[1] },
62 { "carol", &uenum_assocs
[2] },
66 #if __STDC_VERSION__x >= 199901
72 static const struct tvec_floatinfo fenum_fltinfo
=
73 { TVFF_ABSDELTA
, -10, +10, 1e-3 };
75 #define DEFENUM(tag, ty, slot) \
76 static const struct tvec_##slot##enuminfo slot##enum_info = \
77 { slot##enum_NAME, slot##enum_assocs slot##enum_ARGS };
78 #define ienum_NAME "order"
79 #define ienum_ARGS , &tvrange_i16
80 #define uenum_NAME "fruit"
81 #define uenum_ARGS , &tvrange_u16
82 #define fenum_NAME "const"
83 #define fenum_ARGS , &fenum_fltinfo
84 #define penum_NAME "actor"
86 TVEC_MISCSLOTS(DEFENUM
)
89 static const struct tvec_flag attr_flags
[] = {
90 { "black-fg", 0x07, 0x00 },
91 { "blue-fg", 0x07, 0x01 },
92 { "red-fg", 0x07, 0x02 },
93 { "magenta-fg", 0x07, 0x03 },
94 { "green-fg", 0x07, 0x04 },
95 { "cyan-fg", 0x07, 0x05 },
96 { "yellow-fg", 0x07, 0x06 },
97 { "white-fg", 0x07, 0x07 },
99 { "black-bg", 0x38, 0x00 },
100 { "blue-bg", 0x38, 0x08 },
101 { "red-bg", 0x38, 0x10 },
102 { "magenta-bg", 0x38, 0x18 },
103 { "green-bg", 0x38, 0x20 },
104 { "cyan-bg", 0x38, 0x28 },
105 { "yellow-bg", 0x38, 0x30 },
106 { "white-bg", 0x38, 0x38 },
108 { "normal", 0xc0, 0x00 },
109 { "bright", 0x40, 0x40 },
110 { "flash", 0x80, 0x80 },
115 static const struct tvec_flaginfo attr_info
=
116 { "attr", attr_flags
, &tvrange_u16
};
118 static const struct tvec_floatinfo fltish_info
=
119 { TVFF_RELDELTA
, -1.0, +1.0, 1e-6 };
121 static const struct tvec_urange range_32
= { 0, 31 };
123 #define TYPEREGS(_) \
124 _(int, RI, int, p, &tvrange_i16) \
125 _(uint, RU, uint, p, &tvrange_u16) \
126 _(float, RFP, float, p, 0) \
127 _(fltish, RFISH, float, p, &fltish_info) \
128 _(char, RCH, char, p, 0) \
129 _(ienum, RIE, ienum, p, &ienum_info) \
130 _(uenum, RUE, uenum, p, &uenum_info) \
131 _(fenum, RFE, fenum, p, &fenum_info) \
132 _(penum, RPE, penum, p, &penum_info) \
133 _(flags, RF, flags, p, &attr_info) \
134 _(text, RTXT, text, p, &range_32) \
135 _(bytes, RBY, bytes, p, &tvrange_byte) \
136 _(buffer, RBUF, buffer, p, 0)
139 /* Output registers, one for each register type. */
140 #define DEFREG(name, i, ty, argslot, argval) i,
146 RRC
= NTY
, /* return code from deserialize */
147 RSEROUT
, /* serialized output */
151 /* Alternative outputs. */
152 RVOUT
= 0, /* output/copy value */
153 RLEFT
, /* size data remaining in input */
155 /* Additional inputs. */
156 RSAB
= NROUT
, /* which register to sabotage */
157 RV
, /* input value */
158 RSER
, /* serialized input */
163 /*----- Common execution environment --------------------------------------*/
165 struct test_context
{
166 struct tvec_state
*tv
;
171 static void common_setup(struct tvec_state
*tv
,
172 const struct tvec_env
*env
, void *pctx
, void *ctx
)
174 struct test_context
*tctx
= ctx
;
180 static int common_setvar(struct tvec_state
*tv
, const char *var
,
181 const union tvec_regval
*rv
, void *ctx
)
183 struct test_context
*tctx
= ctx
;
185 if (STRCMP(var
, ==, "@show")) {
186 if (rv
->i
) tctx
->f
|= SF_SHOW
;
187 } else assert(!"unknown var");
191 static const struct tvec_vardef show_var
=
192 { sizeof(struct tvec_reg
), common_setvar
,
193 { "@show", -1, &tvty_ienum
, 0, { &tvenum_bool
} } };
195 static const struct tvec_vardef
*common_findvar
196 (struct tvec_state
*tv
, const char *var
, void **ctx_out
, void *ctx
)
198 if (STRCMP(var
, ==, "@show")) { *ctx_out
= ctx
; return (&show_var
); }
202 static void common_run(struct tvec_state
*tv
, tvec_testfn
*fn
, void *ctx
)
204 struct test_context
*tctx
= ctx
;
205 unsigned f
= tctx
->f
;
207 fn(tv
->in
, tv
->out
, tctx
);
208 if (tvec_checkregs(tv
)) { tvec_fail(tv
, 0); f
|= SF_SHOW
; }
209 if (f
&SF_SHOW
) tvec_mismatch(tv
, TVMF_IN
| TVMF_OUT
);
212 static void common_after(struct tvec_state
*tv
, void *ctx
)
213 { struct test_context
*tctx
= ctx
; tctx
->f
= 0; }
215 static const struct tvec_env common_testenv
= {
216 sizeof(struct test_context
),
217 common_setup
, common_findvar
,
218 0, common_run
, common_after
,
222 /*----- Single-type copy tests --------------------------------------------*/
224 static void test_copy_simple
225 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
228 static void test_copy_text
229 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
231 tvec_alloctext(&out
->v
, in
->v
.text
.sz
);
232 memcpy(out
->v
.text
.p
, in
->v
.text
.p
, in
->v
.text
.sz
);
235 static void test_copy_bytes
236 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
238 tvec_allocbytes(&out
->v
, in
->v
.bytes
.sz
);
239 memcpy(out
->v
.bytes
.p
, in
->v
.bytes
.p
, in
->v
.bytes
.sz
);
242 static void test_copy_buffer
243 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
244 { tvec_initbuffer(&out
->v
, &in
->v
, in
->v
.buf
.sz
); }
246 #define test_copy_int test_copy_simple
247 #define test_copy_uint test_copy_simple
248 #define test_copy_ienum test_copy_simple
249 #define test_copy_uenum test_copy_simple
250 #define test_copy_fenum test_copy_simple
251 #define test_copy_penum test_copy_simple
252 #define test_copy_char test_copy_simple
253 #define test_copy_flags test_copy_simple
254 #define test_copy_float test_copy_simple
255 #define test_copy_fltish test_copy_simple
257 #define COPYREG(name, i, ty, argslot, argval) \
258 static DSGINIT(const) struct tvec_regdef name##_copyregs[] = { \
259 { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
265 /*----- Single-type serialization tests -----------------------------------*/
267 static void setup_regdef(struct tvec_regdef
*rd
, unsigned i
,
268 struct tvec_state
*tv
)
270 const struct tvec_regdef
*r
;
272 for (r
= tv
->test
->regs
; r
->name
; r
++) if (r
->i
== i
) goto found
;
273 tvec_error(tv
, "internel: register definition not found"); exit(2);
275 rd
[0] = *r
; rd
[1].name
= 0;
278 static void test_single_serialize
279 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
281 struct test_context
*tctx
= ctx
;
282 struct tvec_state
*tv
= tctx
->tv
;
283 struct tvec_regdef rd
[2];
287 setup_regdef(rd
, RV
, tv
);
288 rc
= tvec_serialize(tv
->in
, DBUF_BUF(&b
), rd
, NREG
,
289 sizeof(struct tvec_reg
));
292 out
[RSEROUT
].f
&= ~TVRF_LIVE
;
294 tvec_allocbytes(&out
[RSEROUT
].v
, BLEN(DBUF_BUF(&b
)));
295 memcpy(out
[RSEROUT
].v
.bytes
.p
, BBASE(DBUF_BUF(&b
)), BLEN(DBUF_BUF(&b
)));
299 static void test_single_deserialize
300 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
302 struct test_context
*tctx
= ctx
;
303 struct tvec_state
*tv
= tctx
->tv
;
304 struct tvec_regdef rd
[2];
308 setup_regdef(rd
, RV
, tv
);
309 buf_init(&b
, in
[RSER
].v
.bytes
.p
, in
[RSER
].v
.bytes
.sz
);
310 rc
= tvec_deserialize(tv
->out
, &b
, rd
, NREG
, sizeof(struct tvec_reg
));
312 if (rc
) out
[RVOUT
].f
&= ~TVRF_LIVE
;
315 #define SERREG(name, i, ty, argslot, argval) \
316 static DSGINIT(const) struct tvec_regdef name##_serregs[] = { \
317 { #name, RV, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
318 { "buf", RSEROUT, &tvty_bytes }, \
319 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \
322 static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \
323 { "buf", RSER, &tvty_bytes }, \
324 { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
325 { "left", RLEFT, &tvty_uint, TVRF_OPT, { &tvrange_size } }, \
326 { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \
332 static void before_single_serialize(struct tvec_state
*tv
, void *ctx
)
334 if (!(tv
->in
[RRC
].f
&TVRF_LIVE
)) {
335 tv
->in
[RRC
].v
.i
= 0; tv
->in
[RRC
].f
|= TVRF_LIVE
;
336 tv
->out
[RRC
].f
|= TVRF_LIVE
;
340 static void before_single_deserialize(struct tvec_state
*tv
, void *ctx
)
342 if (!(tv
->in
[RRC
].f
&TVRF_LIVE
)) {
343 tv
->in
[RRC
].v
.i
= 0; tv
->in
[RRC
].f
|= TVRF_LIVE
;
344 tv
->out
[RRC
].f
|= TVRF_LIVE
;
346 if (!(tv
->in
[RLEFT
].f
&TVRF_LIVE
)) {
347 tv
->in
[RLEFT
].v
.u
= 0; tv
->in
[RLEFT
].f
|= TVRF_LIVE
;
348 tv
->out
[RLEFT
].f
|= TVRF_LIVE
;
352 static const struct tvec_env single_serialize_testenv
= {
353 sizeof(struct test_context
),
354 common_setup
, common_findvar
,
355 before_single_serialize
, common_run
, common_after
,
357 }, single_deserialize_testenv
= {
358 sizeof(struct test_context
),
359 common_setup
, common_findvar
,
360 before_single_deserialize
, common_run
, common_after
,
364 /*----- Multi-type serialization test -------------------------------------*/
366 static void test_multi_serialize
367 (const struct tvec_reg
*in
, struct tvec_reg
*out
, void *ctx
)
369 struct test_context
*tctx
= ctx
;
370 struct tvec_state
*tv
= tctx
->tv
;
371 const struct tvec_regdef
*rd
;
372 union tvec_regval
*rv
;
375 if (tvec_serialize(tv
->in
, DBUF_BUF(&b
), tv
->test
->regs
,
376 NTY
, sizeof(struct tvec_reg
)))
377 { out
[RRC
].v
.i
= -1; goto end
; }
378 tvec_allocbytes(&out
[RSEROUT
].v
, DBLEN(&b
));
379 memcpy(out
[RSEROUT
].v
.bytes
.p
, DBBASE(&b
), DBLEN(&b
));
380 out
[RSEROUT
].f
|= TVRF_LIVE
;
381 buf_flip(DBUF_BUF(&b
));
383 if (tvec_deserialize(tv
->out
, DBUF_BUF(&b
), tv
->test
->regs
,
384 NTY
, sizeof(struct tvec_reg
)))
385 { out
[RRC
].v
.i
= -2; goto end
; }
387 { out
[RRC
].v
.i
= -3; goto end
; }
389 if ((in
[RSAB
].f
&TVRF_LIVE
) && in
[RSAB
].v
.i
>= 0) {
390 rd
= &tv
->test
->regs
[in
[RSAB
].v
.i
]; rv
= &out
[in
[RSAB
].v
.i
].v
;
391 if (rd
->ty
== &tvty_int
|| rd
->ty
== &tvty_ienum
)
393 else if (rd
->ty
== &tvty_uint
||
394 rd
->ty
== &tvty_flags
|| rd
->ty
== &tvty_uenum
)
396 else if (rd
->ty
== &tvty_float
|| rd
->ty
== &tvty_fenum
) {
397 if (rv
->f
== rv
->f
) rv
->f
= -rv
->f
;
399 } else if (rd
->ty
== &tvty_penum
)
402 : (/*unconst*/ void *)
403 ((const struct tvec_penuminfo
*)rd
->arg
.p
)->av
[0].p
;
404 else if (rd
->ty
== &tvty_text
)
405 { if (rv
->text
.sz
) rv
->text
.p
[0] ^= 1; }
406 else if (rd
->ty
== &tvty_bytes
)
407 { if (rv
->bytes
.sz
) rv
->bytes
.p
[0] ^= 1; }
415 static const struct tvec_iassoc reg_assocs
[] = {
417 #define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
422 static const struct tvec_ienuminfo reg_enum
= { "reg", reg_assocs
, 0 };
424 static DSGINIT(const) struct tvec_regdef multi_serialize_regs
[] = {
425 #define DEFREG(name, i, ty, argslot, argval) \
426 { #name, i, &tvty_##ty, TVRF_OPT, \
427 DSGINIT({ .argslot = argval }) },
431 { "rc", RRC
, &tvty_int
, TVRF_OPT
, { &tvrange_int
} },
432 { "serialized", RSEROUT
, &tvty_bytes
, TVRF_OPT
},
433 { "sabotage", RSAB
, &tvty_ienum
, TVRF_OPT
, { ®_enum
} },
438 static void before_multi_serialize(struct tvec_state
*tv
, void *ctx
)
440 if (!(tv
->in
[RRC
].f
&TVRF_LIVE
)) {
441 tv
->in
[RRC
].v
.i
= 0; tv
->in
[RRC
].f
|= TVRF_LIVE
;
442 tv
->out
[RRC
].f
|= TVRF_LIVE
;
446 static const struct tvec_env multi_serialize_testenv
= {
447 sizeof(struct test_context
),
448 common_setup
, common_findvar
,
449 before_multi_serialize
, common_run
, common_after
,
453 /*----- Crash test --------------------------------------------------------*/
455 static void test_crash(const struct tvec_reg
*in
, struct tvec_reg
*out
,
458 out
[RVOUT
].v
.u
= in
[RV
].v
.u
;
459 if (in
[RSAB
].v
.i
) abort();
462 static const struct tvec_remotefork crash_testenv
=
463 { TVEC_REMOTEFORK(0, 0) };
465 static const struct tvec_regdef crash_regs
[] = {
466 { "crash", RSAB
, &tvty_ienum
, 0, { &tvenum_bool
} },
467 { "x", RV
, &tvty_uint
, 0, { &tvrange_uint
} },
468 { "z", RVOUT
, &tvty_uint
, 0, { &tvrange_uint
} },
472 /*----- Sleep test --------------------------------------------------------*/
474 static void test_sleep(const struct tvec_reg
*in
, struct tvec_reg
*out
,
477 struct timeval now
, when
, tv
;
480 rc
= gettimeofday(&now
, 0); assert(!rc
);
481 tv
.tv_sec
= in
[RV
].v
.f
; tv
.tv_usec
= 1e6
*(in
[RV
].v
.f
- tv
.tv_sec
);
483 TV_ADD(&when
, &now
, &tv
);
485 rc
= select(0, 0, 0, 0, &tv
); assert(!rc
);
486 rc
= gettimeofday(&now
, 0); assert(!rc
);
487 if (TV_CMP(&now
, >=, &when
)) break;
488 TV_SUB(&tv
, &when
, &now
);
490 out
[RVOUT
].v
.f
= in
[RV
].v
.f
;
493 static const struct tvec_timeoutenv sleep_subenv
=
494 { TVEC_TIMEOUTINIT(ITIMER_REAL
, 0.25) };
495 static const struct tvec_remotefork sleep_testenv
=
496 { TVEC_REMOTEFORK(&sleep_subenv
._env
, 0) };
498 static const struct tvec_regdef sleep_regs
[] = {
499 { "time", RV
, &tvty_duration
, 0, { &tvflt_nonneg
} },
500 { "z", RVOUT
, &tvty_float
, 0, { &tvflt_nonneg
} },
504 /*----- Front end ---------------------------------------------------------*/
506 static const struct tvec_test tests
[] = {
507 { "multi", multi_serialize_regs
, &multi_serialize_testenv
,
508 test_multi_serialize
},
510 #define DEFSINGLE(name, i, ty, argslot, argval) \
511 { "copy-" #name, name##_copyregs, &common_testenv, test_copy_##name }, \
512 { "serialize-" #name, name##_serregs, &single_serialize_testenv, \
513 test_single_serialize }, \
514 { "deserialize-" #name, name##_deserregs, &single_deserialize_testenv, \
515 test_single_deserialize },
519 { "crash", crash_regs
, &crash_testenv
._env
, test_crash
},
520 { "sleep", sleep_regs
, &sleep_testenv
._env
, test_sleep
},
525 static const struct tvec_config testconfig
= {
527 NROUT
, NREG
, sizeof(struct tvec_reg
)
530 int main(int argc
, char *argv
[])
532 #if __STDC_VERSION__x < 199901
533 # define POKE(name, i, ty, argslot, argval) \
534 multi_serialize_regs[i].arg.argslot = argval; \
535 name##_copyregs->arg.argslot = argval; \
536 name##_serregs->arg.argslot = argval; \
537 name##_deserregs->arg.argslot = argval;
541 return (tvec_main(argc
, argv
, &testconfig
, 0));
544 /*----- That's all, folks -------------------------------------------------*/