chiark / gitweb /
@@@ tvec doc wip
[mLib] / test / t / tvec-test.c
index 6dd0ef0cca4bda9547fc72fafc7cf01558d5c634..aaa4465c6ecad53eba6efd310685669bad9abec6 100644 (file)
 
 /*----- Header files ------------------------------------------------------*/
 
+#include "tv.h"
 #include "tvec.h"
 
+#include <sys/select.h>
+#include <sys/time.h>
+
 /*----- Register definitions ----------------------------------------------*/
 
 static const struct tvec_iassoc ienum_assocs[] = {
@@ -127,7 +131,7 @@ static const struct tvec_urange range_32 = { 0, 31 };
   _(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)
 
@@ -164,18 +168,16 @@ struct test_context {
 #define SF_SHOW 1u
 };
 
-static int common_setup(struct tvec_state *tv,
+static void common_setup(struct tvec_state *tv,
                         const struct tvec_env *env, void *pctx, void *ctx)
 {
   struct test_context *tctx = ctx;
 
   tctx->tv = tv;
   tctx->f = 0;
-  return (0);
 }
 
-static int common_set(struct tvec_state *tv, const char *name,
-                     const struct tvec_env *env, void *ctx)
+static int common_set(struct tvec_state *tv, const char *name, void *ctx)
 {
   struct test_context *tctx = ctx;
   union tvec_regval rv;
@@ -219,18 +221,18 @@ 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);
 }
 
 #define test_copy_int test_copy_simple
@@ -320,16 +322,15 @@ static void test_single_deserialize
 TYPEREGS(SERREG)
 #undef SERREG
 
-static int before_single_serialize(struct tvec_state *tv, void *ctx)
+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;
   }
-  return (0);
 }
 
-static int before_single_deserialize(struct tvec_state *tv, void *ctx)
+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;
@@ -339,7 +340,6 @@ static int before_single_deserialize(struct tvec_state *tv, void *ctx)
     tv->in[RLEFT].v.u = 0; tv->in[RLEFT].f |= TVRF_LIVE;
     tv->out[RLEFT].f |= TVRF_LIVE;
   }
-  return (0);
 }
 
 static const struct tvec_env single_serialize_testenv = {
@@ -394,8 +394,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; }
   }
@@ -428,13 +428,12 @@ static DSGINIT(const) struct tvec_regdef multi_serialize_regs[] = {
   TVEC_ENDREGS
 };
 
-static int before_multi_serialize(struct tvec_state *tv, void *ctx)
+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;
   }
-  return (0);
 }
 
 static const struct tvec_env multi_serialize_testenv = {
@@ -444,6 +443,57 @@ static const struct tvec_env multi_serialize_testenv = {
   0
 };
 
+/*----- Crash test --------------------------------------------------------*/
+
+static void test_crash(const struct tvec_reg *in, struct tvec_reg *out,
+                      void *ctx)
+{
+  out[RVOUT].v.u = in[RV].v.u;
+  if (in[RSAB].v.i) abort();
+}
+
+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 } },
+  TVEC_ENDREGS
+};
+
+/*----- Sleep test --------------------------------------------------------*/
+
+static void test_sleep(const struct tvec_reg *in, struct tvec_reg *out,
+                      void *ctx)
+{
+  struct timeval now, when, tv;
+  int rc;
+
+  rc = gettimeofday(&now, 0); assert(!rc);
+  tv.tv_sec = in[RV].v.f; tv.tv_usec = 1e6*(in[RV].v.f - tv.tv_sec);
+
+  TV_ADD(&when, &now, &tv);
+  for (;;) {
+    rc = select(0, 0, 0, 0, &tv); assert(!rc);
+    rc = gettimeofday(&now, 0); assert(!rc);
+    if (TV_CMP(&now, >=, &when)) break;
+    TV_SUB(&tv, &when, &now);
+  }
+  out[RVOUT].v.f = in[RV].v.f;
+}
+
+static const struct tvec_timeoutenv sleep_subenv =
+  { TVEC_TIMEOUTINIT(ITIMER_REAL, 0.25) };
+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 } },
+  TVEC_ENDREGS
+};
+
 /*----- Front end ---------------------------------------------------------*/
 
 static const struct tvec_test tests[] = {
@@ -459,6 +509,9 @@ static const struct tvec_test tests[] = {
   TYPEREGS(DEFSINGLE)
 #undef DEFSINGLE
 
+  { "crash",   crash_regs,     &crash_testenv._env, test_crash },
+  { "sleep",   sleep_regs,     &sleep_testenv._env, test_sleep },
+
   TVEC_ENDTESTS
 };