opt: wean off getopt_long, beef up tests.
[ccan] / ccan / opt / test / run-iter.c
1 #define _GNU_SOURCE
2 #include <ccan/tap/tap.h>
3 #include <stdarg.h>
4 #include <setjmp.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include "utils.h"
8 #include <ccan/opt/opt.c>
9 #include <ccan/opt/usage.c>
10 #include <ccan/opt/helpers.c>
11 #include <ccan/opt/parse.c>
12
13 static void reset_options(void)
14 {
15         free(opt_table);
16         opt_table = NULL;
17         opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
18 }
19
20 /* Test iterators. */
21 int main(int argc, char *argv[])
22 {
23         unsigned j, i, len;
24         const char *p;
25
26         plan_tests(37 * 2);
27         for (j = 0; j < 2; j ++) {
28                 reset_options();
29                 /* Giving subtable a title makes an extra entry! */
30                 opt_register_table(subtables, j == 0 ? NULL : "subtable");
31
32                 p = first_lopt(&i, &len);
33                 ok1(i == j + 0);
34                 ok1(len == 3);
35                 ok1(strncmp(p, "jjj", len) == 0);
36                 p = next_lopt(p, &i, &len);
37                 ok1(i == j + 0);
38                 ok1(len == 3);
39                 ok1(strncmp(p, "lll", len) == 0);
40                 p = next_lopt(p, &i, &len);
41                 ok1(i == j + 1);
42                 ok1(len == 3);
43                 ok1(strncmp(p, "mmm", len) == 0);
44                 p = next_lopt(p, &i, &len);
45                 ok1(i == j + 5);
46                 ok1(len == 3);
47                 ok1(strncmp(p, "ddd", len) == 0);
48                 p = next_lopt(p, &i, &len);
49                 ok1(i == j + 6);
50                 ok1(len == 3);
51                 ok1(strncmp(p, "eee", len) == 0);
52                 p = next_lopt(p, &i, &len);
53                 ok1(i == j + 7);
54                 ok1(len == 3);
55                 ok1(strncmp(p, "ggg", len) == 0);
56                 p = next_lopt(p, &i, &len);
57                 ok1(i == j + 8);
58                 ok1(len == 3);
59                 ok1(strncmp(p, "hhh", len) == 0);
60                 p = next_lopt(p, &i, &len);
61                 ok1(!p);
62
63                 p = first_sopt(&i);
64                 ok1(i == j + 0);
65                 ok1(*p == 'j');
66                 p = next_sopt(p, &i);
67                 ok1(i == j + 0);
68                 ok1(*p == 'l');
69                 p = next_sopt(p, &i);
70                 ok1(i == j + 1);
71                 ok1(*p == 'm');
72                 p = next_sopt(p, &i);
73                 ok1(i == j + 2);
74                 ok1(*p == 'a');
75                 p = next_sopt(p, &i);
76                 ok1(i == j + 3);
77                 ok1(*p == 'b');
78                 p = next_sopt(p, &i);
79                 ok1(i == j + 7);
80                 ok1(*p == 'g');
81                 p = next_sopt(p, &i);
82                 ok1(i == j + 8);
83                 ok1(*p == 'h');
84                 p = next_sopt(p, &i);
85                 ok1(!p);
86         }
87
88         return exit_status();
89 }