VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp@ 23973

Last change on this file since 23973 was 23868, checked in by vboxsync, 15 years ago

RTGetOpt: Added RTGetOptFetchValue to fetch an additional value for an argument

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.5 KB
Line 
1/* $Id: tstGetOpt.cpp 23868 2009-10-19 14:22:42Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTGetOpt
4 */
5
6/*
7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/net.h>
36#include <iprt/getopt.h>
37#include <iprt/stream.h>
38#include <iprt/initterm.h>
39#include <iprt/string.h>
40#include <iprt/err.h>
41
42
43int main()
44{
45 int cErrors = 0;
46 RTR3Init();
47 RTPrintf("tstGetOpt: TESTING...\n");
48
49 RTGETOPTSTATE GetState;
50 RTGETOPTUNION Val;
51#define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); cErrors++; } } while (0)
52#define CHECK2(expr, fmt) \
53 do { \
54 if (!(expr)) { \
55 RTPrintf("tstGetOpt: error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); \
56 RTPrintf fmt; \
57 cErrors++; \
58 } \
59 } while (0)
60
61#define CHECK_pDef(paOpts, i) \
62 CHECK2(Val.pDef == &(paOpts)[(i)], ("Got #%d (%p) expected #%d\n", (int)(Val.pDef - &(paOpts)[0]), Val.pDef, i));
63
64#define CHECK_GETOPT(expr, chRet, iInc) \
65 do { \
66 const int iPrev = GetState.iNext; \
67 const int rc = (expr); \
68 CHECK2(rc == (chRet), ("got %d, expected %d\n", rc, (chRet))); \
69 CHECK2(GetState.iNext == (iInc) + iPrev, ("iNext=%d expected %d\n", GetState.iNext, (iInc) + iPrev)); \
70 GetState.iNext = (iInc) + iPrev; \
71 } while (0)
72
73
74 /*
75 * The basics.
76 */
77 static const RTGETOPTDEF s_aOpts2[] =
78 {
79 { "--optwithstring", 's', RTGETOPT_REQ_STRING },
80 { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
81 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
82 { NULL, 'q', RTGETOPT_REQ_NOTHING },
83 { "--quiet", 384, RTGETOPT_REQ_NOTHING },
84 { "-novalue", 385, RTGETOPT_REQ_NOTHING },
85 { "-startvm", 386, RTGETOPT_REQ_STRING },
86 { "nodash", 387, RTGETOPT_REQ_NOTHING },
87 { "nodashval", 388, RTGETOPT_REQ_STRING },
88 { "--gateway", 'g', RTGETOPT_REQ_IPV4ADDR },
89 { "--mac", 'm', RTGETOPT_REQ_MACADDR },
90 { "--strindex", 400, RTGETOPT_REQ_STRING | RTGETOPT_FLAG_INDEX },
91 { "strindex", 400, RTGETOPT_REQ_STRING | RTGETOPT_FLAG_INDEX },
92 { "--intindex", 401, RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_INDEX },
93 { "--macindex", 402, RTGETOPT_REQ_MACADDR | RTGETOPT_FLAG_INDEX },
94 { "--indexnovalue", 403, RTGETOPT_REQ_NOTHING | RTGETOPT_FLAG_INDEX },
95 { "--macindexnegative", 404, RTGETOPT_REQ_NOTHING },
96 { "--twovalues", 405, RTGETOPT_REQ_STRING },
97 { "--twovaluesindex", 406, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_INDEX },
98 { "--threevalues", 407, RTGETOPT_REQ_UINT32 },
99 };
100
101 const char *argv2[] =
102 {
103 "-s", "string1",
104 "--optwithstring", "string2",
105
106 "-i", "-42",
107 "-i:-42",
108 "-i=-42",
109 "-i:", "-42",
110 "-i=", "-42",
111
112 "--optwithint", "42",
113 "--optwithint:42",
114 "--optwithint=42",
115 "--optwithint:", "42",
116 "--optwithint=", "42",
117
118 "-v",
119 "--verbose",
120 "-q",
121 "--quiet",
122
123 "-novalue",
124 "-startvm", "myvm",
125
126 "nodash",
127 "nodashval", "string3",
128
129 "filename1",
130 "-q",
131 "filename2",
132
133 "-vqi999",
134
135 "-g192.168.1.1",
136
137 "-m08:0:27:00:ab:f3",
138 "--mac:1:::::c",
139
140 "--strindex786", "string4",
141 "--strindex786:string5",
142 "--strindex786=string6",
143 "strindex687", "string7",
144 "strindex687:string8",
145 "strindex687=string9",
146 "--intindex137", "1000",
147 "--macindex138", "08:0:27:00:ab:f3",
148 "--indexnovalue1",
149 "--macindexnegative",
150
151 "--twovalues", "firstvalue", "secondvalue",
152 "--twovalues:firstvalue", "secondvalue",
153 "--twovaluesindex4", "1", "0xA",
154 "--twovaluesindex5=2", "0xB",
155 "--threevalues", "1", "0xC", "thirdvalue",
156
157 NULL
158 };
159 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
160
161 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, (char **)argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
162
163 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
164 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
165 CHECK(GetState.uIndex == UINT64_MAX);
166 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
167 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
168 CHECK(GetState.uIndex == UINT64_MAX);
169
170 /* -i */
171 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
172 CHECK(Val.i32 == -42);
173 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
174 CHECK(Val.i32 == -42);
175 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
176 CHECK(Val.i32 == -42);
177 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
178 CHECK(Val.i32 == -42);
179 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
180 CHECK(Val.i32 == -42);
181
182 /* --optwithint */
183 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
184 CHECK(Val.i32 == 42);
185 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
186 CHECK(Val.i32 == 42);
187 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
188 CHECK(Val.i32 == 42);
189 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
190 CHECK(Val.i32 == 42);
191 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
192 CHECK(Val.i32 == 42);
193
194 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
195 CHECK_pDef(s_aOpts2, 2);
196 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
197 CHECK_pDef(s_aOpts2, 2);
198
199 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
200 CHECK_pDef(s_aOpts2, 3);
201 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
202 CHECK_pDef(s_aOpts2, 4);
203
204 /* -novalue / -startvm (single dash long options) */
205 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
206 CHECK_pDef(s_aOpts2, 5);
207 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 386, 2);
208 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "myvm"));
209
210 /* no-dash options */
211 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 387, 1);
212 CHECK_pDef(s_aOpts2, 7);
213 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 388, 2);
214 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string3"));
215
216 /* non-option, option, non-option */
217 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
218 CHECK(Val.psz && !strcmp(Val.psz, "filename1"));
219 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
220 CHECK_pDef(s_aOpts2, 3);
221 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
222 CHECK(Val.psz && !strcmp(Val.psz, "filename2"));
223
224 /* compress short options */
225 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 0);
226 CHECK_pDef(s_aOpts2, 2);
227 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 0);
228 CHECK_pDef(s_aOpts2, 3);
229 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
230 CHECK(Val.i32 == 999);
231
232 /* IPv4 */
233 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'g', 1);
234 CHECK(Val.IPv4Addr.u == RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8(192,168,1,1))));
235
236 /* Ethernet MAC address. */
237 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
238 CHECK( Val.MacAddr.au8[0] == 0x08
239 && Val.MacAddr.au8[1] == 0x00
240 && Val.MacAddr.au8[2] == 0x27
241 && Val.MacAddr.au8[3] == 0x00
242 && Val.MacAddr.au8[4] == 0xab
243 && Val.MacAddr.au8[5] == 0xf3);
244 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
245 CHECK( Val.MacAddr.au8[0] == 0x01
246 && Val.MacAddr.au8[1] == 0x00
247 && Val.MacAddr.au8[2] == 0x00
248 && Val.MacAddr.au8[3] == 0x00
249 && Val.MacAddr.au8[4] == 0x00
250 && Val.MacAddr.au8[5] == 0x0c);
251
252 /* string with indexed argument */
253 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
254 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string4"));
255 CHECK(GetState.uIndex == 786);
256
257 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
258 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string5"));
259 CHECK(GetState.uIndex == 786);
260
261 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
262 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string6"));
263 CHECK(GetState.uIndex == 786);
264
265 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
266 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string7"));
267 CHECK(GetState.uIndex == 687);
268
269 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
270 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string8"));
271 CHECK(GetState.uIndex == 687);
272
273 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
274 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string9"));
275 CHECK(GetState.uIndex == 687);
276
277 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 401, 2);
278 CHECK(Val.i32 == 1000);
279 CHECK(GetState.uIndex == 137);
280
281 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 402, 2);
282 CHECK( Val.MacAddr.au8[0] == 0x08
283 && Val.MacAddr.au8[1] == 0x00
284 && Val.MacAddr.au8[2] == 0x27
285 && Val.MacAddr.au8[3] == 0x00
286 && Val.MacAddr.au8[4] == 0xab
287 && Val.MacAddr.au8[5] == 0xf3);
288 CHECK(GetState.uIndex == 138);
289
290 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 403, 1);
291 CHECK(GetState.uIndex == 1);
292
293 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 404, 1);
294 CHECK(GetState.uIndex == UINT64_MAX);
295
296 /* RTGetOptFetchValue tests */
297 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 405, 2);
298 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "firstvalue"));
299 CHECK(GetState.uIndex == UINT64_MAX);
300 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
301 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "secondvalue"));
302 CHECK(GetState.uIndex == UINT64_MAX);
303
304 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 405, 1);
305 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "firstvalue"));
306 CHECK(GetState.uIndex == UINT64_MAX);
307 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
308 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "secondvalue"));
309 CHECK(GetState.uIndex == UINT64_MAX);
310
311 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 406, 2);
312 CHECK(Val.u32 == 1);
313 CHECK(GetState.uIndex == 4);
314 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
315 CHECK(Val.u32 == 10);
316 CHECK(GetState.uIndex == 4);
317
318 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 406, 1);
319 CHECK(Val.u32 == 2);
320 CHECK(GetState.uIndex == 5);
321 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
322 CHECK(Val.u32 == 11);
323 CHECK(GetState.uIndex == 5);
324
325 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 407, 2);
326 CHECK(Val.u32 == 1);
327 CHECK(GetState.uIndex == UINT64_MAX);
328 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_UINT32), VINF_SUCCESS, 1);
329 CHECK(Val.u32 == 12);
330 CHECK(GetState.uIndex == UINT64_MAX);
331 CHECK_GETOPT(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1);
332 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "thirdvalue"));
333 CHECK(GetState.uIndex == UINT64_MAX);
334
335 /* the end */
336 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
337 CHECK(Val.pDef == NULL);
338 CHECK(argc2 == GetState.iNext);
339
340
341 /*
342 * Summary.
343 */
344 if (!cErrors)
345 RTPrintf("tstGetOpt: SUCCESS\n");
346 else
347 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
348
349 return !!cErrors;
350}
351
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