]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/run-add_desc.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / opt / test / run-add_desc.c
1 #include <ccan/tap/tap.h>
2 #include <ccan/opt/opt.c>
3 #include <ccan/opt/usage.c>
4 #include <ccan/opt/helpers.c>
5 #include <ccan/opt/parse.c>
6
7 static bool show_10(char *buf, size_t len, const void *arg UNNEEDED)
8 {
9         memset(buf, 'X', 10);
10         buf[10] = '\0';
11         return true;
12 }
13
14 static bool show_10_false(char *buf, size_t len, const void *arg UNNEEDED)
15 {
16         memset(buf, 'X', 10);
17         buf[10] = '\0';
18         return false;
19 }
20
21 static bool show_max(char *buf, size_t len, const void *arg UNNEEDED)
22 {
23         memset(buf, 'X', OPT_SHOW_LEN);
24         return true;
25 }
26
27 /* Test add_desc helper. */
28 int main(void)
29 {
30         struct opt_table opt;
31         char *ret;
32         size_t len, max;
33
34         plan_tests(32);
35
36         opt.show = NULL;
37         opt.names = "01234";
38         opt.desc = "0123456789 0";
39         opt.type = OPT_NOARG;
40         len = max = 0;
41
42         /* Fits easily. */
43         ret = add_desc(NULL, &len, &max, 10, 30, &opt);
44         ok1(len < max);
45         ret[len] = '\0';
46         ok1(strcmp(ret, "01234     0123456789 0\n") == 0);
47         free(ret); len = max = 0;
48
49         /* Name just fits. */
50         ret = add_desc(NULL, &len, &max, 7, 30, &opt);
51         ok1(len < max);
52         ret[len] = '\0';
53         ok1(strcmp(ret, "01234  0123456789 0\n") == 0);
54         free(ret); len = max = 0;
55
56         /* Name doesn't fit. */
57         ret = add_desc(NULL, &len, &max, 6, 30, &opt);
58         ok1(len < max);
59         ret[len] = '\0';
60         ok1(strcmp(ret,
61                    "01234\n"
62                    "      0123456789 0\n") == 0);
63         free(ret); len = max = 0;
64
65         /* Description just fits. */
66         ret = add_desc(NULL, &len, &max, 7, 19, &opt);
67         ok1(len < max);
68         ret[len] = '\0';
69         ok1(strcmp(ret, "01234  0123456789 0\n") == 0);
70         free(ret); len = max = 0;
71
72         /* Description doesn't quite fit. */
73         ret = add_desc(NULL, &len, &max, 7, 18, &opt);
74         ok1(len < max);
75         ret[len] = '\0';
76         ok1(strcmp(ret,
77                    "01234  0123456789\n"
78                    "       0\n") == 0);
79         free(ret); len = max = 0;
80
81         /* Neither quite fits. */
82         ret = add_desc(NULL, &len, &max, 6, 17, &opt);
83         ok1(len < max);
84         ret[len] = '\0';
85         ok1(strcmp(ret, 
86                    "01234\n"
87                    "      0123456789\n"
88                    "      0\n") == 0);
89         free(ret); len = max = 0;
90
91         /* With show function, fits just. */
92         opt.show = show_10;
93         ret = add_desc(NULL, &len, &max, 7, 41, &opt);
94         ok1(len < max);
95         ret[len] = '\0';
96         ok1(strcmp(ret, "01234  0123456789 0 (default: XXXXXXXXXX)\n") == 0);
97         free(ret); len = max = 0;
98
99         /* With show function, just too long. */
100         ret = add_desc(NULL, &len, &max, 7, 40, &opt);
101         ok1(len < max);
102         ret[len] = '\0';
103         ok1(strcmp(ret,
104                    "01234  0123456789 0\n"
105                    "        (default: XXXXXXXXXX)\n") == 0);
106         free(ret); len = max = 0;
107
108         /* With maximal show function, fits just (we assume OPT_SHOW_LEN = 80. */
109         opt.show = show_max;
110         ret = add_desc(NULL, &len, &max, 7, 114, &opt);
111         ok1(len < max);
112         ret[len] = '\0';
113         ok1(strcmp(ret, "01234  0123456789 0 (default: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...)\n") == 0);
114         free(ret); len = max = 0;
115
116         /* With maximal show function, just too long. */
117         ret = add_desc(NULL, &len, &max, 7, 113, &opt);
118         ok1(len < max);
119         ret[len] = '\0';
120         ok1(strcmp(ret,
121                    "01234  0123456789 0\n"
122                    "        (default: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...)\n") == 0);
123         free(ret); len = max = 0;
124
125         /* With show function which fails doesn't print. */
126         opt.show = show_10_false;
127         ret = add_desc(NULL, &len, &max, 7, 41, &opt);
128         ok1(len < max);
129         ret[len] = '\0';
130         ok1(strcmp(ret, "01234  0123456789 0\n") == 0);
131         free(ret); len = max = 0;
132
133         /* With added " <arg>".  Fits, just. */
134         opt.show = NULL;
135         opt.type = OPT_HASARG;
136         ret = add_desc(NULL, &len, &max, 13, 25, &opt);
137         ok1(len < max);
138         ret[len] = '\0';
139         ok1(strcmp(ret, "01234 <arg>  0123456789 0\n") == 0);
140         free(ret); len = max = 0;
141
142         /* With added " <arg>".  Name doesn't quite fit. */
143         ret = add_desc(NULL, &len, &max, 12, 25, &opt);
144         ok1(len < max);
145         ret[len] = '\0';
146         ok1(strcmp(ret,
147                    "01234 <arg>\n"
148                    "            0123456789 0\n") == 0);
149         free(ret); len = max = 0;
150
151         /* With added " <arg>".  Desc doesn't quite fit. */
152         ret = add_desc(NULL, &len, &max, 13, 24, &opt);
153         ok1(len < max);
154         ret[len] = '\0';
155         ok1(strcmp(ret,
156                    "01234 <arg>  0123456789\n"
157                    "             0\n") == 0);
158         free(ret); len = max = 0;
159
160         /* Empty description, with <arg> and default.  Just fits. */
161         opt.show = show_10;
162         opt.desc = "";
163         ret = add_desc(NULL, &len, &max, 13, 35, &opt);
164         ok1(len < max);
165         ret[len] = '\0';
166         ok1(strcmp(ret, "01234 <arg>   (default: XXXXXXXXXX)\n") == 0);
167         free(ret); len = max = 0;
168
169         /* Empty description, with <arg> and default.  Doesn't quite fit. */
170         opt.show = show_10;
171         opt.desc = "";
172         ret = add_desc(NULL, &len, &max, 13, 34, &opt);
173         ok1(len < max);
174         ret[len] = '\0';
175         ok1(strcmp(ret,
176                    "01234 <arg>  \n"
177                    "              (default: XXXXXXXXXX)\n") == 0);
178         free(ret); len = max = 0;
179
180         return exit_status();
181 }