chiark / gitweb /
@@@ fltfmt mess
[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 "tvec.h"
31 #include "tvec-adhoc.h"
32 #include "tvec-remote.h"
33 #include "tvec-timeout.h"
34 #include "tvec-types.h"
35
36 #include <sys/select.h>
37 #include <sys/time.h>
38
39 #include "tv.h"
40
41 /*----- Register definitions ----------------------------------------------*/
42
43 static const struct tvec_iassoc ienum_assocs[] = {
44   { "less",            -1 },
45   { "equal",            0 },
46   { "greater",         +1 },
47   { 0 }
48 };
49
50 static const struct tvec_uassoc uenum_assocs[] = {
51   { "apple",            0 },
52   { "banana",           1 },
53   { "clementine",       2 },
54   { 0 }
55 };
56
57 static const struct tvec_fassoc fenum_assocs[] = {
58   { "e",                2.718281828459045 },
59   { "pi",               3.141592653589793 },
60   { "tau",              6.283185307179586 },
61   { 0 }
62 };
63
64 static const struct tvec_passoc penum_assocs[] = {
65   { "alice",            &uenum_assocs[0] },
66   { "bob",              &uenum_assocs[1] },
67   { "carol",            &uenum_assocs[2] },
68   { 0 }
69 };
70
71 #if __STDC_VERSION__ >= 199901
72 #  define DSGINIT(x) x
73 #else
74 #  define DSGINIT(x)
75 #endif
76
77 static const struct tvec_floatinfo fenum_fltinfo =
78   { TVFF_ABSDELTA, -10, +10, 1e-3 };
79
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"
90 #define penum_ARGS
91 TVEC_MISCSLOTS(DEFENUM)
92 #undef DEFENUM
93
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 },
103
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 },
112
113   { "normal",           0xc0, 0x00 },
114   { "bright",           0x40, 0x40 },
115   { "flash",            0x80, 0x80 },
116
117   { 0 }
118 };
119
120 static const struct tvec_flaginfo attr_info =
121   { "attr", attr_flags, &tvrange_u16 };
122
123 static const struct tvec_floatinfo fltish_info =
124   { TVFF_RELDELTA, -1.0, +1.0, 1e-6 };
125
126 static const struct tvec_urange range_32 = { 0, 31 };
127
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)
144
145 enum {
146   /* Output registers, one for each register type. */
147 #define DEFREG(name, i, ty, argslot, argval) i,
148   TYPEREGS(DEFREG)
149 #undef DEFREG
150   NTY,
151
152   /* Outputs. */
153   RRC = NTY,                            /* return code from deserialize */
154   RSEROUT,                              /* serialized output */
155
156   NROUT,
157
158   /* Alternative outputs. */
159   RVOUT = 0,                            /* output/copy value */
160   RLEFT,                                /* size data remaining in input */
161
162   /* Additional inputs. */
163   RSAB = NROUT,                         /* which register to sabotage */
164   RV,                                   /* input value */
165   RSER,                                 /* serialized input */
166
167   NREG
168 };
169
170 /*----- Common execution environment --------------------------------------*/
171
172 struct test_context {
173   struct tvec_state *tv;
174   unsigned f;
175 #define SF_SHOW 1u
176 };
177
178 static void common_setup(struct tvec_state *tv,
179                          const struct tvec_env *env, void *pctx, void *ctx)
180 {
181   struct test_context *tctx = ctx;
182
183   tctx->tv = tv;
184   tctx->f = 0;
185 }
186
187 static int common_setvar(struct tvec_state *tv, const char *var,
188                          const union tvec_regval *rv, void *ctx)
189 {
190   struct test_context *tctx = ctx;
191
192   if (STRCMP(var, ==, "@show")) {
193     if (rv->i) tctx->f |= SF_SHOW;
194   } else assert(!"unknown var");
195   return (0);
196 }
197
198 static const struct tvec_vardef show_var =
199   { sizeof(struct tvec_reg), common_setvar,
200     { "@show", &tvty_ienum, -1, 0, { &tvenum_bool } } };
201
202 static const struct tvec_vardef *common_findvar
203   (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx)
204 {
205   if (STRCMP(var, ==, "@show")) { *ctx_out = ctx; return (&show_var); }
206   return (0);
207 }
208
209 static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
210 {
211   struct test_context *tctx = ctx;
212   unsigned f = tctx->f;
213
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);
217 }
218
219 static void common_after(struct tvec_state *tv, void *ctx)
220   { struct test_context *tctx = ctx; tctx->f = 0; }
221
222 static const struct tvec_env common_env = {
223   sizeof(struct test_context),
224   common_setup, common_findvar,
225   0, common_run, common_after,
226   0
227 };
228
229 /*----- Single-type copy tests --------------------------------------------*/
230
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 }) },      \
234   { 0 }                                                                 \
235 };
236 TYPEREGS(COPYREG)
237 #undef COPYREG
238
239 static void test_copy_simple
240   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
241   { out->v = in->v; }
242
243 static void test_copy_text
244   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
245 {
246   tvec_alloctext(&out->v, in->v.text.sz);
247   memcpy(out->v.text.p, in->v.text.p, in->v.text.sz);
248 }
249
250 static void test_copy_bytes
251   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
252 {
253   tvec_allocbytes(&out->v, in->v.bytes.sz);
254   memcpy(out->v.bytes.p, in->v.bytes.p, in->v.bytes.sz);
255 }
256
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); }
260
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
273
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 };
277 TYPEREGS(COPYTEST)
278 #undef COPYTEST
279
280 /*----- Single-type serialization tests -----------------------------------*/
281
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 } }, \
289   TVEC_ENDREGS                                                          \
290 };
291 TYPEREGS(SERREG)
292 #undef SERREG
293
294 static void setup_regdef(struct tvec_regdef *rd, unsigned i,
295                          struct tvec_state *tv)
296 {
297   const struct tvec_regdef *r;
298
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);
301 found:
302   rd[0] = *r; rd[1].name = 0;
303 }
304
305 static void before_single_serialize(struct tvec_state *tv, void *ctx)
306 {
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;
310   }
311 }
312
313 static void test_single_serialize
314   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
315 {
316   struct test_context *tctx = ctx;
317   struct tvec_state *tv = tctx->tv;
318   struct tvec_regdef rd[2];
319   dbuf b = DBUF_INIT;
320   int rc;
321
322   setup_regdef(rd, RV, tv);
323   rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd,
324                       0, 0, NREG, sizeof(struct tvec_reg));
325   out[RRC].v.i = rc;
326   if (rc)
327     out[RSEROUT].f &= ~TVRF_LIVE;
328   else {
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)));
331   }
332 }
333
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,
338   0
339 };
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 };
344 TYPEREGS(SERTEST)
345 #undef SERTEST
346
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 } }, \
356   TVEC_ENDREGS                                                          \
357 };
358 TYPEREGS(DESERREG)
359 #undef DESERREG
360
361 static void before_single_deserialize(struct tvec_state *tv, void *ctx)
362 {
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;
366   }
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;
370   }
371 }
372
373 static void test_single_deserialize
374   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
375 {
376   struct test_context *tctx = ctx;
377   struct tvec_state *tv = tctx->tv;
378   struct tvec_regdef rd[2];
379   buf b;
380   int rc;
381
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));
386   out[RRC].v.i = rc;
387   if (rc) out[RVOUT].f &= ~TVRF_LIVE;
388 }
389
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,
394   0
395 };
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 };
400 TYPEREGS(DESERTEST)
401 #undef DESERTEST
402
403 /*----- Multi-type serialization test -------------------------------------*/
404
405 static const struct tvec_iassoc reg_assocs[] = {
406   { "none",             -1 },
407 #define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
408   TYPEREGS(DEFASSOC)
409 #undef DEFASSOC
410   { 0,                  0 }
411 };
412 static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
413
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 }) },
418   TYPEREGS(DEFREG)
419 #undef DEFREG
420
421   { "rc",       &tvty_int,      RRC,    TVRF_OPT,       { &tvrange_int } },
422   { "serialized", &tvty_bytes,  RSEROUT, TVRF_OPT },
423   { "sabotage", &tvty_ienum,    RSAB,   TVRF_OPT,       { &reg_enum } },
424
425   TVEC_ENDREGS
426 };
427
428 static void before_multi_serialize(struct tvec_state *tv, void *ctx)
429 {
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   }
434 }
435
436 static void test_multi_serialize
437   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
438 {
439   struct test_context *tctx = ctx;
440   struct tvec_state *tv = tctx->tv;
441   const struct tvec_regdef *rd;
442   union tvec_regval *rv;
443   dbuf b = DBUF_INIT;
444
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));
452
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; }
456   if (BLEFT(&b._b))
457     { out[RRC].v.i = -3; goto end; }
458
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)
462       rv->i ^= 1;
463     else if (rd->ty == &tvty_uint ||
464              rd->ty == &tvty_flags || rd->ty == &tvty_uenum)
465       rv->u ^= 1;
466     else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) {
467       if (rv->f == rv->f) rv->f = -rv->f;
468       else rv->f = 1.0;
469     } else if (rd->ty == &tvty_penum)
470       rv->p = rv->p
471         ? 0
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; }
477   }
478
479   out[RRC].v.i = 0;
480 end:
481   dbuf_destroy(&b);
482 }
483
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,
488   0
489 };
490 static const struct tvec_test multi_serialize_test =
491   { "multi", multi_serialize_regs, &multi_serialize_env,
492     test_multi_serialize };
493
494 /*----- Crash test --------------------------------------------------------*/
495
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 } },
500   TVEC_ENDREGS
501 };
502
503 static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
504                        void *ctx)
505 {
506   out[RVOUT].v.u = in[RV].v.u;
507   if (in[RSAB].v.i) abort();
508 }
509
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 };
514
515 /*----- Sleep test --------------------------------------------------------*/
516
517 static const struct tvec_regdef sleep_regs[] = {
518   { "time",     &tvty_duration, RV,     0,              { &tvflt_nonneg } },
519   { "z",        &tvty_float,    RVOUT,  0,              { &tvflt_nonneg } },
520   TVEC_ENDREGS
521 };
522
523 static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
524                        void *ctx)
525 {
526   struct timeval now, when, tv;
527   int rc;
528
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);
531
532   TV_ADD(&when, &now, &tv);
533   for (;;) {
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);
538   }
539   out[RVOUT].v.f = in[RV].v.f;
540 }
541
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 };
548
549 /*----- Front end ---------------------------------------------------------*/
550
551 static const struct tvec_test *const tests[] = {
552   &multi_serialize_test,
553   &sleep_test,
554   &crash_test,
555
556 #define DEFSINGLE(name, i, ty, argslot, argval)                         \
557   &copy_##name##_test, &serialize_##name##_test, &deserialize_##name##_test,
558   TYPEREGS(DEFSINGLE)
559 #undef DEFSINGLE
560
561   0
562 };
563
564 static const struct tvec_config testconfig = {
565   tests,
566   NROUT, NREG, sizeof(struct tvec_reg)
567 };
568
569 int main(int argc, char *argv[])
570 {
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;
577   TYPEREGS(POKE)
578 #  undef POKE
579 #endif
580   return (tvec_main(argc, argv, &testconfig, 0));
581 }
582
583 /*----- That's all, folks -------------------------------------------------*/