X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/31d0247cc58abc0b0720aa7e9972011c5a66995c..c4ccbbf992e1587ae316f66af6a28006780688d8:/test/t/tvec-test.c diff --git a/test/t/tvec-test.c b/test/t/tvec-test.c index ac316b6..2f15779 100644 --- a/test/t/tvec-test.c +++ b/test/t/tvec-test.c @@ -123,15 +123,17 @@ static const struct tvec_urange range_32 = { 0, 31 }; #define TYPEREGS(_) \ _(int, RI, int, p, &tvrange_i16) \ _(uint, RU, uint, p, &tvrange_u16) \ + _(size, RSZ, size, p, 0) \ _(float, RFP, float, p, 0) \ _(fltish, RFISH, float, p, &fltish_info) \ + _(dur, RDUR, duration, p, 0) \ _(char, RCH, char, p, 0) \ _(ienum, RIE, ienum, p, &ienum_info) \ _(uenum, RUE, uenum, p, &uenum_info) \ _(fenum, RFE, fenum, p, &fenum_info) \ _(penum, RPE, penum, p, &penum_info) \ _(flags, RF, flags, p, &attr_info) \ - _(string, RSTR, string, p, &range_32) \ + _(text, RTXT, text, p, &range_32) \ _(bytes, RBY, bytes, p, &tvrange_byte) \ _(buffer, RBUF, buffer, p, 0) @@ -177,22 +179,26 @@ static void common_setup(struct tvec_state *tv, tctx->f = 0; } -static int common_set(struct tvec_state *tv, const char *name, void *ctx) +static int common_setvar(struct tvec_state *tv, const char *var, + const union tvec_regval *rv, void *ctx) { struct test_context *tctx = ctx; - union tvec_regval rv; - static const struct tvec_regdef rd = - { "@show", -1, &tvty_ienum, 0, { &tvenum_bool } }; - - if (STRCMP(name, ==, "@show")) { - if (tvty_ienum.parse(&rv, &rd, tv)) return (-1); - if (tctx) { - if (rv.i) tctx->f |= SF_SHOW; - else tctx->f &= ~SF_SHOW; - } - return (1); - } else - return (0); + + if (STRCMP(var, ==, "@show")) { + if (rv->i) tctx->f |= SF_SHOW; + } else assert(!"unknown var"); + return (0); +} + +static const struct tvec_vardef show_var = + { sizeof(struct tvec_reg), common_setvar, + { "@show", &tvty_ienum, -1, 0, { &tvenum_bool } } }; + +static const struct tvec_vardef *common_findvar + (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx) +{ + if (STRCMP(var, ==, "@show")) { *ctx_out = ctx; return (&show_var); } + return (0); } static void common_run(struct tvec_state *tv, tvec_testfn *fn, void *ctx) @@ -210,7 +216,7 @@ static void common_after(struct tvec_state *tv, void *ctx) static const struct tvec_env common_testenv = { sizeof(struct test_context), - common_setup, common_set, + common_setup, common_findvar, 0, common_run, common_after, 0 }; @@ -221,22 +227,27 @@ static void test_copy_simple (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) { out->v = in->v; } -static void test_copy_string +static void test_copy_text (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) { - tvec_allocstring(&out->v, in->v.str.sz); - memcpy(out->v.str.p, in->v.str.p, in->v.str.sz); + tvec_alloctext(&out->v, in->v.text.sz); + memcpy(out->v.text.p, in->v.text.p, in->v.text.sz); } static void test_copy_bytes (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) { - tvec_allocstring(&out->v, in->v.str.sz); - memcpy(out->v.str.p, in->v.str.p, in->v.str.sz); + tvec_allocbytes(&out->v, in->v.bytes.sz); + memcpy(out->v.bytes.p, in->v.bytes.p, in->v.bytes.sz); } +static void test_copy_buffer + (const struct tvec_reg *in, struct tvec_reg *out, void *ctx) + { tvec_initbuffer(&out->v, &in->v, in->v.buf.sz); } + #define test_copy_int test_copy_simple #define test_copy_uint test_copy_simple +#define test_copy_size test_copy_simple #define test_copy_ienum test_copy_simple #define test_copy_uenum test_copy_simple #define test_copy_fenum test_copy_simple @@ -245,11 +256,11 @@ static void test_copy_bytes #define test_copy_flags test_copy_simple #define test_copy_float test_copy_simple #define test_copy_fltish test_copy_simple -#define test_copy_buffer test_copy_bytes +#define test_copy_dur test_copy_simple #define COPYREG(name, i, ty, argslot, argval) \ static DSGINIT(const) struct tvec_regdef name##_copyregs[] = { \ - { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ + { #name, &tvty_##ty, RVOUT, 0, DSGINIT({ .argslot = argval }) }, \ { 0 } \ }; TYPEREGS(COPYREG) @@ -307,16 +318,21 @@ static void test_single_deserialize #define SERREG(name, i, ty, argslot, argval) \ static DSGINIT(const) struct tvec_regdef name##_serregs[] = { \ - { #name, RV, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ - { "buf", RSEROUT, &tvty_bytes }, \ - { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \ + { #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", RSER, &tvty_bytes }, \ - { #name, RVOUT, &tvty_##ty, 0, DSGINIT({ .argslot = argval }) }, \ - { "left", RLEFT, &tvty_uint, TVRF_OPT, { &tvrange_size } }, \ - { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, \ + { "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) @@ -344,12 +360,12 @@ static void before_single_deserialize(struct tvec_state *tv, void *ctx) static const struct tvec_env single_serialize_testenv = { sizeof(struct test_context), - common_setup, common_set, + common_setup, common_findvar, before_single_serialize, common_run, common_after, 0 }, single_deserialize_testenv = { sizeof(struct test_context), - common_setup, common_set, + common_setup, common_findvar, before_single_deserialize, common_run, common_after, 0 }; @@ -394,8 +410,8 @@ static void test_multi_serialize ? 0 : (/*unconst*/ void *) ((const struct tvec_penuminfo *)rd->arg.p)->av[0].p; - else if (rd->ty == &tvty_string) - { if (rv->str.sz) rv->str.p[0] ^= 1; } + else if (rd->ty == &tvty_text) + { if (rv->text.sz) rv->text.p[0] ^= 1; } else if (rd->ty == &tvty_bytes) { if (rv->bytes.sz) rv->bytes.p[0] ^= 1; } } @@ -416,14 +432,14 @@ 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, i, &tvty_##ty, TVRF_OPT, \ - DSGINIT({ .argslot = argval }) }, + { #name, &tvty_##ty, i, TVRF_OPT, \ + DSGINIT({ .argslot = argval }) }, TYPEREGS(DEFREG) #undef DEFREG - { "rc", RRC, &tvty_int, TVRF_OPT, { &tvrange_int } }, - { "serialized", RSEROUT, &tvty_bytes, TVRF_OPT }, - { "sabotage", RSAB, &tvty_ienum, TVRF_OPT, { ®_enum } }, + { "rc", &tvty_int, RRC, TVRF_OPT, { &tvrange_int } }, + { "serialized", &tvty_bytes, RSEROUT, TVRF_OPT }, + { "sabotage", &tvty_ienum, RSAB, TVRF_OPT, { ®_enum } }, TVEC_ENDREGS }; @@ -438,7 +454,7 @@ static void before_multi_serialize(struct tvec_state *tv, void *ctx) static const struct tvec_env multi_serialize_testenv = { sizeof(struct test_context), - common_setup, common_set, + common_setup, common_findvar, before_multi_serialize, common_run, common_after, 0 }; @@ -456,9 +472,9 @@ static const struct tvec_remotefork crash_testenv = { TVEC_REMOTEFORK(0, 0) }; static const struct tvec_regdef crash_regs[] = { - { "crash", RSAB, &tvty_ienum, 0, { &tvenum_bool } }, - { "x", RV, &tvty_uint, 0, { &tvrange_uint } }, - { "z", RVOUT, &tvty_uint, 0, { &tvrange_uint } }, + { "crash", &tvty_ienum, RSAB, 0, { &tvenum_bool } }, + { "x", &tvty_uint, RV, 0, { &tvrange_uint } }, + { "z", &tvty_uint, RVOUT, 0, { &tvrange_uint } }, TVEC_ENDREGS }; @@ -489,8 +505,8 @@ static const struct tvec_remotefork sleep_testenv = { TVEC_REMOTEFORK(&sleep_subenv._env, 0) }; static const struct tvec_regdef sleep_regs[] = { - { "time", RV, &tvty_float, 0, { &tvflt_nonneg } }, - { "z", RVOUT, &tvty_float, 0, { &tvflt_nonneg } }, + { "time", &tvty_duration, RV, 0, { &tvflt_nonneg } }, + { "z", &tvty_float, RVOUT, 0, { &tvflt_nonneg } }, TVEC_ENDREGS };