From: Nick Bowler Date: Sun, 18 Mar 2012 22:09:06 +0000 (-0400) Subject: Add an English output mode to randomdecl. X-Git-Tag: v1~52 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/b8b1c8789189cf94ec1e1a15623531ebecc3eb85 Add an English output mode to randomdecl. --- diff --git a/test/randomdecl.c b/test/randomdecl.c index 0d53af3..8e40448 100644 --- a/test/randomdecl.c +++ b/test/randomdecl.c @@ -31,15 +31,22 @@ #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); diff --git a/test/test.h b/test/test.h index 7cf89f1..b8f7ee3 100644 --- a/test/test.h +++ b/test/test.h @@ -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); diff --git a/test/testlib.c b/test/testlib.c index 3c8bb8c..2c859a9 100644 --- a/test/testlib.c +++ b/test/testlib.c @@ -23,6 +23,9 @@ #include #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)