diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
commit | 0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9 (patch) | |
tree | 6b552f40eb089db96409a312a98d9b12bd669102 /src/util/test_getopt.c | |
download | gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.tar.gz gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.zip |
ng
Diffstat (limited to 'src/util/test_getopt.c')
-rw-r--r-- | src/util/test_getopt.c | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/src/util/test_getopt.c b/src/util/test_getopt.c new file mode 100644 index 000000000..89e7be863 --- /dev/null +++ b/src/util/test_getopt.c | |||
@@ -0,0 +1,239 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/test_getopt.c | ||
22 | * @brief testcase for util/getopt.c | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_common.h" | ||
26 | #include "gnunet_configuration_lib.h" | ||
27 | #include "gnunet_getopt_lib.h" | ||
28 | |||
29 | #define VERBOSE 0 | ||
30 | |||
31 | static int | ||
32 | testMinimal () | ||
33 | { | ||
34 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
35 | char *const emptyargv[] = { | ||
36 | "test", | ||
37 | NULL | ||
38 | }; | ||
39 | const struct GNUNET_GETOPT_CommandLineOption emptyoptionlist[] = { | ||
40 | GNUNET_GETOPT_OPTION_END | ||
41 | }; | ||
42 | |||
43 | cfg = GNUNET_CONFIGURATION_create (); | ||
44 | if (1 != GNUNET_GETOPT_run ("test", cfg, emptyoptionlist, 1, emptyargv)) | ||
45 | { | ||
46 | GNUNET_CONFIGURATION_destroy (cfg); | ||
47 | return 1; | ||
48 | } | ||
49 | GNUNET_CONFIGURATION_destroy (cfg); | ||
50 | |||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | static int | ||
55 | testVerbose () | ||
56 | { | ||
57 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
58 | char *const myargv[] = { | ||
59 | "test", | ||
60 | "-V", | ||
61 | "-V", | ||
62 | "more", | ||
63 | NULL | ||
64 | }; | ||
65 | unsigned int vflags = 0; | ||
66 | const struct GNUNET_GETOPT_CommandLineOption verboseoptionlist[] = { | ||
67 | GNUNET_GETOPT_OPTION_VERBOSE (&vflags), | ||
68 | GNUNET_GETOPT_OPTION_END | ||
69 | }; | ||
70 | |||
71 | cfg = GNUNET_CONFIGURATION_create (); | ||
72 | if (3 != GNUNET_GETOPT_run ("test", cfg, verboseoptionlist, 4, myargv)) | ||
73 | { | ||
74 | GNUNET_break (0); | ||
75 | GNUNET_CONFIGURATION_destroy (cfg); | ||
76 | return 1; | ||
77 | } | ||
78 | GNUNET_CONFIGURATION_destroy (cfg); | ||
79 | if (vflags != 2) | ||
80 | { | ||
81 | GNUNET_break (0); | ||
82 | return 1; | ||
83 | } | ||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | static int | ||
88 | testVersion () | ||
89 | { | ||
90 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
91 | char *const myargv[] = { | ||
92 | "test_getopt", | ||
93 | "-v", | ||
94 | NULL | ||
95 | }; | ||
96 | const struct GNUNET_GETOPT_CommandLineOption versionoptionlist[] = { | ||
97 | GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION), | ||
98 | GNUNET_GETOPT_OPTION_END | ||
99 | }; | ||
100 | |||
101 | cfg = GNUNET_CONFIGURATION_create (); | ||
102 | if (-1 != GNUNET_GETOPT_run ("test_getopt", | ||
103 | cfg, versionoptionlist, 2, myargv)) | ||
104 | { | ||
105 | GNUNET_break (0); | ||
106 | GNUNET_CONFIGURATION_destroy (cfg); | ||
107 | return 1; | ||
108 | } | ||
109 | GNUNET_CONFIGURATION_destroy (cfg); | ||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | static int | ||
114 | testAbout () | ||
115 | { | ||
116 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
117 | char *const myargv[] = { | ||
118 | "test_getopt", | ||
119 | "-h", | ||
120 | NULL | ||
121 | }; | ||
122 | const struct GNUNET_GETOPT_CommandLineOption aboutoptionlist[] = { | ||
123 | GNUNET_GETOPT_OPTION_HELP ("Testing"), | ||
124 | GNUNET_GETOPT_OPTION_END | ||
125 | }; | ||
126 | |||
127 | cfg = GNUNET_CONFIGURATION_create (); | ||
128 | if (-1 != GNUNET_GETOPT_run ("test_getopt", | ||
129 | cfg, aboutoptionlist, 2, myargv)) | ||
130 | { | ||
131 | GNUNET_break (0); | ||
132 | GNUNET_CONFIGURATION_destroy (cfg); | ||
133 | return 1; | ||
134 | } | ||
135 | GNUNET_CONFIGURATION_destroy (cfg); | ||
136 | return 0; | ||
137 | } | ||
138 | |||
139 | static int | ||
140 | testLogOpts () | ||
141 | { | ||
142 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
143 | char *const myargv[] = { | ||
144 | "test_getopt", | ||
145 | "-l", "filename", | ||
146 | "-L", "WARNING", | ||
147 | NULL | ||
148 | }; | ||
149 | char *level = GNUNET_strdup ("stuff"); | ||
150 | char *fn = NULL; | ||
151 | const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = { | ||
152 | GNUNET_GETOPT_OPTION_LOGFILE (&fn), | ||
153 | GNUNET_GETOPT_OPTION_LOGLEVEL (&level), | ||
154 | GNUNET_GETOPT_OPTION_END | ||
155 | }; | ||
156 | |||
157 | cfg = GNUNET_CONFIGURATION_create (); | ||
158 | if (5 != GNUNET_GETOPT_run ("test_getopt", cfg, logoptionlist, 5, myargv)) | ||
159 | { | ||
160 | GNUNET_break (0); | ||
161 | GNUNET_CONFIGURATION_destroy (cfg); | ||
162 | return 1; | ||
163 | } | ||
164 | GNUNET_assert (fn != NULL); | ||
165 | GNUNET_CONFIGURATION_destroy (cfg); | ||
166 | if ((0 != strcmp (level, "WARNING")) || (0 != strcmp (fn, "filename"))) | ||
167 | { | ||
168 | GNUNET_break (0); | ||
169 | GNUNET_free (level); | ||
170 | GNUNET_free (fn); | ||
171 | return 1; | ||
172 | } | ||
173 | GNUNET_free (level); | ||
174 | GNUNET_free (fn); | ||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | static int | ||
179 | testFlagNum () | ||
180 | { | ||
181 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
182 | char *const myargv[] = { | ||
183 | "test_getopt", | ||
184 | "-f", | ||
185 | "-n", "42", | ||
186 | "-N", "42", | ||
187 | NULL | ||
188 | }; | ||
189 | int flag = 0; | ||
190 | unsigned int num = 0; | ||
191 | unsigned long long lnum = 0; | ||
192 | const struct GNUNET_GETOPT_CommandLineOption logoptionlist[] = { | ||
193 | {'f', "--flag", NULL, "helptext", 0, &GNUNET_GETOPT_set_one, | ||
194 | (void *) &flag}, | ||
195 | {'n', "--num", "ARG", "helptext", 1, &GNUNET_GETOPT_set_uint, | ||
196 | (void *) &num}, | ||
197 | {'N', "--lnum", "ARG", "helptext", 1, &GNUNET_GETOPT_set_ulong, | ||
198 | (void *) &lnum}, | ||
199 | GNUNET_GETOPT_OPTION_END | ||
200 | }; | ||
201 | |||
202 | cfg = GNUNET_CONFIGURATION_create (); | ||
203 | if (6 != GNUNET_GETOPT_run ("test_getopt", cfg, logoptionlist, 6, myargv)) | ||
204 | { | ||
205 | GNUNET_break (0); | ||
206 | GNUNET_CONFIGURATION_destroy (cfg); | ||
207 | return 1; | ||
208 | } | ||
209 | GNUNET_CONFIGURATION_destroy (cfg); | ||
210 | if ((1 != flag) || (42 != num) || (42 != lnum)) | ||
211 | { | ||
212 | GNUNET_break (0); | ||
213 | return 1; | ||
214 | } | ||
215 | return 0; | ||
216 | } | ||
217 | |||
218 | int | ||
219 | main (int argc, char *argv[]) | ||
220 | { | ||
221 | int errCnt = 0; | ||
222 | |||
223 | GNUNET_log_setup ("test_getopt", "WARNING", NULL); | ||
224 | /* suppress output from -h, -v options */ | ||
225 | GNUNET_break (0 == CLOSE (1)); | ||
226 | if (0 != testMinimal ()) | ||
227 | errCnt++; | ||
228 | if (0 != testVerbose ()) | ||
229 | errCnt++; | ||
230 | if (0 != testVersion ()) | ||
231 | errCnt++; | ||
232 | if (0 != testAbout ()) | ||
233 | errCnt++; | ||
234 | if (0 != testLogOpts ()) | ||
235 | errCnt++; | ||
236 | if (0 != testFlagNum ()) | ||
237 | errCnt++; | ||
238 | return errCnt; | ||
239 | } | ||