Line data Source code
1 : /***************************************************************************//**
2 :
3 : @file argstest.c
4 :
5 : @author Stephen Brennan
6 :
7 : @date Created Sunday, 3 November 2013
8 :
9 : @brief Tests argument parsing functions.
10 :
11 : @copyright Copyright (c) 2013-2015, Stephen Brennan. Released under the
12 : Revised BSD License. See the LICENSE.txt file for details.
13 :
14 : *******************************************************************************/
15 :
16 : #include <stdio.h>
17 : #include <string.h>
18 : #include <assert.h>
19 :
20 : #include "libstephen/ad.h"
21 : #include "libstephen/ll.h"
22 : #include "libstephen/ut.h"
23 :
24 :
25 : /**
26 : @brief Test memory allocation on the heap.
27 : */
28 1 : int ad_test_heap(void)
29 : {
30 1 : smb_ad *arg_data = arg_data_create();
31 1 : arg_data_delete(arg_data);
32 1 : return 0;
33 : }
34 :
35 : /**
36 : @brief Test memory allocation on the stack.
37 : */
38 1 : int ad_test_stack(void)
39 : {
40 : smb_ad ad;
41 1 : arg_data_init(&ad);
42 1 : arg_data_destroy(&ad);
43 1 : return 0;
44 : }
45 :
46 : char *basic_flags[] = {
47 : "-a",
48 : "-b",
49 : "-c",
50 : "-d",
51 : "-W",
52 : "-X",
53 : "-Y",
54 : "-Z"
55 : };
56 :
57 : /**
58 : @brief Test 'basic' single character, non grouped flags.
59 : */
60 1 : int ad_test_basic_flags(void)
61 : {
62 : smb_ad ad;
63 1 : arg_data_init(&ad);
64 1 : process_args(&ad, sizeof(basic_flags) / sizeof(char*), basic_flags);
65 :
66 27 : for (char c = 'a'; c <= 'z'; c++) {
67 26 : if (c <= 'd') {
68 4 : TEST_ASSERT(check_flag(&ad, c));
69 : } else {
70 22 : TEST_ASSERT(!check_flag(&ad, c));
71 : }
72 : }
73 :
74 27 : for (char c = 'A'; c <= 'Z'; c++) {
75 26 : if (c < 'W') {
76 22 : TEST_ASSERT(!check_flag(&ad, c));
77 : } else {
78 4 : TEST_ASSERT(check_flag(&ad, c));
79 : }
80 : }
81 :
82 1 : arg_data_destroy(&ad);
83 1 : return 0;
84 : }
85 :
86 : char *grouped_flags[] = {
87 : "-ab",
88 : "-c",
89 : "-WX",
90 : "-Y",
91 : "-dZ"
92 : };
93 :
94 : /**
95 : @brief Test single character grouped flags.
96 : */
97 1 : int ad_test_grouped_flags(void)
98 : {
99 : smb_ad ad;
100 1 : arg_data_init(&ad);
101 1 : process_args(&ad, sizeof(grouped_flags) / sizeof(char*), grouped_flags);
102 :
103 27 : for (char c = 'a'; c <= 'z'; c++) {
104 26 : if (c <= 'd') {
105 4 : TEST_ASSERT(check_flag(&ad, c));
106 : } else {
107 22 : TEST_ASSERT(!check_flag(&ad, c));
108 : }
109 : }
110 :
111 27 : for (char c = 'A'; c <= 'Z'; c++) {
112 26 : if (c < 'W') {
113 22 : TEST_ASSERT(!check_flag(&ad, c));
114 : } else {
115 4 : TEST_ASSERT(check_flag(&ad, c));
116 : }
117 : }
118 :
119 1 : arg_data_destroy(&ad);
120 1 : return 0;
121 : }
122 :
123 : char *flag_params[] = {
124 : "-a",
125 : "param_for_a",
126 : "-bc",
127 : "param_for_b",
128 : "-d"
129 : };
130 :
131 : /**
132 : @brief Test parameters for single character flags.
133 : */
134 1 : int ad_test_flag_params(void)
135 : {
136 : smb_ad ad;
137 : char *str;
138 1 : arg_data_init(&ad);
139 1 : process_args(&ad, sizeof(flag_params) / sizeof(char*), flag_params);
140 :
141 1 : TEST_ASSERT(check_flag(&ad, 'a'));
142 1 : TEST_ASSERT(check_flag(&ad, 'b'));
143 1 : TEST_ASSERT(check_flag(&ad, 'c'));
144 1 : TEST_ASSERT(check_flag(&ad, 'd'));
145 :
146 1 : str = get_flag_parameter(&ad, 'a');
147 1 : TEST_ASSERT(str && strncmp(str, flag_params[1], strlen(flag_params[1])) == 0);
148 :
149 1 : str = get_flag_parameter(&ad, 'c');
150 1 : TEST_ASSERT(str && strncmp(str, flag_params[3], strlen(flag_params[3])) == 0);
151 :
152 1 : str = get_flag_parameter(&ad, 'b');
153 1 : TEST_ASSERT(str == NULL);
154 :
155 1 : str = get_flag_parameter(&ad, 'd');
156 1 : TEST_ASSERT(str == NULL);
157 :
158 1 : arg_data_destroy(&ad);
159 1 : return 0;
160 : }
161 :
162 : char *long_flags[] = {
163 : "--this-is-a-long-flag",
164 : "--this-is-another-long-flag"
165 : };
166 :
167 : /**
168 : @brief Test long flags.
169 : */
170 1 : int ad_test_long_flags(void)
171 : {
172 : smb_ad ad;
173 1 : arg_data_init(&ad);
174 1 : process_args(&ad, sizeof(long_flags) / sizeof(char*), long_flags);
175 :
176 1 : TEST_ASSERT(check_long_flag(&ad, "this-is-a-long-flag"));
177 1 : TEST_ASSERT(check_long_flag(&ad, "this-is-another-long-flag"));
178 1 : TEST_ASSERT(!check_long_flag(&ad, "this-was-not-a-long-flag"));
179 :
180 1 : arg_data_destroy(&ad);
181 1 : return 0;
182 : }
183 :
184 : char *long_params[] = {
185 : "--long-flag1",
186 : "long param 1",
187 : "--long-flag2",
188 : "--long-flag3",
189 : "long param 3"
190 : };
191 :
192 : /**
193 : @brief Test long flag parameters.
194 : */
195 1 : int ad_test_long_params(void)
196 : {
197 : smb_ad ad;
198 : char *str;
199 1 : arg_data_init(&ad);
200 1 : process_args(&ad, sizeof(long_params) / sizeof(char*), long_params);
201 :
202 1 : TEST_ASSERT(check_long_flag(&ad, "long-flag1"));
203 1 : TEST_ASSERT(check_long_flag(&ad, "long-flag2"));
204 1 : TEST_ASSERT(check_long_flag(&ad, "long-flag3"));
205 :
206 1 : str = get_long_flag_parameter(&ad, "long-flag1");
207 1 : TEST_ASSERT(str && strncmp(str, long_params[1], strlen(long_params[1])) == 0);
208 :
209 1 : str = get_long_flag_parameter(&ad, "long-flag3");
210 1 : TEST_ASSERT(str && strncmp(str, long_params[4], strlen(long_params[4])) == 0);
211 :
212 1 : str = get_long_flag_parameter(&ad, "long-flag2");
213 1 : TEST_ASSERT(str == NULL);
214 :
215 1 : arg_data_destroy(&ad);
216 1 : return 0;
217 : }
218 :
219 : char *bare_strings[] = {
220 : "bs0",
221 : "-a",
222 : "param_a",
223 : "bs1",
224 : "--blah",
225 : "param_blah",
226 : "bs2",
227 : "-"
228 : };
229 :
230 : /**
231 : @brief Test whether bare strings work.
232 : */
233 1 : int ad_test_bare_strings(void)
234 : {
235 : smb_ad ad;
236 : char *str;
237 1 : arg_data_init(&ad);
238 1 : process_args(&ad, sizeof(bare_strings) / sizeof(char*), bare_strings);
239 :
240 1 : TEST_ASSERT(check_flag(&ad, 'a'));
241 1 : TEST_ASSERT(check_long_flag(&ad, "blah"));
242 1 : TEST_ASSERT(check_bare_string(&ad, "bs0"));
243 1 : TEST_ASSERT(check_bare_string(&ad, "bs1"));
244 1 : TEST_ASSERT(check_bare_string(&ad, "bs2"));
245 1 : TEST_ASSERT(check_bare_string(&ad, "-"));
246 1 : TEST_ASSERT(ll_length(ad.bare_strings) == 4);
247 :
248 1 : str = get_flag_parameter(&ad, 'a');
249 1 : TEST_ASSERT(str && strncmp(str, bare_strings[2], strlen(bare_strings[2])) == 0);
250 :
251 1 : str = get_long_flag_parameter(&ad, "blah");
252 1 : TEST_ASSERT(str && strncmp(str, bare_strings[5], strlen(bare_strings[5])) == 0);
253 :
254 1 : arg_data_destroy(&ad);
255 1 : return 0;
256 : }
257 :
258 1 : void args_test(void)
259 : {
260 1 : smb_ut_group *group = su_create_test_group("args");
261 :
262 1 : smb_ut_test *heap = su_create_test("heap", &ad_test_heap);
263 1 : su_add_test(group, heap);
264 :
265 1 : smb_ut_test *stack = su_create_test("stack", &ad_test_stack);
266 1 : su_add_test(group, stack);
267 :
268 1 : smb_ut_test *basic_flags = su_create_test("basic_flags", &ad_test_basic_flags);
269 1 : su_add_test(group, basic_flags);
270 :
271 1 : smb_ut_test *grouped_flags = su_create_test("grouped_flags",
272 : &ad_test_grouped_flags);
273 1 : su_add_test(group, grouped_flags);
274 :
275 1 : smb_ut_test *flag_params = su_create_test("flag_params", &ad_test_flag_params);
276 1 : su_add_test(group, flag_params);
277 :
278 1 : smb_ut_test *long_flags = su_create_test("long_flags", &ad_test_long_flags);
279 1 : su_add_test(group, long_flags);
280 :
281 1 : smb_ut_test *long_params = su_create_test("long_params", &ad_test_long_params);
282 1 : su_add_test(group, long_params);
283 :
284 1 : smb_ut_test *bare_strings = su_create_test("bare_strings",
285 : &ad_test_bare_strings);
286 1 : su_add_test(group, bare_strings);
287 :
288 1 : su_run_group(group);
289 1 : su_delete_group(group);
290 1 : }
291 :
292 : ////////////////////////////////////////////////////////////////////////////////
293 : // Example main function that will process and print results of args analysis.
294 : ////////////////////////////////////////////////////////////////////////////////
295 :
296 0 : void iterate_flags(smb_ad *pArgData, char start, char end)
297 : {
298 0 : for (char c = start; c <= end; c++) {
299 0 : if (check_flag(pArgData, c)) {
300 0 : printf("Flag \'%c\' set.\n", c);
301 0 : char *param = get_flag_parameter(pArgData, c);
302 0 : if (param)
303 0 : printf("-->Flag parameter: %s\n", param);
304 : }
305 : }
306 0 : }
307 :
308 0 : void iterate_long_flags(smb_ad *pArgData)
309 : {
310 : DATA d;
311 0 : smb_iter iter = ll_get_iter(pArgData->long_flags);
312 : smb_status status;
313 :
314 0 : while (iter.has_next(&iter)) {
315 0 : d = iter.next(&iter, &status);
316 0 : assert(status == SMB_SUCCESS);
317 0 : char *string = d.data_ptr;
318 0 : printf("Flag \"%s\" set.\n", string);
319 :
320 0 : char *param = get_long_flag_parameter(pArgData, string);
321 0 : if (param)
322 0 : printf("-->Flag parameter: %s\n", param);
323 : }
324 0 : iter.destroy(&iter);
325 0 : }
326 :
327 0 : void iterate_bare_strings(smb_ad *pArgData)
328 : {
329 : DATA d;
330 0 : smb_iter iter = ll_get_iter(pArgData->bare_strings);
331 : smb_status status;
332 :
333 0 : while (iter.has_next(&iter)) {
334 0 : d = iter.next(&iter, &status);
335 0 : assert(status == SMB_SUCCESS);
336 0 : char *string = d.data_ptr;
337 0 : printf("String: \"%s\"\n", string);
338 : }
339 0 : iter.destroy(&iter);
340 0 : }
341 :
342 0 : int args_test_main(int argc, char **argv)
343 : {
344 : smb_ad ArgData;
345 0 : arg_data_init(&ArgData);
346 0 : process_args(&ArgData, argc - 1, argv + 1);
347 :
348 0 : iterate_flags(&ArgData, 'A', 'Z');
349 0 : iterate_flags(&ArgData, 'a', 'z');
350 :
351 0 : iterate_long_flags(&ArgData);
352 0 : iterate_bare_strings(&ArgData);
353 0 : arg_data_destroy(&ArgData);
354 0 : return 0;
355 : }
|