chiark / gitweb /
aaa4465c6ecad53eba6efd310685669bad9abec6
[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   _(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)
137
138 enum {
139   /* Output registers, one for each register type. */
140 #define DEFREG(name, i, ty, argslot, argval) i,
141   TYPEREGS(DEFREG)
142 #undef DEFREG
143   NTY,
144
145   /* Outputs. */
146   RRC = NTY,                            /* return code from deserialize */
147   RSEROUT,                              /* serialized output */
148
149   NROUT,
150
151   /* Alternative outputs. */
152   RVOUT = 0,                            /* output/copy value */
153   RLEFT,                                /* size data remaining in input */
154
155   /* Additional inputs. */
156   RSAB = NROUT,                         /* which register to sabotage */
157   RV,                                   /* input value */
158   RSER,                                 /* serialized input */
159
160   NREG
161 };
162
163 /*----- Common execution environment --------------------------------------*/
164
165 struct test_context {
166   struct tvec_state *tv;
167   unsigned f;
168 #define SF_SHOW 1u
169 };
170
171 static void common_setup(struct tvec_state *tv,
172                          const struct tvec_env *env, void *pctx, void *ctx)
173 {
174   struct test_context *tctx = ctx;
175
176   tctx->tv = tv;
177   tctx->f = 0;
178 }
179
180 static int common_set(struct tvec_state *tv, const char *name, void *ctx)
181 {
182   struct test_context *tctx = ctx;
183   union tvec_regval rv;
184   static const struct tvec_regdef rd =
185     { "@show", -1, &tvty_ienum, 0, { &tvenum_bool } };
186
187   if (STRCMP(name, ==, "@show")) {
188     if (tvty_ienum.parse(&rv, &rd, tv)) return (-1);
189     if (tctx) {
190       if (rv.i) tctx->f |= SF_SHOW;
191       else tctx->f &= ~SF_SHOW;
192     }
193     return (1);
194   } else
195     return (0);
196 }
197
198 static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
199 {
200   struct test_context *tctx = ctx;
201   unsigned f = tctx->f;
202
203   fn(tv->in, tv->out, tctx);
204   if (tvec_checkregs(tv)) { tvec_fail(tv, 0); f |= SF_SHOW; }
205   if (f&SF_SHOW) tvec_mismatch(tv, TVMF_IN | TVMF_OUT);
206 }
207
208 static void common_after(struct tvec_state *tv, void *ctx)
209   { struct test_context *tctx = ctx; tctx->f = 0; }
210
211 static const struct tvec_env common_testenv = {
212   sizeof(struct test_context),
213   common_setup, common_set,
214   0, common_run, common_after,
215   0
216 };
217
218 /*----- Single-type copy tests --------------------------------------------*/
219
220 static void test_copy_simple
221   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
222   { out->v = in->v; }
223
224 static void test_copy_text
225   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
226 {
227   tvec_alloctext(&out->v, in->v.text.sz);
228   memcpy(out->v.text.p, in->v.text.p, in->v.text.sz);
229 }
230
231 static void test_copy_bytes
232   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
233 {
234   tvec_allocbytes(&out->v, in->v.bytes.sz);
235   memcpy(out->v.bytes.p, in->v.bytes.p, in->v.bytes.sz);
236 }
237
238 #define test_copy_int test_copy_simple
239 #define test_copy_uint test_copy_simple
240 #define test_copy_ienum test_copy_simple
241 #define test_copy_uenum test_copy_simple
242 #define test_copy_fenum test_copy_simple
243 #define test_copy_penum test_copy_simple
244 #define test_copy_char test_copy_simple
245 #define test_copy_flags test_copy_simple
246 #define test_copy_float test_copy_simple
247 #define test_copy_fltish test_copy_simple
248 #define test_copy_buffer test_copy_bytes
249
250 #define COPYREG(name, i, ty, argslot, argval)                           \
251         static DSGINIT(const) struct tvec_regdef name##_copyregs[] = {  \
252           { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
253           { 0 }                                                         \
254         };
255 TYPEREGS(COPYREG)
256 #undef COPYREG
257
258 /*----- Single-type serialization tests -----------------------------------*/
259
260 static void setup_regdef(struct tvec_regdef *rd, unsigned i,
261                          struct tvec_state *tv)
262 {
263   const struct tvec_regdef *r;
264
265   for (r = tv->test->regs; r->name; r++) if (r->i == i) goto found;
266   tvec_error(tv, "internel: register definition not found"); exit(2);
267 found:
268   rd[0] = *r; rd[1].name = 0;
269 }
270
271 static void test_single_serialize
272   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
273 {
274   struct test_context *tctx = ctx;
275   struct tvec_state *tv = tctx->tv;
276   struct tvec_regdef rd[2];
277   dbuf b = DBUF_INIT;
278   int rc;
279
280   setup_regdef(rd, RV, tv);
281   rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd, NREG,
282                       sizeof(struct tvec_reg));
283   out[RRC].v.i = rc;
284   if (rc)
285     out[RSEROUT].f &= ~TVRF_LIVE;
286   else {
287     tvec_allocbytes(&out[RSEROUT].v, BLEN(DBUF_BUF(&b)));
288     memcpy(out[RSEROUT].v.bytes.p, BBASE(DBUF_BUF(&b)), BLEN(DBUF_BUF(&b)));
289   }
290 }
291
292 static void test_single_deserialize
293   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
294 {
295   struct test_context *tctx = ctx;
296   struct tvec_state *tv = tctx->tv;
297   struct tvec_regdef rd[2];
298   buf b;
299   int rc;
300
301   setup_regdef(rd, RV, tv);
302   buf_init(&b, in[RSER].v.bytes.p, in[RSER].v.bytes.sz);
303   rc = tvec_deserialize(tv->out, &b, rd, NREG, sizeof(struct tvec_reg));
304   out[RRC].v.i = rc;
305   if (rc) out[RVOUT].f &= ~TVRF_LIVE;
306 }
307
308 #define SERREG(name, i, ty, argslot, argval)                            \
309         static DSGINIT(const) struct tvec_regdef name##_serregs[] = {   \
310           { #name, RV,  &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
311           { "buf", RSEROUT, &tvty_bytes },                              \
312           { "rc", RRC,  &tvty_int,      TVRF_OPT,       { &tvrange_int } }, \
313           TVEC_ENDREGS                                                  \
314         };                                                              \
315         static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \
316           { "buf", RSER, &tvty_bytes },                                 \
317           { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \
318           { "left", RLEFT, &tvty_uint,  TVRF_OPT,      { &tvrange_size } }, \
319           { "rc", RRC,  &tvty_int,      TVRF_OPT,       { &tvrange_int } }, \
320           TVEC_ENDREGS                                                  \
321         };
322 TYPEREGS(SERREG)
323 #undef SERREG
324
325 static void before_single_serialize(struct tvec_state *tv, void *ctx)
326 {
327   if (!(tv->in[RRC].f&TVRF_LIVE)) {
328     tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
329     tv->out[RRC].f |= TVRF_LIVE;
330   }
331 }
332
333 static void before_single_deserialize(struct tvec_state *tv, void *ctx)
334 {
335   if (!(tv->in[RRC].f&TVRF_LIVE)) {
336     tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
337     tv->out[RRC].f |= TVRF_LIVE;
338   }
339   if (!(tv->in[RLEFT].f&TVRF_LIVE)) {
340     tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
341     tv->out[RLEFT].f |= TVRF_LIVE;
342   }
343 }
344
345 static const struct tvec_env single_serialize_testenv = {
346   sizeof(struct test_context),
347   common_setup, common_set,
348   before_single_serialize, common_run, common_after,
349   0
350 }, single_deserialize_testenv = {
351   sizeof(struct test_context),
352   common_setup, common_set,
353   before_single_deserialize, common_run, common_after,
354   0
355 };
356
357 /*----- Multi-type serialization test -------------------------------------*/
358
359 static void test_multi_serialize
360   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
361 {
362   struct test_context *tctx = ctx;
363   struct tvec_state *tv = tctx->tv;
364   const struct tvec_regdef *rd;
365   union tvec_regval *rv;
366   dbuf b = DBUF_INIT;
367
368   if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs,
369                      NTY, sizeof(struct tvec_reg)))
370     { out[RRC].v.i = -1; goto end; }
371   tvec_allocbytes(&out[RSEROUT].v, DBLEN(&b));
372   memcpy(out[RSEROUT].v.bytes.p, DBBASE(&b), DBLEN(&b));
373   out[RSEROUT].f |= TVRF_LIVE;
374   buf_flip(DBUF_BUF(&b));
375
376   if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs,
377                        NTY, sizeof(struct tvec_reg)))
378     { out[RRC].v.i = -2; goto end; }
379   if (BLEFT(&b._b))
380     { out[RRC].v.i = -3; goto end; }
381
382   if ((in[RSAB].f&TVRF_LIVE) && in[RSAB].v.i >= 0) {
383     rd = &tv->test->regs[in[RSAB].v.i]; rv = &out[in[RSAB].v.i].v;
384     if (rd->ty == &tvty_int || rd->ty == &tvty_ienum)
385       rv->i ^= 1;
386     else if (rd->ty == &tvty_uint ||
387              rd->ty == &tvty_flags || rd->ty == &tvty_uenum)
388       rv->u ^= 1;
389     else if (rd->ty == &tvty_float || rd->ty == &tvty_fenum) {
390       if (rv->f == rv->f) rv->f = -rv->f;
391       else rv->f = 1.0;
392     } else if (rd->ty == &tvty_penum)
393       rv->p = rv->p
394         ? 0
395         : (/*unconst*/ void *)
396         ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p;
397     else if (rd->ty == &tvty_text)
398       { if (rv->text.sz) rv->text.p[0] ^= 1; }
399     else if (rd->ty == &tvty_bytes)
400       { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; }
401   }
402
403   out[RRC].v.i = 0;
404 end:
405   dbuf_destroy(&b);
406 }
407
408 static const struct tvec_iassoc reg_assocs[] = {
409   { "none",             -1 },
410 #define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
411   TYPEREGS(DEFASSOC)
412 #undef DEFASSOC
413   { 0,                  0 }
414 };
415 static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
416
417 static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
418 #define DEFREG(name, i, ty, argslot, argval)                            \
419   { #name,      i,      &tvty_##ty,     TVRF_OPT,                       \
420                                             DSGINIT({ .argslot = argval }) },
421   TYPEREGS(DEFREG)
422 #undef DEFREG
423
424   { "rc",       RRC,    &tvty_int,      TVRF_OPT,       { &tvrange_int } },
425   { "serialized", RSEROUT, &tvty_bytes, TVRF_OPT },
426   { "sabotage", RSAB,   &tvty_ienum,    TVRF_OPT,       { &reg_enum } },
427
428   TVEC_ENDREGS
429 };
430
431 static void before_multi_serialize(struct tvec_state *tv, void *ctx)
432 {
433   if (!(tv->in[RRC].f&TVRF_LIVE)) {
434     tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
435     tv->out[RRC].f |= TVRF_LIVE;
436   }
437 }
438
439 static const struct tvec_env multi_serialize_testenv = {
440   sizeof(struct test_context),
441   common_setup, common_set,
442   before_multi_serialize, common_run, common_after,
443   0
444 };
445
446 /*----- Crash test --------------------------------------------------------*/
447
448 static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
449                        void *ctx)
450 {
451   out[RVOUT].v.u = in[RV].v.u;
452   if (in[RSAB].v.i) abort();
453 }
454
455 static const struct tvec_remotefork crash_testenv =
456   { TVEC_REMOTEFORK(0, 0) };
457
458 static const struct tvec_regdef crash_regs[] = {
459   { "crash",    RSAB,   &tvty_ienum,    0,              { &tvenum_bool } },
460   { "x",        RV,     &tvty_uint,     0,              { &tvrange_uint } },
461   { "z",        RVOUT,  &tvty_uint,     0,              { &tvrange_uint } },
462   TVEC_ENDREGS
463 };
464
465 /*----- Sleep test --------------------------------------------------------*/
466
467 static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
468                        void *ctx)
469 {
470   struct timeval now, when, tv;
471   int rc;
472
473   rc = gettimeofday(&now, 0); assert(!rc);
474   tv.tv_sec = in[RV].v.f; tv.tv_usec = 1e6*(in[RV].v.f - tv.tv_sec);
475
476   TV_ADD(&when, &now, &tv);
477   for (;;) {
478     rc = select(0, 0, 0, 0, &tv); assert(!rc);
479     rc = gettimeofday(&now, 0); assert(!rc);
480     if (TV_CMP(&now, >=, &when)) break;
481     TV_SUB(&tv, &when, &now);
482   }
483   out[RVOUT].v.f = in[RV].v.f;
484 }
485
486 static const struct tvec_timeoutenv sleep_subenv =
487   { TVEC_TIMEOUTINIT(ITIMER_REAL, 0.25) };
488 static const struct tvec_remotefork sleep_testenv =
489   { TVEC_REMOTEFORK(&sleep_subenv._env, 0) };
490
491 static const struct tvec_regdef sleep_regs[] = {
492   { "time",     RV,     &tvty_float,    0,              { &tvflt_nonneg } },
493   { "z",        RVOUT,  &tvty_float,    0,              { &tvflt_nonneg } },
494   TVEC_ENDREGS
495 };
496
497 /*----- Front end ---------------------------------------------------------*/
498
499 static const struct tvec_test tests[] = {
500   { "multi",    multi_serialize_regs, &multi_serialize_testenv,
501                                                 test_multi_serialize },
502
503 #define DEFSINGLE(name, i, ty, argslot, argval)                         \
504   { "copy-" #name, name##_copyregs,     &common_testenv, test_copy_##name }, \
505   { "serialize-" #name, name##_serregs, &single_serialize_testenv, \
506                                                 test_single_serialize }, \
507   { "deserialize-" #name, name##_deserregs, &single_deserialize_testenv, \
508                                                 test_single_deserialize },
509   TYPEREGS(DEFSINGLE)
510 #undef DEFSINGLE
511
512   { "crash",    crash_regs,     &crash_testenv._env, test_crash },
513   { "sleep",    sleep_regs,     &sleep_testenv._env, test_sleep },
514
515   TVEC_ENDTESTS
516 };
517
518 static const struct tvec_config testconfig = {
519   tests,
520   NROUT, NREG, sizeof(struct tvec_reg)
521 };
522
523 int main(int argc, char *argv[])
524 {
525 #if __STDC_VERSION__x < 199901
526 #  define POKE(name, i, ty, argslot, argval)                            \
527         multi_serialize_regs[i].arg.argslot = argval;                   \
528         name##_copyregs->arg.argslot = argval;                          \
529         name##_serregs->arg.argslot = argval;                           \
530         name##_deserregs->arg.argslot = argval;
531   TYPEREGS(POKE)
532 #  undef POKE
533 #endif
534   return (tvec_main(argc, argv, &testconfig, 0));
535 }
536
537 /*----- That's all, folks -------------------------------------------------*/