VirtualBox

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

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

Runtime: Added indexed options to RTGetOpt (eg: "--strwithindex14 value")

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/* $Id: tstGetOpt.cpp 23644 2009-10-09 12:28:56Z 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 };
97
98 const char *argv2[] =
99 {
100 "-s", "string1",
101 "--optwithstring", "string2",
102
103 "-i", "-42",
104 "-i:-42",
105 "-i=-42",
106 "-i:", "-42",
107 "-i=", "-42",
108
109 "--optwithint", "42",
110 "--optwithint:42",
111 "--optwithint=42",
112 "--optwithint:", "42",
113 "--optwithint=", "42",
114
115 "-v",
116 "--verbose",
117 "-q",
118 "--quiet",
119
120 "-novalue",
121 "-startvm", "myvm",
122
123 "nodash",
124 "nodashval", "string3",
125
126 "filename1",
127 "-q",
128 "filename2",
129
130 "-vqi999",
131
132 "-g192.168.1.1",
133
134 "-m08:0:27:00:ab:f3",
135 "--mac:1:::::c",
136
137 "--strindex786", "string4",
138 "--strindex786:string5",
139 "--strindex786=string6",
140 "strindex687", "string7",
141 "strindex687:string8",
142 "strindex687=string9",
143 "--intindex137", "1000",
144 "--macindex138", "08:0:27:00:ab:f3",
145 "--indexnovalue1",
146 "--macindexnegative",
147
148 NULL
149 };
150 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
151
152 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, (char **)argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
153
154 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
155 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
156 CHECK(GetState.uIndex == UINT64_MAX);
157 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
158 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
159 CHECK(GetState.uIndex == UINT64_MAX);
160
161 /* -i */
162 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
163 CHECK(Val.i32 == -42);
164 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
165 CHECK(Val.i32 == -42);
166 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
167 CHECK(Val.i32 == -42);
168 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
169 CHECK(Val.i32 == -42);
170 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
171 CHECK(Val.i32 == -42);
172
173 /* --optwithint */
174 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
175 CHECK(Val.i32 == 42);
176 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
177 CHECK(Val.i32 == 42);
178 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
179 CHECK(Val.i32 == 42);
180 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
181 CHECK(Val.i32 == 42);
182 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
183 CHECK(Val.i32 == 42);
184
185 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
186 CHECK_pDef(s_aOpts2, 2);
187 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
188 CHECK_pDef(s_aOpts2, 2);
189
190 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
191 CHECK_pDef(s_aOpts2, 3);
192 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
193 CHECK_pDef(s_aOpts2, 4);
194
195 /* -novalue / -startvm (single dash long options) */
196 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
197 CHECK_pDef(s_aOpts2, 5);
198 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 386, 2);
199 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "myvm"));
200
201 /* no-dash options */
202 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 387, 1);
203 CHECK_pDef(s_aOpts2, 7);
204 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 388, 2);
205 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string3"));
206
207 /* non-option, option, non-option */
208 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
209 CHECK(Val.psz && !strcmp(Val.psz, "filename1"));
210 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
211 CHECK_pDef(s_aOpts2, 3);
212 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
213 CHECK(Val.psz && !strcmp(Val.psz, "filename2"));
214
215 /* compress short options */
216 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 0);
217 CHECK_pDef(s_aOpts2, 2);
218 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 0);
219 CHECK_pDef(s_aOpts2, 3);
220 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
221 CHECK(Val.i32 == 999);
222
223 /* IPv4 */
224 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'g', 1);
225 CHECK(Val.IPv4Addr.u == RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8(192,168,1,1))));
226
227 /* Ethernet MAC address. */
228 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
229 CHECK( Val.MacAddr.au8[0] == 0x08
230 && Val.MacAddr.au8[1] == 0x00
231 && Val.MacAddr.au8[2] == 0x27
232 && Val.MacAddr.au8[3] == 0x00
233 && Val.MacAddr.au8[4] == 0xab
234 && Val.MacAddr.au8[5] == 0xf3);
235 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'm', 1);
236 CHECK( Val.MacAddr.au8[0] == 0x01
237 && Val.MacAddr.au8[1] == 0x00
238 && Val.MacAddr.au8[2] == 0x00
239 && Val.MacAddr.au8[3] == 0x00
240 && Val.MacAddr.au8[4] == 0x00
241 && Val.MacAddr.au8[5] == 0x0c);
242
243 /* string with indexed argument */
244 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
245 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string4"));
246 CHECK(GetState.uIndex == 786);
247
248 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
249 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string5"));
250 CHECK(GetState.uIndex == 786);
251
252 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
253 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string6"));
254 CHECK(GetState.uIndex == 786);
255
256 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 2);
257 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string7"));
258 CHECK(GetState.uIndex == 687);
259
260 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
261 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string8"));
262 CHECK(GetState.uIndex == 687);
263
264 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 400, 1);
265 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string9"));
266 CHECK(GetState.uIndex == 687);
267
268 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 401, 2);
269 CHECK(Val.i32 == 1000);
270 CHECK(GetState.uIndex == 137);
271
272 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 402, 2);
273 CHECK( Val.MacAddr.au8[0] == 0x08
274 && Val.MacAddr.au8[1] == 0x00
275 && Val.MacAddr.au8[2] == 0x27
276 && Val.MacAddr.au8[3] == 0x00
277 && Val.MacAddr.au8[4] == 0xab
278 && Val.MacAddr.au8[5] == 0xf3);
279 CHECK(GetState.uIndex == 138);
280
281 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 403, 1);
282 CHECK(GetState.uIndex == 1);
283
284 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 404, 1);
285 CHECK(GetState.uIndex == UINT64_MAX);
286
287 /* the end */
288 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
289 CHECK(Val.pDef == NULL);
290 CHECK(argc2 == GetState.iNext);
291
292
293 /*
294 * Summary.
295 */
296 if (!cErrors)
297 RTPrintf("tstGetOpt: SUCCESS\n");
298 else
299 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
300
301 return !!cErrors;
302}
303
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