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