]> git.draconx.ca Git - cdecl99.git/blob - test/randomdecl.c
8e404484648f56c6a0e8853e625ca5eca63eddef
[cdecl99.git] / test / randomdecl.c
1 /*
2  *  Generate random C declarations for testing.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <stdbool.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <cdecl.h>
28
29 #include "declgen.h"
30 #include "test.h"
31
32 #define PROGNAME "randomdecl"
33 static const char *progname = PROGNAME;
34 static const char sopts[] = "s:n:ECVH";
35 static const struct option lopts[] = {
36         { "seed",    1, NULL, 's' },
37         { "count",   1, NULL, 'n' },
38         { "cdecl",   0, NULL, 'C' },
39         { "english", 0, NULL, 'E' },
40         { "version", 0, NULL, 'V' },
41         { "help",    0, NULL, 'H' },
42         { 0 }
43 };
44
45 enum {
46         MODE_CDECL,
47         MODE_ENGLISH,
48 };
49
50 static void print_usage(FILE *f)
51 {
52         fprintf(f, "Usage: %s [options]\n", progname);
53 }
54
55 static void print_help(void)
56 {
57         print_usage(stdout);
58         puts("Detailed help coming soon.");
59 }
60
61 static struct cdecl *random_decl(struct gen_rng *rng)
62 {
63         struct cdecl *decl;
64         unsigned flags = 0;
65
66         decl = malloc_nofail(sizeof *decl);
67         *decl = (struct cdecl) { 0 };
68
69         decl->declarators = gen_declarators(rng);
70         if (decl->declarators->type != CDECL_DECL_FUNCTION)
71                 flags |= GEN_NO_FUNCTION;
72         if (cdecl_is_abstract(decl->declarators))
73                 flags |= GEN_NO_STORAGE | GEN_NO_FUNCTION;
74         if (decl->declarators->type == CDECL_DECL_ARRAY
75             || decl->declarators->type == CDECL_DECL_IDENT)
76                 flags |= GEN_NO_VOID;
77
78         decl->specifiers = gen_declspecs(rng, flags);
79         return decl;
80 }
81
82 int main(int argc, char **argv)
83 {
84         const char *seed = "", *count_str = NULL;
85         struct gen_rng *rng;
86         struct cdecl *decl;
87         unsigned long count = 0;
88         int opt, mode = MODE_CDECL;
89
90         if (argc > 0)
91                 progname = argv[0];
92
93         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
94                 switch (opt) {
95                 case 's':
96                         seed = optarg;
97                         break;
98                 case 'n':
99                         count_str = optarg;
100                         break;
101                 case 'C':
102                         mode = MODE_CDECL;
103                         break;
104                 case 'E':
105                         mode = MODE_ENGLISH;
106                         break;
107                 case 'V':
108                         test_print_version(PROGNAME);
109                         return EXIT_SUCCESS;
110                 case 'H':
111                         print_help();
112                         return EXIT_SUCCESS;
113                 default:
114                         print_usage(stderr);
115                         return EXIT_FAILURE;
116                 }
117         }
118
119         if (count_str && !strict_strtoul(&count, count_str, 0)) {
120                 fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
121                 return EXIT_FAILURE;
122         }
123
124         rng = gen_alloc_rng(seed);
125         if (!rng)
126                 return EXIT_FAILURE;
127
128         for (unsigned long i = 0; !count || i < count; i++) {
129                 decl = random_decl(rng);
130
131                 if (mode == MODE_ENGLISH) {
132                         test_explain_decl(decl);
133                 } else {
134                         test_print_decl(decl);
135                 }
136
137                 gen_free_declspecs(decl->specifiers);
138                 gen_free_declarators(decl->declarators);
139                 free(decl);
140         }
141
142         gen_free_rng(rng);
143
144         return EXIT_SUCCESS;
145 }