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