VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTGetOpt.cpp@ 32970

Last change on this file since 32970 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.1 KB
Line 
1/* $Id: tstRTGetOpt.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTGetOpt
4 */
5
6/*
7 * Copyright (C) 2007-2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/net.h>
32#include <iprt/getopt.h>
33
34#include <iprt/err.h>
35#include <iprt/string.h>
36#include <iprt/test.h>
37
38
39int main()
40{
41 RTTEST hTest;
42 int rc = RTTestInitAndCreate("tstRTGetOpt", &hTest);
43 if (rc)
44 return rc;
45
46 RTGETOPTSTATE GetState;
47 RTGETOPTUNION Val;
48#define CHECK(expr) do { if (!(expr)) { RTTestIFailed("error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); } } while (0)
49#define CHECK2(expr, fmt) \
50 do { \
51 if (!(expr)) { \
52 RTTestIFailed("error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); \
53 RTTestIFailureDetails fmt; \
54 } \
55 } while (0)
56
57#define CHECK_pDef(paOpts, i) \
58 CHECK2(Val.pDef == &(paOpts)[(i)], ("Got #%d (%p) expected #%d\n", (int)(Val.pDef - &(paOpts)[0]), Val.pDef, i));
59
60#define CHECK_GETOPT(expr, chRet, iInc) \
61 do { \
62 const int iPrev = GetState.iNext; \
63 const int rcGetOpt = (expr); \
64 CHECK2(rcGetOpt == (chRet), ("got %d, expected %d\n", rcGetOpt, (chRet))); \
65 CHECK2(GetState.iNext == (iInc) + iPrev, ("iNext=%d expected %d\n", GetState.iNext, (iInc) + iPrev)); \
66 GetState.iNext = (iInc) + iPrev; \
67 } while (0)
68
69#define CHECK_GETOPT_STR(expr, chRet, iInc, str) \
70 do { \
71 const int iPrev = GetState.iNext; \
72 const int rcGetOpt = (expr); \
73 CHECK2(rcGetOpt == (chRet), ("got %d, expected %d\n", rcGetOpt, (chRet))); \
74 CHECK2(GetState.iNext == (iInc) + iPrev, ("iNext=%d expected %d\n", GetState.iNext, (iInc) + iPrev)); \
75 CHECK2(VALID_PTR(Val.psz) && !strcmp(Val.psz, (str)), ("got %s, expected %s\n", Val.psz, (str))); \
76 GetState.iNext = (iInc) + iPrev; \
77 } while (0)
78
79
80 /*
81 * The basics.
82 */
83 RTTestSub(hTest, "Basics");
84 static const RTGETOPTDEF s_aOpts2[] =
85 {
86 { "--optwithstring", 's', RTGETOPT_REQ_STRING },
87 { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
88 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
89 { NULL, 'q', RTGETOPT_REQ_NOTHING },
90 { "--quiet", 384, RTGETOPT_REQ_NOTHING },
91 { "-novalue", 385, RTGETOPT_REQ_NOTHING },
92 { "-startvm", 386, RTGETOPT_REQ_STRING },
93 { "nodash", 387, RTGETOPT_REQ_NOTHING },
94 { "nodashval", 388, RTGETOPT_REQ_STRING },
95 { "--gateway", 'g', RTGETOPT_REQ_IPV4ADDR },
96 { "--mac", 'm', RTGETOPT_REQ_MACADDR },
97 { "--strindex", 400, RTGETOPT_REQ_STRING | RTGETOPT_FLAG_INDEX },
98 { "strindex", 400, RTGETOPT_REQ_STRING | RTGETOPT_FLAG_INDEX },
99 { "--intindex", 401, RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_INDEX },
100 { "--macindex", 402, RTGETOPT_REQ_MACADDR | RTGETOPT_FLAG_INDEX },
101 { "--indexnovalue", 403, RTGETOPT_REQ_NOTHING | RTGETOPT_FLAG_INDEX },
102 { "--macindexnegative", 404, RTGETOPT_REQ_NOTHING },
103 { "--twovalues", 405, RTGETOPT_REQ_STRING },
104 { "--twovaluesindex", 406, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_INDEX },
105 { "--threevalues", 407, RTGETOPT_REQ_UINT32 },
106 { "--boolean", 408, RTGETOPT_REQ_BOOL_ONOFF },
107 { "--booleanindex", 409, RTGETOPT_REQ_BOOL_ONOFF | RTGETOPT_FLAG_INDEX },
108 };
109
110 const char *argv2[] =
111 {
112 "-s", "string1",
113 "--optwithstring", "string2",
114
115 "-i", "-42",
116 "-i:-42",
117 "-i=-42",
118 "-i:", "-42",
119 "-i=", "-42",
120
121 "--optwithint", "42",
122 "--optwithint:42",
123 "--optwithint=42",
124 "--optwithint:", "42",
125 "--optwithint=", "42",
126
127 "-v",
128 "--verbose",
129 "-q",
130 "--quiet",
131
132 "-novalue",
133 "-startvm", "myvm",
134
135 "nodash",
136 "nodashval", "string3",
137
138 "filename1",
139 "-q",
140 "filename2",
141
142 "-vqi999",
143
144 "-g192.168.1.1",
145
146 "-m08:0:27:00:ab:f3",
147 "--mac:1:::::c",
148
149 "--strindex786", "string4",
150 "--strindex786:string5",
151 "--strindex786=string6",
152 "strindex687", "string7",
153 "strindex687:string8",
154 "strindex687=string9",
155 "--intindex137", "1000",
156 "--macindex138", "08:0:27:00:ab:f3",
157 "--indexnovalue1",
158 "--macindexnegative",
159
160 "--twovalues", "firstvalue", "secondvalue",
161 "--twovalues:firstvalue", "secondvalue",
162 "--twovaluesindex4", "1", "0xA",
163 "--twovaluesindex5=2", "0xB",
164 "--threevalues", "1", "0xC", "thirdvalue",
165
166 /* bool on/off */
167 "--boolean", "on",
168 "--boolean", "off",
169 "--boolean", "invalid",
170 "--booleanindex2", "on",
171 "--booleanindex7", "off",
172 "--booleanindex9", "invalid",
173
174 /* standard options */
175 "--help",
176 "-help",
177 "-?",
178 "-h",
179 "--version",
180 "-version",
181 "-V",
182
183 /* done */
184 NULL
185 };
186 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
187
188 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, (char **)argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
189
190 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
191 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
192 CHECK(GetState.uIndex == UINT32_MAX);
193 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
194 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
195 CHECK(GetState.uIndex == UINT32_MAX);
196
197 /* -i */
198 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
199 CHECK(Val.i32 == -42);
200 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
201 CHECK(Val.i32 == -42);
202 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
203 CHECK(Val.i32 == -42);
204 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
205 CHECK(Val.i32 == -42);
206 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
207 CHECK(Val.i32 == -42);
208
209 /* --optwithint */
210 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
211 CHECK(Val.i32 == 42);
212 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
213 CHECK(Val.i32 == 42);
214 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
215 CHECK(Val.i32 == 42);
216 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
217 CHECK(Val.i32 == 42);
218 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
219 CHECK(Val.i32 == 42);
220
221 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
222 CHECK_pDef(s_aOpts2, 2);
223 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
224 CHECK_pDef(s_aOpts2, 2);
225
226 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
227 CHECK_pDef(s_aOpts2, 3);
228 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
229 CHECK_pDef(s_aOpts2, 4);
230
231 /* -novalue / -startvm (single dash long options) */
232 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
233 CHECK_pDef(s_aOpts2, 5);
234 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 386, 2);
235 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "myvm"));
236
237 /* no-dash options */
238 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 387, 1);
239 CHECK_pDef(s_aOpts2, 7);
240 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 388, 2);
241 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string3"));
242
243 /* non-option, option, non-option */
244 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
245 CHECK(Val.psz && !strcmp(Val.psz, "filename1"));
246 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
247 CHECK_pDef(s_aOpts2, 3);
248 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
249 CHECK(Val.psz && !strcmp(Val.psz, "filename2"));
250
251 /* compress short options */
252 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 0);
253 CHECK_pDef(s_aOpts2, 2);
254 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 0);
255 CHECK_pDef(s_aOpts2, 3);
256 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
257 CHECK(Val.i32 == 999);
258
259 /* IPv4 */
260 RTTestSub(hTest, "RTGetOpt - IPv4");
261 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'g', 1);
262 CHECK(Val.IPv4Addr.u == RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8(192,168,1,1))));
263
264 /* Ethernet MAC address. */
265 RTTestSub(hTest, "RTGetOpt - MAC Address");
266 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
267 CHECK( Val.MacAddr.au8[0] == 0x08
268 && Val.MacAddr.au8[1] == 0x00
269 && Val.MacAddr.au8[2] == 0x27
270 && Val.MacAddr.au8[3] == 0x00
271 && Val.MacAddr.au8[4] == 0xab
272 && Val.MacAddr.au8[5] == 0xf3);
273 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
274 CHECK( Val.MacAddr.au8[0] == 0x01
275 && Val.MacAddr.au8[1] == 0x00
276 && Val.MacAddr.au8[2] == 0x00
277 && Val.MacAddr.au8[3] == 0x00
278 && Val.MacAddr.au8[4] == 0x00
279 && Val.MacAddr.au8[5] == 0x0c);
280
281 /* string with indexed argument */
282 RTTestSub(hTest, "RTGetOpt - Option w/ Index");
283 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
284 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string4"));
285 CHECK(GetState.uIndex == 786);
286
287 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
288 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string5"));
289 CHECK(GetState.uIndex == 786);
290
291 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
292 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string6"));
293 CHECK(GetState.uIndex == 786);
294
295 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
296 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string7"));
297 CHECK(GetState.uIndex == 687);
298
299 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
300 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string8"));
301 CHECK(GetState.uIndex == 687);
302
303 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
304 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string9"));
305 CHECK(GetState.uIndex == 687);
306
307 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 401, 2);
308 CHECK(Val.i32 == 1000);
309 CHECK(GetState.uIndex == 137);
310
311 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 402, 2);
312 CHECK( Val.MacAddr.au8[0] == 0x08
313 && Val.MacAddr.au8[1] == 0x00
314 && Val.MacAddr.au8[2] == 0x27
315 && Val.MacAddr.au8[3] == 0x00
316 && Val.MacAddr.au8[4] == 0xab
317 && Val.MacAddr.au8[5] == 0xf3);
318 CHECK(GetState.uIndex == 138);
319
320 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 403, 1);
321 CHECK(GetState.uIndex == 1);
322
323 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 404, 1);
324 CHECK(GetState.uIndex == UINT32_MAX);
325
326 /* RTGetOptFetchValue tests */
327 RTTestSub(hTest, "RTGetOptFetchValue");
328 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 405, 2);
329 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "firstvalue"));
330 CHECK(GetState.uIndex == UINT32_MAX);
331 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
332 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "secondvalue"));
333 CHECK(GetState.uIndex == UINT32_MAX);
334
335 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 405, 1);
336 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "firstvalue"));
337 CHECK(GetState.uIndex == UINT32_MAX);
338 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
339 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "secondvalue"));
340 CHECK(GetState.uIndex == UINT32_MAX);
341
342 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 406, 2);
343 CHECK(Val.u32 == 1);
344 CHECK(GetState.uIndex == 4);
345 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
346 CHECK(Val.u32 == 10);
347 CHECK(GetState.uIndex == 4);
348
349 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 406, 1);
350 CHECK(Val.u32 == 2);
351 CHECK(GetState.uIndex == 5);
352 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
353 CHECK(Val.u32 == 11);
354 CHECK(GetState.uIndex == 5);
355
356 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 407, 2);
357 CHECK(Val.u32 == 1);
358 CHECK(GetState.uIndex == UINT32_MAX);
359 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
360 CHECK(Val.u32 == 12);
361 CHECK(GetState.uIndex == UINT32_MAX);
362 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
363 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "thirdvalue"));
364 CHECK(GetState.uIndex == UINT32_MAX);
365
366 /* bool on/off tests */
367 RTTestSub(hTest, "RTGetOpt - bool on/off");
368 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 408, 2);
369 CHECK(Val.f);
370 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 408, 2);
371 CHECK(!Val.f);
372 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VERR_GETOPT_UNKNOWN_OPTION, 2);
373 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "invalid"));
374
375 /* bool on/off with indexed argument */
376 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 409, 2);
377 CHECK(Val.f);
378 CHECK(GetState.uIndex == 2);
379 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 409, 2);
380 CHECK(!Val.f);
381 CHECK(GetState.uIndex == 7);
382 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VERR_GETOPT_UNKNOWN_OPTION, 2);
383 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "invalid"));
384
385 /* standard options. */
386 RTTestSub(hTest, "Standard options");
387 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'h', 1);
388 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'h', 1);
389 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'h', 1);
390 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'h', 1);
391 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'V', 1);
392 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'V', 1);
393 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'V', 1);
394
395 /* the end */
396 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
397 CHECK(Val.pDef == NULL);
398 CHECK(argc2 == GetState.iNext);
399
400 /*
401 * Options first.
402 */
403 RTTestSub(hTest, "Options first");
404 const char *argv3[] =
405 {
406 "foo1",
407 "-s", "string1",
408 "foo2",
409 "--optwithstring", "string2",
410 "foo3",
411 "-i", "-42",
412 "foo4",
413 "-i:-42",
414 "-i=-42",
415 "foo5",
416 "foo6",
417 "foo7",
418 "-i:", "-42",
419 "-i=", "-42",
420 "foo8",
421 "--twovalues", "firstvalue", "secondvalue",
422 "foo9",
423 "--twovalues:firstvalue", "secondvalue",
424 "foo10",
425 "--",
426 "--optwithstring",
427 "foo11",
428 "foo12",
429
430 /* done */
431 NULL
432 };
433 int argc3 = (int)RT_ELEMENTS(argv3) - 1;
434
435 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc3, (char **)argv3, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0,
436 RTGETOPTINIT_FLAGS_OPTS_FIRST)));
437
438 /* -s */
439 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 's', 2, "string1");
440 CHECK(GetState.uIndex == UINT32_MAX);
441 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 's', 2, "string2");
442 CHECK(GetState.uIndex == UINT32_MAX);
443
444 /* -i */
445 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
446 CHECK(Val.i32 == -42);
447 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
448 CHECK(Val.i32 == -42);
449 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
450 CHECK(Val.i32 == -42);
451 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
452 CHECK(Val.i32 == -42);
453 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
454 CHECK(Val.i32 == -42);
455
456 /* --twovalues */
457 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 405, 2, "firstvalue");
458 CHECK(GetState.uIndex == UINT32_MAX);
459 CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
460 CHECK(GetState.uIndex == UINT32_MAX);
461
462 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 405, 1, "firstvalue");
463 CHECK(GetState.uIndex == UINT32_MAX);
464 CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
465 CHECK(GetState.uIndex == UINT32_MAX);
466
467 /* -- */
468 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 2, "foo1");
469 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo2");
470 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo3");
471 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo4");
472 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo5");
473 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo6");
474 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo7");
475 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo8");
476 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo9");
477 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo10");
478 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "--optwithstring");
479 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo11");
480 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo12");
481
482 /* the end */
483 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
484 CHECK(Val.pDef == NULL);
485 CHECK(argc3 == GetState.iNext);
486
487 /*
488 * Options first, part 2: No dash-dash.
489 */
490 const char *argv4[] =
491 {
492 "foo1",
493 "-s", "string1",
494 "foo2",
495 "--optwithstring", "string2",
496 "foo3",
497 "-i", "-42",
498 "foo4",
499 "-i:-42",
500 "-i=-42",
501 "foo5",
502 "foo6",
503 "foo7",
504 "-i:", "-42",
505 "-i=", "-42",
506 "foo8",
507 "--twovalues", "firstvalue", "secondvalue",
508 "foo9",
509 "--twovalues:firstvalue", "secondvalue",
510 "foo10",
511 "foo11",
512 "foo12",
513
514 /* done */
515 NULL
516 };
517 int argc4 = (int)RT_ELEMENTS(argv4) - 1;
518
519 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc4, (char **)argv4, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0,
520 RTGETOPTINIT_FLAGS_OPTS_FIRST)));
521
522 /* -s */
523 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 's', 2, "string1");
524 CHECK(GetState.uIndex == UINT32_MAX);
525 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 's', 2, "string2");
526 CHECK(GetState.uIndex == UINT32_MAX);
527
528 /* -i */
529 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
530 CHECK(Val.i32 == -42);
531 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
532 CHECK(Val.i32 == -42);
533 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
534 CHECK(Val.i32 == -42);
535 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
536 CHECK(Val.i32 == -42);
537 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
538 CHECK(Val.i32 == -42);
539
540 /* --twovalues */
541 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 405, 2, "firstvalue");
542 CHECK(GetState.uIndex == UINT32_MAX);
543 CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
544 CHECK(GetState.uIndex == UINT32_MAX);
545
546 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), 405, 1, "firstvalue");
547 CHECK(GetState.uIndex == UINT32_MAX);
548 CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
549 CHECK(GetState.uIndex == UINT32_MAX);
550
551 /* -- */
552 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo1");
553 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo2");
554 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo3");
555 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo4");
556 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo5");
557 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo6");
558 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo7");
559 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo8");
560 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo9");
561 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo10");
562 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo11");
563 CHECK_GETOPT_STR(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1, "foo12");
564
565 /* the end */
566 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
567 CHECK(Val.pDef == NULL);
568 CHECK(argc4 == GetState.iNext);
569
570
571
572 /*
573 * Summary.
574 */
575 return RTTestSummaryAndDestroy(hTest);
576}
577
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette