]> git.draconx.ca Git - cdecl99.git/blob - t/rendertest.c
e5299c292fc662f842c03113b232dfa0dbc95d90
[cdecl99.git] / t / rendertest.c
1 /*
2  * Helper to verify the output rendering routines.
3  * Copyright © 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 <getopt.h>
24 #include <assert.h>
25
26 #include <cdecl.h>
27 #include "test.h"
28
29 #define PROGNAME "rendertest"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "n:ECVH";
32 static const struct option lopts[] = {
33         { "count",   1, NULL, 'n' },
34         { "cdecl",   0, NULL, 'C' },
35         { "english", 0, NULL, 'E' },
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("Helper application to test output rendering.\n");
50         test_print_options(lopts);
51 }
52
53 enum { MODE_CDECL, MODE_ENGLISH };
54
55 static int do_test(char *line, unsigned long n, int mode)
56 {
57         struct cdecl *decl;
58         size_t rc;
59
60         if (mode)
61                 decl = cdecl_parse_english(line);
62         else
63                 decl = cdecl_parse_decl(line);
64
65         if (!decl) {
66                 fprintf(stderr, "%s: %s\n", progname, cdecl_get_error()->str);
67                 fprintf(stderr, "%s: the failed input was: %s\n",
68                                 progname, line);
69                 return -1;
70         }
71
72         memset(line, '\a', n+1);
73         if (mode)
74                 rc = cdecl_explain(line, n, decl);
75         else
76                 rc = cdecl_declare(line, n, decl);
77
78         if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
79                 fprintf(stderr, "%s: output is not 0-terminated\n", progname);
80                 return -1;
81         }
82
83         if (line[n] != '\a') {
84                 fprintf(stderr, "%s: output overflow\n", progname);
85                 return -1;
86         }
87
88         printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
89         cdecl_free(decl);
90         return 0;
91 }
92
93 int main(int argc, char **argv)
94 {
95         int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS;
96         unsigned long n = 60;
97         char *line;
98         size_t sz;
99
100         if (argc > 0)
101                 progname = argv[0];
102
103         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
104                 switch (opt) {
105                 case 'C':
106                         mode = MODE_CDECL;
107                         break;
108                 case 'E':
109                         mode = MODE_ENGLISH;
110                         break;
111                 case 'n':
112                         if (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
113                                 fprintf(stderr, "%s: invalid count: %s\n",
114                                         progname, optarg);
115                                 return EXIT_FAILURE;
116                         }
117                         break;
118                 case 'V':
119                         test_print_version(PROGNAME);
120                         return EXIT_SUCCESS;
121                 case 'H':
122                         print_help();
123                         return EXIT_SUCCESS;
124                 default:
125                         print_usage(stderr);
126                         return EXIT_FAILURE;
127                 }
128         }
129
130         line = malloc_nofail((sz = n+1));
131         while (getline(&line, &sz, stdin) >= 0) {
132                 char *c = strchr(line, '\n');
133                 if (c)
134                         *c = '\0';
135
136                 if (do_test(line, n, mode) < 0)
137                         ret = EXIT_FAILURE;
138         }
139
140         free(line);
141         return ret;
142 }