*** empty log message ***
[arla.git] / tests / ga-test.c
blob1c5a1474267ded0c2ca365a68ef1fa5fe270f585
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.
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.
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.
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.
35 * Test if agetarg works as expected
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
42 #include <stdio.h>
44 #include <err.h>
45 #include <roken.h>
47 #include <agetarg.h>
49 RCSID("$Id$");
51 typedef struct {
52 int style;
53 int argc;
54 char *argv[10];
55 enum { GA_SUCCESS = 0, GA_FAILURE } retval ;
56 } ga_tests;
59 /* XXX TODO: aarg_negative_flag, manualpage generation ? */
65 static void
66 test_simple_string (void)
68 char *string;
69 int i, optind;
70 ga_tests tests[] = {
71 { AARG_GNUSTYLE, 2, { "string", "--string=foo", NULL } },
72 { AARG_GNUSTYLE, 3, { "string", "-s", "foo", NULL} },
73 { AARG_AFSSTYLE, 3, { "string", "-string", "foo", NULL} },
74 { AARG_AFSSTYLE, 3, { "string", "-strin", "foo", NULL} },
75 { AARG_AFSSTYLE, 3, { "string", "-st", "foo", NULL}, GA_FAILURE },
76 { AARG_AFSSTYLE, 2, { "string", "--flag"}, GA_FAILURE },
77 { AARG_AFSSTYLE, 2, { "string", "foo", NULL} }
80 struct agetargs args[] = {
81 { "string", 's', aarg_string, NULL,
82 "string test", "stringfoo", aarg_mandatory},
83 { "strip", 0, aarg_string, NULL,
84 "strip test", "stripfoo", aarg_optional},
85 { NULL, 0, aarg_end, NULL, NULL }
86 }, *a = args;
88 a->value = &string;
90 for (i = 0 ; i < sizeof(tests)/sizeof(*tests); i++) {
91 string = NULL;
92 optind = 0;
94 if (agetarg (args, tests[i].argc, tests[i].argv, &optind,
95 tests[i].style)) {
96 if (tests[i].retval == GA_FAILURE)
97 continue;
98 warnx ("test_string: %s failed for test %d",
99 tests[i].argv[1], i);
100 continue;
101 } else {
102 if (tests[i].retval != GA_SUCCESS) {
103 warnx ("test_string: %s failed to fail for test %d",
104 tests[i].argv[1], i);
105 continue;
109 if (optind != tests[i].argc) {
110 warnx ("argc != optind for test %s, %d", tests[i].argv[1], i);
111 continue;
114 if (string == NULL || strcmp (string, "foo") != 0) {
115 warnx ("error parsing for test %d: string", i);
116 continue;
125 static void
126 test_simple_strings (void)
128 agetarg_strings strings;
130 int i, optind;
131 ga_tests tests[] = {
132 { AARG_GNUSTYLE, 3, { "strings",
133 "--strings=foo", "--strings=bar", NULL } },
134 { AARG_GNUSTYLE, 5, { "strings", "-s", "foo", "-s", "bar", NULL} },
135 { AARG_AFSSTYLE, 4, { "strings", "-string", "foo", "bar", NULL} }
136 #if 0
137 { AARG_AFSSTYLE, 3, { "strings", "foo", "bar", NULL} }
138 #endif
141 struct agetargs args[] = {
142 { "strings", 's', aarg_strings, NULL,
143 "strings test", "stringsfoo", aarg_optional},
144 { NULL, 0, aarg_end, NULL, NULL }
145 }, *a = args;
147 a->value = &strings;
149 for (i = 0 ; i < sizeof(tests)/sizeof(*tests); i++) {
150 strings.num_strings = 0;
151 strings.strings = NULL;
152 optind = 0;
154 if (agetarg (args, tests[i].argc, tests[i].argv, &optind,
155 tests[i].style)) {
156 if (tests[i].retval == GA_FAILURE)
157 continue;
158 warnx ("test_strings: %s failed for test %d",
159 tests[i].argv[1], i);
160 continue;
161 } else {
162 if (tests[i].retval != GA_SUCCESS) {
163 warnx ("test_strings: %s failed to fail for test %d",
164 tests[i].argv[1], i);
165 continue;
169 if (optind != tests[i].argc) {
170 warnx ("argc != optind for test %s, %d",
171 tests[i].argv[1], i);
172 continue;
175 if (strings.num_strings != 2
176 || strcmp(strings.strings[0], "foo") != 0
177 || strcmp(strings.strings[1], "bar") != 0)
179 warnx ("error parsing for test %d: strings", i);
180 continue;
189 static void
190 test_simple_integer (void)
192 int integer;
193 int i, optind;
194 ga_tests tests[] = {
195 { AARG_GNUSTYLE, 2, { "integer", "--integer=4711", NULL } },
196 { AARG_GNUSTYLE, 3, { "integer", "-i", "4711", NULL} },
197 { AARG_AFSSTYLE, 3, { "integer", "-integer", "4711", NULL} },
198 { AARG_AFSSTYLE, 2, { "integer", "4711", NULL} }
201 struct agetargs args[] = {
202 { "integer", 'i', aarg_integer, NULL,
203 "integer test", "integer", aarg_mandatory},
204 { NULL, 0, aarg_end, NULL, NULL }
205 }, *a = args;
207 a->value = &integer;
209 for (i = 0 ; i < sizeof(tests)/sizeof(*tests); i++) {
210 integer = 0;
211 optind = 0;
213 if (agetarg (args, tests[i].argc, tests[i].argv, &optind,
214 tests[i].style)) {
215 if (tests[i].retval == GA_FAILURE)
216 continue;
217 warnx ("test_integer: %s failed for test %d",
218 tests[i].argv[1], i);
219 continue;
220 } else {
221 if (tests[i].retval != GA_SUCCESS) {
222 warnx ("test_integer: %s failed to fail for test %d",
223 tests[i].argv[1], i);
224 continue;
228 if (optind != tests[i].argc) {
229 warnx ("argc != optind for test %s, %d",
230 tests[i].argv[1], i);
231 continue;
234 if (integer != 4711) {
235 warnx ("error parsing for test %d: integer 4711", i);
236 continue;
245 static void
246 test_simple_flag (void)
248 int flag;
249 int i, optind;
250 ga_tests tests[] = {
251 { AARG_GNUSTYLE, 2, { "flag", "--flag=yes", NULL }, GA_SUCCESS },
252 { AARG_GNUSTYLE, 2, { "flag", "-g", NULL}, GA_SUCCESS },
253 { AARG_AFSSTYLE, 2, { "flag", "--flag"}, GA_FAILURE },
254 { AARG_AFSSTYLE, 2, { "flag", "-flag", NULL}, GA_SUCCESS },
255 #if 0
256 /* XXX */
257 { AARG_AFSSTYLE, 2, { "flag", "yes", NULL}, GA_SUCCESS },
258 #endif
259 { AARG_GNUSTYLE, 2, { "flag", "--no-flag", NULL}, GA_SUCCESS }
262 struct agetargs args[] = {
263 { "flag", 'g', aarg_flag, NULL,
264 "flag", "flag bar", aarg_optional},
265 { NULL, 0, aarg_end, NULL, NULL }
266 }, *a = args;
268 a->value = &flag;
270 for (i = 0 ; i < sizeof(tests)/sizeof(*tests); i++) {
271 if (i < 4)
272 flag = 0;
273 else
274 flag = 1;
275 optind = 0;
277 if (agetarg (args, tests[i].argc, tests[i].argv, &optind,
278 tests[i].style)) {
279 if (tests[i].retval == GA_FAILURE)
280 continue;
281 warnx ("test_flag: %s failed for test %d",
282 tests[i].argv[1], i);
283 continue;
284 } else {
285 if (tests[i].retval != GA_SUCCESS) {
286 warnx ("test_flag: %s failed to fail for test %d",
287 tests[i].argv[1], i);
288 continue;
292 if (optind != tests[i].argc) {
293 warnx ("argc != optind for test %s, %d",
294 tests[i].argv[1], i);
295 continue;
298 if (i < 4) {
299 if (flag == 0) {
300 warnx ("error parsing for test %d: flag %s",
302 tests[i].argv[1]);
303 continue;
305 } else {
306 if (flag != 0) {
307 warnx ("error parsing test %d: flag %s",
309 tests[i].argv[1]);
310 continue;
321 main (int argc, char **argv)
323 setprogname (argv[0]);
325 test_simple_string();
326 test_simple_strings();
327 test_simple_integer();
328 test_simple_flag();
330 return 0;