chiark / gitweb /
@@@ fltfmt mess
[mLib] / test / t / tvec-test.c
index 2f15779d5532aab15f6644354b66e63952945e79..a86f23022055453b3c77d103767a936601f8ebf0 100644 (file)
 
 /*----- Header files ------------------------------------------------------*/
 
-#include "tv.h"
 #include "tvec.h"
+#include "tvec-adhoc.h"
+#include "tvec-remote.h"
+#include "tvec-timeout.h"
+#include "tvec-types.h"
 
 #include <sys/select.h>
 #include <sys/time.h>
 
+#include "tv.h"
+
 /*----- Register definitions ----------------------------------------------*/
 
 static const struct tvec_iassoc ienum_assocs[] = {
@@ -63,7 +68,7 @@ static const struct tvec_passoc penum_assocs[] = {
   { 0 }
 };
 
-#if __STDC_VERSION__x >= 199901
+#if __STDC_VERSION__ >= 199901
 #  define DSGINIT(x) x
 #else
 #  define DSGINIT(x)
@@ -214,7 +219,7 @@ static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
 static void common_after(struct tvec_state *tv, void *ctx)
   { struct test_context *tctx = ctx; tctx->f = 0; }
 
-static const struct tvec_env common_testenv = {
+static const struct tvec_env common_env = {
   sizeof(struct test_context),
   common_setup, common_findvar,
   0, common_run, common_after,
@@ -223,6 +228,14 @@ static const struct tvec_env common_testenv = {
 
 /*----- Single-type copy tests --------------------------------------------*/
 
+#define COPYREG(name, i, ty, argslot, argval)                          \
+static DSGINIT(const) struct tvec_regdef name##_copyregs[] = {         \
+  { #name, &tvty_##ty, RVOUT, 0, DSGINIT({ .argslot = argval }) },     \
+  { 0 }                                                                        \
+};
+TYPEREGS(COPYREG)
+#undef COPYREG
+
 static void test_copy_simple
   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
   { out->v = in->v; }
@@ -258,16 +271,26 @@ static void test_copy_buffer
 #define test_copy_fltish test_copy_simple
 #define test_copy_dur test_copy_simple
 
-#define COPYREG(name, i, ty, argslot, argval)                          \
-       static DSGINIT(const) struct tvec_regdef name##_copyregs[] = {  \
-         { #name, &tvty_##ty, RVOUT, 0, DSGINIT({ .argslot = argval }) }, \
-         { 0 }                                                         \
-       };
-TYPEREGS(COPYREG)
-#undef COPYREG
+#define COPYTEST(name, i, ty, argslot, argval)                         \
+static const struct tvec_test copy_##name##_test =                     \
+  { "copy-" #name, name##_copyregs, &common_env, test_copy_##name };
+TYPEREGS(COPYTEST)
+#undef COPYTEST
 
 /*----- Single-type serialization tests -----------------------------------*/
 
+#define SERREG(name, i, ty, argslot, argval)                           \
+static DSGINIT(const) struct tvec_regdef name##_serregs[] = {          \
+  { #name,     &tvty_##ty,     RV,     0,                              \
+                                     DSGINIT({ .argslot = argval }) }, \
+  { "buf",     &tvty_bytes,    RSEROUT, 0 },                           \
+  { "rc",      &tvty_int,      RRC,    TVRF_OPT,                       \
+                                                   { &tvrange_int } }, \
+  TVEC_ENDREGS                                                         \
+};
+TYPEREGS(SERREG)
+#undef SERREG
+
 static void setup_regdef(struct tvec_regdef *rd, unsigned i,
                         struct tvec_state *tv)
 {
@@ -279,6 +302,14 @@ found:
   rd[0] = *r; rd[1].name = 0;
 }
 
+static void before_single_serialize(struct tvec_state *tv, void *ctx)
+{
+  if (!(tv->in[RRC].f&TVRF_LIVE)) {
+    tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
+    tv->out[RRC].f |= TVRF_LIVE;
+  }
+}
+
 static void test_single_serialize
   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
 {
@@ -289,8 +320,8 @@ static void test_single_serialize
   int rc;
 
   setup_regdef(rd, RV, tv);
-  rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd, NREG,
-                     sizeof(struct tvec_reg));
+  rc = tvec_serialize(tv->in, DBUF_BUF(&b), rd,
+                     0, 0, NREG, sizeof(struct tvec_reg));
   out[RRC].v.i = rc;
   if (rc)
     out[RSEROUT].f &= ~TVRF_LIVE;
@@ -300,6 +331,45 @@ static void test_single_serialize
   }
 }
 
+static const struct tvec_env single_serialize_env = {
+  sizeof(struct test_context),
+  common_setup, common_findvar,
+  before_single_serialize, common_run, common_after,
+  0
+};
+#define SERTEST(name, i, ty, argslot, argval)                          \
+static const struct tvec_test serialize_##name##_test =                        \
+  { "serialize-" #name, name##_serregs,                                        \
+    &single_serialize_env, test_single_serialize };
+TYPEREGS(SERTEST)
+#undef SERTEST
+
+#define DESERREG(name, i, ty, argslot, argval)                         \
+static DSGINIT(const) struct tvec_regdef name##_deserregs[] = {                \
+  { "buf",     &tvty_bytes,    RSER,   0 },                            \
+  { #name,     &tvty_##ty,     RVOUT,  0,                              \
+                                     DSGINIT({ .argslot = argval }) }, \
+  { "left",    &tvty_uint,     RLEFT,  TVRF_OPT,                       \
+                                                  { &tvrange_size } }, \
+  { "rc",      &tvty_int,      RRC,    TVRF_OPT,                       \
+                                                   { &tvrange_int } }, \
+  TVEC_ENDREGS                                                         \
+};
+TYPEREGS(DESERREG)
+#undef DESERREG
+
+static void before_single_deserialize(struct tvec_state *tv, void *ctx)
+{
+  if (!(tv->in[RRC].f&TVRF_LIVE)) {
+    tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
+    tv->out[RRC].f |= TVRF_LIVE;
+  }
+  if (!(tv->in[RLEFT].f&TVRF_LIVE)) {
+    tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
+    tv->out[RLEFT].f |= TVRF_LIVE;
+  }
+}
+
 static void test_single_deserialize
   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
 {
@@ -311,67 +381,58 @@ static void test_single_deserialize
 
   setup_regdef(rd, RV, tv);
   buf_init(&b, in[RSER].v.bytes.p, in[RSER].v.bytes.sz);
-  rc = tvec_deserialize(tv->out, &b, rd, NREG, sizeof(struct tvec_reg));
+  rc = tvec_deserialize(tv->out, &b, rd,
+                       0, 0, NREG, sizeof(struct tvec_reg));
   out[RRC].v.i = rc;
   if (rc) out[RVOUT].f &= ~TVRF_LIVE;
 }
 
-#define SERREG(name, i, ty, argslot, argval)                           \
-       static DSGINIT(const) struct tvec_regdef name##_serregs[] = {   \
-         { #name,      &tvty_##ty,     RV,     0,                      \
-                                     DSGINIT({ .argslot = argval }) }, \
-         { "buf",      &tvty_bytes,    RSEROUT, 0 },                   \
-         { "rc",       &tvty_int,      RRC,    TVRF_OPT,               \
-                                                   { &tvrange_int } }, \
-         TVEC_ENDREGS                                                  \
-       };                                                              \
-       static DSGINIT(const) struct tvec_regdef name##_deserregs[] = { \
-         { "buf",      &tvty_bytes,    RSER,   0 },                    \
-         { #name,      &tvty_##ty,     RVOUT,  0,                      \
-                                     DSGINIT({ .argslot = argval }) }, \
-         { "left",     &tvty_uint,     RLEFT,  TVRF_OPT,               \
-                                                  { &tvrange_size } }, \
-         { "rc",       &tvty_int,      RRC,    TVRF_OPT,               \
-                                                   { &tvrange_int } }, \
-         TVEC_ENDREGS                                                  \
-       };
-TYPEREGS(SERREG)
-#undef SERREG
+static const struct tvec_env single_deserialize_env = {
+  sizeof(struct test_context),
+  common_setup, common_findvar,
+  before_single_deserialize, common_run, common_after,
+  0
+};
+#define DESERTEST(name, i, ty, argslot, argval)                                \
+static const struct tvec_test deserialize_##name##_test =              \
+  { "deserialize-" #name, name##_deserregs,                            \
+    &single_deserialize_env, test_single_deserialize };
+TYPEREGS(DESERTEST)
+#undef DESERTEST
 
