@@@ tvec setvar
[mLib] / test / tvec-core.c
index 65c74cf..51b46d2 100644 (file)
@@ -317,6 +317,23 @@ int tvec_syntax_v(struct tvec_state *tv, int ch,
   dstr_destroy(&d); return (-1);
 }
 
+/* --- @tvec_unkreg@ --- *
+ *
+ * Arguments:  @struct tvec_state *tv@ = test-vector state
+ *             @const char *name@ = register or pseudoregister name
+ *
+ * Returns:    %$-1$%.
+ *
+ * Use:                Reports an error that the register or pseudoregister is
+ *             unrecognized.
+ */
+
+int tvec_unkreg(struct tvec_state *tv, const char *name)
+{
+  return (tvec_error(tv, "unknown special register `%s' for test `%s'",
+                    name, tv->test->name));
+}
+
 /* --- @tvec_dupreg@ --- *
  *
  * Arguments:  @struct tvec_state *tv@ = test-vector state
@@ -513,8 +530,11 @@ int tvec_readword_v(struct tvec_state *tv, dstr *d, const char *delims,
 
 struct groupstate {
   void *ctx;                           /* test environment context */
+  unsigned f;                          /* flags */
+#define GRPF_SETOUTC 1u                        /*   set outcome */
+#define GRPF_SETMASK (GRPF_SETOUTC)    /*   mask of all variable flags */
 };
-#define GROUPSTATE_INIT { 0 }
+#define GROUPSTATE_INIT { 0, 0 }
 
 /* --- @tvec_initregs@, @tvec_releaseregs@ --- *
  *
@@ -746,6 +766,8 @@ static void check(struct tvec_state *tv, struct groupstate *g)
   const struct tvec_test *t = tv->test;
   const struct tvec_env *env = t->env;
   const struct tvec_regdef *rd;
+  unsigned f = 0;
+#define f_err 1u
 
   if (!(tv->f&TVSF_OPEN)) return;
 
@@ -755,15 +777,16 @@ static void check(struct tvec_state *tv, struct groupstate *g)
     else if (!(rd->f&TVRF_OPT)) {
       tvec_error(tv, "required register `%s' not set in test `%s'",
                 rd->name, t->name);
-      goto end;
+      f |= f_err;
     }
   }
 
   if (!(tv->f&TVSF_SKIP)) {
     begin_test(tv);
+    if (f&f_err) tvec_skip(tv, "erroneous test data");
     if (env && env->before) env->before(tv, g->ctx);
     if (!(tv->f&TVSF_ACTIVE))
-      /* setup forced a skip */;
+      /* forced a skip */;
     else if (env && env->run)
       env->run(tv, t->fn, g->ctx);
     else {
@@ -772,10 +795,12 @@ static void check(struct tvec_state *tv, struct groupstate *g)
     }
     tvec_endtest(tv);
   }
-  if (env && env->after) env->after(tv, g->ctx);
 
-end:
+  if (env && env->after) env->after(tv, g->ctx);
+  g->f &= ~GRPF_SETMASK;
   tv->f &= ~TVSF_OPEN; tvec_releaseregs(tv); tvec_initregs(tv);
+
+#undef f_err
 }
 
 /* --- @begin_test_group@ --- *
@@ -864,21 +889,44 @@ static void end_test_group(struct tvec_state *tv, struct groupstate *g)
   tvec_releaseregs(tv); tv->test = 0; xfree(g->ctx); g->ctx = 0;
 }
 
-/* --- @tvec_read@ --- *
+/* --- @core_findvar@, @core_setvar@ --- *
  *
- * Arguments:  @struct tvec_state *tv@ = test-vector state
- *             @const char *infile@ = the name of the input file
- *             @FILE *fp@ = stream to read from
+ * Arguments:  @struct tvec_state *tv@ = test vector state
+ *             @const char *var@ = variable name to set
+ *             @const union tvec_regval *rv@ = register value
+ *             @void **ctx_out@ = where to put the @setvar@ context
+ *             @void *ctx@ = context pointer
  *
- * Returns:    Zero on success, @-1@ on error.
+ * Returns:    @core_findvar@ returns a pointer to the variable definition,
+ *             or null; @core_setvar@ returns zero on success or %$-1$% on
+ *             error.
  *
- * Use:                Read test vector data from @fp@ and exercise test functions.
- *             THe return code doesn't indicate test failures: it's only
- *             concerned with whether there were problems with the input
- *             file or with actually running the tests.
+ * Use:                Find a definition for a special variable.  The following
+ *             special variables are supported.
+ *
+ *               * %|@outcome|% is a token describing how a successful
+ *                 outcome of the test should be interpreted: %|success|% or
+ *                 %|win|% are the default: a successful test is counted as
+ *                 a pass; or %|expected-failure|% or %|xfail|% means a
+ *                 successful test is counted as an expected failure.  A
+ *                 mismatch is always considered a failure.
  */
 
 enum { WIN, XFAIL, NOUT };
