1 | /* $Id: tstGetOpt.cpp 6000 2007-12-07 15:12:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime Testcase - RTGetOpt
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
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 |
|
---|
27 |
|
---|
28 | #include <iprt/getopt.h>
|
---|
29 | #include <iprt/stream.h>
|
---|
30 | #include <iprt/initterm.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | int main()
|
---|
36 | {
|
---|
37 | int cErrors = 0;
|
---|
38 | RTR3Init();
|
---|
39 |
|
---|
40 | int i;
|
---|
41 | RTOPTIONUNION Val;
|
---|
42 | #define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (i=%d): %s\n", __LINE__, i, #expr); cErrors++; } } while (0)
|
---|
43 |
|
---|
44 | #define CHECK_GETOPT(expr, chRet, iNext) \
|
---|
45 | do { \
|
---|
46 | CHECK((expr) == (chRet)); \
|
---|
47 | CHECK(i == (iNext)); \
|
---|
48 | i = (iNext); \
|
---|
49 | } while (0)
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Simple.
|
---|
53 | */
|
---|
54 | static const RTOPTIONDEF s_aOpts2[] =
|
---|
55 | {
|
---|
56 | { "--optwithstring", 's', RTGETOPT_REQ_STRING },
|
---|
57 | { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
|
---|
58 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
59 | { NULL, 'q', RTGETOPT_REQ_NOTHING },
|
---|
60 | { "--quiet", 384, RTGETOPT_REQ_NOTHING },
|
---|
61 | };
|
---|
62 |
|
---|
63 | char *argv2[] =
|
---|
64 | {
|
---|
65 | "-s", "string1",
|
---|
66 | "--optwithstring", "string2",
|
---|
67 | "-i", "-42",
|
---|
68 | "--optwithint", "42",
|
---|
69 | "-v",
|
---|
70 | "--verbose",
|
---|
71 | "-q",
|
---|
72 | "--quiet",
|
---|
73 | /* "filename1", */
|
---|
74 | /* "filename2", */
|
---|
75 | NULL
|
---|
76 | };
|
---|
77 | int argc2 = (int)RT_ELEMENTS(argv2) - 1;
|
---|
78 |
|
---|
79 | i = 0;
|
---|
80 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
|
---|
81 | CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
|
---|
82 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 4);
|
---|
83 | CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
|
---|
84 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 6);
|
---|
85 | CHECK(Val.i32 == -42);
|
---|
86 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 8);
|
---|
87 | CHECK(Val.i32 == 42);
|
---|
88 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 9);
|
---|
89 | CHECK(Val.pDef == &s_aOpts2[2]);
|
---|
90 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 10);
|
---|
91 | CHECK(Val.pDef == &s_aOpts2[2]);
|
---|
92 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'q', 11);
|
---|
93 | CHECK(Val.pDef == &s_aOpts2[3]);
|
---|
94 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 384, 12);
|
---|
95 | CHECK(Val.pDef == &s_aOpts2[4]);
|
---|
96 | CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 0, 12);
|
---|
97 | CHECK(Val.pDef == NULL);
|
---|
98 | CHECK(argc2 == i);
|
---|
99 |
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Summary.
|
---|
103 | */
|
---|
104 | if (!cErrors)
|
---|
105 | RTPrintf("tstGetOpt: SUCCESS\n");
|
---|
106 | else
|
---|
107 | RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
|
---|
108 |
|
---|
109 | return !!cErrors;
|
---|
110 | }
|
---|