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