+static void test_single_serialize
+ (const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
+{
+ struct test_context *tctx = ctx;
+ struct tvec_state *tv = tctx->tv;
+ struct tvec_regdef rd[2];
+ dbuf b = DBUF_INIT;
+ int rc;
+
+ setup_regdef(rd, RV, tv);
+ 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;
+ else {
+ tvec_allocbytes(&out[RSEROUT].v, BLEN(DBUF_BUF(&b)));
+ memcpy(out[RSEROUT].v.bytes.p, BBASE(DBUF_BUF(&b)), BLEN(DBUF_BUF(&b)));
+ }
+}
+
+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)
+{
+ struct test_context *tctx = ctx;
+ struct tvec_state *tv = tctx->tv;
+ struct tvec_regdef rd[2];
+ buf b;
+ int rc;
+
+ 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,
+ 0, 0, NREG, sizeof(struct tvec_reg));
+ out[RRC].v.i = rc;
+ if (rc) out[RVOUT].f &= ~TVRF_LIVE;
+}
+
+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
+
+/*----- Multi-type serialization test -------------------------------------*/
+
+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, { ®_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 void test_multi_serialize