]> git.draconx.ca Git - cdecl99.git/commitdiff
Add an English output mode to randomdecl.
authorNick Bowler <nbowler@draconx.ca>
Sun, 18 Mar 2012 22:09:06 +0000 (18:09 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 18 Mar 2012 22:09:06 +0000 (18:09 -0400)
test/randomdecl.c
test/test.h
test/testlib.c

index 0d53af32c2fe4fb293f2c6ed7686668d13771525..8e404484648f56c6a0e8853e625ca5eca63eddef 100644 (file)
 
 #define PROGNAME "randomdecl"
 static const char *progname = PROGNAME;
-static const char sopts[] = "s:n:VH";
+static const char sopts[] = "s:n:ECVH";
 static const struct option lopts[] = {
        { "seed",    1, NULL, 's' },
        { "count",   1, NULL, 'n' },
+       { "cdecl",   0, NULL, 'C' },
+       { "english", 0, NULL, 'E' },
        { "version", 0, NULL, 'V' },
        { "help",    0, NULL, 'H' },
        { 0 }
 };
 
+enum {
+       MODE_CDECL,
+       MODE_ENGLISH,
+};
+
 static void print_usage(FILE *f)
 {
        fprintf(f, "Usage: %s [options]\n", progname);
@@ -78,7 +85,7 @@ int main(int argc, char **argv)
        struct gen_rng *rng;
        struct cdecl *decl;
        unsigned long count = 0;
-       int opt;
+       int opt, mode = MODE_CDECL;
 
        if (argc > 0)
                progname = argv[0];
@@ -91,6 +98,12 @@ int main(int argc, char **argv)
                case 'n':
                        count_str = optarg;
                        break;
+               case 'C':
+                       mode = MODE_CDECL;
+                       break;
+               case 'E':
+                       mode = MODE_ENGLISH;
+                       break;
                case 'V':
                        test_print_version(PROGNAME);
                        return EXIT_SUCCESS;
@@ -114,7 +127,12 @@ int main(int argc, char **argv)
 
        for (unsigned long i = 0; !count || i < count; i++) {
                decl = random_decl(rng);
-               test_print_decl(decl);
+
+               if (mode == MODE_ENGLISH) {
+                       test_explain_decl(decl);
+               } else {
+                       test_print_decl(decl);
+               }
 
                gen_free_declspecs(decl->specifiers);
                gen_free_declarators(decl->declarators);
index 7cf89f1d7d2ec92b95c0cc771c0bc300cd4f71ee..b8f7ee39273ddaca8ca72abdd7efb8c1856f47a2 100644 (file)
@@ -11,6 +11,7 @@ void *malloc_nofail(size_t size);
 void *realloc_nofail(void *ptr, size_t size);
 void test_print_specifiers(struct cdecl_declspec *spec);
 void test_print_decl(struct cdecl *decl);
+void test_explain_decl(struct cdecl *decl);
 
 bool strict_strtoul(unsigned long *val, const char *str, int base);
 
index 3c8bb8c03fc4f06333bbc0dab825e59a47c51891..2c859a9e79a8d3ed9d6eeac9bdb02c3b929f8702 100644 (file)
@@ -23,6 +23,9 @@
 #include <cdecl.h>
 #include "test.h"
 
+static size_t printbuf_size;
+static char *printbuf;
+
 void *realloc_nofail(void *ptr, size_t size)
 {
        void *p;
@@ -43,19 +46,32 @@ void *malloc_nofail(size_t size)
 
 void test_print_decl(struct cdecl *decl)
 {
-       static size_t bufsz;
-       static char *buf;
        size_t rc;
 
 retry:
-       rc = cdecl_declare(buf, bufsz, decl);
-       if (rc >= bufsz) {
-               bufsz = rc + 1;
-               buf = realloc_nofail(buf, bufsz);
+       rc = cdecl_declare(printbuf, printbuf_size, decl);
+       if (rc >= printbuf_size) {
+               printbuf_size = rc + 1;
+               printbuf = realloc_nofail(printbuf, printbuf_size);
+               goto retry;
+       }
+
+       printf("%s\n", printbuf);
+}
+
+void test_explain_decl(struct cdecl *decl)
+{
+       size_t rc;
+
+retry:
+       rc = cdecl_explain(printbuf, printbuf_size, decl);
+       if (rc >= printbuf_size) {
+               printbuf_size = rc + 1;
+               printbuf = realloc_nofail(printbuf, printbuf_size);
                goto retry;
        }
 
-       printf("%s\n", buf);
+       printf("%s\n", printbuf);
 }
 
 bool strict_strtoul(unsigned long *val, const char *str, int base)