@@@ timeout wip
[mLib] / test / t / tvec-test.c
index a9ef269..ac316b6 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[] = {
@@ -173,8 +177,7 @@ static void common_setup(struct tvec_state *tv,
   tctx->f = 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;
@@ -442,7 +445,8 @@ static const struct tvec_env multi_serialize_testenv = {
 
 /*----- Crash test --------------------------------------------------------*/
 
-static void test_crash(const struct tvec_reg *in, struct tvec_reg *out, void *ctx)
+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();
@@ -458,6 +462,38 @@ static const struct tvec_regdef crash_regs[] = {
   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[] = {
@@ -473,7 +509,8 @@ static const struct tvec_test tests[] = {
   TYPEREGS(DEFSINGLE)
 #undef DEFSINGLE
 
-  { "crash",   crash_regs,     &crash_testenv._env, test_crash } ,
+  { "crash",   crash_regs,     &crash_testenv._env, test_crash },
+  { "sleep",   sleep_regs,     &sleep_testenv._env, test_sleep },
 
   TVEC_ENDTESTS
 };