Mercurial > gnulib
annotate tests/test-argp.c @ 40214:452ab00796c7
Fix undefined behaviour.
* lib/bitrotate.h (rotl16, rotr16, rotl8, rotr8): Case x to
'unsigned int', to avoid shift operations on 'int'.
* lib/xmemdup0.c (xmemdup0): Don't invoke memcpy with a zero size.
* tests/test-count-leading-zeros.c (main): Use a random number that has
as many bits as TYPE, not only 2*15 or 2*31 bits.
* tests/test-count-trailing-zeros.c (main): Likewise.
* tests/test-count-one-bits.c (main): Likewise.
* tests/test-memmem.c: Don't include "null-ptr.h".
(main): Use zerosize_ptr() instead of null_ptr().
* modules/memmem-tests (Files): Remove tests/null-ptr.h.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 09 Mar 2019 20:32:25 +0100 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
6579 | 1 /* Test suite for argp. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2006-2007, 2009-2019 Free Software Foundation, Inc. |
6579 | 3 This file is part of the GNUlib Library. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
6579 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
8 (at your option) any later version. |
6579 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8891
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
6579 | 17 |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
18 #include <config.h> |
6579 | 19 |
8285
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
20 #include "argp.h" |
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
21 |
6579 | 22 #include <stdio.h> |
23 #include <stdlib.h> | |
14186
cd4635d4dbe6
assume <ctype.h>, ..., <time.h> exist
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
24 #include <string.h> |
6579 | 25 #if HAVE_STRINGS_H |
26 # include <strings.h> | |
27 #endif | |
28 | |
18930
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
29 #include "macros.h" |
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
30 |
6579 | 31 struct test_args |
32 { | |
33 int test; | |
34 int verbose; | |
35 char *file; | |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
36 int read; |
6579 | 37 char *hidden; |
38 int opt; | |
39 char *optional; | |
40 int optional_set; | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
41 int group_2_1_option; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
42 int group_1_1_option; |
6579 | 43 }; |
44 | |
12850 | 45 static struct argp_option group1_option[] = { |
46 { NULL, 0, NULL, 0, "Option Group 1", 0 }, | |
6579 | 47 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 }, |
48 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 }, | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
49 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 }, |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
50 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 }, |
6579 | 51 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 }, |
52 { NULL, 0, NULL, 0, NULL, 0 } | |
53 }; | |
54 | |
55 static error_t | |
56 group1_parser (int key, char *arg, struct argp_state *state) | |
57 { | |
58 struct test_args *args = state->input; | |
59 | |
60 switch (key) | |
61 { | |
62 case 'v': | |
63 args->verbose++; | |
64 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
65 |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
66 case 'r': |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
67 args->read = 1; |
18930
50d1e186bf78
fallthrough: update for GCC 7/8
Jim Meyering <meyering@fb.com>
parents:
18626
diff
changeset
|
68 FALLTHROUGH; |
6579 | 69 case 'f': |
70 args->file = arg; | |
71 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
72 |
6579 | 73 case 'H': |
74 args->hidden = arg; | |
75 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
76 |
6579 | 77 default: |
78 return ARGP_ERR_UNKNOWN; | |
79 } | |
80 return 0; | |
81 } | |
82 | |
83 struct argp group1_argp = { | |
84 group1_option, | |
85 group1_parser | |
86 }; | |
87 | |
88 struct argp_child group1_child = { | |
89 &group1_argp, | |
90 0, | |
91 "", | |
92 1 | |
93 }; | |
12850 | 94 |
6579 | 95 |
12850 | 96 static struct argp_option group1_1_option[] = { |
97 { NULL, 0, NULL, 0, "Option Group 1.1", 0 }, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
98 { "cantiga", 'C', NULL, 0, "create a cantiga" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
99 { "sonet", 'S', NULL, 0, "create a sonet" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
100 { NULL, 0, NULL, 0, NULL, 0 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
101 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
102 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
103 static error_t |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
104 group1_1_parser (int key, char *arg, struct argp_state *state) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
105 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
106 struct test_args *args = state->input; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
107 switch (key) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
108 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
109 case 'C': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
110 case 'S': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
111 args->group_1_1_option = key; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
112 break; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
113 default: |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
114 return ARGP_ERR_UNKNOWN; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
115 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
116 return 0; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
117 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
118 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
119 struct argp group1_1_argp = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
120 group1_1_option, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
121 group1_1_parser |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
122 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
123 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
124 struct argp_child group1_1_child = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
125 &group1_1_argp, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
126 0, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
127 "", |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
128 2 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
129 }; |
12850 | 130 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
131 |
12850 | 132 static struct argp_option group2_option[] = { |
133 { NULL, 0, NULL, 0, "Option Group 2", 0 }, | |
6579 | 134 { "option", 'O', NULL, 0, "An option", 1 }, |
135 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
136 "Option with an optional argument. ARG is one of the following:", 2 }, |
12850 | 137 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 }, |
138 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 }, | |
139 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 }, | |
6579 | 140 { NULL, 0, NULL, 0, NULL, 0 } |
141 }; | |
142 | |
143 static error_t | |
144 group2_parser (int key, char *arg, struct argp_state *state) | |
145 { | |
146 struct test_args *args = state->input; | |
147 | |
148 switch (key) | |
149 { | |
150 case 'O': | |
151 args->opt = 1; | |
152 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
153 |
6579 | 154 case 'o': |
155 args->optional_set = 1; | |
156 args->optional = arg; | |
157 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
158 |
6579 | 159 default: |
160 return ARGP_ERR_UNKNOWN; | |
161 } | |
162 return 0; | |
163 } | |
164 | |
165 struct argp group2_argp = { | |
166 group2_option, | |
167 group2_parser | |
168 }; | |
169 | |
170 struct argp_child group2_child = { | |
171 &group2_argp, | |
172 0, | |
173 "", | |
174 2 | |
175 }; | |
12850 | 176 |
6579 | 177 |
12850 | 178 static struct argp_option group2_1_option[] = { |
179 { NULL, 0, NULL, 0, "Option Group 2.1", 0 }, | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
180 { "poem", 'p', NULL, 0, "create a poem" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
181 { "limerick", 'l', NULL, 0, "create a limerick" }, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
182 { NULL, 0, NULL, 0, NULL, 0 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
183 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
184 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
185 static error_t |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
186 group2_1_parser (int key, char *arg, struct argp_state *state) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
187 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
188 struct test_args *args = state->input; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
189 switch (key) |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
190 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
191 case 'p': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
192 case 'e': |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
193 args->group_2_1_option = key; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
194 break; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
195 default: |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
196 return ARGP_ERR_UNKNOWN; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
197 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
198 return 0; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
199 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
200 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
201 struct argp group2_1_argp = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
202 group2_1_option, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
203 group2_1_parser |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
204 }; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
205 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
206 struct argp_child group2_1_child = { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
207 &group2_1_argp, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
208 0, |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
209 "", |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
210 2 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
211 }; |
12850 | 212 |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
213 |
12850 | 214 static struct argp_option main_options[] = { |
215 { NULL, 0, NULL, 0, "Main options", 0 }, | |
216 { "test", 't', NULL, 0, NULL, 1 }, | |
217 { NULL, 0, NULL, 0, NULL, 0 } | |
218 }; | |
6579 | 219 |
220 static error_t | |
221 parse_opt (int key, char *arg, struct argp_state *state) | |
222 { | |
223 struct test_args *args = state->input; | |
224 int i; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
225 |
6579 | 226 switch (key) |
227 { | |
228 case ARGP_KEY_INIT: | |
229 for (i = 0; state->root_argp->children[i].argp; i++) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
230 state->child_inputs[i] = args; |
6579 | 231 break; |
232 | |
233 case 't': | |
234 args->test = 1; | |
235 break; | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
236 |
6579 | 237 default: |
238 return ARGP_ERR_UNKNOWN; | |
239 } | |
240 return 0; | |
241 } | |
242 | |
243 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION; | |
244 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">"; | |
245 static char doc[] = "documentation string"; | |
246 | |
247 struct argp test_argp = { | |
248 main_options, | |
249 parse_opt, | |
250 "ARGS...", | |
251 doc, | |
252 NULL, | |
253 NULL, | |
254 NULL | |
255 }; | |
256 | |
257 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1) | |
258 #define ARGV0 "test-argp" | |
259 #define init_args(a) memset (&(a), 0, sizeof (a)); | |
260 | |
261 #define INIT_TEST_COMMON(n) \ | |
262 int argc = NARGS (argv); \ | |
263 struct test_args test_args; \ | |
13051 | 264 init_args (test_args); \ |
6579 | 265 test_number = n; |
266 | |
267 #define INIT_TEST1(n, arg1) \ | |
268 char *argv[] = { ARGV0, arg1, NULL }; \ | |
13051 | 269 INIT_TEST_COMMON (n) |
6579 | 270 |
271 #define INIT_TEST2(n, arg1, arg2) \ | |
272 char *argv[] = { ARGV0, arg1, arg2, NULL }; \ | |
13051 | 273 INIT_TEST_COMMON (n) |
6579 | 274 |
275 #define INIT_TEST3(n, arg1, arg2, arg3) \ | |
276 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \ | |
13051 | 277 INIT_TEST_COMMON (n) |
6579 | 278 |
279 int test_number; | |
280 unsigned failure_count = 0; | |
281 | |
282 void | |
12850 | 283 fail (const char *msg) |
6579 | 284 { |
12850 | 285 fprintf (stderr, "Test %d: %s\n", test_number, msg); |
6579 | 286 failure_count++; |
287 } | |
288 | |
289 void | |
12850 | 290 test1 (struct argp *argp) |
6579 | 291 { |
292 INIT_TEST1 (1, "--test"); | |
293 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) | |
12850 | 294 fail ("argp_parse failed"); |
6579 | 295 else if (test_args.test != 1) |
12850 | 296 fail ("option not processed"); |
6579 | 297 } |
298 | |
299 void | |
12850 | 300 test2 (struct argp *argp) |
6579 | 301 { |
302 INIT_TEST1 (2, "-t"); | |
303 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) | |
12850 | 304 fail ("argp_parse failed"); |
6579 | 305 else if (test_args.test != 1) |
12850 | 306 fail ("option not processed"); |
6579 | 307 } |
308 | |
309 void | |
12850 | 310 test_file (struct argp *argp, int argc, char **argv, struct test_args *args) |
6579 | 311 { |
312 if (argp_parse (argp, argc, argv, 0, NULL, args)) | |
12850 | 313 fail ("argp_parse failed"); |
6579 | 314 else if (!args->file) |
12850 | 315 fail ("option not processed"); |
6579 | 316 else if (strcmp (args->file, "FILE")) |
12850 | 317 fail ("option processed incorrectly"); |
6579 | 318 } |
319 | |
320 void | |
12850 | 321 test3 (struct argp *argp) |
6579 | 322 { |
323 INIT_TEST1 (3, "--file=FILE"); | |
324 test_file (argp, argc, argv, &test_args); | |
325 } | |
326 | |
327 void | |
12850 | 328 test4 (struct argp *argp) |
6579 | 329 { |
330 INIT_TEST2 (4, "--file", "FILE"); | |
331 test_file (argp, argc, argv, &test_args); | |
332 } | |
333 | |
334 void | |
12850 | 335 test5 (struct argp *argp) |
6579 | 336 { |
337 INIT_TEST1 (5, "--input=FILE"); | |
338 test_file (argp, argc, argv, &test_args); | |
339 } | |
340 | |
341 void | |
12850 | 342 test6 (struct argp *argp) |
6579 | 343 { |
344 INIT_TEST2 (6, "--input", "FILE"); | |
345 test_file (argp, argc, argv, &test_args); | |
346 } | |
347 | |
348 void | |
12850 | 349 test_optional (struct argp *argp, int argc, char **argv, |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
350 struct test_args *args, const char *val, const char *a) |
6579 | 351 { |
352 int index; | |
353 if (argp_parse (argp, argc, argv, 0, &index, args)) | |
12850 | 354 fail ("argp_parse failed"); |
6579 | 355 else if (!args->optional_set) |
12850 | 356 fail ("option not processed"); |
6579 | 357 |
358 if (!val) | |
359 { | |
360 if (args->optional) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
361 fail ("option processed incorrectly"); |
6579 | 362 } |
363 else if (strcmp (args->optional, val)) | |
12850 | 364 fail ("option processed incorrectly"); |
6579 | 365 |
366 if (a) | |
367 { | |
368 if (index == argc) | |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
369 fail ("expected command line argument not found"); |
12850 | 370 else if (strcmp (argv[index], a)) |
15338
03e2cd1cbbe8
maint: indent with spaces, not TABs, and add a rule to check this
Jim Meyering <meyering@redhat.com>
parents:
14186
diff
changeset
|
371 fail ("expected command line argument does not match"); |
6579 | 372 } |
373 } | |
374 | |
375 void | |
12850 | 376 test7 (struct argp *argp) |
6579 | 377 { |
378 INIT_TEST1 (7, "-oARG"); | |
12850 | 379 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 380 } |
381 | |
382 void | |
12850 | 383 test8 (struct argp *argp) |
6579 | 384 { |
385 INIT_TEST2 (8, "-o", "ARG"); | |
12850 | 386 test_optional (argp, argc, argv, &test_args, NULL, "ARG"); |
6579 | 387 } |
388 | |
389 void | |
12850 | 390 test9 (struct argp *argp) |
6579 | 391 { |
392 INIT_TEST1 (9, "--optional=ARG"); | |
12850 | 393 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 394 } |
395 | |
396 void | |
12850 | 397 test10 (struct argp *argp) |
6579 | 398 { |
399 INIT_TEST2 (10, "--optional", "ARG"); | |
12850 | 400 test_optional (argp, argc, argv, &test_args, NULL, "ARG"); |
6579 | 401 } |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
402 |
6579 | 403 void |
12850 | 404 test11 (struct argp *argp) |
6579 | 405 { |
406 INIT_TEST1 (11, "--optiona=ARG"); | |
12850 | 407 test_optional (argp, argc, argv, &test_args, "ARG", NULL); |
6579 | 408 } |
409 | |
410 void | |
12850 | 411 test12 (struct argp *argp) |
6579 | 412 { |
413 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE"); | |
12850 | 414 test_optional (argp, argc, argv, &test_args, "OPT", "FILE"); |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
415 } |
6579 | 416 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
417 void |
12850 | 418 test13 (struct argp *argp) |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
419 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
420 INIT_TEST1 (1, "--cantiga"); |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
421 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) |
12850 | 422 fail ("argp_parse failed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
423 else if (test_args.group_1_1_option != 'C') |
12850 | 424 fail ("option not processed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
425 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
426 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
427 void |
12850 | 428 test14 (struct argp *argp) |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
429 { |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
430 INIT_TEST1 (1, "--limerick"); |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
431 if (argp_parse (argp, argc, argv, 0, NULL, &test_args)) |
12850 | 432 fail ("argp_parse failed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
433 else if (test_args.group_2_1_option != 'l') |
12850 | 434 fail ("option not processed"); |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
435 } |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
436 |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
437 void |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
438 test15 (struct argp *argp) |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
439 { |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
440 INIT_TEST2 (1, "-r", "FILE"); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
441 test_file (argp, argc, argv, &test_args); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
442 if (!test_args.read) |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
443 fail ("short alias not recognized properly"); |
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
444 } |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
445 |
6579 | 446 |
447 typedef void (*test_fp) (struct argp *argp); | |
448 | |
449 test_fp test_fun[] = { | |
450 test1, test2, test3, test4, | |
451 test5, test6, test7, test8, | |
452 test9, test10, test11, test12, | |
12851
72c76b99f97b
Argp: fix recognition of short alias options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
12850
diff
changeset
|
453 test13, test14, test15, |
6579 | 454 NULL |
455 }; | |
456 | |
457 int | |
458 main (int argc, char **argv) | |
459 { | |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
460 struct argp_child argp_children[3], group1_children[2], group2_children[2]; |
6579 | 461 test_fp *fun; |
8285
fd56d321b961
Provide a program_name variable on platforms other than glibc.
Bruno Haible <bruno@clisp.org>
parents:
6579
diff
changeset
|
462 |
8757
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
463 group1_children[0] = group1_1_child; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
464 group1_children[1].argp = NULL; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
465 group1_argp.children = group1_children; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
466 |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
467 group2_children[0] = group2_1_child; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
468 group2_children[1].argp = NULL; |
9f37008981ab
Fix option ordering. Test deeply clustered options.
Sergey Poznyakoff <gray@gnu.org.ua>
parents:
8285
diff
changeset
|
469 group2_argp.children = group2_children; |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
470 |
6579 | 471 argp_children[0] = group1_child; |
472 argp_children[1] = group2_child; | |
473 argp_children[2].argp = NULL; | |
474 test_argp.children = argp_children; | |
475 | |
476 if (argc > 0) | |
477 { | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
478 struct test_args test_args; |
12850 | 479 init_args (test_args); |
6579 | 480 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args); |
481 } | |
8891
633babea5f62
Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents:
8766
diff
changeset
|
482 |
6579 | 483 for (fun = test_fun; *fun; fun++) |
484 (*fun) (&test_argp); | |
485 | |
486 if (failure_count) | |
487 return 1; | |
488 | |
489 return 0; | |
490 } |