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