VirtualBox

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

Last change on this file since 8971 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/* $Id: tstGetOpt.cpp 8245 2008-04-21 17:24:28Z 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 int i;
45 RTOPTIONUNION Val;
46#define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (i=%d): %s\n", __LINE__, i, #expr); cErrors++; } } while (0)
47
48#define CHECK_GETOPT(expr, chRet, iInc) \
49 do { \
50 const int iPrev = i; \
51 CHECK((expr) == (chRet)); \
52 CHECK(i == (iInc) + iPrev); \
53 i = (iInc) + iPrev; \
54 } while (0)
55
56 /*
57 * Simple.
58 */
59 static const RTOPTIONDEF 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 };
67
68 char *argv2[] =
69 {
70 "-s", "string1",
71 "--optwithstring", "string2",
72 "-i", "-42",
73 "-i:-42",
74 "-i=-42",
75 "-i:", "-42",
76 "-i=", "-42",
77 "--optwithint", "42",
78 "--optwithint:42",
79 "--optwithint=42",
80 "--optwithint:", "42",
81 "--optwithint=", "42",
82 "-v",
83 "--verbose",
84 "-q",
85 "--quiet",
86 /* "filename1", */
87 /* "filename2", */
88 NULL
89 };
90 int argc2 = (int)RT_ELEMENTS(argv2) - 1;
91
92 i = 0;
93 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
94 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
95 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
96 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
97
98 /* -i */
99 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
100 CHECK(Val.i32 == -42);
101 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
102 CHECK(Val.i32 == -42);
103 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
104 CHECK(Val.i32 == -42);
105 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
106 CHECK(Val.i32 == -42);
107 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
108 CHECK(Val.i32 == -42);
109
110 /* --optwithint */
111 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
112 CHECK(Val.i32 == 42);
113 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
114 CHECK(Val.i32 == 42);
115 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
116 CHECK(Val.i32 == 42);
117 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
118 CHECK(Val.i32 == 42);
119 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
120 CHECK(Val.i32 == 42);
121
122 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);
123 CHECK(Val.pDef == &s_aOpts2[2]);
124 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);
125 CHECK(Val.pDef == &s_aOpts2[2]);
126 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'q', 1);
127 CHECK(Val.pDef == &s_aOpts2[3]);
128 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 384, 1);
129 CHECK(Val.pDef == &s_aOpts2[4]);
130 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 0, 0);
131 CHECK(Val.pDef == NULL);
132 CHECK(argc2 == i);
133
134
135 /*
136 * Summary.
137 */
138 if (!cErrors)
139 RTPrintf("tstGetOpt: SUCCESS\n");
140 else
141 RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
142
143 return !!cErrors;
144}
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