-static void before_single_serialize(struct tvec_state *tv, void *ctx)
-{
-  if (!(tv->in[RRC].f&TVRF_LIVE)) {
-    tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
-    tv->out[RRC].f |= TVRF_LIVE;
-  }
-}
+/*----- Multi-type serialization test -------------------------------------*/
 
-static void before_single_deserialize(struct tvec_state *tv, void *ctx)
+static const struct tvec_iassoc reg_assocs[] = {
+  { "none",            -1 },
+#define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
+  TYPEREGS(DEFASSOC)
+#undef DEFASSOC
+  { 0,                 0 }
+};
+static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
+
+static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
+#define DEFREG(name, i, ty, argslot, argval)                           \
+  { #name,     &tvty_##ty,     i,      TVRF_OPT,                       \
+                                     DSGINIT({ .argslot = argval }) },
+  TYPEREGS(DEFREG)
+#undef DEFREG
+
+  { "rc",      &tvty_int,      RRC,    TVRF_OPT,       { &tvrange_int } },
+  { "serialized", &tvty_bytes, RSEROUT, TVRF_OPT },
+  { "sabotage",        &tvty_ienum,    RSAB,   TVRF_OPT,       { &reg_enum } },
+
+  TVEC_ENDREGS
+};
+
+static void before_multi_serialize(struct tvec_state *tv, void *ctx)
 {
   if (!(tv->in[RRC].f&TVRF_LIVE)) {
     tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
     tv->out[RRC].f |= TVRF_LIVE;
   }
-  if (!(tv->in[RLEFT].f&TVRF_LIVE)) {
-    tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
-    tv->out[RLEFT].f |= TVRF_LIVE;
-  }
 }
 
