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