]> git.draconx.ca Git - cdecl99.git/blob - t/rendertest.c
Port to use getline.h from dxcommon.
[cdecl99.git] / t / rendertest.c
1 /*
2  * Helper to verify the output rendering routines.
3  * Copyright © 2023-2024 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
28 #define PROGNAME "rendertest"
29 #include "test.h"
30
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                 print_error("%s", cdecl_get_error()->str);
67                 print_error("the failed input was: %s", line);
68                 return -1;
69         }
70
71         memset(line, '\a', n+1);
72         if (mode)
73                 rc = cdecl_explain(line, n, decl);
74         else
75                 rc = cdecl_declare(line, n, decl);
76
77         if (n && ( rc < n ? line[rc] : line[n-1] ) != '\0') {
78                 print_error("output is not 0-terminated");
79                 return -1;
80         }
81
82         if (line[n] != '\a') {
83                 print_error("output overflow");
84                 return -1;
85         }
86
87         printf(n > 0 ? "%lu %s\n" : "%lu\n", (unsigned long)rc, line);
88         cdecl_free(decl);
89         return 0;
90 }
91
92 int main(int argc, char **argv)
93 {
94         int opt, mode = MODE_CDECL, ret = EXIT_SUCCESS;
95         unsigned long n = 60;
96         char *line;
97         size_t sz;
98
99         if (argc > 0)
100                 progname = argv[0];
101
102         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
103                 switch (opt) {
104                 case 'C':
105                         mode = MODE_CDECL;
106                         break;
107                 case 'E':
108                         mode = MODE_ENGLISH;
109                         break;
110                 case 'n':
111                         if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
112                                 print_error("invalid count: %s", optarg);
113                                 return EXIT_FAILURE;
114                         }
115                         break;
116                 case 'V':
117                         test_print_version(PROGNAME);
118                         return EXIT_SUCCESS;
119                 case 'H':
120                         print_help();
121                         return EXIT_SUCCESS;
122                 default:
123                         print_usage(stderr);
124                         return EXIT_FAILURE;
125                 }
126         }
127
128         /*
129          * Ensure the preallocated buffer is more than one byte, otherwise we
130          * will hit a bug in AIX 7.2 getline and fall into an infinite loop.
131          */
132         line = malloc_nofail((sz = MAX(n+1, 10)));
133         while (test_getline(&line, &sz)) {
134                 if (do_test(line, n, mode) < 0)
135                         ret = EXIT_FAILURE;
136         }
137
138         free(line);
139         return ret;
140 }