-static const struct tvec_env single_serialize_testenv = {
-  sizeof(struct test_context),
-  common_setup, common_findvar,
-  before_single_serialize, common_run, common_after,
-  0
-}, single_deserialize_testenv = {
-  sizeof(struct test_context),
-  common_setup, common_findvar,
-  before_single_deserialize, common_run, common_after,
-  0
-};
-
-/*----- Multi-type serialization test -------------------------------------*/
-
 static void test_multi_serialize
   (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
 {
@@ -382,7 +443,7 @@ static void test_multi_serialize
   dbuf b = DBUF_INIT;
 
   if (tvec_serialize(tv->in, DBUF_BUF(&b), tv->test->regs,
-                    NTY, sizeof(struct tvec_reg)))
+                    0, 0, NTY, sizeof(struct tvec_reg)))
     { out[RRC].v.i = -1; goto end; }
   tvec_allocbytes(&out[RSEROUT].v, DBLEN(&b));
   memcpy(out[RSEROUT].v.bytes.p, DBBASE(&b), DBLEN(&b));
@@ -390,7 +451,7 @@ static void test_multi_serialize
   buf_flip(DBUF_BUF(&b));
 
   if (tvec_deserialize(tv->out, DBUF_BUF(&b), tv->test->regs,
-                      NTY, sizeof(struct tvec_reg)))
+                      0, 0, NTY, sizeof(struct tvec_reg)))
     { out[RRC].v.i = -2; goto end; }
   if (BLEFT(&b._b))
     { out[RRC].v.i = -3; goto end; }
@@ -408,8 +469,7 @@ static void test_multi_serialize
     } else if (rd->ty == &tvty_penum)
       rv->p = rv->p
        ? 0
-       : (/*unconst*/ void *)
-       ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p;
+       : UNCONST(void, ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p);
     else if (rd->ty == &tvty_text)
       { if (rv->text.sz) rv->text.p[0] ^= 1; }
     else if (rd->ty == &tvty_bytes)
@@ -421,46 +481,25 @@ end:
   dbuf_destroy(&b);
 }
 
-static const struct tvec_iassoc reg_assocs[] = {
-  { "none",            -1 },
-#define DEFASSOC(name, i, ty, argslot, argval) { #name, i },
-  TYPEREGS(DEFASSOC)
-#undef DEFASSOC
-  { 0,                 0 }
-};
-static const struct tvec_ienuminfo reg_enum = { "reg", reg_assocs, 0 };
-
-static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
-#define DEFREG(name, i, ty, argslot, argval)                           \
-  { #name,     &tvty_##ty,     i,      TVRF_OPT,                       \
-                                     DSGINIT({ .argslot = argval }) },
-  TYPEREGS(DEFREG)
-#undef DEFREG
-
-  { "rc",      &tvty_int,      RRC,    TVRF_OPT,       { &tvrange_int } },
-  { "serialized", &tvty_bytes, RSEROUT, TVRF_OPT },
-  { "sabotage",        &tvty_ienum,    RSAB,   TVRF_OPT,       { &reg_enum } },
-
-  TVEC_ENDREGS
-};
-
-static void before_multi_serialize(struct tvec_state *tv, void *ctx)
-{
-  if (!(tv->in[RRC].f&TVRF_LIVE)) {
-    tv->in[RRC].v.i = 0; tv->in[RRC].f |= TVRF_LIVE;
-    tv->out[RRC].f |= TVRF_LIVE;
-  }
-}
-
-static const struct tvec_env multi_serialize_testenv = {
+static const struct tvec_env multi_serialize_env = {
   sizeof(struct test_context),
   common_setup, common_findvar,
   before_multi_serialize, common_run, common_after,
   0
 };
+static const struct tvec_test multi_serialize_test =
+  { "multi", multi_serialize_regs, &multi_serialize_env,
+    test_multi_serialize };
 
 /*----- Crash test --------------------------------------------------------*/
 
+static const struct tvec_regdef crash_regs[] = {
+  { "crash",   &tvty_ienum,    RSAB,   0,              { &tvenum_bool } },
+  { "x",       &tvty_uint,     RV,     0,              { &tvrange_uint } },
+  { "z",       &tvty_uint,     RVOUT,  0,              { &tvrange_uint } },
+  TVEC_ENDREGS
+};
+
 static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
                       void *ctx)
 {
@@ -468,18 +507,19 @@ static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
   if (in[RSAB].v.i) abort();
 }
 
