VirtualBox

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

最後變更 在這個檔案從17093是 17093,由 vboxsync 提交於 16 年 前

RTGetOpt interface changes.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 4.7 KB
 
1/* $Id: tstGetOpt.cpp 17093 2009-02-24 19:58:09Z 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.alldomusa.eu.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 { "-quiet2", 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 "-quiet2",
88 /* "filename1", */
89 /* "filename2", */
90 NULL
91 };
92 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
93
94 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
95
96 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
97 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
98 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
99 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
100
101 /* -i */
102 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
103 CHECK(Val.i32 == -42);
104 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
105 CHECK(Val.i32 == -42);
106 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
107 CHECK(Val.i32 == -42);
108 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
109 CHECK(Val.i32 == -42);
110 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
111 CHECK(Val.i32 == -42);
112
113 /* --optwithint */
114 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
115 CHECK(Val.i32 == 42);
116 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
117 CHECK(Val.i32 == 42);
118 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
119 CHECK(Val.i32 == 42);
120 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
121 CHECK(Val.i32 == 42);
122 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
123 CHECK(Val.i32 == 42);
124
125 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
126 CHECK(Val.pDef == &s_aOpts2[2]);
127 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
128 CHECK(Val.pDef == &s_aOpts2[2]);
129 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
130 CHECK(Val.pDef == &s_aOpts2[3]);
131 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
132 CHECK(Val.pDef == &s_aOpts2[4]);
133 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
134 CHECK(Val.pDef == &s_aOpts2[5]);
135 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
136 CHECK(Val.pDef == NULL);
137 CHECK(argc2 == GetState.iNext);
138
139
140 /*
141 * Summary.
142 */
143 if (!cErrors)
144 RTPrintf("tstGetOpt: SUCCESS\n");
145 else
146 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
147
148 return !!cErrors;
149}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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