VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp@ 44535

Last change on this file since 44535 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: tstRTGetOptArgv.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTGetOptArgv*.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
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* Header Files *
29*******************************************************************************/
30#include <iprt/path.h>
31
32#include <iprt/err.h>
33#include <iprt/param.h>
34#include <iprt/getopt.h>
35#include <iprt/string.h>
36#include <iprt/test.h>
37
38
39static void tst2(void)
40{
41 RTTestISub("RTGetOptArgvToString / MS_CRT");
42
43 static const struct
44 {
45 const char * const apszArgs[5];
46 const char *pszCmdLine;
47 } s_aMscCrtTests[] =
48 {
49 {
50 { "abcd", "a ", " b", " c ", NULL },
51 "abcd \"a \" \" b\" \" c \""
52 },
53 {
54 { "a\\\\\\b", "de fg", "h", NULL, NULL },
55 "a\\\\\\b \"de fg\" h"
56 },
57 {
58 { "a\\\"b", "c", "d", "\"", NULL },
59 "\"a\\\\\\\"b\" c d \"\\\"\""
60 },
61 {
62 { "a\\\\b c", "d", "e", " \\", NULL },
63 "\"a\\\\b c\" d e \" \\\\\""
64 },
65 };
66
67 for (size_t i = 0; i < RT_ELEMENTS(s_aMscCrtTests); i++)
68 {
69 char *pszCmdLine = NULL;
70 int rc = RTGetOptArgvToString(&pszCmdLine, s_aMscCrtTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
71 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
72 if (strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
73 RTTestIFailed("g_aTest[%i] failed:\n"
74 " got '%s'\n"
75 " expected '%s'\n",
76 i, pszCmdLine, s_aMscCrtTests[i].pszCmdLine);
77 RTStrFree(pszCmdLine);
78 }
79
80
81 RTTestISub("RTGetOptArgvToString / BOURNE_SH");
82
83 static const struct
84 {
85 const char * const apszArgs[5];
86 const char *pszCmdLine;
87 } s_aBournShTests[] =
88 {
89 {
90 { "abcd", "a ", " b", " c ", NULL },
91 "abcd 'a ' ' b' ' c '"
92 },
93 {
94 { "a\n\\b", "de'fg", "h", "'", NULL },
95 "'a\n\\b' 'de'\"'\"'fg' h ''\"'\"''"
96 }
97 };
98
99 for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
100 {
101 char *pszCmdLine = NULL;
102 int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
103 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
104 if (strcmp(s_aBournShTests[i].pszCmdLine, pszCmdLine))
105 RTTestIFailed("g_aTest[%i] failed:\n"
106 " got |%s|\n"
107 " expected |%s|\n",
108 i, pszCmdLine, s_aBournShTests[i].pszCmdLine);
109 RTStrFree(pszCmdLine);
110 }
111
112
113 RTTestISub("RTGetOptArgvToString <-> RTGetOptArgvFromString");
114
115 for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
116 {
117 char *pszCmdLine = NULL;
118 int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
119 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
120
121 char **papszArgs;
122 int cArgs;
123 rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszCmdLine, NULL);
124 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
125
126 size_t j = 0;
127 while (papszArgs[j] && s_aBournShTests[i].apszArgs[j])
128 {
129 if (strcmp(papszArgs[j], s_aBournShTests[i].apszArgs[j]))
130 RTTestIFailed("Test #%u, argument #%u mismatch:\n"
131 " FromString: |%s| (got)\n"
132 " ToString: |%s| (expected)\n",
133 i, j, papszArgs[j], s_aBournShTests[i].apszArgs[j]);
134
135 /* next */
136 j++;
137 }
138 RTTESTI_CHECK(papszArgs[j] == NULL);
139 RTTESTI_CHECK(s_aBournShTests[i].apszArgs[j] == NULL);
140
141 RTGetOptArgvFree(papszArgs);
142 RTStrFree(pszCmdLine);
143 }
144}
145
146static void tst1(void)
147{
148 RTTestISub("RTGetOptArgvFromString");
149 char **papszArgs = NULL;
150 int cArgs = -1;
151 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "", NULL), VINF_SUCCESS);
152 RTTESTI_CHECK_RETV(cArgs == 0);
153 RTTESTI_CHECK_RETV(papszArgs);
154 RTTESTI_CHECK_RETV(!papszArgs[0]);
155 RTGetOptArgvFree(papszArgs);
156
157 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0 1 \"\"2'' '3' 4 5 '''''6' 7 8 9 10 11", NULL), VINF_SUCCESS);
158 RTTESTI_CHECK_RETV(cArgs == 12);
159 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
160 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
161 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
162 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
163 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
164 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
165 RTTESTI_CHECK_RETV(!strcmp(papszArgs[6], "6"));
166 RTTESTI_CHECK_RETV(!strcmp(papszArgs[7], "7"));
167 RTTESTI_CHECK_RETV(!strcmp(papszArgs[8], "8"));
168 RTTESTI_CHECK_RETV(!strcmp(papszArgs[9], "9"));
169 RTTESTI_CHECK_RETV(!strcmp(papszArgs[10], "10"));
170 RTTESTI_CHECK_RETV(!strcmp(papszArgs[11], "11"));
171 RTTESTI_CHECK_RETV(!papszArgs[12]);
172 RTGetOptArgvFree(papszArgs);
173
174 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "\t\" asdf \" '\"'xyz \"\t\" '\n' '\"' \"'\"\n\r ", NULL), VINF_SUCCESS);
175 RTTESTI_CHECK_RETV(cArgs == 6);
176 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], " asdf "));
177 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "\"xyz"));
178 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "\t"));
179 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "\n"));
180 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "\""));
181 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "\'"));
182 RTTESTI_CHECK_RETV(!papszArgs[6]);
183 RTGetOptArgvFree(papszArgs);
184
185 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, ":0::1::::2:3:4:5:", ":"), VINF_SUCCESS);
186 RTTESTI_CHECK_RETV(cArgs == 6);
187 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
188 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
189 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
190 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
191 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
192 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
193 RTTESTI_CHECK_RETV(!papszArgs[6]);
194 RTGetOptArgvFree(papszArgs);
195
196 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0:1;2:3;4:5", ";;;;;;;;;;;;;;;;;;;;;;:"), VINF_SUCCESS);
197 RTTESTI_CHECK_RETV(cArgs == 6);
198 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
199 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
200 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
201 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
202 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
203 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
204 RTTESTI_CHECK_RETV(!papszArgs[6]);
205 RTGetOptArgvFree(papszArgs);
206}
207
208int main()
209{
210 /*
211 * Init RT+Test.
212 */
213 RTTEST hTest;
214 int rc = RTTestInitAndCreate("tstRTGetOptArgv", &hTest);
215 if (rc)
216 return rc;
217 RTTestBanner(hTest);
218
219 /*
220 * The test.
221 */
222 tst1();
223 tst2();
224
225 /*
226 * Summary.
227 */
228 return RTTestSummaryAndDestroy(hTest);
229}
230
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