]> git.draconx.ca Git - cdecl99.git/blobdiff - t/randomdecl.c
tests: Remove some unneeded C99 syntax.
[cdecl99.git] / t / randomdecl.c
index 9e9271e5b0ad12303a44dd3159641a090fc98b6f..f4b00bc622c34a5f52acbce480f32ef0bdc37a28 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Generate random C declarations for testing.
- * Copyright © 2012, 2020, 2022 Nick Bowler
+ * Copyright © 2012, 2020, 2022-2024 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <stdbool.h>
 #include <errno.h>
 #include <getopt.h>
-#include <cdecl.h>
 
+#include "cdecl.h"
 #include "declgen.h"
-#include "test.h"
 
 #define PROGNAME "randomdecl"
-static const char *progname = PROGNAME;
+#include "test.h"
+
 static const char sopts[] = "s:n:ECVH";
 static const struct option lopts[] = {
        { "seed",    1, NULL, 's' },
@@ -44,7 +44,7 @@ static const struct option lopts[] = {
 
 enum {
        MODE_CDECL,
-       MODE_ENGLISH,
+       MODE_ENGLISH
 };
 
 static void print_usage(FILE *f)
@@ -67,7 +67,7 @@ static struct cdecl *random_decl(struct test_rng *rng)
        unsigned flags = 0;
 
        decl = malloc_nofail(sizeof *decl);
-       *decl = (struct cdecl) { 0 };
+       decl->next = NULL;
 
        decl->declarators = gen_declarators(rng);
        if (decl->declarators->type != CDECL_DECL_FUNCTION)
@@ -82,13 +82,30 @@ static struct cdecl *random_decl(struct test_rng *rng)
        return decl;
 }
 
+static void
+print_decl(struct cdecl *decl, size_t func(char *, size_t, struct cdecl *))
+{
+       static size_t printbuf_size;
+       static char *printbuf;
+       size_t rc;
+retry:
+       rc = func(printbuf, printbuf_size, decl);
+       if (rc >= printbuf_size) {
+               printbuf_size = rc + 1;
+               printbuf = realloc_nofail(printbuf, printbuf_size);
+               goto retry;
+       }
+
+       printf("%s\n", printbuf);
+}
+
 int main(int argc, char **argv)
 {
        const char *seed = "", *count_str = NULL;
+       unsigned long i, count = 0;
+       int opt, mode = MODE_CDECL;
        struct test_rng *rng;
        struct cdecl *decl;
-       unsigned long count = 0;
-       int opt, mode = MODE_CDECL;
 
        if (argc > 0)
                progname = argv[0];
@@ -119,8 +136,8 @@ int main(int argc, char **argv)
                }
        }
 
-       if (count_str && !strict_strtoul(&count, count_str, 0)) {
-               fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
+       if (count_str && !test_strtoul(&count, count_str)) {
+               print_error("invalid count: %s", count_str);
                return EXIT_FAILURE;
        }
 
@@ -128,13 +145,13 @@ int main(int argc, char **argv)
        if (!rng)
                return EXIT_FAILURE;
 
-       for (unsigned long i = 0; !count || i < count; i++) {
+       for (i = 0; !count || i < count; i++) {
                decl = random_decl(rng);
 
                if (mode == MODE_ENGLISH) {
-                       test_explain_decl(decl);
+                       print_decl(decl, cdecl_explain);
                } else {
-                       test_print_decl(decl);
+                       print_decl(decl, cdecl_declare);
                }
 
                gen_free_declspecs(decl->specifiers);