chiark / gitweb /
buf: Fix two embarassing bugs found while writing Lisp bindings.
[mLib] / testrig.c
index 2f40b5bfe517f62e1b4ca6ffdb9759f185d2b94a..4b6980192d7785ec0c6728c3568ceadc8f1ea086 100644 (file)
--- a/testrig.c
+++ b/testrig.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: testrig.c,v 1.2 1999/05/05 18:50:31 mdw Exp $
+ * $Id: testrig.c,v 1.10 2004/04/08 01:36:13 mdw Exp $
  *
  * Generic test driver
  *
  * GNU Library General Public License for more details.
  * 
  * You should have received a copy of the GNU Library General Public
- * License along with mLib; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: testrig.c,v $
- * Revision 1.2  1999/05/05 18:50:31  mdw
- * Change licensing conditions to LGPL.
- *
- * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
- * Initial version of mLib
- *
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
  */
 
 /*----- Header files ------------------------------------------------------*/
 
 /*----- Static variables --------------------------------------------------*/
 
-static dstr test__tok;
+static dstr tok = DSTR_INIT;
 
 enum {
-  tok_eof = 0x100,
-  tok_word
+  TOK_EOF = 0x100,
+  TOK_WORD
 };
 
 /*----- Main code ---------------------------------------------------------*/
 
-/* --- @test__decode@ --- *
+/* --- @decode@ --- *
  *
  * Arguments:  @int tok@ = token type to decode
  *
@@ -70,24 +60,24 @@ enum {
  * Use:                Produces a readable representation of a token.
  */
 
-static const char *test__decode(int tok)
+static const char *decode(int t)
 {
   static char buf[4];
 
-  switch (tok) {
-    case tok_eof:
+  switch (t) {
+    case TOK_EOF:
       return ("<eof>");
-    case tok_word:
-      return (test__tok.buf);
+    case TOK_WORD:
+      return (tok.buf);
     default:
-      buf[0] = tok;
+      buf[0] = t;
       buf[1] = 0;
       return (buf);
   }
   return ("<buggy-program>");
 }
 
-/* --- @test__gettok@ --- *
+/* --- @gettok@ --- *
  *
  * Arguments:  @FILE *fp@ = file handle to read from
  *
@@ -96,13 +86,13 @@ static const char *test__decode(int tok)
  * Use:                Reads a token from the input stream.
  */
 
-static int test__gettok(FILE *fp)
+static int gettok(FILE *fp)
 {
   int ch;
 
   /* --- Clear the token accumulator --- */
 
-  dstr_reset(&test__tok);
+  DRESET(&tok);
 
   /* --- Prime the lookahead character --- */
 
@@ -127,7 +117,7 @@ again:
     /* --- End of file --- */
       
     case EOF:
-      return (tok_eof);
+      return (TOK_EOF);
 
     /* --- Quote characters --- */
 
@@ -146,35 +136,48 @@ again:
          if (ch == EOF)
            ch = '\\';
        }
-       DPUTC(&test__tok, ch);
+       DPUTC(&tok, ch);
       }
-      DPUTZ(&test__tok);
-      return (tok_word);
+      DPUTZ(&tok);
+      return (TOK_WORD);
     }
 
-    /* --- Anything else is either a word or self-delimiting --- */
+    /* --- Self-delimiting things --- */
+
+    case ';':
+    case '{':
+    case '}':
+      return (ch);
+
+    /* --- Anything else is a word --- */
 
     default:
-      if (isalnum((unsigned char)ch)) {
-       for (;;) {
-         DPUTC(&test__tok, ch);
+      for (;;) {
+       DPUTC(&tok, ch);
+       ch = getc(fp);
+       switch (ch) {
+         case EOF:
+         case ';':
+         case '{':
+         case '}':
+         case '\"':
+         case '\'':
+         case '`':
+           goto done;
+         default:
+           if (isspace((unsigned char)ch))
+             goto done;
+       }
+       if (ch == '\\') {
          ch = getc(fp);
-         if (ch == EOF ||
-             ch == ';' ||
-             ch == '\"' || ch == '\'' || ch == '`' ||
-             isspace((unsigned char)ch))
-           break;
-         if (ch == '\\') {
-           ch = getc(fp);
-           if (ch == EOF)
-             ch = '\\';
-         }
+         if (ch == EOF)
+           ch = '\\';
        }
-       ungetc(ch, fp);
-       DPUTZ(&test__tok);
-       return (tok_word);
-      } else
-       return (ch);
+      }
+    done:
+      ungetc(ch, fp);
+      DPUTZ(&tok);
+      return (TOK_WORD);
   }
 }
 
@@ -204,7 +207,7 @@ static void dump_hex(dstr *d, FILE *fp)
     fprintf(fp, "%02x", *(unsigned char *)p);
 }
 
-test_type type_hex = { cvt_hex, dump_hex };
+const test_type type_hex = { cvt_hex, dump_hex };
 
 /* --- @type_string@ --- */
 
@@ -215,25 +218,70 @@ static void cvt_string(const char *s, dstr *d)
 
 static void dump_string(dstr *d, FILE *fp)
 {
-  dstr_write(d, fp);
+  DWRITE(d, fp);
 }
 
