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