VirtualBox

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

Last change on this file since 17319 was 17319, checked in by vboxsync, 16 years ago

IPRT: Added RTGETOPT_REQ_IPV4ADDR to RTGetOpt.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: tstGetOpt.cpp 17319 2009-03-04 03:18:37Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTGetOpt
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 /*
76 * The basics.
77 */
78 static const RTGETOPTDEF s_aOpts2[] =
79 {
80 { "--optwithstring", 's', RTGETOPT_REQ_STRING },
81 { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
82 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
83 { NULL, 'q', RTGETOPT_REQ_NOTHING },
84 { "--quiet", 384, RTGETOPT_REQ_NOTHING },
85 { "-novalue", 385, RTGETOPT_REQ_NOTHING },
86 { "-startvm", 386, RTGETOPT_REQ_STRING },
87 { "nodash", 387, RTGETOPT_REQ_NOTHING },
88 { "nodashval", 388, RTGETOPT_REQ_STRING },
89 { "--gateway", 'g', RTGETOPT_REQ_IPV4ADDR },
90 };
91
92 char *argv2[] =
93 {
94 "-s", "string1",
95 "--optwithstring", "string2",
96
97 "-i", "-42",
98 "-i:-42",
99 "-i=-42",
100 "-i:", "-42",
101 "-i=", "-42",
102
103 "--optwithint", "42",
104 "--optwithint:42",
105 "--optwithint=42",
106 "--optwithint:", "42",
107 "--optwithint=", "42",
108
109 "-v",
110 "--verbose",
111 "-q",
112 "--quiet",
113
114 "-novalue",
115 "-startvm", "myvm",
116
117 "nodash",
118 "nodashval", "string3",
119
120 "filename1",
121 "-q",
122 "filename2",
123
124 "-vqi999",
125
126 "-g192.168.1.1",
127 NULL
128 };
129 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
130
131 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
132
133 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
134 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
135 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
136 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
137
138 /* -i */
139 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
140 CHECK(Val.i32 == -42);
141 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
142 CHECK(Val.i32 == -42);
143 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
144 CHECK(Val.i32 == -42);
145 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
146 CHECK(Val.i32 == -42);
147 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
148 CHECK(Val.i32 == -42);
149
150 /* --optwithint */
151 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
152 CHECK(Val.i32 == 42);
153 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
154 CHECK(Val.i32 == 42);
155 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
156 CHECK(Val.i32 == 42);
157 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
158 CHECK(Val.i32 == 42);
159 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
160 CHECK(Val.i32 == 42);
161
162 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
163 CHECK_pDef(s_aOpts2, 2);
164 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
165 CHECK_pDef(s_aOpts2, 2);
166
167 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
168 CHECK_pDef(s_aOpts2, 3);
169 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
170 CHECK_pDef(s_aOpts2, 4);
171
172 /* -novalue / -startvm (single dash long options) */
173 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
174 CHECK_pDef(s_aOpts2, 5);
175 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 386, 2);
176 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "myvm"));
177
178 /* no-dash options */
179 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 387, 1);
180 CHECK_pDef(s_aOpts2, 7);
181 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 388, 2);
182 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string3"));
183
184 /* non-option, option, non-option */
185 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
186 CHECK(Val.psz && !strcmp(Val.psz, "filename1"));
187 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
188 CHECK_pDef(s_aOpts2, 3);
189 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
190 CHECK(Val.psz && !strcmp(Val.psz, "filename2"));
191
192 /* compress short options */
193 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 0);
194 CHECK_pDef(s_aOpts2, 2);
195 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 0);
196 CHECK_pDef(s_aOpts2, 3);
197 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
198 CHECK(Val.i32 == 999);
199
200 /* IPv4 */
201 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'g', 1);
202 CHECK(Val.IPv4Addr.u == RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8(192,168,1,1))));
203
204 /* the end */
205 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
206 CHECK(Val.pDef == NULL);
207 CHECK(argc2 == GetState.iNext);
208
209
210 /*
211 * Summary.
212 */
213 if (!cErrors)
214 RTPrintf("tstGetOpt: SUCCESS\n");
215 else
216 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
217
218 return !!cErrors;
219}
220
Note: See TracBrowser for help on using the repository browser.

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