Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / ga-test.c
1 /*
2 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * Test if agetarg works as expected
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <stdio.h>
43
44 #include <err.h>
45
46 #include <agetarg.h>
47
48
49 typedef struct {
50 int style;
51 int argc;
52 char *argv[10];
53 enum { GA_SUCCESS = 0, GA_FAILURE } retval;
54 } ga_tests;
55
56
57 /* XXX TODO: aarg_negative_flag, manualpage generation ? */
58
59 /*
60 *
61 */
62
63 static void
64 test_simple_string(void)
65 {
66 char *string;
67 int i, optind;
68 ga_tests tests[] = {
69 {AARG_GNUSTYLE, 2, {"string", "--string=foo", NULL}},
70 {AARG_GNUSTYLE, 3, {"string", "-s", "foo", NULL}},
71 {AARG_AFSSTYLE, 3, {"string", "-string", "foo", NULL}},
72 {AARG_AFSSTYLE, 3, {"string", "-strin", "foo", NULL}},
73 {AARG_AFSSTYLE, 3, {"string", "-st", "foo", NULL}, GA_FAILURE},
74 {AARG_AFSSTYLE, 2, {"string", "--flag"}, GA_FAILURE},
75 {AARG_AFSSTYLE, 2, {"string", "foo", NULL}}
76 };
77
78 struct agetargs args[] = {
79 {"string", 's', aarg_string, NULL,
80 "string test", "stringfoo", aarg_mandatory},
81 {"strip", 0, aarg_string, NULL,
82 "strip test", "stripfoo", aarg_optional},
83 {NULL, 0, aarg_end, NULL, NULL}
84 }, *a = args;
85
86 a->value = &string;
87
88 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
89 string = NULL;
90 optind = 0;
91
92 if (agetarg
93 (args, tests[i].argc, tests[i].argv, &optind, tests[i].style)) {
94 if (tests[i].retval == GA_FAILURE)
95 continue;
96 warnx("test_string: %s failed for test %d", tests[i].argv[1], i);
97 continue;
98 } else {
99 if (tests[i].retval != GA_SUCCESS) {
100 warnx("test_string: %s failed to fail for test %d",
101 tests[i].argv[1], i);
102 continue;
103 }
104 }
105
106 if (optind != tests[i].argc) {
107 warnx("argc != optind for test %s, %d", tests[i].argv[1], i);
108 continue;
109 }
110
111 if (string == NULL || strcmp(string, "foo") != 0) {
112 warnx("error parsing for test %d: string", i);
113 continue;
114 }
115 }
116 }
117
118 /*
119 *
120 */
121
122 static void
123 test_simple_strings(void)
124 {
125 agetarg_strings strings;
126
127 int i, optind;
128 ga_tests tests[] = {
129 {AARG_GNUSTYLE, 3, {"strings",
130 "--strings=foo", "--strings=bar", NULL}},
131 {AARG_GNUSTYLE, 5, {"strings", "-s", "foo", "-s", "bar", NULL}},
132 {AARG_AFSSTYLE, 4, {"strings", "-string", "foo", "bar", NULL}}
133 #if 0
134 {AARG_AFSSTYLE, 3, {"strings", "foo", "bar", NULL}}
135 #endif
136 };
137
138 struct agetargs args[] = {
139 {"strings", 's', aarg_strings, NULL,
140 "strings test", "stringsfoo", aarg_optional},
141 {NULL, 0, aarg_end, NULL, NULL}
142 }, *a = args;
143
144 a->value = &strings;
145
146 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
147 strings.num_strings = 0;
148 strings.strings = NULL;
149 optind = 0;
150
151 if (agetarg
152 (args, tests[i].argc, tests[i].argv, &optind, tests[i].style)) {
153 if (tests[i].retval == GA_FAILURE)
154 continue;
155 warnx("test_strings: %s failed for test %d", tests[i].argv[1], i);
156 continue;
157 } else {
158 if (tests[i].retval != GA_SUCCESS) {
159 warnx("test_strings: %s failed to fail for test %d",
160 tests[i].argv[1], i);
161 continue;
162 }
163 }
164
165 if (optind != tests[i].argc) {
166 warnx("argc != optind for test %s, %d", tests[i].argv[1], i);
167 continue;
168 }
169
170 if (strings.num_strings != 2 || strcmp(strings.strings[0], "foo") != 0
171 || strcmp(strings.strings[1], "bar") != 0) {
172 warnx("error parsing for test %d: strings", i);
173 continue;
174 }
175 }
176 }
177
178 /*
179 *
180 */
181
182 static void
183 test_simple_integer(void)
184 {
185 int integer;
186 int i, optind;
187 ga_tests tests[] = {
188 {AARG_GNUSTYLE, 2, {"integer", "--integer=4711", NULL}},
189 {AARG_GNUSTYLE, 3, {"integer", "-i", "4711", NULL}},
190 {AARG_AFSSTYLE, 3, {"integer", "-integer", "4711", NULL}},
191 {AARG_AFSSTYLE, 2, {"integer", "4711", NULL}}
192 };
193
194 struct agetargs args[] = {
195 {"integer", 'i', aarg_integer, NULL,
196 "integer test", "integer", aarg_mandatory},
197 {NULL, 0, aarg_end, NULL, NULL}
198 }, *a = args;
199
200 a->value = &integer;
201
202 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
203 integer = 0;
204 optind = 0;
205
206 if (agetarg
207 (args, tests[i].argc, tests[i].argv, &optind, tests[i].style)) {
208 if (tests[i].retval == GA_FAILURE)
209 continue;
210 warnx("test_integer: %s failed for test %d", tests[i].argv[1], i);
211 continue;
212 } else {
213 if (tests[i].retval != GA_SUCCESS) {
214 warnx("test_integer: %s failed to fail for test %d",
215 tests[i].argv[1], i);
216 continue;
217 }
218 }
219
220 if (optind != tests[i].argc) {
221 warnx("argc != optind for test %s, %d", tests[i].argv[1], i);
222 continue;
223 }
224
225 if (integer != 4711) {
226 warnx("error parsing for test %d: integer 4711", i);
227 continue;
228 }
229 }
230 }
231
232 /*
233 *
234 */
235
236 static void
237 test_simple_flag(void)
238 {
239 int flag;
240 int i, optind;
241 ga_tests tests[] = {
242 {AARG_GNUSTYLE, 2, {"flag", "--flag=yes", NULL}, GA_SUCCESS},
243 {AARG_GNUSTYLE, 2, {"flag", "-g", NULL}, GA_SUCCESS},
244 {AARG_AFSSTYLE, 2, {"flag", "--flag"}, GA_FAILURE},
245 {AARG_AFSSTYLE, 2, {"flag", "-flag", NULL}, GA_SUCCESS},
246 #if 0
247 /* XXX */
248 {AARG_AFSSTYLE, 2, {"flag", "yes", NULL}, GA_SUCCESS},
249 #endif
250 {AARG_GNUSTYLE, 2, {"flag", "--no-flag", NULL}, GA_SUCCESS}
251 };
252
253 struct agetargs args[] = {
254 {"flag", 'g', aarg_flag, NULL,
255 "flag", "flag bar", aarg_optional},
256 {NULL, 0, aarg_end, NULL, NULL}
257 }, *a = args;
258
259 a->value = &flag;
260
261 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
262 if (i < 4)
263 flag = 0;
264 else
265 flag = 1;
266 optind = 0;
267
268 if (agetarg
269 (args, tests[i].argc, tests[i].argv, &optind, tests[i].style)) {
270 if (tests[i].retval == GA_FAILURE)
271 continue;
272 warnx("test_flag: %s failed for test %d", tests[i].argv[1], i);
273 continue;
274 } else {
275 if (tests[i].retval != GA_SUCCESS) {
276 warnx("test_flag: %s failed to fail for test %d",
277 tests[i].argv[1], i);
278 continue;
279 }
280 }
281
282 if (optind != tests[i].argc) {
283 warnx("argc != optind for test %s, %d", tests[i].argv[1], i);
284 continue;
285 }
286
287 if (i < 4) {
288 if (flag == 0) {
289 warnx("error parsing for test %d: flag %s", i,
290 tests[i].argv[1]);
291 continue;
292 }
293 } else {
294 if (flag != 0) {
295 warnx("error parsing test %d: flag %s", i, tests[i].argv[1]);
296 continue;
297 }
298 }
299 }
300 }
301
302 /*
303 *
304 */
305
306 int
307 main(int argc, char **argv)
308 {
309
310 test_simple_string();
311 test_simple_strings();
312 test_simple_integer();
313 test_simple_flag();
314
315 return 0;
316 }