opt: add float/double helpers.
[ccan] / ccan / opt / test / run-helpers.c
1 #include "config.h"
2 #include <stdio.h>
3 #include <ccan/tap/tap.h>
4 #include <setjmp.h>
5 #include <stdlib.h>
6 #include <limits.h>
7 #include "utils.h"
8
9 /* We don't actually want it to exit... */
10 static jmp_buf exited;
11 #define exit(status) longjmp(exited, (status) + 1)
12
13 #define printf saved_printf
14 static int saved_printf(const char *fmt, ...);
15
16 #define fprintf saved_fprintf
17 static int saved_fprintf(FILE *ignored, const char *fmt, ...);
18
19 #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
20 static int saved_vprintf(const char *fmt, va_list ap);
21
22 #include <ccan/opt/helpers.c>
23 #include <ccan/opt/opt.c>
24 #include <ccan/opt/usage.c>
25 #include <ccan/opt/parse.c>
26
27 static char *output = NULL;
28
29 static int saved_vprintf(const char *fmt, va_list ap)
30 {
31         char *p;
32         int ret = vasprintf(&p, fmt, ap);
33
34         if (output) {
35                 output = realloc(output, strlen(output) + strlen(p) + 1);
36                 strcat(output, p);
37                 free(p);
38         } else
39                 output = p;
40         return ret;
41 }
42
43 static int saved_printf(const char *fmt, ...)
44 {
45         va_list ap;
46         int ret;
47
48         va_start(ap, fmt);
49         ret = saved_vprintf(fmt, ap);
50         va_end(ap);
51         return ret;
52 }
53
54 static int saved_fprintf(FILE *ignored, const char *fmt, ...)
55 {
56         va_list ap;
57         int ret;
58
59         va_start(ap, fmt);
60         ret = saved_vprintf(fmt, ap);
61         va_end(ap);
62         return ret;
63 }
64
65 static void set_args(int *argc, char ***argv, ...)
66 {
67         va_list ap;
68         *argv = malloc(sizeof(**argv) * 20);
69
70         va_start(ap, argv);
71         for (*argc = 0;
72              ((*argv)[*argc] = va_arg(ap, char*)) != NULL;
73              (*argc)++);
74         va_end(ap);
75 }
76
77 /* Test helpers. */
78 int main(int argc, char *argv[])
79 {
80         plan_tests(476);
81
82         /* opt_set_bool */
83         {
84                 bool arg = false;
85                 reset_options();
86                 opt_register_noarg("-a", opt_set_bool, &arg, "");
87                 ok1(parse_args(&argc, &argv, "-a", NULL));
88                 ok1(arg);
89                 opt_register_arg("-b", opt_set_bool_arg, NULL, &arg, "");
90                 ok1(parse_args(&argc, &argv, "-b", "no", NULL));
91                 ok1(!arg);
92                 ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
93                 ok1(arg);
94                 ok1(parse_args(&argc, &argv, "-b", "false", NULL));
95                 ok1(!arg);
96                 ok1(parse_args(&argc, &argv, "-b", "true", NULL));
97                 ok1(arg);
98                 ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL));
99                 ok1(arg);
100                 ok1(strstr(err_output, ": -b: Invalid argument 'unknown'"));
101         }
102         /* opt_set_invbool */
103         {
104                 bool arg = true;
105                 reset_options();
106                 opt_register_noarg("-a", opt_set_invbool, &arg, "");
107                 ok1(parse_args(&argc, &argv, "-a", NULL));
108                 ok1(!arg);
109                 opt_register_arg("-b", opt_set_invbool_arg, NULL,
110                                  &arg, "");
111                 ok1(parse_args(&argc, &argv, "-b", "no", NULL));
112                 ok1(arg);
113                 ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
114                 ok1(!arg);
115                 ok1(parse_args(&argc, &argv, "-b", "false", NULL));
116                 ok1(arg);
117                 ok1(parse_args(&argc, &argv, "-b", "true", NULL));
118                 ok1(!arg);
119                 ok1(!parse_args(&argc, &argv, "-b", "unknown", NULL));
120                 ok1(!arg);
121                 ok1(strstr(err_output, ": -b: Invalid argument 'unknown'"));
122         }
123         /* opt_set_charp */
124         {
125                 char *arg = cast_const(char *, "wrong");
126                 reset_options();
127                 opt_register_arg("-a", opt_set_charp, NULL, &arg, "All");
128                 ok1(parse_args(&argc, &argv, "-a", "string", NULL));
129                 ok1(strcmp(arg, "string") == 0);
130         }
131         /* opt_set_intval */
132         {
133                 int arg = 1000;
134                 reset_options();
135                 opt_register_arg("-a", opt_set_intval, NULL, &arg, "All");
136                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
137                 ok1(arg == 9999);
138                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
139                 ok1(arg == -9999);
140                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
141                 ok1(arg == 0);
142                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
143                 if (sizeof(int) == 4)
144                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
145                 else
146                         fail("Handle other int sizes");
147         }
148         /* opt_set_uintval */
149         {
150                 unsigned int arg = 1000;
151                 reset_options();
152                 opt_register_arg("-a", opt_set_uintval, NULL, &arg, "All");
153                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
154                 ok1(arg == 9999);
155                 ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
156                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
157                 ok1(arg == 0);
158                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
159                 ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
160                 if (ULONG_MAX == UINT_MAX) {
161                         pass("Can't test overflow");
162                         pass("Can't test error message");
163                 } else {
164                         char buf[30];
165                         sprintf(buf, "%lu", ULONG_MAX);
166                         ok1(!parse_args(&argc, &argv, "-a", buf, NULL));
167                         ok1(strstr(err_output, ": -a: value '")
168                             && strstr(err_output, buf)
169                             && strstr(err_output, "' does not fit into an integer"));
170                 }
171         }
172         /* opt_set_longval */
173         {
174                 long int arg = 1000;
175                 reset_options();
176                 opt_register_arg("-a", opt_set_longval, NULL, &arg, "All");
177                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
178                 ok1(arg == 9999);
179                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
180                 ok1(arg == -9999);
181                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
182                 ok1(arg == 0);
183                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
184                 if (sizeof(long) == 4)
185                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
186                 else if (sizeof(long)== 8)
187                         ok1(!parse_args(&argc, &argv, "-a", "18446744073709551616", NULL));
188                 else
189                         fail("FIXME: Handle other long sizes");
190         }
191         /* opt_set_ulongval */
192         {
193                 unsigned long int arg = 1000;
194                 reset_options();
195                 opt_register_arg("-a", opt_set_ulongval, NULL, &arg, "All");
196                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
197                 ok1(arg == 9999);
198                 ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
199                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
200                 ok1(arg == 0);
201                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
202                 if (sizeof(long) == 4)
203                         ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
204                 else if (sizeof(long)== 8)
205                         ok1(!parse_args(&argc, &argv, "-a", "18446744073709551616", NULL));
206                 else
207                         fail("FIXME: Handle other long sizes");
208         }
209         /* opt_set_floatval */
210         {
211                 float arg = 1000;
212                 reset_options();
213                 opt_register_arg("-a", opt_set_floatval, NULL, &arg, "All");
214                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
215                 ok1(arg == 9999);
216                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
217                 ok1(arg == -9999);
218                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
219                 ok1(arg == 0);
220                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
221         }
222         /* opt_set_doubleval */
223         {
224                 double arg = 1000;
225                 reset_options();
226                 opt_register_arg("-a", opt_set_doubleval, NULL, &arg, "All");
227                 ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
228                 ok1(arg == 9999);
229                 ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
230                 ok1(arg == -9999);
231                 ok1(parse_args(&argc, &argv, "-a", "0", NULL));
232                 ok1(arg == 0);
233                 ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
234         }
235
236         {
237                 const long long k = 1024;
238                 const long long M = k * k;
239                 const long long G = k * k * k;
240                 const long long T = k * k * k * k;
241                 const long long P = k * k * k * k * k;
242                 const long long E = k * k * k * k * k * k;
243
244                 /* opt_set_uintval_bi */
245                 {
246                         unsigned int arg = 1000;
247                         reset_options();
248                         opt_register_arg("-a", opt_set_uintval_bi, NULL,
249                                          &arg, "All");
250                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
251                         ok1(arg == 9999);
252                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
253                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
254                         ok1(arg == 0);
255                         arg = 1;
256                         ok1(parse_args(&argc, &argv, "-a", "0k", NULL));
257                         ok1(arg == 0);
258                         arg = 1;
259                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
260                         ok1(arg == 0);
261                         ok1(!parse_args(&argc, &argv, "-a", "3Q", NULL));
262                         ok1(parse_args(&argc, &argv, "-a", "30k", NULL));
263                         ok1(arg == 30 * k);
264                         ok1(!parse_args(&argc, &argv, "-a", "-1K", NULL));
265         }
266
267                 /* opt_set_intval_bi */
268                 {
269                         int arg = 1000;
270                         reset_options();
271                         opt_register_arg("-a", opt_set_intval_bi, NULL,
272                                          &arg, "All");
273                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
274                         ok1(arg == 9999);
275                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
276                         ok1(arg == -9999);
277                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
278                         ok1(arg == 0);
279                         arg = 1;
280                         ok1(parse_args(&argc, &argv, "-a", "0k", NULL));
281                         ok1(arg == 0);
282                         arg = 1;
283                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
284                         ok1(arg == 0);
285                         ok1(!parse_args(&argc, &argv, "-a", "3Q", NULL));
286                         ok1(parse_args(&argc, &argv, "-a", "30k", NULL));
287                         ok1(arg == 30 * k);
288                         ok1(parse_args(&argc, &argv, "-a", "-1K", NULL));
289                         ok1(arg == -1 * k);
290                 }
291
292
293                 /* opt_set_ulongval_bi */
294                 {
295                         unsigned long int arg = 1000;
296
297                         reset_options();
298                         opt_register_arg("-a", opt_set_ulongval_bi, NULL,
299                                          &arg, "All");
300                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
301                         ok1(arg == 9999);
302                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
303                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
304                         ok1(arg == 0);
305                         arg = 1;
306                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
307                         ok1(arg == 0);
308                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
309                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
310                         ok1(arg == 100 * k);
311                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
312                         ok1(arg == 1 * k);
313                         ok1(parse_args(&argc, &argv, "-a", "99M", NULL));
314                         ok1(arg == 99 * M);
315                         /*note, 2999M > max signed 32 bit long, 1 << 31*/
316                         ok1(parse_args(&argc, &argv, "-a", "2999m", NULL));
317                         ok1(arg == 2999 * M);
318                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
319                         ok1(arg == 1 * G);
320                         ok1(!parse_args(&argc, &argv, "-a", "-1G", NULL));
321                         if (sizeof(long) == 4){
322                                 ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
323                                 ok1(!parse_args(&argc, &argv, "-a", "1T", NULL));
324                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
325                         }
326                         else if (sizeof(long) == 8){
327                                 ok1(!parse_args(&argc, &argv, "-a",
328                                                 "18446744073709551616", NULL));
329                                 ok1(!parse_args(&argc, &argv, "-a", "8E", NULL));
330                                 ok1(parse_args(&argc, &argv, "-a", "3E", NULL));
331                         }
332                         else
333                                 fail("FIXME: Handle other long sizes");
334                 }
335
336                 /* opt_set_longval_bi */
337                 {
338                         long int arg = 1000;
339
340                         reset_options();
341                         opt_register_arg("-a", opt_set_longval_bi, NULL,
342                                          &arg, "All");
343                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
344                         ok1(arg == 9999);
345                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
346                         ok1(arg == -9999);
347                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
348                         ok1(arg == 0);
349                         ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
350                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
351                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
352                         ok1(arg == 100 * k);
353                         ok1(parse_args(&argc, &argv, "-a", "-100k", NULL));
354                         ok1(arg == -100 * k);
355                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
356                         ok1(arg == 1 * k);
357                         ok1(parse_args(&argc, &argv, "-a", "99M", NULL));
358                         ok1(arg == 99 * M);
359                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
360                         ok1(arg == 1 * G);
361                         ok1(parse_args(&argc, &argv, "-a", "-1G", NULL));
362                         ok1(arg == -1 * G);
363                         if (sizeof(long) == 4){
364                                 ok1(!parse_args(&argc, &argv, "-a", "2147483648", NULL));
365                                 ok1(!parse_args(&argc, &argv, "-a", "2G", NULL));
366                                 ok1(!parse_args(&argc, &argv, "-a", "2048m", NULL));
367                                 ok1(!parse_args(&argc, &argv, "-a", "1T", NULL));
368                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
369                         }
370                         else if (sizeof(long) == 8){
371                                 ok1(!parse_args(&argc, &argv, "-a",
372                                                 "9223372036854775808", NULL));
373                                 ok1(parse_args(&argc, &argv, "-a", "3E", NULL));
374                                 ok1(arg == 3 * E);
375                                 ok1(parse_args(&argc, &argv, "-a", "123T", NULL));
376                                 ok1(arg == 123 * T);
377                         }
378                         else
379                                 fail("FIXME: Handle other long sizes");
380                 }
381
382
383                 /* opt_set_longlongval_bi */
384                 {
385                         long long int arg = 1000;
386                         reset_options();
387                         opt_register_arg("-a", opt_set_longlongval_bi, NULL,
388                                          &arg, "All");
389                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
390                         ok1(arg == 9999);
391                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
392                         ok1(arg == -9999);
393                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
394                         ok1(arg == 0);
395                         ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
396                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
397                         ok1(!parse_args(&argc, &argv, "-a", "1kk", NULL));
398                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
399                         ok1(arg == 100 * k);
400                         ok1(parse_args(&argc, &argv, "-a", "-100k", NULL));
401                         ok1(arg == -100 * k);
402                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
403                         ok1(arg == 1 * k);
404                         ok1(parse_args(&argc, &argv, "-a", "-333333M", NULL));
405                         ok1(arg == -333333 * M);
406                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
407                         ok1(arg == 1 * G);
408                         ok1(parse_args(&argc, &argv, "-a", "1024t", NULL));
409                         ok1(arg == 1024 * T);
410                         ok1(parse_args(&argc, &argv, "-a", "123P", NULL));
411                         ok1(arg == 123 * P);
412                         ok1(parse_args(&argc, &argv, "-a", "-3E", NULL));
413                         ok1(arg == -3 * E);
414
415                         if (sizeof(long long) == 8){
416                                 ok1(!parse_args(&argc, &argv, "-a",
417                                                 "9223372036854775808", NULL));
418                                 /*8E and 922337.. are both 1 << 63*/
419                                 ok1(!parse_args(&argc, &argv, "-a", "8E", NULL));
420                         }
421                         else
422                                 fail("FIXME: Handle other long long int"
423                                      " sizes (specifically %zu bytes)",
424                                      sizeof(long long));
425                 }
426                 /* opt_set_ulonglongval_bi */
427                 {
428                         unsigned long long int arg = 1000;
429                         reset_options();
430                         opt_register_arg("-a", opt_set_ulonglongval_bi, NULL,
431                                          &arg, "All");
432                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
433                         ok1(arg == 9999);
434                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
435                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
436                         ok1(arg == 0);
437                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
438                         ok1(!parse_args(&argc, &argv, "-a", "1kk", NULL));
439                         ok1(parse_args(&argc, &argv, "-a", "100G", NULL));
440                         ok1(arg == 100 * G);
441                         ok1(!parse_args(&argc, &argv, "-a", "-100G", NULL));
442                         ok1(parse_args(&argc, &argv, "-a", "8191P", NULL));
443                         ok1(arg == 8191 * P);
444                 }
445
446                 /* opt_show_intval_bi */
447                 {
448                         int i;
449                         char buf[OPT_SHOW_LEN+2] = { 0 };
450                         buf[OPT_SHOW_LEN] = '!';
451                         i = -77;
452                         opt_show_intval_bi(buf, &i);
453                         ok1(strcmp(buf, "-77") == 0);
454                         i = 0;
455                         opt_show_intval_bi(buf, &i);
456                         ok1(strcmp(buf, "0") == 0);
457                         ok1(buf[OPT_SHOW_LEN] == '!');
458                         i = 77;
459                         opt_show_intval_bi(buf, &i);
460                         ok1(strcmp(buf, "77") == 0);
461                         ok1(buf[OPT_SHOW_LEN] == '!');
462                         i = -1234 * k;
463                         opt_show_intval_bi(buf, &i);
464                         ok1(strcmp(buf, "-1234k") == 0);
465                         ok1(buf[OPT_SHOW_LEN] == '!');
466                         i = 500 * M;
467                         opt_show_intval_bi(buf, &i);
468                         ok1(strcmp(buf, "500M") == 0);
469                         ok1(buf[OPT_SHOW_LEN] == '!');
470                         i = 1024 * M;
471                         opt_show_intval_bi(buf, &i);
472                         ok1(strcmp(buf, "1G") == 0);
473                         ok1(buf[OPT_SHOW_LEN] == '!');
474                 }
475
476                 /* opt_show_longval_bi */
477                 {
478                         long i;
479                         char buf[OPT_SHOW_LEN+2] = { 0 };
480                         buf[OPT_SHOW_LEN] = '!';
481                         i = -77;
482                         opt_show_longval_bi(buf, &i);
483                         ok1(strcmp(buf, "-77") == 0);
484                         ok1(buf[OPT_SHOW_LEN] == '!');
485                         i = 77;
486                         opt_show_longval_bi(buf, &i);
487                         ok1(strcmp(buf, "77") == 0);
488                         ok1(buf[OPT_SHOW_LEN] == '!');
489                         i = -1 * k;
490                         opt_show_longval_bi(buf, &i);
491                         ok1(strcmp(buf, "-1k") == 0);
492                         ok1(buf[OPT_SHOW_LEN] == '!');
493                         i = 500 * M;
494                         opt_show_longval_bi(buf, &i);
495                         ok1(strcmp(buf, "500M") == 0);
496                         ok1(buf[OPT_SHOW_LEN] == '!');
497                         i = 1024 * M;
498                         opt_show_longval_bi(buf, &i);
499                         ok1(strcmp(buf, "1G") == 0);
500                         ok1(buf[OPT_SHOW_LEN] == '!');
501                         i = 0;
502                         opt_show_longval_bi(buf, &i);
503                         ok1(strcmp(buf, "0") == 0);
504                         ok1(buf[OPT_SHOW_LEN] == '!');
505                 }
506
507                 /* opt_show_llongval_bi */
508                 {
509                         long long i;
510                         char buf[OPT_SHOW_LEN+2] = { 0 };
511                         buf[OPT_SHOW_LEN] = '!';
512                         i = -7777;
513                         opt_show_longlongval_bi(buf, &i);
514                         ok1(strcmp(buf, "-7777") == 0);
515                         ok1(buf[OPT_SHOW_LEN] == '!');
516                         i = 7777;
517                         opt_show_longlongval_bi(buf, &i);
518                         ok1(strcmp(buf, "7777") == 0);
519                         ok1(buf[OPT_SHOW_LEN] == '!');
520                         i = -10240000 * k;
521                         opt_show_longlongval_bi(buf, &i);
522                         ok1(strcmp(buf, "-10000M") == 0);
523                         ok1(buf[OPT_SHOW_LEN] == '!');
524                         i = 5 * P;
525                         opt_show_longlongval_bi(buf, &i);
526                         ok1(strcmp(buf, "5P") == 0);
527                         ok1(buf[OPT_SHOW_LEN] == '!');
528                         i = 1024 * P;
529                         opt_show_longlongval_bi(buf, &i);
530                         ok1(strcmp(buf, "1E") == 0);
531                         ok1(buf[OPT_SHOW_LEN] == '!');
532                 }
533
534                 /* opt_show_uintval_bi */
535                 {
536                         unsigned int i;
537                         char buf[OPT_SHOW_LEN+2] = { 0 };
538                         buf[OPT_SHOW_LEN] = '!';
539                         i = 77;
540                         opt_show_uintval_bi(buf, &i);
541                         ok1(strcmp(buf, "77") == 0);
542                         ok1(buf[OPT_SHOW_LEN] == '!');
543                         i = 1234 * k;
544                         opt_show_uintval_bi(buf, &i);
545                         ok1(strcmp(buf, "1234k") == 0);
546                         ok1(buf[OPT_SHOW_LEN] == '!');
547                         i = 500 * M;
548                         opt_show_uintval_bi(buf, &i);
549                         ok1(strcmp(buf, "500M") == 0);
550                         ok1(buf[OPT_SHOW_LEN] == '!');
551                         i = 1024 * M;
552                         opt_show_uintval_bi(buf, &i);
553                         ok1(strcmp(buf, "1G") == 0);
554                         ok1(buf[OPT_SHOW_LEN] == '!');
555                 }
556
557                 /* opt_show_ulongval_bi */
558                 {
559                         unsigned long i;
560                         char buf[OPT_SHOW_LEN+2] = { 0 };
561                         buf[OPT_SHOW_LEN] = '!';
562                         i = 77;
563                         opt_show_ulongval_bi(buf, &i);
564                         ok1(strcmp(buf, "77") == 0);
565                         ok1(buf[OPT_SHOW_LEN] == '!');
566                         i = k;
567                         opt_show_ulongval_bi(buf, &i);
568                         ok1(strcmp(buf, "1k") == 0);
569                         ok1(buf[OPT_SHOW_LEN] == '!');
570                         i = 500 * M;
571                         opt_show_ulongval_bi(buf, &i);
572                         ok1(strcmp(buf, "500M") == 0);
573                         ok1(buf[OPT_SHOW_LEN] == '!');
574                         i = 1024 * M;
575                         opt_show_ulongval_bi(buf, &i);
576                         ok1(strcmp(buf, "1G") == 0);
577                         ok1(buf[OPT_SHOW_LEN] == '!');
578                         i = 0;
579                         opt_show_ulongval_bi(buf, &i);
580                         ok1(strcmp(buf, "0") == 0);
581                         ok1(buf[OPT_SHOW_LEN] == '!');
582                 }
583
584                 /* opt_show_ullongval_bi */
585                 {
586                         long long i;
587                         char buf[OPT_SHOW_LEN+2] = { 0 };
588                         buf[OPT_SHOW_LEN] = '!';
589                         i = 7777;
590                         opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
591                         ok1(strcmp(buf, "7777") == 0);
592                         ok1(buf[OPT_SHOW_LEN] == '!');
593                         i = 10240000 * k;
594                         opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
595                         ok1(strcmp(buf, "10000M") == 0);
596                         ok1(buf[OPT_SHOW_LEN] == '!');
597                         i = 5 * P;
598                         opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
599                         ok1(strcmp(buf, "5P") == 0);
600                         ok1(buf[OPT_SHOW_LEN] == '!');
601                         i = 1024 * P;
602                         opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
603                         ok1(strcmp(buf, "1E") == 0);
604                         ok1(buf[OPT_SHOW_LEN] == '!');
605                 }
606         }
607
608         {
609                 const long long k = 1000;
610                 const long long M = k * k;
611                 const long long G = k * k * k;
612                 const long long T = k * k * k * k;
613                 const long long P = k * k * k * k * k;
614                 const long long E = k * k * k * k * k * k;
615
616                 /* opt_set_uintval_si */
617                 {
618                         unsigned int arg = 1000;
619                         reset_options();
620                         opt_register_arg("-a", opt_set_uintval_si, NULL,
621                                          &arg, "All");
622                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
623                         ok1(arg == 9999);
624                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
625                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
626                         ok1(arg == 0);
627                         arg = 1;
628                         ok1(parse_args(&argc, &argv, "-a", "0k", NULL));
629                         ok1(arg == 0);
630                         arg = 1;
631                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
632                         ok1(arg == 0);
633                         ok1(!parse_args(&argc, &argv, "-a", "3Q", NULL));
634                         ok1(parse_args(&argc, &argv, "-a", "30k", NULL));
635                         ok1(arg == 30 * k);
636                         ok1(!parse_args(&argc, &argv, "-a", "-1K", NULL));
637                         if (sizeof(unsigned int) < 8)
638                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
639                         else
640                                 pass("can't test int truncation when int is so huge");
641         }
642
643                 /* opt_set_intval_si */
644                 {
645                         int arg = 1000;
646                         reset_options();
647                         opt_register_arg("-a", opt_set_intval_si, NULL,
648                                          &arg, "All");
649                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
650                         ok1(arg == 9999);
651                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
652                         ok1(arg == -9999);
653                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
654                         ok1(arg == 0);
655                         arg = 1;
656                         ok1(parse_args(&argc, &argv, "-a", "0k", NULL));
657                         ok1(arg == 0);
658                         arg = 1;
659                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
660                         ok1(arg == 0);
661                         ok1(!parse_args(&argc, &argv, "-a", "", NULL));
662                         ok1(!parse_args(&argc, &argv, "-a", "3Q", NULL));
663                         ok1(parse_args(&argc, &argv, "-a", "30k", NULL));
664                         ok1(arg == 30 * k);
665                         ok1(parse_args(&argc, &argv, "-a", "-1K", NULL));
666                         ok1(arg == -1 * k);
667                         if (sizeof(int) < 8)
668                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
669                         else
670                                 pass("can't test int truncation when int is so huge");
671                 }
672
673
674                 /* opt_set_ulongval_si */
675                 {
676                         unsigned long int arg = 1000;
677
678                         reset_options();
679                         opt_register_arg("-a", opt_set_ulongval_si, NULL,
680                                          &arg, "All");
681                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
682                         ok1(arg == 9999);
683                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
684                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
685                         ok1(arg == 0);
686                         ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
687                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
688                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
689                         ok1(arg == 100 * k);
690                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
691                         ok1(arg == 1 * k);
692                         ok1(parse_args(&argc, &argv, "-a", "99M", NULL));
693                         ok1(arg == 99 * M);
694                         /*note, 2999M > max signed 32 bit long, 1 << 31*/
695                         ok1(parse_args(&argc, &argv, "-a", "2999m", NULL));
696                         ok1(arg == 2999 * M);
697                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
698                         ok1(arg == 1 * G);
699                         ok1(!parse_args(&argc, &argv, "-a", "-1G", NULL));
700                         ok1(parse_args(&argc, &argv, "-a", "4G", NULL));
701                         ok1(arg == 4000000000U);
702                         if (sizeof(long) == 4){
703                                 ok1(!parse_args(&argc, &argv, "-a", "4294967296", NULL));
704                                 ok1(!parse_args(&argc, &argv, "-a", "4295M", NULL));
705                                 ok1(!parse_args(&argc, &argv, "-a", "1T", NULL));
706                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
707                         }
708                         else if (sizeof(long)== 8){
709                                 ok1(!parse_args(&argc, &argv, "-a",
710                                                 "18446744073709551616", NULL));
711                                 ok1(parse_args(&argc, &argv, "-a", "9E", NULL));
712                                 ok1(arg == 9000000000000000000ULL);
713                                 ok1(!parse_args(&argc, &argv, "-a", "19E", NULL));
714                         }
715                         else
716                                 fail("FIXME: Handle other long sizes");
717                 }
718
719                 /* opt_set_longval_si */
720                 {
721                         long int arg = 1000;
722
723                         reset_options();
724                         opt_register_arg("-a", opt_set_longval_si, NULL,
725                                          &arg, "All");
726                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
727                         ok1(arg == 9999);
728                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
729                         ok1(arg == -9999);
730                         ok1(parse_args(&argc, &argv, "-a", "0P", NULL));
731                         ok1(arg == 0);
732                         ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
733                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
734                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
735                         ok1(arg == 100 * k);
736                         ok1(parse_args(&argc, &argv, "-a", "-100k", NULL));
737                         ok1(arg == -100 * k);
738                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
739                         ok1(arg == 1 * k);
740                         ok1(parse_args(&argc, &argv, "-a", "99M", NULL));
741                         ok1(arg == 99 * M);
742                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
743                         ok1(arg == 1 * G);
744                         ok1(parse_args(&argc, &argv, "-a", "-1G", NULL));
745                         ok1(arg == -1 * G);
746                         if (sizeof(long) == 4){
747                                 ok1(!parse_args(&argc, &argv, "-a", "2147483648", NULL));
748                                 ok1(!parse_args(&argc, &argv, "-a", "4G", NULL));
749                                 ok1(!parse_args(&argc, &argv, "-a", "1T", NULL));
750                                 ok1(!parse_args(&argc, &argv, "-a", "1E", NULL));
751                                 ok1(parse_args(&argc, &argv, "-a", "1999m", NULL));
752                                 ok1(arg == 1999 * M);
753                         }
754                         else if (sizeof(long)== 8){
755                                 ok1(!parse_args(&argc, &argv, "-a",
756                                                 "9223372036854775808", NULL));
757                                 ok1(!parse_args(&argc, &argv, "-a", "9224P", NULL));
758                                 ok1(parse_args(&argc, &argv, "-a", "9E", NULL));
759                                 ok1(arg == 9 * E);
760                                 ok1(parse_args(&argc, &argv, "-a", "123T", NULL));
761                                 ok1(arg == 123 * T);
762                         }
763                         else
764                                 fail("FIXME: Handle other long sizes");
765                 }
766
767
768                 /* opt_set_longlongval_si */
769                 {
770                         long long int arg = 1000;
771                         reset_options();
772                         opt_register_arg("-a", opt_set_longlongval_si, NULL,
773                                          &arg, "All");
774                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
775                         ok1(arg == 9999);
776                         ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
777                         ok1(arg == -9999);
778                         ok1(parse_args(&argc, &argv, "-a", "0T", NULL));
779                         ok1(arg == 0);
780                         ok1(!parse_args(&argc, &argv, "-a", "100crap", NULL));
781                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
782                         ok1(!parse_args(&argc, &argv, "-a", "1kk", NULL));
783                         ok1(parse_args(&argc, &argv, "-a", "100k", NULL));
784                         ok1(arg == 100 * k);
785                         ok1(parse_args(&argc, &argv, "-a", "-100k", NULL));
786                         ok1(arg == -100 * k);
787                         ok1(parse_args(&argc, &argv, "-a", "1K", NULL));
788                         ok1(arg == 1 * k);
789                         ok1(parse_args(&argc, &argv, "-a", "-333333M", NULL));
790                         ok1(arg == -333333 * M);
791                         ok1(parse_args(&argc, &argv, "-a", "1G", NULL));
792                         ok1(arg == 1 * G);
793                         ok1(parse_args(&argc, &argv, "-a", "1024t", NULL));
794                         ok1(arg == 1024 * T);
795                         ok1(parse_args(&argc, &argv, "-a", "123P", NULL));
796                         ok1(arg == 123 * P);
797                         ok1(parse_args(&argc, &argv, "-a", "-3E", NULL));
798                         ok1(arg == -3 * E);
799                         ok1(parse_args(&argc, &argv, "-a", "8E", NULL));
800                         if (sizeof(long long) == 8){
801                                 ok1(!parse_args(&argc, &argv, "-a",
802                                                 "9223372036854775808", NULL));
803                                 ok1(!parse_args(&argc, &argv, "-a",
804                                                 "10E", NULL));
805                         }
806                         else
807                                 fail("FIXME: Handle other long long int"
808                                      " sizes (specifically %zu bytes)",
809                                      sizeof(long long));
810
811                 }
812                 /* opt_set_ulonglongval_si */
813                 {
814                         unsigned long long int arg = 1000;
815                         reset_options();
816                         opt_register_arg("-a", opt_set_ulonglongval_si, NULL,
817                                          &arg, "All");
818                         ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
819                         ok1(arg == 9999);
820                         ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
821                         ok1(parse_args(&argc, &argv, "-a", "0", NULL));
822                         ok1(arg == 0);
823                         ok1(!parse_args(&argc, &argv, "-a", "1Q", NULL));
824                         ok1(!parse_args(&argc, &argv, "-a", "1kk", NULL));
825                         ok1(parse_args(&argc, &argv, "-a", "100G", NULL));
826                         ok1(arg == 100 * G);
827                         ok1(!parse_args(&argc, &argv, "-a", "-100G", NULL));
828                         ok1(parse_args(&argc, &argv, "-a", "8E", NULL));
829                 }
830                 /* opt_show_intval_si */
831                 {
832                         int i;
833                         char buf[OPT_SHOW_LEN+2] = { 0 };
834                         buf[OPT_SHOW_LEN] = '!';
835                         i = -77;
836                         opt_show_intval_si(buf, &i);
837                         ok1(strcmp(buf, "-77") == 0);
838                         i = 0;
839                         opt_show_intval_si(buf, &i);
840                         ok1(strcmp(buf, "0") == 0);
841                         ok1(buf[OPT_SHOW_LEN] == '!');
842                         i = 77;
843                         opt_show_intval_si(buf, &i);
844                         ok1(strcmp(buf, "77") == 0);
845                         ok1(buf[OPT_SHOW_LEN] == '!');
846                         i = -1234 * k;
847                         opt_show_intval_si(buf, &i);
848                         ok1(strcmp(buf, "-1234k") == 0);
849                         ok1(buf[OPT_SHOW_LEN] == '!');
850                         i = 500 * M;
851                         opt_show_intval_si(buf, &i);
852                         ok1(strcmp(buf, "500M") == 0);
853                         ok1(buf[OPT_SHOW_LEN] == '!');
854                         i = 1000 * M;
855                         opt_show_intval_si(buf, &i);
856                         ok1(strcmp(buf, "1G") == 0);
857                         ok1(buf[OPT_SHOW_LEN] == '!');
858                 }
859
860                 /* opt_show_longval_si */
861                 {
862                         long i;
863                         char buf[OPT_SHOW_LEN+2] = { 0 };
864                         buf[OPT_SHOW_LEN] = '!';
865                         i = -77;
866                         opt_show_longval_si(buf, &i);
867                         ok1(strcmp(buf, "-77") == 0);
868                         ok1(buf[OPT_SHOW_LEN] == '!');
869                         i = 77;
870                         opt_show_longval_si(buf, &i);
871                         ok1(strcmp(buf, "77") == 0);
872                         ok1(buf[OPT_SHOW_LEN] == '!');
873                         i = -1 * k;
874                         opt_show_longval_si(buf, &i);
875                         ok1(strcmp(buf, "-1k") == 0);
876                         ok1(buf[OPT_SHOW_LEN] == '!');
877                         i = 500 * M;
878                         opt_show_longval_si(buf, &i);
879                         ok1(strcmp(buf, "500M") == 0);
880                         ok1(buf[OPT_SHOW_LEN] == '!');
881                         i = 1000 * M;
882                         opt_show_longval_si(buf, &i);
883                         ok1(strcmp(buf, "1G") == 0);
884                         ok1(buf[OPT_SHOW_LEN] == '!');
885                         i = 0;
886                         opt_show_longval_si(buf, &i);
887                         ok1(strcmp(buf, "0") == 0);
888                         ok1(buf[OPT_SHOW_LEN] == '!');
889                 }
890
891                 /* opt_show_llongval_si */
892                 {
893                         long long i;
894                         char buf[OPT_SHOW_LEN+2] = { 0 };
895                         buf[OPT_SHOW_LEN] = '!';
896                         i = -7777;
897                         opt_show_longlongval_si(buf, &i);
898                         ok1(strcmp(buf, "-7777") == 0);
899                         ok1(buf[OPT_SHOW_LEN] == '!');
900                         i = 7777;
901                         opt_show_longlongval_si(buf, &i);
902                         ok1(strcmp(buf, "7777") == 0);
903                         ok1(buf[OPT_SHOW_LEN] == '!');
904                         i = -10240000 * k;
905                         opt_show_longlongval_si(buf, &i);
906                         ok1(strcmp(buf, "-10240M") == 0);
907                         ok1(buf[OPT_SHOW_LEN] == '!');
908                         i = 5 * P;
909                         opt_show_longlongval_si(buf, &i);
910                         ok1(strcmp(buf, "5P") == 0);
911                         ok1(buf[OPT_SHOW_LEN] == '!');
912                         i = 2000 * P;
913                         opt_show_longlongval_si(buf, &i);
914                         ok1(strcmp(buf, "2E") == 0);
915                         ok1(buf[OPT_SHOW_LEN] == '!');
916                 }
917
918                 /* opt_show_uintval_si */
919                 {
920                         unsigned int i;
921                         char buf[OPT_SHOW_LEN+2] = { 0 };
922                         buf[OPT_SHOW_LEN] = '!';
923                         i = 77;
924                         opt_show_uintval_si(buf, &i);
925                         ok1(strcmp(buf, "77") == 0);
926                         ok1(buf[OPT_SHOW_LEN] == '!');
927                         i = 1234 * k;
928                         opt_show_uintval_si(buf, &i);
929                         ok1(strcmp(buf, "1234k") == 0);
930                         ok1(buf[OPT_SHOW_LEN] == '!');
931                         i = 500 * M;
932                         opt_show_uintval_si(buf, &i);
933                         ok1(strcmp(buf, "500M") == 0);
934                         ok1(buf[OPT_SHOW_LEN] == '!');
935                         i = 1000 * M;
936                         opt_show_uintval_si(buf, &i);
937                         ok1(strcmp(buf, "1G") == 0);
938                         ok1(buf[OPT_SHOW_LEN] == '!');
939                 }
940
941                 /* opt_show_ulongval_si */
942                 {
943                         unsigned long i;
944                         char buf[OPT_SHOW_LEN+2] = { 0 };
945                         buf[OPT_SHOW_LEN] = '!';
946                         i = 77;
947                         opt_show_ulongval_si(buf, &i);
948                         ok1(strcmp(buf, "77") == 0);
949                         ok1(buf[OPT_SHOW_LEN] == '!');
950                         i = k;
951                         opt_show_ulongval_si(buf, &i);
952                         ok1(strcmp(buf, "1k") == 0);
953                         ok1(buf[OPT_SHOW_LEN] == '!');
954                         i = 500 * M;
955                         opt_show_ulongval_si(buf, &i);
956                         ok1(strcmp(buf, "500M") == 0);
957                         ok1(buf[OPT_SHOW_LEN] == '!');
958                         i = 1024 * M;
959                         opt_show_ulongval_si(buf, &i);
960                         ok1(strcmp(buf, "1024M") == 0);
961                         ok1(buf[OPT_SHOW_LEN] == '!');
962                         i = 0;
963                         opt_show_ulongval_si(buf, &i);
964                         ok1(strcmp(buf, "0") == 0);
965                         ok1(buf[OPT_SHOW_LEN] == '!');
966                 }
967
968                 /* opt_show_ullongval_si */
969                 {
970                         long long i;
971                         char buf[OPT_SHOW_LEN+2] = { 0 };
972                         buf[OPT_SHOW_LEN] = '!';
973                         i = 7777;
974                         opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
975                         ok1(strcmp(buf, "7777") == 0);
976                         ok1(buf[OPT_SHOW_LEN] == '!');
977                         i = 10240000 * k;
978                         opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
979                         ok1(strcmp(buf, "10240M") == 0);
980                         ok1(buf[OPT_SHOW_LEN] == '!');
981                         i = 5 * P;
982                         opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
983                         ok1(strcmp(buf, "5P") == 0);
984                         ok1(buf[OPT_SHOW_LEN] == '!');
985                         i = 1000 * P;
986                         opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
987                         ok1(strcmp(buf, "1E") == 0);
988                         ok1(buf[OPT_SHOW_LEN] == '!');
989                 }
990
991         }
992
993
994         /* opt_inc_intval */
995         {
996                 int arg = 1000;
997                 reset_options();
998                 opt_register_noarg("-a", opt_inc_intval, &arg, "");
999                 ok1(parse_args(&argc, &argv, "-a", NULL));
1000                 ok1(arg == 1001);
1001                 ok1(parse_args(&argc, &argv, "-a", "-a", NULL));
1002                 ok1(arg == 1003);
1003                 ok1(parse_args(&argc, &argv, "-aa", NULL));
1004                 ok1(arg == 1005);
1005         }
1006
1007         /* opt_show_version_and_exit. */
1008         {
1009                 int exitval;
1010                 reset_options();
1011                 opt_register_noarg("-a",
1012                                    opt_version_and_exit, "1.2.3", "");
1013                 /* parse_args allocates argv */
1014                 free(argv);
1015
1016                 set_args(&argc, &argv, "thisprog", "-a", NULL);
1017
1018                 exitval = setjmp(exited);
1019                 if (exitval == 0) {
1020                         opt_parse(&argc, argv, save_err_output);
1021                         fail("opt_show_version_and_exit returned?");
1022                 } else {
1023                         ok1(exitval - 1 == 0);
1024                         /* We should have freed table!. */
1025                         ok1(opt_table == NULL);
1026                 }
1027                 ok1(strcmp(output, "1.2.3\n") == 0);
1028                 free(output);
1029                 free(argv);
1030                 output = NULL;
1031         }
1032
1033         /* opt_usage_and_exit. */
1034         {
1035                 int exitval;
1036                 reset_options();
1037                 opt_register_noarg("-a",
1038                                    opt_usage_and_exit, "[args]", "");
1039
1040                 set_args(&argc, &argv, "thisprog", "-a", NULL);
1041
1042                 exitval = setjmp(exited);
1043                 if (exitval == 0) {
1044                         opt_parse(&argc, argv, save_err_output);
1045                         fail("opt_usage_and_exit returned?");
1046                 } else {
1047                         ok1(exitval - 1 == 0);
1048                         /* We should have freed table!. */
1049                         ok1(opt_table == NULL);
1050                 }
1051                 ok1(strstr(output, "[args]"));
1052                 ok1(strstr(output, argv[0]));
1053                 ok1(strstr(output, "\n-a"));
1054                 free(output);
1055                 free(argv);
1056                 output = NULL;
1057         }
1058
1059         /* opt_show_bool */
1060         {
1061                 bool b;
1062                 char buf[OPT_SHOW_LEN+2] = { 0 };
1063                 buf[OPT_SHOW_LEN] = '!';
1064
1065                 b = true;
1066                 opt_show_bool(buf, &b);
1067                 ok1(strcmp(buf, "true") == 0);
1068                 ok1(buf[OPT_SHOW_LEN] == '!');
1069
1070                 b = false;
1071                 opt_show_bool(buf, &b);
1072                 ok1(strcmp(buf, "false") == 0);
1073                 ok1(buf[OPT_SHOW_LEN] == '!');
1074         }
1075
1076         /* opt_show_invbool */
1077         {
1078                 bool b;
1079                 char buf[OPT_SHOW_LEN+2] = { 0 };
1080                 buf[OPT_SHOW_LEN] = '!';
1081
1082                 b = true;
1083                 opt_show_invbool(buf, &b);
1084                 ok1(strcmp(buf, "false") == 0);
1085                 ok1(buf[OPT_SHOW_LEN] == '!');
1086
1087                 b = false;
1088                 opt_show_invbool(buf, &b);
1089                 ok1(strcmp(buf, "true") == 0);
1090                 ok1(buf[OPT_SHOW_LEN] == '!');
1091         }
1092
1093         /* opt_show_charp */
1094         {
1095                 char str[OPT_SHOW_LEN*2], *p;
1096                 char buf[OPT_SHOW_LEN+2] = { 0 };
1097                 buf[OPT_SHOW_LEN] = '!';
1098
1099                 /* Short test. */
1100                 p = str;
1101                 strcpy(p, "short");
1102                 opt_show_charp(buf, &p);
1103                 ok1(strcmp(buf, "\"short\"") == 0);
1104                 ok1(buf[OPT_SHOW_LEN] == '!');
1105
1106                 /* Truncate test. */
1107                 memset(p, 'x', OPT_SHOW_LEN*2);
1108                 p[OPT_SHOW_LEN*2-1] = '\0';
1109                 opt_show_charp(buf, &p);
1110                 ok1(buf[0] == '"');
1111                 ok1(buf[OPT_SHOW_LEN-1] == '"');
1112                 ok1(buf[OPT_SHOW_LEN] == '!');
1113                 ok1(strspn(buf+1, "x") == OPT_SHOW_LEN-2);
1114         }
1115
1116         /* opt_show_intval */
1117         {
1118                 int i;
1119                 char buf[OPT_SHOW_LEN+2] = { 0 };
1120                 buf[OPT_SHOW_LEN] = '!';
1121
1122                 i = -77;
1123                 opt_show_intval(buf, &i);
1124                 ok1(strcmp(buf, "-77") == 0);
1125                 ok1(buf[OPT_SHOW_LEN] == '!');
1126
1127                 i = 77;
1128                 opt_show_intval(buf, &i);
1129                 ok1(strcmp(buf, "77") == 0);
1130                 ok1(buf[OPT_SHOW_LEN] == '!');
1131         }
1132
1133         /* opt_show_uintval */
1134         {
1135                 unsigned int ui;
1136                 char buf[OPT_SHOW_LEN+2] = { 0 };
1137                 buf[OPT_SHOW_LEN] = '!';
1138
1139                 ui = 4294967295U;
1140                 opt_show_uintval(buf, &ui);
1141                 ok1(strcmp(buf, "4294967295") == 0);
1142                 ok1(buf[OPT_SHOW_LEN] == '!');
1143         }
1144
1145         /* opt_show_longval */
1146         {
1147                 long l;
1148                 char buf[OPT_SHOW_LEN+2] = { 0 };
1149                 buf[OPT_SHOW_LEN] = '!';
1150
1151                 l = 1234567890L;
1152                 opt_show_longval(buf, &l);
1153                 ok1(strcmp(buf, "1234567890") == 0);
1154                 ok1(buf[OPT_SHOW_LEN] == '!');
1155         }
1156
1157         /* opt_show_ulongval */
1158         {
1159                 unsigned long ul;
1160                 char buf[OPT_SHOW_LEN+2] = { 0 };
1161                 buf[OPT_SHOW_LEN] = '!';
1162
1163                 ul = 4294967295UL;
1164                 opt_show_ulongval(buf, &ul);
1165                 ok1(strcmp(buf, "4294967295") == 0);
1166                 ok1(buf[OPT_SHOW_LEN] == '!');
1167         }
1168
1169         /* opt_show_floatval */
1170         {
1171                 float f;
1172                 char buf[OPT_SHOW_LEN+2] = { 0 };
1173                 buf[OPT_SHOW_LEN] = '!';
1174
1175                 f = -77.5;
1176                 opt_show_floatval(buf, &f);
1177                 ok1(strcmp(buf, "-77.500000") == 0);
1178                 ok1(buf[OPT_SHOW_LEN] == '!');
1179
1180                 f = 77.5;
1181                 opt_show_floatval(buf, &f);
1182                 ok1(strcmp(buf, "77.500000") == 0);
1183                 ok1(buf[OPT_SHOW_LEN] == '!');
1184         }
1185
1186         /* opt_show_doubleval */
1187         {
1188                 double d;
1189                 char buf[OPT_SHOW_LEN+2] = { 0 };
1190                 buf[OPT_SHOW_LEN] = '!';
1191
1192                 d = -77;
1193                 opt_show_doubleval(buf, &d);
1194                 ok1(strcmp(buf, "-77.000000") == 0);
1195                 ok1(buf[OPT_SHOW_LEN] == '!');
1196
1197                 d = 77;
1198                 opt_show_doubleval(buf, &d);
1199                 ok1(strcmp(buf, "77.000000") == 0);
1200                 ok1(buf[OPT_SHOW_LEN] == '!');
1201         }
1202
1203         /* opt_log_stderr. */
1204         {
1205                 reset_options();
1206                 opt_register_noarg("-a",
1207                                    opt_usage_and_exit, "[args]", "");
1208
1209                 set_args(&argc, &argv, "thisprog", "--garbage", NULL);
1210                 ok1(!opt_parse(&argc, argv, opt_log_stderr));
1211                 ok1(!strcmp(output,
1212                             "thisprog: --garbage: unrecognized option\n"));
1213                 free(output);
1214                 free(argv);
1215                 output = NULL;
1216         }
1217
1218         /* opt_log_stderr_exit. */
1219         {
1220                 int exitval;
1221                 reset_options();
1222                 opt_register_noarg("-a",
1223                                    opt_usage_and_exit, "[args]", "");
1224                 set_args(&argc, &argv, "thisprog", "--garbage", NULL);
1225                 exitval = setjmp(exited);
1226                 if (exitval == 0) {
1227                         opt_parse(&argc, argv, opt_log_stderr_exit);
1228                         fail("opt_log_stderr_exit returned?");
1229                 } else {
1230                         ok1(exitval - 1 == 1);
1231                 }
1232                 free(argv);
1233                 ok1(!strcmp(output,
1234                             "thisprog: --garbage: unrecognized option\n"));
1235                 free(output);
1236                 output = NULL;
1237         }
1238
1239         //diag("%s\n", err_output);
1240         return exit_status();
1241 }