chiark / gitweb /
Fix description of CRCs.
[mLib] / testrig.c
index 95aca952591bedd959cbe37494a4878b24e3e2c5..5ac5414cf80b2d618a37bb1a751da39e090be3b3 100644 (file)
--- a/testrig.c
+++ b/testrig.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: testrig.c,v 1.5 1999/05/21 22:14:30 mdw Exp $
+ * $Id: testrig.c,v 1.9 2000/06/17 10:35:07 mdw Exp $
  *
  * Generic test driver
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: testrig.c,v $
+ * Revision 1.9  2000/06/17 10:35:07  mdw
+ * Initialize the token buffer properly.
+ *
+ * Revision 1.8  1999/12/10 23:41:37  mdw
+ * Support for different sizes and types of integers.
+ *
+ * Revision 1.7  1999/11/21 13:01:39  mdw
+ * Allow more characters to start words in test vector files.
+ *
+ * Revision 1.6  1999/11/16 15:03:23  mdw
+ * Mark test types as constant.
+ *
  * Revision 1.5  1999/05/21 22:14:30  mdw
  * Take advantage of the new dynamic string macros.
  *
@@ -64,7 +76,7 @@
 
 /*----- Static variables --------------------------------------------------*/
 
-static dstr tok;
+static dstr tok = DSTR_INIT;
 
 enum {
   TOK_EOF = 0x100,
@@ -164,29 +176,42 @@ again:
       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(&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(&tok);
-       return (TOK_WORD);
-      } else
-       return (ch);
+      }
+    done:
+      ungetc(ch, fp);
+      DPUTZ(&tok);
+      return (TOK_WORD);
   }
 }
 
@@ -216,7 +241,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@ --- */
 
@@ -230,22 +255,67 @@ static void dump_string(dstr *d, FILE *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);
 }
 
-test_type type_int = { cvt_int, dump_int };
+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);
+}
+
+const test_type type_uint32 = { cvt_uint32, dump_uint32 };
 
 /* --- @test_run@ --- *
  *
@@ -348,7 +418,7 @@ void test_run(int argc, char *argv[],
     /* --- Past the open brace to the first chunk --- */
 
     if ((t = gettok(fp)) != '{')
-      die(1, "expected '{'; found `%s'", decode(t));
+      die(1, "expected `{'; found `%s'", decode(t));
 
     /* --- Start on the test data now --- */