-static const struct tvec_remotefork crash_testenv =
+static const struct tvec_remotefork crash_env =
   { TVEC_REMOTEFORK(0, 0) };
+static const struct tvec_test crash_test =
+  { "crash",   crash_regs,     &crash_env._env, test_crash };
 
-static const struct tvec_regdef crash_regs[] = {
-  { "crash",   &tvty_ienum,    RSAB,   0,              { &tvenum_bool } },
-  { "x",       &tvty_uint,     RV,     0,              { &tvrange_uint } },
-  { "z",       &tvty_uint,     RVOUT,  0,              { &tvrange_uint } },
+/*----- Sleep test --------------------------------------------------------*/
+
+static const struct tvec_regdef sleep_regs[] = {
+  { "time",    &tvty_duration, RV,     0,              { &tvflt_nonneg } },
+  { "z",       &tvty_float,    RVOUT,  0,              { &tvflt_nonneg } },
   TVEC_ENDREGS
 };
 
-/*----- Sleep test --------------------------------------------------------*/
-
 static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
                       void *ctx)
 {
@@ -501,34 +541,24 @@ static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
 
 static const struct tvec_timeoutenv sleep_subenv =
   { TVEC_TIMEOUTINIT(ITIMER_REAL, 0.25) };
-static const struct tvec_remotefork sleep_testenv =
+static const struct tvec_remotefork sleep_env =
   { TVEC_REMOTEFORK(&sleep_subenv._env, 0) };
-
-static const struct tvec_regdef sleep_regs[] = {
-  { "time",    &tvty_duration, RV,     0,              { &tvflt_nonneg } },
-  { "z",       &tvty_float,    RVOUT,  0,              { &tvflt_nonneg } },
-  TVEC_ENDREGS
-};
+static const struct tvec_test sleep_test =
+  { "sleep",   sleep_regs,     &sleep_env._env, test_sleep };
 
 /*----- Front end ---------------------------------------------------------*/
 
-static const struct tvec_test tests[] = {
-  { "multi",   multi_serialize_regs, &multi_serialize_testenv,
-                                               test_multi_serialize },
+static const struct tvec_test *const tests[] = {
+  &multi_serialize_test,
+  &sleep_test,
+  &crash_test,
 
 #define DEFSINGLE(name, i, ty, argslot, argval)                                \
-  { "copy-" #name, name##_copyregs,    &common_testenv, test_copy_##name }, \
-  { "serialize-" #name, name##_serregs,        &single_serialize_testenv, \
-                                               test_single_serialize }, \
-  { "deserialize-" #name, name##_deserregs, &single_deserialize_testenv, \
-                                               test_single_deserialize },
+  &copy_##name##_test, &serialize_##name##_test, &deserialize_##name##_test,
   TYPEREGS(DEFSINGLE)
 #undef DEFSINGLE
 
-  { "crash",   crash_regs,     &crash_testenv._env, test_crash },
-  { "sleep",   sleep_regs,     &sleep_testenv._env, test_sleep },
-
-  TVEC_ENDTESTS
+  0
 };
 
 static const struct tvec_config testconfig = {
@@ -538,7 +568,7 @@ static const struct tvec_config testconfig = {
 
 int main(int argc, char *argv[])
 {
-#if __STDC_VERSION__x < 199901
+#if __STDC_VERSION__ < 199901
 #  define POKE(name, i, ty, argslot, argval)                           \
        multi_serialize_regs[i].arg.argslot = argval;                   \
        name##_copyregs->arg.argslot = argval;                          \