1 | /* $Id: tstStrToNum.cpp 4071 2007-08-07 17:07:59Z 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <iprt/string.h>
|
---|
19 | #include <iprt/stream.h>
|
---|
20 | #include <iprt/err.h>
|
---|
21 |
|
---|
22 | struct TstI64
|
---|
23 | {
|
---|
24 | const char *psz;
|
---|
25 | unsigned uBase;
|
---|
26 | int rc;
|
---|
27 | int64_t Result;
|
---|
28 | };
|
---|
29 |
|
---|
30 | struct TstU64
|
---|
31 | {
|
---|
32 | const char *psz;
|
---|
33 | unsigned uBase;
|
---|
34 | int rc;
|
---|
35 | uint64_t Result;
|
---|
36 | };
|
---|
37 |
|
---|
38 | struct TstI32
|
---|
39 | {
|
---|
40 | const char *psz;
|
---|
41 | unsigned uBase;
|
---|
42 | int rc;
|
---|
43 | int32_t Result;
|
---|
44 | };
|
---|
45 |
|
---|
46 | struct TstU32
|
---|
47 | {
|
---|
48 | const char *psz;
|
---|
49 | unsigned uBase;
|
---|
50 | int rc;
|
---|
51 | uint32_t Result;
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | #define TEST(Test, Type, Fmt, Fun) \
|
---|
56 | do \
|
---|
57 | { \
|
---|
58 | Type Result; \
|
---|
59 | int rc = Fun(Test.psz, NULL, Test.uBase, &Result); \
|
---|
60 | if (Result != Test.Result) \
|
---|
61 | { \
|
---|
62 | RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s)\n", Test.psz, Result, Test.Result, #Fun); \
|
---|
63 | cErrors++; \
|
---|
64 | } \
|
---|
65 | else if (rc != Test.rc) \
|
---|
66 | { \
|
---|
67 | RTPrintf("failure: '%s' -> rc=%Vrc expected %Vrc. (%s)\n", Test.psz, rc, Test.rc, #Fun); \
|
---|
68 | cErrors++; \
|
---|
69 | } \
|
---|
70 | } while (0)
|
---|
71 |
|
---|
72 |
|
---|
73 | #define RUN_TESTS(aTests, Type, Fmt, Fun) \
|
---|
74 | do \
|
---|
75 | { \
|
---|
76 | for (unsigned iTest = 0; iTest < ELEMENTS(aTests); iTest++) \
|
---|
77 | { \
|
---|
78 | TEST(aTests[iTest], Type, Fmt, Fun); \
|
---|
79 | } \
|
---|
80 | } while (0)
|
---|
81 |
|
---|
82 | int main()
|
---|
83 | {
|
---|
84 | int cErrors = 0;
|
---|
85 | static const struct TstU64 aTstU64[] =
|
---|
86 | {
|
---|
87 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
88 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
89 | { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0ULL },
|
---|
90 | { "0x", 0, VINF_SUCCESS, 0 },
|
---|
91 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
92 | { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
|
---|
93 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
|
---|
94 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
95 | { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
|
---|
96 | };
|
---|
97 | RUN_TESTS(aTstU64, uint64_t, "%#llx", RTStrToUInt64Ex);
|
---|
98 |
|
---|
99 | static const struct TstI64 aTstI64[] =
|
---|
100 | {
|
---|
101 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
102 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
103 | { "-1", 0, VINF_SUCCESS, -1 },
|
---|
104 | { "-1", 10, VINF_SUCCESS, -1 },
|
---|
105 | { "-31", 0, VINF_SUCCESS, -31 },
|
---|
106 | { "-31", 10, VINF_SUCCESS, -31 },
|
---|
107 | { "-32", 0, VINF_SUCCESS, -32 },
|
---|
108 | { "-33", 0, VINF_SUCCESS, -33 },
|
---|
109 | { "-64", 0, VINF_SUCCESS, -64 },
|
---|
110 | { "-127", 0, VINF_SUCCESS, -127 },
|
---|
111 | { "-128", 0, VINF_SUCCESS, -128 },
|
---|
112 | { "-129", 0, VINF_SUCCESS, -129 },
|
---|
113 | { "-254", 0, VINF_SUCCESS, -254 },
|
---|
114 | { "-255", 0, VINF_SUCCESS, -255 },
|
---|
115 | { "-256", 0, VINF_SUCCESS, -256 },
|
---|
116 | { "-257", 0, VINF_SUCCESS, -257 },
|
---|
117 | { "-511", 0, VINF_SUCCESS, -511 },
|
---|
118 | { "-512", 0, VINF_SUCCESS, -512 },
|
---|
119 | { "-513", 0, VINF_SUCCESS, -513 },
|
---|
120 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
121 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
122 | { "-1023", 0, VINF_SUCCESS, -1023},
|
---|
123 | { "-1023", 10, VINF_SUCCESS, -1023 },
|
---|
124 | { "-4564678", 0, VINF_SUCCESS, -4564678 },
|
---|
125 | { "-4564678", 10, VINF_SUCCESS, -4564678 },
|
---|
126 | { "-1234567890123456789", 0, VINF_SUCCESS, -1234567890123456789LL },
|
---|
127 | { "-1234567890123456789", 10, VINF_SUCCESS, -1234567890123456789LL },
|
---|
128 | { "0x", 0, VINF_SUCCESS, 0 },
|
---|
129 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
130 | { "0x1", 10, VINF_SUCCESS, 0 },
|
---|
131 | { "0x1", 16, VINF_SUCCESS, 1 },
|
---|
132 | { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
|
---|
133 | //{ "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x1111111111111111ULL },
|
---|
134 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
|
---|
135 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
136 | { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
|
---|
137 | };
|
---|
138 | RUN_TESTS(aTstI64, int64_t, "%#lld", RTStrToInt64Ex);
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | static const struct TstI32 aTstI32[] =
|
---|
143 | {
|
---|
144 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
145 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
146 | { "-1", 0, VINF_SUCCESS, -1 },
|
---|
147 | { "-1", 10, VINF_SUCCESS, -1 },
|
---|
148 | { "-31", 0, VINF_SUCCESS, -31 },
|
---|
149 | { "-31", 10, VINF_SUCCESS, -31 },
|
---|
150 | { "-32", 0, VINF_SUCCESS, -32 },
|
---|
151 | { "-33", 0, VINF_SUCCESS, -33 },
|
---|
152 | { "-64", 0, VINF_SUCCESS, -64 },
|
---|
153 | { "-127", 0, VINF_SUCCESS, -127 },
|
---|
154 | { "-128", 0, VINF_SUCCESS, -128 },
|
---|
155 | { "-129", 0, VINF_SUCCESS, -129 },
|
---|
156 | { "-254", 0, VINF_SUCCESS, -254 },
|
---|
157 | { "-255", 0, VINF_SUCCESS, -255 },
|
---|
158 | { "-256", 0, VINF_SUCCESS, -256 },
|
---|
159 | { "-257", 0, VINF_SUCCESS, -257 },
|
---|
160 | { "-511", 0, VINF_SUCCESS, -511 },
|
---|
161 | { "-512", 0, VINF_SUCCESS, -512 },
|
---|
162 | { "-513", 0, VINF_SUCCESS, -513 },
|
---|
163 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
164 | { "-1023", 0, VINF_SUCCESS, -1023 },
|
---|
165 | { "-1023", 0, VINF_SUCCESS, -1023},
|
---|
166 | { "-1023", 10, VINF_SUCCESS, -1023 },
|
---|
167 | { "-4564678", 0, VINF_SUCCESS, -4564678 },
|
---|
168 | { "-4564678", 10, VINF_SUCCESS, -4564678 },
|
---|
169 | { "4564678", 0, VINF_SUCCESS, 4564678 },
|
---|
170 | { "4564678", 10, VINF_SUCCESS, 4564678 },
|
---|
171 | { "-1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
|
---|
172 | { "-1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
|
---|
173 | { "1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
|
---|
174 | { "1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
|
---|
175 | { "0x", 0, VINF_SUCCESS, 0 },
|
---|
176 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
177 | { "0x1", 10, VINF_SUCCESS, 0 },
|
---|
178 | { "0x1", 16, VINF_SUCCESS, 1 },
|
---|
179 | { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
|
---|
180 | { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x11111111 },
|
---|
181 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
|
---|
182 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
183 | { "0x1111111", 0, VINF_SUCCESS, 0x01111111 },
|
---|
184 | };
|
---|
185 | RUN_TESTS(aTstI32, int32_t, "%#d", RTStrToInt32Ex);
|
---|
186 |
|
---|
187 | static const struct TstU32 aTstU32[] =
|
---|
188 | {
|
---|
189 | { "0", 0, VINF_SUCCESS, 0 },
|
---|
190 | { "1", 0, VINF_SUCCESS, 1 },
|
---|
191 | { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0 },
|
---|
192 | { "0x", 0, VINF_SUCCESS, 0 },
|
---|
193 | { "0x1", 0, VINF_SUCCESS, 1 },
|
---|
194 | { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
|
---|
195 | { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
|
---|
196 | { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
|
---|
197 | { "0x1111111", 0, VINF_SUCCESS, 0x1111111 },
|
---|
198 | };
|
---|
199 | RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Summary.
|
---|
203 | */
|
---|
204 | if (!cErrors)
|
---|
205 | RTPrintf("tstStrToNum: SUCCESS\n");
|
---|
206 | else
|
---|
207 | RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
|
---|
208 | return !!cErrors;
|
---|
209 | }
|
---|