VirtualBox

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

Last change on this file since 27955 was 24781, checked in by vboxsync, 15 years ago

tstStrToNum: Added RTR3Init().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $Id: tstStrToNum.cpp 24781 2009-11-19 10:46:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String To Number Conversion.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#include <iprt/initterm.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 { \
76 RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
77 cErrors++; \
78 } \
79 else if (rc != Test.rc) \
80 { \
81 RTPrintf("failure: '%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
82 cErrors++; \
83 } \
84 } while (0)
85
86
87#define RUN_TESTS(aTests, Type, Fmt, Fun) \
88 do \
89 { \
90 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
91 { \
92 TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
93 } \
94 } while (0)
95
96int main()
97{
98 RTR3Init();
99
100 int cErrors = 0;
101 static const struct TstU64 aTstU64[] =
102 {
103 { "0", 0, VINF_SUCCESS, 0 },
104 { "1", 0, VINF_SUCCESS, 1 },
105 { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0ULL },
106 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
107 { "0x1", 0, VINF_SUCCESS, 1 },
108 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
109 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
110 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
111 { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
112 { "4D9702C5CBD9B778", 16, VINF_SUCCESS, UINT64_C(0x4D9702C5CBD9B778) },
113 };
114 RUN_TESTS(aTstU64, uint64_t, "%#llx", RTStrToUInt64Ex);
115
116 static const struct TstI64 aTstI64[] =
117 {
118 { "0", 0, VINF_SUCCESS, 0 },
119 { "1", 0, VINF_SUCCESS, 1 },
120 { "-1", 0, VINF_SUCCESS, -1 },
121 { "-1", 10, VINF_SUCCESS, -1 },
122 { "-31", 0, VINF_SUCCESS, -31 },
123 { "-31", 10, VINF_SUCCESS, -31 },
124 { "-32", 0, VINF_SUCCESS, -32 },
125 { "-33", 0, VINF_SUCCESS, -33 },
126 { "-64", 0, VINF_SUCCESS, -64 },
127 { "-127", 0, VINF_SUCCESS, -127 },
128 { "-128", 0, VINF_SUCCESS, -128 },
129 { "-129", 0, VINF_SUCCESS, -129 },
130 { "-254", 0, VINF_SUCCESS, -254 },
131 { "-255", 0, VINF_SUCCESS, -255 },
132 { "-256", 0, VINF_SUCCESS, -256 },
133 { "-257", 0, VINF_SUCCESS, -257 },
134 { "-511", 0, VINF_SUCCESS, -511 },
135 { "-512", 0, VINF_SUCCESS, -512 },
136 { "-513", 0, VINF_SUCCESS, -513 },
137 { "-1023", 0, VINF_SUCCESS, -1023 },
138 { "-1023", 0, VINF_SUCCESS, -1023 },
139 { "-1023", 0, VINF_SUCCESS, -1023},
140 { "-1023", 10, VINF_SUCCESS, -1023 },
141 { "-4564678", 0, VINF_SUCCESS, -4564678 },
142 { "-4564678", 10, VINF_SUCCESS, -4564678 },
143 { "-1234567890123456789", 0, VINF_SUCCESS, -1234567890123456789LL },
144 { "-1234567890123456789", 10, VINF_SUCCESS, -1234567890123456789LL },
145 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
146 { "0x1", 0, VINF_SUCCESS, 1 },
147 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
148 { "0x1", 16, VINF_SUCCESS, 1 },
149 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
150 { "0x7fffffffffffffff", 0, VINF_SUCCESS, 0x7fffffffffffffffULL },
151 { "0xffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
152 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x1111111111111111ULL },
153 { "0x02222222222222222222222",0, VWRN_NUMBER_TOO_BIG, 0x2222222222222222ULL },
154 { "0x03333333333333333333333",0, VWRN_NUMBER_TOO_BIG, 0x3333333333333333ULL },
155 { "0x04444444444444444444444",0, VWRN_NUMBER_TOO_BIG, 0x4444444444444444ULL },
156 { "0x07777777777777777777777",0, VWRN_NUMBER_TOO_BIG, 0x7777777777777777ULL },
157 { "0x07f7f7f7f7f7f7f7f7f7f7f",0, VWRN_NUMBER_TOO_BIG, 0x7f7f7f7f7f7f7f7fULL },
158 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
159 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
160 { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
161 };
162 RUN_TESTS(aTstI64, int64_t, "%#lld", RTStrToInt64Ex);
163
164
165
166 static const struct TstI32 aTstI32[] =
167 {
168 { "0", 0, VINF_SUCCESS, 0 },
169 { "1", 0, VINF_SUCCESS, 1 },
170 { "-1", 0, VINF_SUCCESS, -1 },
171 { "-1", 10, VINF_SUCCESS, -1 },
172 { "-31", 0, VINF_SUCCESS, -31 },
173 { "-31", 10, VINF_SUCCESS, -31 },
174 { "-32", 0, VINF_SUCCESS, -32 },
175 { "-33", 0, VINF_SUCCESS, -33 },
176 { "-64", 0, VINF_SUCCESS, -64 },
177 { "-127", 0, VINF_SUCCESS, -127 },
178 { "-128", 0, VINF_SUCCESS, -128 },
179 { "-129", 0, VINF_SUCCESS, -129 },
180 { "-254", 0, VINF_SUCCESS, -254 },
181 { "-255", 0, VINF_SUCCESS, -255 },
182 { "-256", 0, VINF_SUCCESS, -256 },
183 { "-257", 0, VINF_SUCCESS, -257 },
184 { "-511", 0, VINF_SUCCESS, -511 },
185 { "-512", 0, VINF_SUCCESS, -512 },
186 { "-513", 0, VINF_SUCCESS, -513 },
187 { "-1023", 0, VINF_SUCCESS, -1023 },
188 { "-1023", 0, VINF_SUCCESS, -1023 },
189 { "-1023", 0, VINF_SUCCESS, -1023},
190 { "-1023", 10, VINF_SUCCESS, -1023 },
191 { "-4564678", 0, VINF_SUCCESS, -4564678 },
192 { "-4564678", 10, VINF_SUCCESS, -4564678 },
193 { "4564678", 0, VINF_SUCCESS, 4564678 },
194 { "4564678", 10, VINF_SUCCESS, 4564678 },
195 { "-1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
196 { "-1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
197 { "1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
198 { "1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
199 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
200 { "0x1", 0, VINF_SUCCESS, 1 },
201 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
202 { "0x1", 16, VINF_SUCCESS, 1 },
203 { "0x7fffffff", 0, VINF_SUCCESS, 0x7fffffff },
204 { "0x80000000", 0, VWRN_NUMBER_TOO_BIG, INT32_MIN },
205 { "0xffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
206 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
207 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x11111111 },
208 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
209 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
210 { "0x1111111", 0, VINF_SUCCESS, 0x01111111 },
211 };
212 RUN_TESTS(aTstI32, int32_t, "%#d", RTStrToInt32Ex);
213
214 static const struct TstU32 aTstU32[] =
215 {
216 { "0", 0, VINF_SUCCESS, 0 },
217 { "1", 0, VINF_SUCCESS, 1 },
218 /// @todo { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0 }, - no longer true. bad idea?
219 { "-1", 0, VWRN_NUMBER_TOO_BIG, ~0 },
220 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
221 { "0x1", 0, VINF_SUCCESS, 1 },
222 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
223 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
224 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
225 { "0x1111111", 0, VINF_SUCCESS, 0x1111111 },
226 };
227 RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
228
229 /*
230 * Summary.
231 */
232 if (!cErrors)
233 RTPrintf("tstStrToNum: SUCCESS\n");
234 else
235 RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
236 return !!cErrors;
237}
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