VirtualBox

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

Last change on this file since 5781 was 5781, checked in by vboxsync, 17 years ago

Fixed some overflow detection issues.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.2 KB
Line 
1/* $Id: tstStrToNum.cpp 5781 2007-11-16 18:55:05Z 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
22struct TstI64
23{
24 const char *psz;
25 unsigned uBase;
26 int rc;
27 int64_t Result;
28};
29
30struct TstU64
31{
32 const char *psz;
33 unsigned uBase;
34 int rc;
35 uint64_t Result;
36};
37
38struct TstI32
39{
40 const char *psz;
41 unsigned uBase;
42 int rc;
43 int32_t Result;
44};
45
46struct 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, iTest) \
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/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
63 cErrors++; \
64 } \
65 else if (rc != Test.rc) \
66 { \
67 RTPrintf("failure: '%s' -> rc=%Vrc expected %Vrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
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, iTest); \
79 } \
80 } while (0)
81
82int 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, VWRN_TRAILING_CHARS, 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 { "4D9702C5CBD9B778", 16, VINF_SUCCESS, UINT64_C(0x4D9702C5CBD9B778) },
97 };
98 RUN_TESTS(aTstU64, uint64_t, "%#llx", RTStrToUInt64Ex);
99
100 static const struct TstI64 aTstI64[] =
101 {
102 { "0", 0, VINF_SUCCESS, 0 },
103 { "1", 0, VINF_SUCCESS, 1 },
104 { "-1", 0, VINF_SUCCESS, -1 },
105 { "-1", 10, VINF_SUCCESS, -1 },
106 { "-31", 0, VINF_SUCCESS, -31 },
107 { "-31", 10, VINF_SUCCESS, -31 },
108 { "-32", 0, VINF_SUCCESS, -32 },
109 { "-33", 0, VINF_SUCCESS, -33 },
110 { "-64", 0, VINF_SUCCESS, -64 },
111 { "-127", 0, VINF_SUCCESS, -127 },
112 { "-128", 0, VINF_SUCCESS, -128 },
113 { "-129", 0, VINF_SUCCESS, -129 },
114 { "-254", 0, VINF_SUCCESS, -254 },
115 { "-255", 0, VINF_SUCCESS, -255 },
116 { "-256", 0, VINF_SUCCESS, -256 },
117 { "-257", 0, VINF_SUCCESS, -257 },
118 { "-511", 0, VINF_SUCCESS, -511 },
119 { "-512", 0, VINF_SUCCESS, -512 },
120 { "-513", 0, VINF_SUCCESS, -513 },
121 { "-1023", 0, VINF_SUCCESS, -1023 },
122 { "-1023", 0, VINF_SUCCESS, -1023 },
123 { "-1023", 0, VINF_SUCCESS, -1023},
124 { "-1023", 10, VINF_SUCCESS, -1023 },
125 { "-4564678", 0, VINF_SUCCESS, -4564678 },
126 { "-4564678", 10, VINF_SUCCESS, -4564678 },
127 { "-1234567890123456789", 0, VINF_SUCCESS, -1234567890123456789LL },
128 { "-1234567890123456789", 10, VINF_SUCCESS, -1234567890123456789LL },
129 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
130 { "0x1", 0, VINF_SUCCESS, 1 },
131 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
132 { "0x1", 16, VINF_SUCCESS, 1 },
133 { "0x0fffffffffffffff", 0, VINF_SUCCESS, 0x0fffffffffffffffULL },
134 { "0x7fffffffffffffff", 0, VINF_SUCCESS, 0x7fffffffffffffffULL },
135 { "0xffffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
136 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x1111111111111111ULL },
137 { "0x02222222222222222222222",0, VWRN_NUMBER_TOO_BIG, 0x2222222222222222ULL },
138 { "0x03333333333333333333333",0, VWRN_NUMBER_TOO_BIG, 0x3333333333333333ULL },
139 { "0x04444444444444444444444",0, VWRN_NUMBER_TOO_BIG, 0x4444444444444444ULL },
140 { "0x07777777777777777777777",0, VWRN_NUMBER_TOO_BIG, 0x7777777777777777ULL },
141 { "0x07f7f7f7f7f7f7f7f7f7f7f",0, VWRN_NUMBER_TOO_BIG, 0x7f7f7f7f7f7f7f7fULL },
142 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffffffffffULL },
143 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
144 { "0x111111111", 0, VINF_SUCCESS, 0x111111111ULL },
145 };
146 RUN_TESTS(aTstI64, int64_t, "%#lld", RTStrToInt64Ex);
147
148
149
150 static const struct TstI32 aTstI32[] =
151 {
152 { "0", 0, VINF_SUCCESS, 0 },
153 { "1", 0, VINF_SUCCESS, 1 },
154 { "-1", 0, VINF_SUCCESS, -1 },
155 { "-1", 10, VINF_SUCCESS, -1 },
156 { "-31", 0, VINF_SUCCESS, -31 },
157 { "-31", 10, VINF_SUCCESS, -31 },
158 { "-32", 0, VINF_SUCCESS, -32 },
159 { "-33", 0, VINF_SUCCESS, -33 },
160 { "-64", 0, VINF_SUCCESS, -64 },
161 { "-127", 0, VINF_SUCCESS, -127 },
162 { "-128", 0, VINF_SUCCESS, -128 },
163 { "-129", 0, VINF_SUCCESS, -129 },
164 { "-254", 0, VINF_SUCCESS, -254 },
165 { "-255", 0, VINF_SUCCESS, -255 },
166 { "-256", 0, VINF_SUCCESS, -256 },
167 { "-257", 0, VINF_SUCCESS, -257 },
168 { "-511", 0, VINF_SUCCESS, -511 },
169 { "-512", 0, VINF_SUCCESS, -512 },
170 { "-513", 0, VINF_SUCCESS, -513 },
171 { "-1023", 0, VINF_SUCCESS, -1023 },
172 { "-1023", 0, VINF_SUCCESS, -1023 },
173 { "-1023", 0, VINF_SUCCESS, -1023},
174 { "-1023", 10, VINF_SUCCESS, -1023 },
175 { "-4564678", 0, VINF_SUCCESS, -4564678 },
176 { "-4564678", 10, VINF_SUCCESS, -4564678 },
177 { "4564678", 0, VINF_SUCCESS, 4564678 },
178 { "4564678", 10, VINF_SUCCESS, 4564678 },
179 { "-1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
180 { "-1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)-1234567890123456789LL },
181 { "1234567890123456789", 0, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
182 { "1234567890123456789", 10, VWRN_NUMBER_TOO_BIG, (int32_t)1234567890123456789LL },
183 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
184 { "0x1", 0, VINF_SUCCESS, 1 },
185 { "0x1", 10, VWRN_TRAILING_CHARS, 0 },
186 { "0x1", 16, VINF_SUCCESS, 1 },
187 { "0x7fffffff", 0, VINF_SUCCESS, 0x7fffffff },
188 { "0x80000000", 0, VWRN_NUMBER_TOO_BIG, INT32_MIN },
189 { "0xffffffff", 0, VWRN_NUMBER_TOO_BIG, -1 },
190 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
191 { "0x01111111111111111111111",0, VWRN_NUMBER_TOO_BIG, 0x11111111 },
192 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffff },
193 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
194 { "0x1111111", 0, VINF_SUCCESS, 0x01111111 },
195 };
196 RUN_TESTS(aTstI32, int32_t, "%#d", RTStrToInt32Ex);
197
198 static const struct TstU32 aTstU32[] =
199 {
200 { "0", 0, VINF_SUCCESS, 0 },
201 { "1", 0, VINF_SUCCESS, 1 },
202 { "-1", 0, VWRN_NEGATIVE_UNSIGNED, ~0 },
203 { "0x", 0, VWRN_TRAILING_CHARS, 0 },
204 { "0x1", 0, VINF_SUCCESS, 1 },
205 { "0x0fffffffffffffff", 0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
206 { "0x0ffffffffffffffffffffff",0, VWRN_NUMBER_TOO_BIG, 0xffffffffU },
207 { "asdfasdfasdf", 0, VERR_NO_DIGITS, 0 },
208 { "0x1111111", 0, VINF_SUCCESS, 0x1111111 },
209 };
210 RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
211
212 /*
213 * Summary.
214 */
215 if (!cErrors)
216 RTPrintf("tstStrToNum: SUCCESS\n");
217 else
218 RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
219 return !!cErrors;
220}
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