]> git.draconx.ca Git - cdecl99.git/blob - t/crossparse.c
630e02e05c0417e6b4cca4b224adef56cd3b826a
[cdecl99.git] / t / crossparse.c
1 /*
2  * Test that libcdecl can parse its own output.
3  * Copyright © 2012, 2020, 2022-2023 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 <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <getopt.h>
26 #include <cdecl.h>
27 #include "test.h"
28
29 #define PROGNAME "crossparse"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "f:ECVH";
32 static const struct option lopts[] = {
33         { "cdecl",   0, NULL, 'C' },
34         { "english", 0, NULL, 'E' },
35         { "file",    2, NULL, 'f' },
36         { "version", 0, NULL, 'V' },
37         { "help",    0, NULL, 'H' },
38         { 0 }
39 };
40
41 static void print_usage(FILE *f)
42 {
43         fprintf(f, "Usage: %s [options]\n", progname);
44 }
45
46 static void print_help(void)
47 {
48         print_usage(stdout);
49         puts("Test that libcdecl can parse its own output.\n");
50         test_print_options(lopts);
51 }
52
53 enum {
54         MODE_CDECL,
55         MODE_ENGLISH,
56 };
57
58 typedef struct cdecl *parse_func(const char *);
59 typedef size_t render_func(char *, size_t, struct cdecl *);
60
61 char *rerender(const char *str, const char *parse_name,  parse_func *parse,
62                                 const char *render_name, render_func *render)
63 {
64         struct cdecl *decl = NULL;
65         char *buf = NULL;
66         size_t len;
67
68         decl = parse(str);
69         if (!decl) {
70                 fprintf(stderr, "%s: %s: failed to parse input: %s\n",
71                                 progname, parse_name, cdecl_get_error()->str);
72                 goto err;
73         }
74
75         len = render(NULL, 0, decl);
76         buf = malloc_nofail(len+1);
77         if (render(buf, len+1, decl) != len) {
78                 fprintf(stderr, "%s: %s: inconsistent length returned\n",
79                                 progname, render_name);
80                 goto err;
81         }
82
83         cdecl_free(decl);
84         return buf;
85 err:
86         cdecl_free(decl);
87         free(buf);
88
89         fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
90         return NULL;
91 }
92 #define rerender(str, p, r) (rerender(str, #p, p, #r, r))
93
94 static bool test_crossparse(const char *str, int mode)
95 {
96         char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
97         bool ret = false;
98
99         if (mode == MODE_ENGLISH) {
100                 buf1 = rerender(str, cdecl_parse_english, cdecl_declare);
101                 if (!buf1)
102                         goto out;
103                 buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
104                 if (!buf2)
105                         goto out;
106                 buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
107                 if (!buf3)
108                         goto out;
109         } else {
110                 buf1 = rerender(str, cdecl_parse_decl, cdecl_explain);
111                 if (!buf1)
112                         goto out;
113                 buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
114                 if (!buf2)
115                         goto out;
116                 buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
117                 if (!buf3)
118                         goto out;
119         }
120
121         if (!strcmp(buf1, buf3))
122                 ret = true;
123 out:
124         free(buf1);
125         free(buf2);
126         free(buf3);
127
128         if (!ret) {
129                 fprintf(stderr, "%s: failed cross-parse check of: %s\n",
130                                 progname, str);
131         }
132
133         return ret;
134 }
135
136 int main(int argc, char **argv)
137 {
138         int opt, mode = MODE_CDECL;
139         int ret = EXIT_SUCCESS;
140
141         const char *filename = NULL;
142         FILE *infile = NULL;
143
144         if (argc > 0)
145                 progname = argv[0];
146
147         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
148                 switch (opt) {
149                 case 'C':
150                         mode = MODE_CDECL;
151                         break;
152                 case 'E':
153                         mode = MODE_ENGLISH;
154                         break;
155                 case 'f':
156                         infile = stdin;
157                         filename = optarg;
158                         break;
159                 case 'V':
160                         test_print_version(PROGNAME);
161                         return EXIT_SUCCESS;
162                 case 'H':
163                         print_help();
164                         return EXIT_SUCCESS;
165                 default:
166                         print_usage(stderr);
167                         return EXIT_FAILURE;
168                 }
169         }
170
171         if (infile) {
172                 char *line = NULL;
173                 size_t n;
174
175                 if (filename) {
176                         infile = fopen(filename, "r");
177                         if (!infile) {
178                                 fprintf(stderr, "%s: %s: %s\n", progname,
179                                                 filename, strerror(errno));
180                                 return EXIT_FAILURE;
181                         }
182                 }
183
184                 while (getline(&line, &n, infile) >= 0) {
185                         char *c = strchr(line, '\n');
186                         if (c)
187                                 *c = '\0';
188                         if (!test_crossparse(line, mode))
189                                 ret = EXIT_FAILURE;
190                 }
191
192                 free(line);
193                 fclose(infile);
194         } else if (argv[optind]) {
195                 for (int i = optind; i < argc; i++) {
196                         if (!test_crossparse(argv[i], mode))
197                                 ret = EXIT_FAILURE;
198                 }
199         } else {
200                 print_usage(stderr);
201                 return EXIT_FAILURE;
202         }
203
204         return ret;
205 }