1 | /* $Id: tstStrToNum.cpp 62723 2016-07-30 00:02:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - String To Number Conversion.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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 | #include <iprt/initterm.h>
|
---|
28 | #include <iprt/string.h>
|
---|
29 | #include <iprt/stream.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 |
|
---|
32 | struct TstI64
|
---|
33 | {
|
---|
34 | const char *psz;
|
---|
35 | unsigned uBase;
|
---|
36 | int rc;
|
---|
37 | int64_t Result;
|
---|
38 | };
|
---|
39 |
|
---|
40 | struct TstU64
|
---|
41 | {
|
---|
42 | const char *psz;
|
---|
43 | unsigned uBase;
|
---|
44 | int rc;
|
---|
45 | uint64_t Result;
|
---|
46 | };
|
---|
47 |
|
---|
48 | struct TstI32
|
---|
49 | {
|
---|
50 | const char *psz;
|
---|
51 | unsigned uBase;
|
---|
52 | int rc;
|
---|
53 | int32_t Result;
|
---|
54 | };
|
---|
55 |
|
---|
56 | struct TstU32
|
---|
57 | {
|
---|
58 | const char *psz;
|
---|
59 | unsigned uBase;
|
---|
60 | int rc;
|
---|
61 | uint32_t Result;
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | #define TEST(Test, Type, Fmt, Fun, iTest) \
|
---|
66 | do \
|
---|
67 | { \
|
---|
68 | Type Result; \
|
---|
69 | int rc = Fun(Test.psz, NULL, Test.uBase, &Result); \
|
---|
70 | if (Result != Test.Result) \
|
---|
71 | { \
|
---|
72 | RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
|
---|
73 | cErrors++; \
|
---|
74 | } \
|
---|
75 | else if (rc != Test.rc) \
|
---|
76 | { \
|
---|
77 | RTPrintf("failure: '%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
|
---|
78 | cErrors++; \
|
---|
79 | } \
|
---|
80 | } while (0)
|
---|
81 |
|
---|
82 |
|
---|
83 | #define RUN_TESTS(aTests, Type, Fmt, Fun) \
|
---|
84 | do \
|
---|
85 | { \
|
---|
86 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
|
---|
87 | { \
|
---|
88 | TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
|
---|
89 | } \
|
---|
90 | } while (0)
|
---|
91 |
|
---|
92 | int main()
|
---|
93 | {
|
---|
94 | RTR3InitExeNoArguments(0);
|
---|
95 |
|
---|
96 | int cErrors = 0;
|
---|
97 | static const struct TstU64 aTstU64[] =
|
---|
98 | {
|
---|
99 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
100 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
101 | { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0ULL },
|
---|
102 | { "0x", 0, VWRN_TRAILING_CHARS, 0 },
|
---|
103 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
104 | { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
|
---|
105 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
|
---|
106 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
107 | { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
|
---|
108 | { "4D9702C5CBD9B778", 16, VINF_SUCCESS, UINT64_C(0x4D9702C5CBD9B778) },
|
---|
109 | };
|
---|
110 | RUN_TESTS(aTstU64, uint64_t, "%#llx", RTStrToUInt64Ex);
|
---|
111 |
|
---|
112 | static const struct TstI64 aTstI64[] =
|
---|
113 | {
|
---|
114 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
115 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
116 | { "-1", 0, VINF_SUCCESS, -1 },
|
---|
117 | { "-1", 10, VINF_SUCCESS, -1 },
|
---|
118 | { "-31", 0, VINF_SUCCESS, -31 },
|
---|
119 | { "-31", 10, VINF_SUCCESS, -31 },
|
---|
120 | { "-32", 0, VINF_SUCCESS, -32 },
|
---|
121 | { "-33", 0, VINF_SUCCESS, -33 },
|
---|
122 | { "-64", 0, VINF_SUCCESS, -64 },
|
---|
123 | { "-127", 0, VINF_SUCCESS, -127 },
|
---|
124 | { "-128", 0, VINF_SUCCESS, -128 },
|
---|
125 | { "-129", 0, VINF_SUCCESS, -129 },
|
---|
126 | { "-254", 0, VINF_SUCCESS, -254 },
|
---|
127 | { "-255", 0, VINF_SUCCESS, -255 },
|
---|
128 | { "-256", 0, VINF_SUCCESS, -256 },
|
---|
129 | { "-257", 0, VINF_SUCCESS, -257 },
|
---|
130 | { "-511", 0, VINF_SUCCESS, -511 },
|
---|
131 | { "-512", 0, VINF_SUCCESS, -512 },
|
---|
132 | { "-513", 0, VINF_SUCCESS, -513 },
|
---|
133 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
134 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
135 | { "-1023", 0, VINF_SUCCESS, -1023},
|
---|
136 | { "-1023", 10, VINF_SUCCESS, -1023 },
|
---|
137 | { "-4564678", 0, VINF_SUCCESS, -4564678 },
|
---|
138 | { "-4564678", 10, VINF_SUCCESS, -4564678 },
|
---|
139 | { "-1234567890123456789", 0, VINF_SUCCESS, -1234567890123456789LL },
|
---|
140 | { "-1234567890123456789", 10, VINF_SUCCESS, -1234567890123456789LL },
|
---|
141 | { "0x", 0, VWRN_TRAILING_CHARS, 0 },
|
---|
142 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
143 | { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
|
---|
144 | { "0x1", 16, VINF_SUCCESS, 1 },
|
---|
145 | { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
|
---|
146 | { "0x7fffffffffffffff", 0, VINF_SUCCESS, 0x7fffffffffffffffULL },
|
---|
147 | { "0xffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
|
---|
148 | { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x1111111111111111ULL },
|
---|
149 | { "0x02222222222222222222222",0, VWRN_NUMBER_TOO_BIG, 0x2222222222222222ULL },
|
---|
150 | { "0x03333333333333333333333",0, VWRN_NUMBER_TOO_BIG, 0x3333333333333333ULL },
|
---|
151 | { "0x04444444444444444444444",0, VWRN_NUMBER_TOO_BIG, 0x4444444444444444ULL },
|
---|
152 | { "0x07777777777777777777777",0, VWRN_NUMBER_TOO_BIG, 0x7777777777777777ULL },
|
---|
153 | { "0x07f7f7f7f7f7f7f7f7f7f7f",0, VWRN_NUMBER_TOO_BIG, 0x7f7f7f7f7f7f7f7fULL },
|
---|
154 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, (int64_t)0xffffffffffffffffULL },
|
---|
155 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
156 | { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
|
---|
157 | };
|
---|
158 | RUN_TESTS(aTstI64, int64_t, "%#lld", RTStrToInt64Ex);
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | static const struct TstI32 aTstI32[] =
|
---|
163 | {
|
---|
164 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
165 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
166 | { "-1", 0, VINF_SUCCESS, -1 },
|
---|
167 | { "-1", 10, VINF_SUCCESS, -1 },
|
---|
168 | { "-31", 0, VINF_SUCCESS, -31 },
|
---|
169 | { "-31", 10, VINF_SUCCESS, -31 },
|
---|
170 | { "-32", 0, VINF_SUCCESS, -32 },
|
---|
171 | { "-33", 0, VINF_SUCCESS, -33 },
|
---|
172 | { "-64", 0, VINF_SUCCESS, -64 },
|
---|
173 | { "-127", 0, VINF_SUCCESS, -127 },
|
---|
174 | { "-128", 0, VINF_SUCCESS, -128 },
|
---|
175 | { "-129", 0, VINF_SUCCESS, -129 },
|
---|
176 | { "-254", 0, VINF_SUCCESS, -254 },
|
---|
177 | { "-255", 0, VINF_SUCCESS, -255 },
|
---|
178 | { "-256", 0, VINF_SUCCESS, -256 },
|
---|
179 | { "-257", 0, VINF_SUCCESS, -257 },
|
---|
180 | { "-511", 0, VINF_SUCCESS, -511 },
|
---|
181 | { "-512", 0, VINF_SUCCESS, -512 },
|
---|
182 | { "-513", 0, VINF_SUCCESS, -513 },
|
---|
183 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
184 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
185 | { "-1023", 0, VINF_SUCCESS, -1023},
|
---|
186 | { "-1023", 10, VINF_SUCCESS, -1023 },
|
---|
187 | { "-4564678", 0, VINF_SUCCESS, -4564678 },
|
---|
188 | { "-4564678", 10, VINF_SUCCESS, -4564678 },
|
---|
189 | { "4564678", 0, VINF_SUCCESS, 4564678 },
|
---|
190 | { "4564678", 10, VINF_SUCCESS, 4564678 },
|
---|
191 | { "-1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)((uint64_t)INT64_C(-1234567890123456789) & UINT32_MAX) },
|
---|
192 | { "-1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)((uint64_t)INT64_C(-1234567890123456789) & UINT32_MAX) },
|
---|
193 | { "1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)(INT64_C(1234567890123456789) & UINT32_MAX) },
|
---|
194 | { "1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)(INT64_C(1234567890123456789) & UINT32_MAX) },
|
---|
195 | { "0x", 0, VWRN_TRAILING_CHARS, 0 },
|
---|
196 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
197 | { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
|
---|
198 | { "0x1", 16, VINF_SUCCESS, 1 },
|
---|
199 | { "0x7fffffff", 0, VINF_SUCCESS, 0x7fffffff },
|
---|
200 | { "0x80000000", 0, VWRN_NUMBER_TOO_BIG, INT32_MIN },
|
---|
201 | { "0xffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
|
---|
202 | { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, (int32_t)0xffffffff },
|
---|
203 | { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x11111111 },
|
---|
204 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, (int32_t)0xffffffff },
|
---|
205 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
206 | { "0x1111111", 0, VINF_SUCCESS, 0x01111111 },
|
---|
207 | };
|
---|
208 | RUN_TESTS(aTstI32, int32_t, "%#d", RTStrToInt32Ex);
|
---|
209 |
|
---|
210 | static const struct TstU32 aTstU32[] =
|
---|
211 | {
|
---|
212 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
213 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
214 | /// @todo { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0 }, - no longer true. bad idea?
|
---|
215 | { "-1", 0, VWRN_NUMBER_TOO_BIG, ~0U },
|
---|
216 | { "0x", 0, VWRN_TRAILING_CHARS, 0 },
|
---|
217 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
218 | { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
|
---|
219 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
|
---|
220 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
221 | { "0x1111111", 0, VINF_SUCCESS, 0x1111111 },
|
---|
222 | };
|
---|
223 | RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Summary.
|
---|
227 | */
|
---|
228 | if (!cErrors)
|
---|
229 | RTPrintf("tstStrToNum: SUCCESS\n");
|
---|
230 | else
|
---|
231 | RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
|
---|
232 | return !!cErrors;
|
---|
233 | }
|
---|