VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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