chiark / gitweb /
cleanup: All the whitespace fixes, all at once.
[mLib] / testrig.c
index 01ffa8413dde9046bb3aa35e0e8b847bee0a782b..05fef07f377b17be56e2b3ab80dd8b3f7553cee6 100644 (file)
--- a/testrig.c
+++ b/testrig.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: testrig.c,v 1.4 1999/05/19 19:02:17 mdw Exp $
+ * $Id: testrig.c,v 1.10 2004/04/08 01:36:13 mdw Exp $
  *
  * Generic test driver
  *
  * (c) 1998 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the mLib utilities library.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * mLib is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * 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.4  1999/05/19 19:02:17  mdw
- * Aesthetic changes: fix spelling of `initialize'; use uppercase token
- * constants; abandon old double-underscore convention for internal
- * functions and variables.
- *
- * Revision 1.3  1999/05/06 19:51:35  mdw
- * Reformatted the LGPL notice a little bit.
- *
- * 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
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <ctype.h>
@@ -61,7 +42,7 @@
 
 /*----- Static variables --------------------------------------------------*/
 
-static dstr tok;
+static dstr tok = DSTR_INIT;
 
 enum {
   TOK_EOF = 0x100,
@@ -111,7 +92,7 @@ static int gettok(FILE *fp)
 
   /* --- Clear the token accumulator --- */
 
-  dstr_reset(&tok);
+  DRESET(&tok);
 
   /* --- Prime the lookahead character --- */
 
@@ -134,7 +115,7 @@ again:
       goto again;
 
     /* --- End of file --- */
-      
+
     case EOF:
       return (TOK_EOF);
 
@@ -161,29 +142,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);
   }
 }
 
@@ -213,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@ --- */
 
@@ -224,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);
 }
 
-test_type type_int = { cvt_int, dump_int };
+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);
+}
+
+const test_type type_uint32 = { cvt_uint32, dump_uint32 };
 
 /* --- @test_run@ --- *
  *
@@ -273,7 +312,7 @@ void test_run(int argc, char *argv[],
   ego(argv[0]);
 
   for (i = 0; i < TEST_FIELDMAX; i++)
-    dstr_create(&dv[i]);
+    DCREATE(&dv[i]);
 
   /* --- Parse command line arguments --- */
 
@@ -345,7 +384,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 --- */
 
@@ -365,7 +404,7 @@ void test_run(int argc, char *argv[],
       /* --- Otherwise I expect a list of words --- */
 
       for (i = 0; cch->f[i]; i++) {
-       dstr_reset(&dv[i]);
+       DRESET(&dv[i]);
        if (t != TOK_WORD)
          die(1, "expected <word>; found `%s'", decode(t));
        cch->f[i]->cvt(tok.buf, &dv[i]);
@@ -404,7 +443,7 @@ void test_run(int argc, char *argv[],
        t = gettok(fp);
       if (t != ';')
        die(1, "expected `;'; found `%s'", decode(t));
-    }      
+    }
   }
 
   exit(fail);