f8041ead39d1f73c3b095e0fb890db2ec886453a
[ccan] / ccan / opt / test / run-helpers.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <ccan/tap/tap.h>
4 #include <setjmp.h>
5 #include <stdlib.h>
6 #include "utils.h"
7
8 /* We don't actually want it to exit... */
9 static jmp_buf exited;
10 #define exit(status) longjmp(exited, (status) + 1)
11
12 #define printf saved_printf
13 static int saved_printf(const char *fmt, ...);
14
15 #include <ccan/opt/helpers.c>
16 #include <ccan/opt/opt.c>
17 #include <ccan/opt/usage.c>
18
19 static void reset_options(void)
20 {
21         free(opt_table);
22         opt_table = NULL;
23         opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
24 }
25
26 static char *output = NULL;
27
28 static int saved_printf(const char *fmt, ...)
29 {
30         va_list ap;
31         char *p;
32         int ret;
33
34         va_start(ap, fmt);
35         ret = vasprintf(&p, fmt, ap);
36         va_end(ap);
37
38         if (output) {
39                 output = realloc(output, strlen(output) + strlen(p) + 1);
40                 strcat(output, p);
41                 free(p);
42         } else
43                 output = p;
44
45         return ret;
46 }       
47
48 /* Test helpers. */
49 int main(int argc, char *argv[])
50 {
51         plan_tests(88);
52
53         /* opt_set_bool */
54         {
55                 bool arg = false;
56                 reset_options();
57                 opt_register_noarg("-a", opt_set_bool, &arg, NULL);
58                 ok1(parse_args(&argc, &argv, "-a", NULL));
59                 ok1(arg);
60                 opt_register_arg("-b", opt_set_bool_arg, NULL, &arg, NULL);
61                 ok1(parse_args(&argc, &argv, "-b", "no", NULL));
62                 ok1(!arg);
63                 ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
64                 ok1(arg);
65                 ok1(parse_args(&argc, &argv, "-b", "false", NULL));
66                 ok1(!arg);
67                 ok1(parse_args(&argc, &argv, "-b", "true", NULL));
68                 ok1(arg);
69         }
70         /* opt_set_invbool */
71         {
72                 bool arg = true;
73                 reset_options();
74                 opt_register_noarg("-a", opt_set_invbool, &arg, NULL);
75                 ok1(parse_args(&argc, &argv, "-a", NULL));
76                 ok1(!arg);
77                 opt_register_arg("-b", opt_set_invbool_arg, NULL,
78                                  &arg, NULL);
79                 ok1(parse_args(&argc, &argv, "-b", "no", NULL));
80                 ok1(arg);
81                 ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
82                 ok1(!arg);
83                 ok1(parse_args(&argc, &argv, "-b", "false", NULL));
84                 ok1(arg);
85                 ok1(parse_args(&argc, &argv, "-b", "true", NULL));
86                 ok1(!arg);
87         }
88         /* opt_set_charp */
89         {
90                 char *arg = (char *)"wrong";
91                 reset_options();
92                 opt_register_arg("-a", opt_set_charp, NULL, &arg, NULL);
93                 ok1(parse_args(&argc, &argv, "-a", "string", NULL));
94                 ok1(strcmp(arg, "string") == 0);
95         }
96         /* opt_set_intval */
97         {
98                 int arg = 1000;
99                 reset_options();
100                 opt_register_arg("-a", opt_set_intval, NULL, &arg, NULL);
101                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
102                 ok1(arg == 9999);
103                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
104                 ok1(arg == -9999);
105                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
106                 ok1(arg == 0);
107                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
108                 if (sizeof(int) == 4)
109                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
110                 else
111                         fail("Handle other int sizes");
112         }
113         /* opt_set_uintval */
114         {
115                 unsigned int arg = 1000;
116                 reset_options();
117                 opt_register_arg("-a", opt_set_uintval, NULL, &arg, NULL);
118                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
119                 ok1(arg == 9999);
120                 ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
121                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
122                 ok1(arg == 0);
123                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
124                 ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
125         }
126         /* opt_set_longval */
127         {
128                 long int arg = 1000;
129                 reset_options();
130                 opt_register_arg("-a", opt_set_longval, NULL, &arg, NULL);
131                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
132                 ok1(arg == 9999);
133                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
134                 ok1(arg == -9999);
135                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
136                 ok1(arg == 0);
137                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
138                 if (sizeof(long) == 4)
139                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
140                 else if (sizeof(long)== 8)
141                         ok1(!parse_args(&argc, &argv, "-a", "18446744073709551616", NULL));
142                 else
143                         fail("FIXME: Handle other long sizes");
144         }
145         /* opt_set_ulongval */
146         {
147                 unsigned long int arg = 1000;
148                 reset_options();
149                 opt_register_arg("-a", opt_set_ulongval, NULL, &arg, NULL);
150                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
151                 ok1(arg == 9999);
152                 ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
153                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
154                 ok1(arg == 0);
155                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
156                 if (sizeof(long) == 4)
157                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
158                 else if (sizeof(long)== 8)
159                         ok1(!parse_args(&argc, &argv, "-a", "18446744073709551616", NULL));
160                 else
161                         fail("FIXME: Handle other long sizes");
162         }
163         /* opt_inc_intval */
164         {
165                 int arg = 1000;
166                 reset_options();
167                 opt_register_noarg("-a", opt_inc_intval, &arg, NULL);
168                 ok1(parse_args(&argc, &argv, "-a", NULL));
169                 ok1(arg == 1001);
170                 ok1(parse_args(&argc, &argv, "-a", "-a", NULL));
171                 ok1(arg == 1003);
172                 ok1(parse_args(&argc, &argv, "-aa", NULL));
173                 ok1(arg == 1005);
174         }
175
176         /* opt_show_version_and_exit. */
177         {
178                 int exitval;
179                 reset_options();
180                 opt_register_noarg("-a",
181                                    opt_version_and_exit, "1.2.3", NULL);
182                 exitval = setjmp(exited);
183                 if (exitval == 0) {
184                         parse_args(&argc, &argv, "-a", NULL);
185                         fail("opt_show_version_and_exit returned?");
186                 } else {
187                         ok1(exitval - 1 == 0);
188                 }
189                 ok1(strcmp(output, "1.2.3\n") == 0);
190                 free(output);
191                 output = NULL;
192         }
193
194         /* opt_usage_and_exit. */
195         {
196                 int exitval;
197                 reset_options();
198                 opt_register_noarg("-a",
199                                    opt_usage_and_exit, "[args]", NULL);
200                 exitval = setjmp(exited);
201                 if (exitval == 0) {
202                         parse_args(&argc, &argv, "-a", NULL);
203                         fail("opt_usage_and_exit returned?");
204                 } else {
205                         ok1(exitval - 1 == 0);
206                 }
207                 ok1(strstr(output, "[args]"));
208                 ok1(strstr(output, argv[0]));
209                 ok1(strstr(output, "[-a]"));
210                 free(output);
211                 output = NULL;
212         }
213
214         /* opt_show_bool */
215         {
216                 bool b;
217                 char buf[OPT_SHOW_LEN+2] = { 0 };
218                 buf[OPT_SHOW_LEN] = '!';
219
220                 b = true;
221                 opt_show_bool(buf, &b);
222                 ok1(strcmp(buf, "true") == 0);
223                 ok1(buf[OPT_SHOW_LEN] == '!');
224
225                 b = false;
226                 opt_show_bool(buf, &b);
227                 ok1(strcmp(buf, "false") == 0);
228                 ok1(buf[OPT_SHOW_LEN] == '!');
229         }
230
231         /* opt_show_invbool */
232         {
233                 bool b;
234                 char buf[OPT_SHOW_LEN+2] = { 0 };
235                 buf[OPT_SHOW_LEN] = '!';
236
237                 b = true;
238                 opt_show_invbool(buf, &b);
239                 ok1(strcmp(buf, "false") == 0);
240                 ok1(buf[OPT_SHOW_LEN] == '!');
241
242                 b = false;
243                 opt_show_invbool(buf, &b);
244                 ok1(strcmp(buf, "true") == 0);
245                 ok1(buf[OPT_SHOW_LEN] == '!');
246         }
247
248         /* opt_show_charp */
249         {
250                 char str[OPT_SHOW_LEN*2], *p;
251                 char buf[OPT_SHOW_LEN+2] = { 0 };
252                 buf[OPT_SHOW_LEN] = '!';
253
254                 /* Short test. */
255                 p = str;
256                 strcpy(p, "short");
257                 opt_show_charp(buf, &p);
258                 ok1(strcmp(buf, "\"short\"") == 0);
259                 ok1(buf[OPT_SHOW_LEN] == '!');
260
261                 /* Truncate test. */
262                 memset(p, 'x', OPT_SHOW_LEN*2);
263                 p[OPT_SHOW_LEN*2-1] = '\0';
264                 opt_show_charp(buf, &p);
265                 ok1(buf[0] == '"');
266                 ok1(buf[OPT_SHOW_LEN-1] == '"');
267                 ok1(buf[OPT_SHOW_LEN] == '!');
268                 ok1(strspn(buf+1, "x") == OPT_SHOW_LEN-2);
269         }
270
271         /* opt_show_intval */
272         {
273                 int i;
274                 char buf[OPT_SHOW_LEN+2] = { 0 };
275                 buf[OPT_SHOW_LEN] = '!';
276
277                 i = -77;
278                 opt_show_intval(buf, &i);
279                 ok1(strcmp(buf, "-77") == 0);
280                 ok1(buf[OPT_SHOW_LEN] == '!');
281
282                 i = 77;
283                 opt_show_intval(buf, &i);
284                 ok1(strcmp(buf, "77") == 0);
285                 ok1(buf[OPT_SHOW_LEN] == '!');
286         }
287
288         /* opt_show_uintval */
289         {
290                 unsigned int ui;
291                 char buf[OPT_SHOW_LEN+2] = { 0 };
292                 buf[OPT_SHOW_LEN] = '!';
293
294                 ui = 4294967295U;
295                 opt_show_uintval(buf, &ui);
296                 ok1(strcmp(buf, "4294967295") == 0);
297                 ok1(buf[OPT_SHOW_LEN] == '!');
298         }
299
300         /* opt_show_longval */
301         {
302                 long l;
303                 char buf[OPT_SHOW_LEN+2] = { 0 };
304                 buf[OPT_SHOW_LEN] = '!';
305
306                 l = 1234567890L;
307                 opt_show_longval(buf, &l);
308                 ok1(strcmp(buf, "1234567890") == 0);
309                 ok1(buf[OPT_SHOW_LEN] == '!');
310         }
311
312         /* opt_show_ulongval */
313         {
314                 unsigned long ul;
315                 char buf[OPT_SHOW_LEN+2] = { 0 };
316                 buf[OPT_SHOW_LEN] = '!';
317
318                 ul = 4294967295UL;
319                 opt_show_ulongval(buf, &ul);
320                 ok1(strcmp(buf, "4294967295") == 0);
321                 ok1(buf[OPT_SHOW_LEN] == '!');
322         }
323
324         return exit_status();
325 }