VirtualBox

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

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

RTGetOpt: Fixed 'tstHeadless -startvm vm' breakage from earlier today.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/* $Id: tstGetOpt.cpp 17100 2009-02-24 21:37:20Z 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.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#include <iprt/getopt.h>
33#include <iprt/stream.h>
34#include <iprt/initterm.h>
35#include <iprt/string.h>
36#include <iprt/err.h>
37
38
39int main()
40{
41 int cErrors = 0;
42 RTR3Init();
43
44 RTGETOPTSTATE GetState;
45 RTGETOPTUNION Val;
46#define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); cErrors++; } } while (0)
47
48#define CHECK_GETOPT(expr, chRet, iInc) \
49 do { \
50 const int iPrev = GetState.iNext; \
51 CHECK((expr) == (chRet)); \
52 CHECK(GetState.iNext == (iInc) + iPrev); \
53 GetState.iNext = (iInc) + iPrev; \
54 } while (0)
55
56 /*
57 * Simple.
58 */
59 static const RTGETOPTDEF s_aOpts2[] =
60 {
61 { "--optwithstring", 's', RTGETOPT_REQ_STRING },
62 { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
63 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
64 { NULL, 'q', RTGETOPT_REQ_NOTHING },
65 { "--quiet", 384, RTGETOPT_REQ_NOTHING },
66 { "-startvm", 385, RTGETOPT_REQ_NOTHING },
67 };
68
69 char *argv2[] =
70 {
71 "-s", "string1",
72 "--optwithstring", "string2",
73 "-i", "-42",
74 "-i:-42",
75 "-i=-42",
76 "-i:", "-42",
77 "-i=", "-42",
78 "--optwithint", "42",
79 "--optwithint:42",
80 "--optwithint=42",
81 "--optwithint:", "42",
82 "--optwithint=", "42",
83 "-v",
84 "--verbose",
85 "-q",
86 "--quiet",
87 "-startvm",
88 "filename1",
89 "-q",
90 "filename2",
91 NULL
92 };
93 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
94
95 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
96
97 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
98 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
99 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
100 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
101
102 /* -i */
103 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
104 CHECK(Val.i32 == -42);
105 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
106 CHECK(Val.i32 == -42);
107 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
108 CHECK(Val.i32 == -42);
109 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
110 CHECK(Val.i32 == -42);
111 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
112 CHECK(Val.i32 == -42);
113
114 /* --optwithint */
115 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
116 CHECK(Val.i32 == 42);
117 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
118 CHECK(Val.i32 == 42);
119 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
120 CHECK(Val.i32 == 42);
121 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
122 CHECK(Val.i32 == 42);
123 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
124 CHECK(Val.i32 == 42);
125
126 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
127 CHECK(Val.pDef == &s_aOpts2[2]);
128 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
129 CHECK(Val.pDef == &s_aOpts2[2]);
130 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
131 CHECK(Val.pDef == &s_aOpts2[3]);
132 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
133 CHECK(Val.pDef == &s_aOpts2[4]);
134 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
135 CHECK(Val.pDef == &s_aOpts2[5]);
136
137 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
138 CHECK(Val.psz && !strcmp(Val.psz, "filename1"));
139 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
140 CHECK(Val.pDef == &s_aOpts2[3]);
141 CHECK_GETOPT(RTGetOpt(&GetState, &Val), VINF_GETOPT_NOT_OPTION, 1);
142 CHECK(Val.psz && !strcmp(Val.psz, "filename2"));
143
144 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
145 CHECK(Val.pDef == NULL);
146 CHECK(argc2 == GetState.iNext);
147
148
149 /*
150 * Summary.
151 */
152 if (!cErrors)
153 RTPrintf("tstGetOpt: SUCCESS\n");
154 else
155 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
156
157 return !!cErrors;
158}
159
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