+
+static int core_setvar(struct tvec_state *tv, const char *name,
+                      const union tvec_regval *rv, void *ctx)
+{
+  struct groupstate *g = ctx;
+
+  if (STRCMP(name, ==, "@outcome")) {
+    if (g->f&GRPF_SETOUTC) return (tvec_dupreg(tv, name));
+    if (rv->u == XFAIL) tvec_xfail(tv);
+    g->f |= GRPF_SETOUTC;
+  } else assert(!"unknown var");
+  return (0);
+}
+
 static const struct tvec_uassoc outcome_assoc[] = {
   { "success",         WIN },
   { "win",             WIN },
@@ -889,8 +937,32 @@ static const struct tvec_uassoc outcome_assoc[] = {
 static const struct tvec_urange outcome_range = { 0, NOUT - 1 };
 static const struct tvec_uenuminfo outcome_enum =
   { "test-outcome", outcome_assoc, &outcome_range };
-static const struct tvec_regdef outcome_regdef =
-  { "outcome", 0, &tvty_uenum, 0, { &outcome_enum } };
+static const struct tvec_vardef outcome_vardef =
+  { sizeof(struct tvec_reg), core_setvar,
+    { "@outcome", 0, &tvty_uenum, 0, { &outcome_enum } } };
+
+static const struct tvec_vardef *core_findvar
+  (struct tvec_state *tv, const char *name, void **ctx_out, void *ctx)
+{
+  if (STRCMP(name, ==, "@outcome"))
+    { *ctx_out = ctx; return (&outcome_vardef); }
+  else
+    return (0);
+}
+
+/* --- @tvec_read@ --- *
+ *
+ * Arguments:  @struct tvec_state *tv@ = test-vector state
+ *             @const char *infile@ = the name of the input file
+ *             @FILE *fp@ = stream to read from
+ *
+ * Returns:    Zero on success, @-1@ on error.
+ *
+ * Use:                Read test vector data from @fp@ and exercise test functions.
+ *             THe return code doesn't indicate test failures: it's only
+ *             concerned with whether there were problems with the input
+ *             file or with actually running the tests.
+ */
 
 int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
 {
@@ -898,10 +970,10 @@ int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
   const struct tvec_test *test;
   const struct tvec_env *env;
   const struct tvec_regdef *rd;
-  struct tvec_reg *r;
+  const struct tvec_vardef *vd = 0; void *varctx;
+  struct tvec_reg *r = 0, rbuf, *r_alloc = 0; size_t rsz = 0;
   struct groupstate g = GROUPSTATE_INIT;
-  union tvec_regval rv;
-  int ch, ret, rc = 0;
+  int ch, rc = 0;
 
   /* Set the initial location. */
   tv->infile = infile; tv->lno = 1; tv->fp = fp;
@@ -1014,34 +1086,40 @@ int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
 
          if (d.buf[0] == '@') {
            /* A special register assignment.  */
-           env = tv->test->env;
-
-           /* See if it's one of the core settings. */
-           if (STRCMP(d.buf, ==, "@outcome")) {
 
-             /* Parse the value. */
-             if (tvty_uenum.parse(&rv, &outcome_regdef, tv))
-               ret = -1;
-             else {
+           env = tv->test->env;
 
-               /* Act on the result. */
-               if (rv.u == XFAIL) tvec_xfail(tv);
-               ret = 1;
+           /* Find a variable definition. */
+           vd = core_findvar(tv, d.buf, &varctx, &g);
+             if (vd) goto found_var;
+           if (env && env->findvar) {
+             vd = env->findvar(tv, d.buf, &varctx, g.ctx);
+               if (vd) goto found_var;
+           }
+           tvec_unkreg(tv, d.buf); goto flush_line;
+         found_var:
+
+           /* Set up the register. */
+           if (vd->regsz <= sizeof(rbuf))
+             r = &rbuf;
+           else {
+             if (rsz < vd->regsz) {
+               xfree(r_alloc);
+               if (!rsz) rsz = 8*sizeof(void *);
+               while (rsz < vd->regsz) rsz *= 2;
+               r_alloc = xmalloc(rsz);
              }
+             r = r_alloc;
            }
 
-           /* If there's no environment, this is an unknown setting. */
-           else if (!env || !env->set) ret = 0;
+           /* Read and set the value. */
+           vd->def.ty->init(&r->v, &vd->def);
+           if (vd->def.ty->parse(&r->v, &vd->def, tv)) goto flush_line;
+           if (!(tv->f&TVSF_SKIP) && vd->setvar(tv, d.buf, &r->v, varctx))
+             goto bad;
 
-           /* Otherwise pass the setting on to the environment. */
-           else ret = env->set(tv, d.buf, g.ctx);
-
-           /* If it wasn't understood, report an error and flush. */
-           if (ret <= 0) {
-             if (!ret)
-               tvec_error(tv, "unknown special register `%s'", d.buf);
-             goto flush_line;
-           }
+           /* Clean up. */
+           vd->def.ty->release(&r->v, &vd->def); vd = 0;
          } else {
            /* A standard register. */
 
@@ -1071,7 +1149,10 @@ int tvec_read(struct tvec_state *tv, const char *infile, FILE *fp)
     /* This is a general parse-failure handler.  Skip to the next line and
      * remember that things didn't go so well.
      */
-    tvec_flushtoeol(tv, TVFF_ALLOWANY); rc = -1;
+    tvec_flushtoeol(tv, TVFF_ALLOWANY);
+  bad:
+    if (vd) { vd->def.ty->release(&r->v, &vd->def); vd = 0; }
+    rc = -1;
   }
 
   /* We reached the end.  If that was actually an I/O error then report it.
@@ -1088,7 +1169,10 @@ end:
   /* Clean up. */
   tv->infile = 0; tv->fp = 0;
   dstr_destroy(&d);
+  xfree(r_alloc);
   return (rc);
+
+#undef rlive
 }
 
 /*----- Session lifecycle -------------------------------------------------*/