-test_type type_string = { cvt_string, dump_string };
+const test_type type_string = { cvt_string, dump_string };
 
 /* --- @type_int@ --- */
 
 static void cvt_int(const char *s, dstr *d)
 {
-  DENSURE(d, sizeof(long));
-  sscanf(s, "%i", (int *)d->buf + d->len);
+  DENSURE(d, sizeof(int));
+  sscanf(s, "%i", (int *)d->buf);
 }
 
 static void dump_int(dstr *d, FILE *fp)
 {
-  fprintf(fp, "%i", *(int *)(d->buf));
+  fprintf(fp, "%i", *(int *)d->buf);
+}
+
+const test_type type_int = { cvt_int, dump_int };
+
+/* --- @type_long@ --- */
+
+static void cvt_long(const char *s, dstr *d)
+{
+  DENSURE(d, sizeof(long));
+  *(long *)d->buf = strtol(s, 0, 0);
+}
+
+static void dump_long(dstr *d, FILE *fp)
+{
+  fprintf(fp, "%li", *(long *)d->buf);
+}
+
+const test_type type_long = { cvt_long, dump_long };
+
+/* --- @type_ulong@ --- */
+
+static void cvt_ulong(const char *s, dstr *d)
+{
+  DENSURE(d, sizeof(unsigned long));
+  *(unsigned long *)d->buf = strtoul(s, 0, 0);
+}
+
+static void dump_ulong(dstr *d, FILE *fp)
+{
+  fprintf(fp, "%lu", *(unsigned long *)d->buf);
+}
+
+const test_type type_ulong = { cvt_ulong, dump_ulong };
+
+/* --- @type_uint32@ --- */
+
+static void cvt_uint32(const char *buf, dstr *d)
+{
+  DENSURE(d, sizeof(uint32));
+  *(uint32 *)d->buf = strtoul(buf, 0, 0);
+}
+
+static void dump_uint32(dstr *d, FILE *fp)
+{
+  fprintf(fp, "%lu\n", (unsigned long)*(uint32 *)d->buf);
 }
 
-test_type type_int = { cvt_int, dump_int };
+const test_type type_uint32 = { cvt_uint32, dump_uint32 };
 
 /* --- @test_run@ --- *
  *
@@ -259,12 +307,12 @@ void test_run(int argc, char *argv[],
   int fail = 0, ok = 1;
   int sofar = 0;
 
-  /* --- Silly bits of initialisation --- */
+  /* --- Silly bits of initialization --- */
 
   ego(argv[0]);
 
   for (i = 0; i < TEST_FIELDMAX; i++)
-    dstr_create(&dv[i]);
+    DCREATE(&dv[i]);
 
   /* --- Parse command line arguments --- */
 
@@ -312,31 +360,31 @@ void test_run(int argc, char *argv[],
     die(1, "couldn't open test vector file `%s': %s", vec, strerror(errno));
 
   for (;;) {
-    int tok = test__gettok(fp);
+    int t = gettok(fp);
 
     /* --- This is a reasonable place to stop --- */
 
-    if (tok == tok_eof)
+    if (t == TOK_EOF)
       break;
 
     /* --- Pick out the chunk name --- */
 
-    if (tok != tok_word)
-      die(1, "expected <word>; found `%s'", test__decode(tok));
+    if (t != TOK_WORD)
+      die(1, "expected <word>; found `%s'", decode(t));
 
     /* --- Find the right chunk block --- */
 
     for (cch = chunk; ; cch++) {
       if (!cch->name)
        goto skip_chunk;
-      if (strcmp(test__tok.buf, cch->name) == 0)
+      if (strcmp(tok.buf, cch->name) == 0)
        break;
     }
 
     /* --- Past the open brace to the first chunk --- */
 
-    if ((tok = test__gettok(fp)) != '{')
-      die(1, "expected '{'; found `%s'", test__decode(tok));
+    if ((t = gettok(fp)) != '{')
+      die(1, "expected `{'; found `%s'", decode(t));
 
     /* --- Start on the test data now --- */
 
@@ -346,27 +394,27 @@ void test_run(int argc, char *argv[],
     ok = 1;
 
     for (;;) {
-      tok = test__gettok(fp);
+      t = gettok(fp);
 
       /* --- Accept a close brace --- */
 
-      if (tok == '}')
+      if (t == '}')
        break;
 
       /* --- Otherwise I expect a list of words --- */
 
       for (i = 0; cch->f[i]; i++) {
-       dstr_reset(&dv[i]);
-       if (tok != tok_word)
-         die(1, "expected <word>; found `%s'", test__decode(tok));
-       cch->f[i]->cvt(test__tok.buf, &dv[i]);
-       tok = test__gettok(fp);
+       DRESET(&dv[i]);
+       if (t != TOK_WORD)
+         die(1, "expected <word>; found `%s'", decode(t));
+       cch->f[i]->cvt(tok.buf, &dv[i]);
+       t = gettok(fp);
       }
 
       /* --- And a terminating semicolon --- */
 
-      if (tok != ';')
-       die(1, "expected `;'; found `%s'", test__decode(tok));
+      if (t != ';')
+       die(1, "expected `;'; found `%s'", decode(t));
 
       /* --- Run the test code --- */
 
@@ -385,16 +433,16 @@ void test_run(int argc, char *argv[],
     continue;
 
   skip_chunk:
-    if ((tok = test__gettok(fp)) != '{')
-      die(1, "expected '{'; found `%s'", test__decode(tok));
+    if ((t = gettok(fp)) != '{')
+      die(1, "expected '{'; found `%s'", decode(t));
     for (;;) {
-      tok = test__gettok(fp);
-      if (tok == '}')
+      t = gettok(fp);
+      if (t == '}')
        break;
-      while (tok == tok_word)
-       tok = test__gettok(fp);
-      if (tok != ';')
-       die(1, "expected `;'; found `%s'", test__decode(tok));
+      while (t == TOK_WORD)
+       t = gettok(fp);
+      if (t != ';')
+       die(1, "expected `;'; found `%s'", decode(t));
     }      
   }