VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstStrToNum.cpp@ 96127

Last change on this file since 96127 was 96127, checked in by vboxsync, 3 years ago

IPRT/RTStrTo[U]Int[8|16|32|64][Ex|Full]: Tweaked the base parameter to allow passing a maximum length. Need this for the no-CRT sscanf code. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.5 KB
Line 
1/* $Id: tstStrToNum.cpp 96127 2022-08-09 10:00:57Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String To Number Conversion.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/test.h>
32#include <iprt/string.h>
33#include <iprt/stream.h>
34#include <iprt/err.h>
35
36struct TstI64
37{
38 const char *psz;
39 unsigned uBase;
40 int rc;
41 int64_t Result;
42};
43
44struct TstU64
45{
46 const char *psz;
47 unsigned uBase;
48 int rc;
49 uint64_t Result;
50};
51
52struct TstI32
53{
54 const char *psz;
55 unsigned uBase;
56 int rc;
57 int32_t Result;
58};
59
60struct TstU32
61{
62 const char *psz;
63 unsigned uBase;
64 int rc;
65 uint32_t Result;
66};
67
68
69#define TEST(Test, Type, Fmt, Fun, iTest) \
70 do \
71 { \
72 Type Result; \
73 int rc = Fun(Test.psz, NULL, Test.uBase, &Result); \
74 if (Result != Test.Result) \
75 RTTestIFailed("'%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
76 else if (rc != Test.rc) \
77 RTTestIFailed("'%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
78 } while (0)
79
80
81#define RUN_TESTS(aTests, Type, Fmt, Fun) \
82 do \
83 { \
84 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
85 { \
86 TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
87 } \
88 } while (0)
89
90#define FULL_TEST(Test, Type, Fmt, Fun, iTest) \
91 do \
92 { \
93 Type Result; \
94 int rc = Fun(Test.psz, Test.uBase, &Result); \
95 if (Result != Test.Result) \
96 RTTestIFailed("'%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
97 else if (rc != Test.rc) \
98 RTTestIFailed("'%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
99 } while (0)
100
101
102#define RUN_FULL_TESTS(aTests, Type, Fmt, Fun) \
103 do \
104 { \
105 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
106 { \
107 FULL_TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
108 } \
109 } while (0)
110
111int main()
112{
113 RTTEST hTest;
114 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrToNum", &hTest);
115 if (rcExit != RTEXITCODE_SUCCESS)
116 return rcExit;
117
118 static const struct TstU64 aTstU64[] =
119 {
120 { "0", 0, VINF_SUCCESS, 0 },
121 { "1", 0, VINF_SUCCESS, 1 },
122 { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0ULL },
123 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
124 { "0x1", 0, VINF_SUCCESS, 1 },
125 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
126 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
127 { "0x0ffffffffffffffffffffff", 10 << 8, VINF_SUCCESS, 0x0fffffff },
128 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
129 { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
130 { "4D9702C5CBD9B778", 16, VINF_SUCCESS, UINT64_C(0x4D9702C5CBD9B778) },
131 };
132 RUN_TESTS(aTstU64, uint64_t, "%#llx", RTStrToUInt64Ex);
133
134 static const struct TstU64 aTstFullU64[] =
135 {
136 { "42", 0, VINF_SUCCESS, 42 },
137 { "42 ", 0, VERR_TRAILING_SPACES, 42 },
138 { "42! ", 0, VERR_TRAILING_CHARS, 42 },
139 { "42 !", 0, VERR_TRAILING_CHARS, 42 },
140 { "42 !", 2<<8, VINF_SUCCESS, 42 },
141 { "42 !", 3<<8, VERR_TRAILING_SPACES, 42 },
142 { "42 !", 4<<8, VERR_TRAILING_CHARS, 42 },
143 { "-1", 0, VWRN_NEGATIVE_UNSIGNED, UINT64_MAX },
144 { "-1 ", 0, VERR_TRAILING_SPACES, UINT64_MAX },
145 { "-1 ", 2<<8, VWRN_NEGATIVE_UNSIGNED, UINT64_MAX },
146 { "-1 ", 3<<8, VERR_TRAILING_SPACES, UINT64_MAX },
147 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
148 { "0x0ffffffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
149 { "0x0ffffffffffffffffffff ", 0, VERR_TRAILING_SPACES, 0xffffffffffffffffULL },
150 { "0x0ffffffffffffffffffff! ", 0, VERR_TRAILING_CHARS, 0xffffffffffffffffULL },
151 { "0x0ffffffffffffffffffff !", 0, VERR_TRAILING_CHARS, 0xffffffffffffffffULL },
152 { "0x0ffffffffffffffffffff", 10 << 8, VINF_SUCCESS, 0x0fffffff },
153 };
154 RUN_FULL_TESTS(aTstFullU64, uint64_t, "%#llx", RTStrToUInt64Full);
155
156 static const struct TstI64 aTstI64[] =
157 {
158 { "0", 0, VINF_SUCCESS, 0 },
159 { "1", 0, VINF_SUCCESS, 1 },
160 { "-1", 0, VINF_SUCCESS, -1 },
161 { "-1", 10, VINF_SUCCESS, -1 },
162 { "-31", 0, VINF_SUCCESS, -31 },
163 { "-31", 10, VINF_SUCCESS, -31 },
164 { "-32", 0, VINF_SUCCESS, -32 },
165 { "-33", 0, VINF_SUCCESS, -33 },
166 { "-64", 0, VINF_SUCCESS, -64 },
167 { "-127", 0, VINF_SUCCESS, -127 },
168 { "-128", 0, VINF_SUCCESS, -128 },
169 { "-129", 0, VINF_SUCCESS, -129 },
170 { "-254", 0, VINF_SUCCESS, -254 },
171 { "-255", 0, VINF_SUCCESS, -255 },
172 { "-256", 0, VINF_SUCCESS, -256 },
173 { "-257", 0, VINF_SUCCESS, -257 },
174 { "-511", 0, VINF_SUCCESS, -511 },
175 { "-512", 0, VINF_SUCCESS, -512 },
176 { "-513", 0, VINF_SUCCESS, -513 },
177 { "-1023", 0, VINF_SUCCESS, -1023 },
178 { "-1023", 0, VINF_SUCCESS, -1023 },
179 { "-1023", 0, VINF_SUCCESS, -1023},
180 { "-1023", 10, VINF_SUCCESS, -1023 },
181 { "-4564678", 0, VINF_SUCCESS, -4564678 },
182 { "-4564678", 10, VINF_SUCCESS, -4564678 },
183 { "-1234567890123456789", 0, VINF_SUCCESS, -1234567890123456789LL },
184 { "-1234567890123456789", 10, VINF_SUCCESS, -1234567890123456789LL },
185 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
186 { "0x1", 0, VINF_SUCCESS, 1 },
187 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
188 { "0x1", 16, VINF_SUCCESS, 1 },
189 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
190 { "0x7fffffffffffffff", 0, VINF_SUCCESS, 0x7fffffffffffffffULL },
191 { "0xffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
192 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x1111111111111111ULL },
193 { "0x02222222222222222222222",0, VWRN_NUMBER_TOO_BIG, 0x2222222222222222ULL },
194 { "0x03333333333333333333333",0, VWRN_NUMBER_TOO_BIG, 0x3333333333333333ULL },
195 { "0x04444444444444444444444",0, VWRN_NUMBER_TOO_BIG, 0x4444444444444444ULL },
196 { "0x07777777777777777777777",0, VWRN_NUMBER_TOO_BIG, 0x7777777777777777ULL },
197 { "0x07f7f7f7f7f7f7f7f7f7f7f",0, VWRN_NUMBER_TOO_BIG, 0x7f7f7f7f7f7f7f7fULL },
198 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, (int64_t)0xffffffffffffffffULL },
199 { "0x0ffffffffffffffffffffff", 10 << 8, VINF_SUCCESS, INT64_C(0x0fffffff) },
200 { "0x0ffffffffffffffffffffff", 18 << 8, VINF_SUCCESS, INT64_C(0x0fffffffffffffff) },
201 { "0x0ffffffffffffffffffffff", 19 << 8, VWRN_NUMBER_TOO_BIG, -1 },
202 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
203 { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
204 };
205 RUN_TESTS(aTstI64, int64_t, "%#lld", RTStrToInt64Ex);
206
207 static const struct TstI64 aTstI64Full[] =
208 {
209 { "1", 0, VINF_SUCCESS, 1 },
210 { "1 ", 0, VERR_TRAILING_SPACES, 1 },
211 { "1! ", 0, VERR_TRAILING_CHARS, 1 },
212 { "1 !", 0, VERR_TRAILING_CHARS, 1 },
213 { "1 !", 1<<8, VINF_SUCCESS, 1 },
214 { "1 !", 2<<8, VERR_TRAILING_SPACES, 1 },
215 { "1 !", 3<<8, VERR_TRAILING_CHARS, 1 },
216 { "0xffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
217 { "0xffffffffffffffff ", 0, VERR_TRAILING_SPACES, -1 },
218 { "0xffffffffffffffff!", 0, VERR_TRAILING_CHARS, -1 },
219 { "0xffffffffffffffff !", 18<<8, VWRN_NUMBER_TOO_BIG, -1 },
220 { "0xffffffffffffffff !", 19<<8, VERR_TRAILING_SPACES, -1 },
221 { "0xffffffffffffffff !", 20<<8, VERR_TRAILING_CHARS, -1 },
222 };
223 RUN_FULL_TESTS(aTstI64Full, int64_t, "%#lld", RTStrToInt64Full);
224
225
226 static const struct TstI32 aTstI32[] =
227 {
228 { "0", 0, VINF_SUCCESS, 0 },
229 { "1", 0, VINF_SUCCESS, 1 },
230 { "-1", 0, VINF_SUCCESS, -1 },
231 { "-1", 10, VINF_SUCCESS, -1 },
232 { "-31", 0, VINF_SUCCESS, -31 },
233 { "-31", 10, VINF_SUCCESS, -31 },
234 { "-32", 0, VINF_SUCCESS, -32 },
235 { "-33", 0, VINF_SUCCESS, -33 },
236 { "-64", 0, VINF_SUCCESS, -64 },
237 { "-127", 0, VINF_SUCCESS, -127 },
238 { "-128", 0, VINF_SUCCESS, -128 },
239 { "-129", 0, VINF_SUCCESS, -129 },
240 { "-254", 0, VINF_SUCCESS, -254 },
241 { "-255", 0, VINF_SUCCESS, -255 },
242 { "-256", 0, VINF_SUCCESS, -256 },
243 { "-257", 0, VINF_SUCCESS, -257 },
244 { "-511", 0, VINF_SUCCESS, -511 },
245 { "-512", 0, VINF_SUCCESS, -512 },
246 { "-513", 0, VINF_SUCCESS, -513 },
247 { "-1023", 0, VINF_SUCCESS, -1023 },
248 { "-1023", 0, VINF_SUCCESS, -1023 },
249 { "-1023", 0, VINF_SUCCESS, -1023},
250 { "-1023", 10, VINF_SUCCESS, -1023 },
251 { "-4564678", 0, VINF_SUCCESS, -4564678 },
252 { "-4564678", 10, VINF_SUCCESS, -4564678 },
253 { "4564678", 0, VINF_SUCCESS, 4564678 },
254 { "4564678", 10, VINF_SUCCESS, 4564678 },
255 { "-1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)((uint64_t)INT64_C(-1234567890123456789) & UINT32_MAX) },
256 { "-1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)((uint64_t)INT64_C(-1234567890123456789) & UINT32_MAX) },
257 { "1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)(INT64_C(1234567890123456789) & UINT32_MAX) },
258 { "1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)(INT64_C(1234567890123456789) & UINT32_MAX) },
259 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
260 { "0x1", 0, VINF_SUCCESS, 1 },
261 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
262 { "0x1", 16, VINF_SUCCESS, 1 },
263 { "0x7fffffff", 0, VINF_SUCCESS, 0x7fffffff },
264 { "0x80000000", 0, VWRN_NUMBER_TOO_BIG, INT32_MIN },
265 { "0xffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
266 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, (int32_t)0xffffffff },
267 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x11111111 },
268 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, (int32_t)0xffffffff },
269 { "0x0ffffffffffffffffffffff", 10 << 8, VINF_SUCCESS, 0x0fffffff },
270 { "0x0ffffffffffffffffffffff", 11 << 8, VWRN_NUMBER_TOO_BIG, -1 },
271 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
272 { "0x1111111", 0, VINF_SUCCESS, 0x01111111 },
273 };
274 RUN_TESTS(aTstI32, int32_t, "%#d", RTStrToInt32Ex);
275
276 static const struct TstU32 aTstU32[] =
277 {
278 { "0", 0, VINF_SUCCESS, 0 },
279 { "1", 0, VINF_SUCCESS, 1 },
280 /// @todo { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0 }, - no longer true. bad idea?
281 { "-1", 0, VWRN_NUMBER_TOO_BIG, ~0U },
282 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
283 { "0x1", 0, VINF_SUCCESS, 1 },
284 { "0x1 ", 0, VWRN_TRAILING_SPACES, 1 },
285 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
286 { "0x0ffffffffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
287 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
288 { "0x1111111", 0, VINF_SUCCESS, 0x1111111 },
289 };
290 RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
291
292
293 static const struct TstU32 aTstFullU32[] =
294 {
295 { "0", 0, VINF_SUCCESS, 0 },
296 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
297 { "0x0fffffffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
298 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
299 { "42 ", 0, VERR_TRAILING_SPACES, 42 },
300 { "42 ", 2<<8, VINF_SUCCESS, 42 },
301 { "42! ", 0, VERR_TRAILING_CHARS, 42 },
302 { "42! ", 2<<8, VINF_SUCCESS, 42 },
303 { "42 !", 0, VERR_TRAILING_CHARS, 42 },
304 { "42 !", 2<<8, VINF_SUCCESS, 42 },
305 { "42 !", 3<<8, VERR_TRAILING_SPACES, 42 },
306 { "42 !", 4<<8, VERR_TRAILING_CHARS, 42 },
307 { "0x0fffffffffffffffffffff ", 0, VERR_TRAILING_SPACES, 0xffffffffU },
308 { "0x0fffffffffffffffffffff !", 0, VERR_TRAILING_CHARS, 0xffffffffU },
309 };
310 RUN_FULL_TESTS(aTstFullU32, uint32_t, "%#x", RTStrToUInt32Full);
311
312 /*
313 * Test the some hex stuff too.
314 */
315 static const struct
316 {
317 const char *pszHex;
318 size_t cbOut;
319 size_t offNext;
320 uint8_t bLast;
321 bool fColon;
322 int rc;
323 } s_aConvertHexTests[] =
324 {
325 { "00", 1, 2, 0x00, true, VINF_SUCCESS },
326 { "00", 1, 2, 0x00, false, VINF_SUCCESS },
327 { "000102", 3, 6, 0x02, true, VINF_SUCCESS },
328 { "00019", 2, 4, 0x01, false, VERR_UNEVEN_INPUT },
329 { "00019", 2, 4, 0x01, true, VERR_UNEVEN_INPUT },
330 { "0001:9", 3, 6, 0x09, true, VINF_SUCCESS},
331 { "000102", 3, 6, 0x02, false, VINF_SUCCESS },
332 { "0:1", 2, 3, 0x01, true, VINF_SUCCESS },
333 { ":", 2, 1, 0x00, true, VINF_SUCCESS },
334 { "0:01", 2, 4, 0x01, true, VINF_SUCCESS },
335 { "00:01", 2, 5, 0x01, true, VINF_SUCCESS },
336 { ":1:2:3:4:5", 6, 10, 0x05, true, VINF_SUCCESS },
337 { ":1:2:3::5", 6, 9, 0x05, true, VINF_SUCCESS },
338 { ":1:2:3:4:", 6, 9, 0x00, true, VINF_SUCCESS },
339 };
340 for (unsigned i = 0; i < RT_ELEMENTS(s_aConvertHexTests); i++)
341 {
342 uint8_t abBuf[1024];
343 memset(abBuf, 0xf6, sizeof(abBuf));
344 const char *pszExpectNext = &s_aConvertHexTests[i].pszHex[s_aConvertHexTests[i].offNext];
345 const char *pszNext = "";
346 size_t cbReturned = 77777;
347 int rc = RTStrConvertHexBytesEx(s_aConvertHexTests[i].pszHex, abBuf, s_aConvertHexTests[i].cbOut,
348 s_aConvertHexTests[i].fColon ? RTSTRCONVERTHEXBYTES_F_SEP_COLON : 0,
349 &pszNext, &cbReturned);
350 if ( rc != s_aConvertHexTests[i].rc
351 || pszNext != pszExpectNext
352 || abBuf[s_aConvertHexTests[i].cbOut - 1] != s_aConvertHexTests[i].bLast
353 )
354 RTTestFailed(hTest, "RTStrConvertHexBytesEx/#%u %s -> %Rrc %p %#zx %#02x, expected %Rrc %p %#zx %#02x\n",
355 i, s_aConvertHexTests[i].pszHex,
356 rc, pszNext, cbReturned, abBuf[s_aConvertHexTests[i].cbOut - 1],
357 s_aConvertHexTests[i].rc, pszExpectNext, s_aConvertHexTests[i].cbOut, s_aConvertHexTests[i].bLast);
358 }
359
360
361 /*
362 * Summary.
363 */
364 return RTTestSummaryAndDestroy(hTest);
365}
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