1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include <stdlib.h>
29
30 #include "double-conversion/bignum.h"
31 #include "cctest.h"
32 #include "double-conversion/diy-fp.h"
33 #include "double-conversion/ieee.h"
34 #include "double-conversion/strtod.h"
35 #include "double-conversion/utils.h"
36
37 using namespace double_conversion;
38
StringToVector(const char * str)39 static Vector<const char> StringToVector(const char* str) {
40 return Vector<const char>(str, strlen(str));
41 }
42
43
StrtodChar(const char * str,int exponent)44 static double StrtodChar(const char* str, int exponent) {
45 return Strtod(StringToVector(str), exponent);
46 }
47
48
StrtodTrimmedChar(const char * str,int exponent)49 static double StrtodTrimmedChar(const char* str, int exponent) {
50 return StrtodTrimmed(StringToVector(str), exponent);
51 }
52
53
StrtofChar(const char * str,int exponent)54 static float StrtofChar(const char* str, int exponent) {
55 return Strtof(StringToVector(str), exponent);
56 }
57
StrtofTrimmedChar(const char * str,int exponent)58 static float StrtofTrimmedChar(const char* str, int exponent) {
59 return StrtofTrimmed(StringToVector(str), exponent);
60 }
61
62
TEST(Strtod)63 TEST(Strtod) {
64 Vector<const char> vector;
65
66 vector = StringToVector("0");
67 CHECK_EQ(0.0, Strtod(vector, 1));
68 CHECK_EQ(0.0, Strtod(vector, 2));
69 CHECK_EQ(0.0, Strtod(vector, -2));
70 CHECK_EQ(0.0, Strtod(vector, -999));
71 CHECK_EQ(0.0, Strtod(vector, +999));
72
73 vector = StringToVector("1");
74 CHECK_EQ(1.0, Strtod(vector, 0));
75 CHECK_EQ(10.0, Strtod(vector, 1));
76 CHECK_EQ(100.0, Strtod(vector, 2));
77 CHECK_EQ(1e20, Strtod(vector, 20));
78 CHECK_EQ(1e22, Strtod(vector, 22));
79 CHECK_EQ(1e23, Strtod(vector, 23));
80 CHECK_EQ(1e35, Strtod(vector, 35));
81 CHECK_EQ(1e36, Strtod(vector, 36));
82 CHECK_EQ(1e37, Strtod(vector, 37));
83 CHECK_EQ(1e-1, Strtod(vector, -1));
84 CHECK_EQ(1e-2, Strtod(vector, -2));
85 CHECK_EQ(1e-5, Strtod(vector, -5));
86 CHECK_EQ(1e-20, Strtod(vector, -20));
87 CHECK_EQ(1e-22, Strtod(vector, -22));
88 CHECK_EQ(1e-23, Strtod(vector, -23));
89 CHECK_EQ(1e-25, Strtod(vector, -25));
90 CHECK_EQ(1e-39, Strtod(vector, -39));
91
92 vector = StringToVector("2");
93 CHECK_EQ(2.0, Strtod(vector, 0));
94 CHECK_EQ(20.0, Strtod(vector, 1));
95 CHECK_EQ(200.0, Strtod(vector, 2));
96 CHECK_EQ(2e20, Strtod(vector, 20));
97 CHECK_EQ(2e22, Strtod(vector, 22));
98 CHECK_EQ(2e23, Strtod(vector, 23));
99 CHECK_EQ(2e35, Strtod(vector, 35));
100 CHECK_EQ(2e36, Strtod(vector, 36));
101 CHECK_EQ(2e37, Strtod(vector, 37));
102 CHECK_EQ(2e-1, Strtod(vector, -1));
103 CHECK_EQ(2e-2, Strtod(vector, -2));
104 CHECK_EQ(2e-5, Strtod(vector, -5));
105 CHECK_EQ(2e-20, Strtod(vector, -20));
106 CHECK_EQ(2e-22, Strtod(vector, -22));
107 CHECK_EQ(2e-23, Strtod(vector, -23));
108 CHECK_EQ(2e-25, Strtod(vector, -25));
109 CHECK_EQ(2e-39, Strtod(vector, -39));
110
111 vector = StringToVector("9");
112 CHECK_EQ(9.0, Strtod(vector, 0));
113 CHECK_EQ(90.0, Strtod(vector, 1));
114 CHECK_EQ(900.0, Strtod(vector, 2));
115 CHECK_EQ(9e20, Strtod(vector, 20));
116 CHECK_EQ(9e22, Strtod(vector, 22));
117 CHECK_EQ(9e23, Strtod(vector, 23));
118 CHECK_EQ(9e35, Strtod(vector, 35));
119 CHECK_EQ(9e36, Strtod(vector, 36));
120 CHECK_EQ(9e37, Strtod(vector, 37));
121 CHECK_EQ(9e-1, Strtod(vector, -1));
122 CHECK_EQ(9e-2, Strtod(vector, -2));
123 CHECK_EQ(9e-5, Strtod(vector, -5));
124 CHECK_EQ(9e-20, Strtod(vector, -20));
125 CHECK_EQ(9e-22, Strtod(vector, -22));
126 CHECK_EQ(9e-23, Strtod(vector, -23));
127 CHECK_EQ(9e-25, Strtod(vector, -25));
128 CHECK_EQ(9e-39, Strtod(vector, -39));
129
130 vector = StringToVector("12345");
131 CHECK_EQ(12345.0, Strtod(vector, 0));
132 CHECK_EQ(123450.0, Strtod(vector, 1));
133 CHECK_EQ(1234500.0, Strtod(vector, 2));
134 CHECK_EQ(12345e20, Strtod(vector, 20));
135 CHECK_EQ(12345e22, Strtod(vector, 22));
136 CHECK_EQ(12345e23, Strtod(vector, 23));
137 CHECK_EQ(12345e30, Strtod(vector, 30));
138 CHECK_EQ(12345e31, Strtod(vector, 31));
139 CHECK_EQ(12345e32, Strtod(vector, 32));
140 CHECK_EQ(12345e35, Strtod(vector, 35));
141 CHECK_EQ(12345e36, Strtod(vector, 36));
142 CHECK_EQ(12345e37, Strtod(vector, 37));
143 CHECK_EQ(12345e-1, Strtod(vector, -1));
144 CHECK_EQ(12345e-2, Strtod(vector, -2));
145 CHECK_EQ(12345e-5, Strtod(vector, -5));
146 CHECK_EQ(12345e-20, Strtod(vector, -20));
147 CHECK_EQ(12345e-22, Strtod(vector, -22));
148 CHECK_EQ(12345e-23, Strtod(vector, -23));
149 CHECK_EQ(12345e-25, Strtod(vector, -25));
150 CHECK_EQ(12345e-39, Strtod(vector, -39));
151
152 vector = StringToVector("12345678901234");
153 CHECK_EQ(12345678901234.0, Strtod(vector, 0));
154 CHECK_EQ(123456789012340.0, Strtod(vector, 1));
155 CHECK_EQ(1234567890123400.0, Strtod(vector, 2));
156 CHECK_EQ(12345678901234e20, Strtod(vector, 20));
157 CHECK_EQ(12345678901234e22, Strtod(vector, 22));
158 CHECK_EQ(12345678901234e23, Strtod(vector, 23));
159 CHECK_EQ(12345678901234e30, Strtod(vector, 30));
160 CHECK_EQ(12345678901234e31, Strtod(vector, 31));
161 CHECK_EQ(12345678901234e32, Strtod(vector, 32));
162 CHECK_EQ(12345678901234e35, Strtod(vector, 35));
163 CHECK_EQ(12345678901234e36, Strtod(vector, 36));
164 CHECK_EQ(12345678901234e37, Strtod(vector, 37));
165 CHECK_EQ(12345678901234e-1, Strtod(vector, -1));
166 CHECK_EQ(12345678901234e-2, Strtod(vector, -2));
167 CHECK_EQ(12345678901234e-5, Strtod(vector, -5));
168 CHECK_EQ(12345678901234e-20, Strtod(vector, -20));
169 CHECK_EQ(12345678901234e-22, Strtod(vector, -22));
170 CHECK_EQ(12345678901234e-23, Strtod(vector, -23));
171 CHECK_EQ(12345678901234e-25, Strtod(vector, -25));
172 CHECK_EQ(12345678901234e-39, Strtod(vector, -39));
173
174 vector = StringToVector("123456789012345");
175 CHECK_EQ(123456789012345.0, Strtod(vector, 0));
176 CHECK_EQ(1234567890123450.0, Strtod(vector, 1));
177 CHECK_EQ(12345678901234500.0, Strtod(vector, 2));
178 CHECK_EQ(123456789012345e20, Strtod(vector, 20));
179 CHECK_EQ(123456789012345e22, Strtod(vector, 22));
180 CHECK_EQ(123456789012345e23, Strtod(vector, 23));
181 CHECK_EQ(123456789012345e35, Strtod(vector, 35));
182 CHECK_EQ(123456789012345e36, Strtod(vector, 36));
183 CHECK_EQ(123456789012345e37, Strtod(vector, 37));
184 CHECK_EQ(123456789012345e39, Strtod(vector, 39));
185 CHECK_EQ(123456789012345e-1, Strtod(vector, -1));
186 CHECK_EQ(123456789012345e-2, Strtod(vector, -2));
187 CHECK_EQ(123456789012345e-5, Strtod(vector, -5));
188 CHECK_EQ(123456789012345e-20, Strtod(vector, -20));
189 CHECK_EQ(123456789012345e-22, Strtod(vector, -22));
190 CHECK_EQ(123456789012345e-23, Strtod(vector, -23));
191 CHECK_EQ(123456789012345e-25, Strtod(vector, -25));
192 CHECK_EQ(123456789012345e-39, Strtod(vector, -39));
193
194 CHECK_EQ(0.0, StrtodChar("0", 12345));
195 CHECK_EQ(0.0, StrtodChar("", 1324));
196 CHECK_EQ(0.0, StrtodChar("000000000", 123));
197 CHECK_EQ(0.0, StrtodChar("2", -324));
198 CHECK_EQ(4e-324, StrtodChar("3", -324));
199 // It would be more readable to put non-zero literals on the left side (i.e.
200 // CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
201 // they are truncated to zero.
202 CHECK_EQ(0.0, StrtodChar("1", -325));
203 CHECK_EQ(0.0, StrtodChar("1", -325));
204 CHECK_EQ(0.0, StrtodChar("20000", -328));
205 CHECK_EQ(40000e-328, StrtodChar("30000", -328));
206 CHECK_EQ(0.0, StrtodChar("10000", -329));
207 CHECK_EQ(0.0, StrtodChar("90000", -329));
208 CHECK_EQ(0.0, StrtodChar("000000001", -325));
209 CHECK_EQ(0.0, StrtodChar("000000001", -325));
210 CHECK_EQ(0.0, StrtodChar("0000000020000", -328));
211 CHECK_EQ(40000e-328, StrtodChar("00000030000", -328));
212 CHECK_EQ(0.0, StrtodChar("0000000010000", -329));
213 CHECK_EQ(0.0, StrtodChar("0000000090000", -329));
214
215 // It would be more readable to put the literals (and not Double::Infinity())
216 // on the left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
217 // complains that the floating constant exceeds range of 'double'.
218 CHECK_EQ(Double::Infinity(), StrtodChar("1", 309));
219 CHECK_EQ(1e308, StrtodChar("1", 308));
220 CHECK_EQ(1234e305, StrtodChar("1234", 305));
221 CHECK_EQ(1234e304, StrtodChar("1234", 304));
222 CHECK_EQ(Double::Infinity(), StrtodChar("18", 307));
223 CHECK_EQ(17e307, StrtodChar("17", 307));
224 CHECK_EQ(Double::Infinity(), StrtodChar("0000001", 309));
225 CHECK_EQ(1e308, StrtodChar("00000001", 308));
226 CHECK_EQ(1234e305, StrtodChar("00000001234", 305));
227 CHECK_EQ(1234e304, StrtodChar("000000001234", 304));
228 CHECK_EQ(Double::Infinity(), StrtodChar("0000000018", 307));
229 CHECK_EQ(17e307, StrtodChar("0000000017", 307));
230 CHECK_EQ(Double::Infinity(), StrtodChar("1000000", 303));
231 CHECK_EQ(1e308, StrtodChar("100000", 303));
232 CHECK_EQ(1234e305, StrtodChar("123400000", 300));
233 CHECK_EQ(1234e304, StrtodChar("123400000", 299));
234 CHECK_EQ(Double::Infinity(), StrtodChar("180000000", 300));
235 CHECK_EQ(17e307, StrtodChar("170000000", 300));
236 CHECK_EQ(Double::Infinity(), StrtodChar("00000001000000", 303));
237 CHECK_EQ(1e308, StrtodChar("000000000000100000", 303));
238 CHECK_EQ(1234e305, StrtodChar("00000000123400000", 300));
239 CHECK_EQ(1234e304, StrtodChar("0000000123400000", 299));
240 CHECK_EQ(Double::Infinity(), StrtodChar("00000000180000000", 300));
241 CHECK_EQ(17e307, StrtodChar("00000000170000000", 300));
242 CHECK_EQ(1.7976931348623157E+308, StrtodChar("17976931348623157", 292));
243 CHECK_EQ(1.7976931348623158E+308, StrtodChar("17976931348623158", 292));
244 CHECK_EQ(Double::Infinity(), StrtodChar("17976931348623159", 292));
245
246 // The following number is the result of 89255.0/1e-22. Both floating-point
247 // numbers can be accurately represented with doubles. However on Linux,x86
248 // the floating-point stack is set to 80bits and the double-rounding
249 // introduces an error.
250 CHECK_EQ(89255e-22, StrtodChar("89255", -22));
251
252 // Some random values.
253 CHECK_EQ(358416272e-33, StrtodChar("358416272", -33));
254 CHECK_EQ(104110013277974872254e-225,
255 StrtodChar("104110013277974872254", -225));
256
257 CHECK_EQ(123456789e108, StrtodChar("123456789", 108));
258 CHECK_EQ(123456789e109, StrtodChar("123456789", 109));
259 CHECK_EQ(123456789e110, StrtodChar("123456789", 110));
260 CHECK_EQ(123456789e111, StrtodChar("123456789", 111));
261 CHECK_EQ(123456789e112, StrtodChar("123456789", 112));
262 CHECK_EQ(123456789e113, StrtodChar("123456789", 113));
263 CHECK_EQ(123456789e114, StrtodChar("123456789", 114));
264 CHECK_EQ(123456789e115, StrtodChar("123456789", 115));
265
266 CHECK_EQ(1234567890123456789012345e108,
267 StrtodChar("1234567890123456789012345", 108));
268 CHECK_EQ(1234567890123456789012345e109,
269 StrtodChar("1234567890123456789012345", 109));
270 CHECK_EQ(1234567890123456789012345e110,
271 StrtodChar("1234567890123456789012345", 110));
272 CHECK_EQ(1234567890123456789012345e111,
273 StrtodChar("1234567890123456789012345", 111));
274 CHECK_EQ(1234567890123456789012345e112,
275 StrtodChar("1234567890123456789012345", 112));
276 CHECK_EQ(1234567890123456789012345e113,
277 StrtodChar("1234567890123456789012345", 113));
278 CHECK_EQ(1234567890123456789012345e114,
279 StrtodChar("1234567890123456789012345", 114));
280 CHECK_EQ(1234567890123456789012345e115,
281 StrtodChar("1234567890123456789012345", 115));
282
283 CHECK_EQ(1234567890123456789052345e108,
284 StrtodChar("1234567890123456789052345", 108));
285 CHECK_EQ(1234567890123456789052345e109,
286 StrtodChar("1234567890123456789052345", 109));
287 CHECK_EQ(1234567890123456789052345e110,
288 StrtodChar("1234567890123456789052345", 110));
289 CHECK_EQ(1234567890123456789052345e111,
290 StrtodChar("1234567890123456789052345", 111));
291 CHECK_EQ(1234567890123456789052345e112,
292 StrtodChar("1234567890123456789052345", 112));
293 CHECK_EQ(1234567890123456789052345e113,
294 StrtodChar("1234567890123456789052345", 113));
295 CHECK_EQ(1234567890123456789052345e114,
296 StrtodChar("1234567890123456789052345", 114));
297 CHECK_EQ(1234567890123456789052345e115,
298 StrtodChar("1234567890123456789052345", 115));
299
300 CHECK_EQ(5.445618932859895e-255,
301 StrtodChar("5445618932859895362967233318697132813618813095743952975"
302 "4392982234069699615600475529427176366709107287468930197"
303 "8628345413991790019316974825934906752493984055268219809"
304 "5012176093045431437495773903922425632551857520884625114"
305 "6241265881735209066709685420744388526014389929047617597"
306 "0302268848374508109029268898695825171158085457567481507"
307 "4162979705098246243690189880319928315307816832576838178"
308 "2563074014542859888710209237525873301724479666744537857"
309 "9026553346649664045621387124193095870305991178772256504"
310 "4368663670643970181259143319016472430928902201239474588"
311 "1392338901353291306607057623202353588698746085415097902"
312 "6640064319118728664842287477491068264828851624402189317"
313 "2769161449825765517353755844373640588822904791244190695"
314 "2998382932630754670573838138825217065450843010498555058"
315 "88186560731", -1035));
316
317 // Boundary cases. Boundaries themselves should round to even.
318 //
319 // 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928
320 // next: 72057594037927936
321 // boundary: 72057594037927932 should round up.
322 CHECK_EQ(72057594037927928.0, StrtodChar("72057594037927928", 0));
323 CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927936", 0));
324 CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927932", 0));
325 CHECK_EQ(72057594037927928.0, StrtodChar("7205759403792793199999", -5));
326 CHECK_EQ(72057594037927936.0, StrtodChar("7205759403792793200001", -5));
327
328 // 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784
329 // next: 9223372036854775808
330 // boundary: 9223372036854775296 should round up.
331 CHECK_EQ(9223372036854774784.0, StrtodChar("9223372036854774784", 0));
332 CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775808", 0));
333 CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775296", 0));
334 CHECK_EQ(9223372036854774784.0, StrtodChar("922337203685477529599999", -5));
335 CHECK_EQ(9223372036854775808.0, StrtodChar("922337203685477529600001", -5));
336
337 // 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384
338 // next: 10141204801825835211973625643008
339 // boundary: 10141204801825834649023672221696 should round up.
340 CHECK_EQ(10141204801825834086073718800384.0,
341 StrtodChar("10141204801825834086073718800384", 0));
342 CHECK_EQ(10141204801825835211973625643008.0,
343 StrtodChar("10141204801825835211973625643008", 0));
344 CHECK_EQ(10141204801825835211973625643008.0,
345 StrtodChar("10141204801825834649023672221696", 0));
346 CHECK_EQ(10141204801825834086073718800384.0,
347 StrtodChar("1014120480182583464902367222169599999", -5));
348 CHECK_EQ(10141204801825835211973625643008.0,
349 StrtodChar("1014120480182583464902367222169600001", -5));
350
351 // 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808
352 // next: 5708990770823839524233143877797980545530986496
353 // boundary: 5708990770823839207320493820740630171355185152
354 // The boundary should round up.
355 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
356 StrtodChar("5708990770823838890407843763683279797179383808", 0));
357 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
358 StrtodChar("5708990770823839524233143877797980545530986496", 0));
359 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
360 StrtodChar("5708990770823839207320493820740630171355185152", 0));
361 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
362 StrtodChar("5708990770823839207320493820740630171355185151999", -3));
363 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
364 StrtodChar("5708990770823839207320493820740630171355185152001", -3));
365
366 // The following test-cases got some public attention in early 2011 when they
367 // sent Java and PHP into an infinite loop.
368 CHECK_EQ(2.225073858507201e-308, StrtodChar("22250738585072011", -324));
369 CHECK_EQ(2.22507385850720138309e-308,
370 StrtodChar("22250738585072011360574097967091319759348195463516456480"
371 "23426109724822222021076945516529523908135087914149158913"
372 "03962110687008643869459464552765720740782062174337998814"
373 "10632673292535522868813721490129811224514518898490572223"
374 "07285255133155755015914397476397983411801999323962548289"
375 "01710708185069063066665599493827577257201576306269066333"
376 "26475653000092458883164330377797918696120494973903778297"
377 "04905051080609940730262937128958950003583799967207254304"
378 "36028407889577179615094551674824347103070260914462157228"
379 "98802581825451803257070188608721131280795122334262883686"
380 "22321503775666622503982534335974568884423900265498198385"
381 "48794829220689472168983109969836584681402285424333066033"
382 "98508864458040010349339704275671864433837704860378616227"
383 "71738545623065874679014086723327636718751", -1076));
384 }
385
386
TEST(StrtodTrimmed)387 TEST(StrtodTrimmed) {
388 Vector<const char> vector;
389
390 vector = StringToVector("1");
391 CHECK_EQ(1.0, StrtodTrimmed(vector, 0));
392 CHECK_EQ(10.0, StrtodTrimmed(vector, 1));
393 CHECK_EQ(100.0, StrtodTrimmed(vector, 2));
394 CHECK_EQ(1e20, StrtodTrimmed(vector, 20));
395 CHECK_EQ(1e22, StrtodTrimmed(vector, 22));
396 CHECK_EQ(1e23, StrtodTrimmed(vector, 23));
397 CHECK_EQ(1e35, StrtodTrimmed(vector, 35));
398 CHECK_EQ(1e36, StrtodTrimmed(vector, 36));
399 CHECK_EQ(1e37, StrtodTrimmed(vector, 37));
400 CHECK_EQ(1e-1, StrtodTrimmed(vector, -1));
401 CHECK_EQ(1e-2, StrtodTrimmed(vector, -2));
402 CHECK_EQ(1e-5, StrtodTrimmed(vector, -5));
403 CHECK_EQ(1e-20, StrtodTrimmed(vector, -20));
404 CHECK_EQ(1e-22, StrtodTrimmed(vector, -22));
405 CHECK_EQ(1e-23, StrtodTrimmed(vector, -23));
406 CHECK_EQ(1e-25, StrtodTrimmed(vector, -25));
407 CHECK_EQ(1e-39, StrtodTrimmed(vector, -39));
408
409 vector = StringToVector("2");
410 CHECK_EQ(2.0, StrtodTrimmed(vector, 0));
411 CHECK_EQ(20.0, StrtodTrimmed(vector, 1));
412 CHECK_EQ(200.0, StrtodTrimmed(vector, 2));
413 CHECK_EQ(2e20, StrtodTrimmed(vector, 20));
414 CHECK_EQ(2e22, StrtodTrimmed(vector, 22));
415 CHECK_EQ(2e23, StrtodTrimmed(vector, 23));
416 CHECK_EQ(2e35, StrtodTrimmed(vector, 35));
417 CHECK_EQ(2e36, StrtodTrimmed(vector, 36));
418 CHECK_EQ(2e37, StrtodTrimmed(vector, 37));
419 CHECK_EQ(2e-1, StrtodTrimmed(vector, -1));
420 CHECK_EQ(2e-2, StrtodTrimmed(vector, -2));
421 CHECK_EQ(2e-5, StrtodTrimmed(vector, -5));
422 CHECK_EQ(2e-20, StrtodTrimmed(vector, -20));
423 CHECK_EQ(2e-22, StrtodTrimmed(vector, -22));
424 CHECK_EQ(2e-23, StrtodTrimmed(vector, -23));
425 CHECK_EQ(2e-25, StrtodTrimmed(vector, -25));
426 CHECK_EQ(2e-39, StrtodTrimmed(vector, -39));
427
428 vector = StringToVector("9");
429 CHECK_EQ(9.0, StrtodTrimmed(vector, 0));
430 CHECK_EQ(90.0, StrtodTrimmed(vector, 1));
431 CHECK_EQ(900.0, StrtodTrimmed(vector, 2));
432 CHECK_EQ(9e20, StrtodTrimmed(vector, 20));
433 CHECK_EQ(9e22, StrtodTrimmed(vector, 22));
434 CHECK_EQ(9e23, StrtodTrimmed(vector, 23));
435 CHECK_EQ(9e35, StrtodTrimmed(vector, 35));
436 CHECK_EQ(9e36, StrtodTrimmed(vector, 36));
437 CHECK_EQ(9e37, StrtodTrimmed(vector, 37));
438 CHECK_EQ(9e-1, StrtodTrimmed(vector, -1));
439 CHECK_EQ(9e-2, StrtodTrimmed(vector, -2));
440 CHECK_EQ(9e-5, StrtodTrimmed(vector, -5));
441 CHECK_EQ(9e-20, StrtodTrimmed(vector, -20));
442 CHECK_EQ(9e-22, StrtodTrimmed(vector, -22));
443 CHECK_EQ(9e-23, StrtodTrimmed(vector, -23));
444 CHECK_EQ(9e-25, StrtodTrimmed(vector, -25));
445 CHECK_EQ(9e-39, StrtodTrimmed(vector, -39));
446
447 vector = StringToVector("12345");
448 CHECK_EQ(12345.0, StrtodTrimmed(vector, 0));
449 CHECK_EQ(123450.0, StrtodTrimmed(vector, 1));
450 CHECK_EQ(1234500.0, StrtodTrimmed(vector, 2));
451 CHECK_EQ(12345e20, StrtodTrimmed(vector, 20));
452 CHECK_EQ(12345e22, StrtodTrimmed(vector, 22));
453 CHECK_EQ(12345e23, StrtodTrimmed(vector, 23));
454 CHECK_EQ(12345e30, StrtodTrimmed(vector, 30));
455 CHECK_EQ(12345e31, StrtodTrimmed(vector, 31));
456 CHECK_EQ(12345e32, StrtodTrimmed(vector, 32));
457 CHECK_EQ(12345e35, StrtodTrimmed(vector, 35));
458 CHECK_EQ(12345e36, StrtodTrimmed(vector, 36));
459 CHECK_EQ(12345e37, StrtodTrimmed(vector, 37));
460 CHECK_EQ(12345e-1, StrtodTrimmed(vector, -1));
461 CHECK_EQ(12345e-2, StrtodTrimmed(vector, -2));
462 CHECK_EQ(12345e-5, StrtodTrimmed(vector, -5));
463 CHECK_EQ(12345e-20, StrtodTrimmed(vector, -20));
464 CHECK_EQ(12345e-22, StrtodTrimmed(vector, -22));
465 CHECK_EQ(12345e-23, StrtodTrimmed(vector, -23));
466 CHECK_EQ(12345e-25, StrtodTrimmed(vector, -25));
467 CHECK_EQ(12345e-39, StrtodTrimmed(vector, -39));
468
469 vector = StringToVector("12345678901234");
470 CHECK_EQ(12345678901234.0, StrtodTrimmed(vector, 0));
471 CHECK_EQ(123456789012340.0, StrtodTrimmed(vector, 1));
472 CHECK_EQ(1234567890123400.0, StrtodTrimmed(vector, 2));
473 CHECK_EQ(12345678901234e20, StrtodTrimmed(vector, 20));
474 CHECK_EQ(12345678901234e22, StrtodTrimmed(vector, 22));
475 CHECK_EQ(12345678901234e23, StrtodTrimmed(vector, 23));
476 CHECK_EQ(12345678901234e30, StrtodTrimmed(vector, 30));
477 CHECK_EQ(12345678901234e31, StrtodTrimmed(vector, 31));
478 CHECK_EQ(12345678901234e32, StrtodTrimmed(vector, 32));
479 CHECK_EQ(12345678901234e35, StrtodTrimmed(vector, 35));
480 CHECK_EQ(12345678901234e36, StrtodTrimmed(vector, 36));
481 CHECK_EQ(12345678901234e37, StrtodTrimmed(vector, 37));
482 CHECK_EQ(12345678901234e-1, StrtodTrimmed(vector, -1));
483 CHECK_EQ(12345678901234e-2, StrtodTrimmed(vector, -2));
484 CHECK_EQ(12345678901234e-5, StrtodTrimmed(vector, -5));
485 CHECK_EQ(12345678901234e-20, StrtodTrimmed(vector, -20));
486 CHECK_EQ(12345678901234e-22, StrtodTrimmed(vector, -22));
487 CHECK_EQ(12345678901234e-23, StrtodTrimmed(vector, -23));
488 CHECK_EQ(12345678901234e-25, StrtodTrimmed(vector, -25));
489 CHECK_EQ(12345678901234e-39, StrtodTrimmed(vector, -39));
490
491 vector = StringToVector("123456789012345");
492 CHECK_EQ(123456789012345.0, StrtodTrimmed(vector, 0));
493 CHECK_EQ(1234567890123450.0, StrtodTrimmed(vector, 1));
494 CHECK_EQ(12345678901234500.0, StrtodTrimmed(vector, 2));
495 CHECK_EQ(123456789012345e20, StrtodTrimmed(vector, 20));
496 CHECK_EQ(123456789012345e22, StrtodTrimmed(vector, 22));
497 CHECK_EQ(123456789012345e23, StrtodTrimmed(vector, 23));
498 CHECK_EQ(123456789012345e35, StrtodTrimmed(vector, 35));
499 CHECK_EQ(123456789012345e36, StrtodTrimmed(vector, 36));
500 CHECK_EQ(123456789012345e37, StrtodTrimmed(vector, 37));
501 CHECK_EQ(123456789012345e39, StrtodTrimmed(vector, 39));
502 CHECK_EQ(123456789012345e-1, StrtodTrimmed(vector, -1));
503 CHECK_EQ(123456789012345e-2, StrtodTrimmed(vector, -2));
504 CHECK_EQ(123456789012345e-5, StrtodTrimmed(vector, -5));
505 CHECK_EQ(123456789012345e-20, StrtodTrimmed(vector, -20));
506 CHECK_EQ(123456789012345e-22, StrtodTrimmed(vector, -22));
507 CHECK_EQ(123456789012345e-23, StrtodTrimmed(vector, -23));
508 CHECK_EQ(123456789012345e-25, StrtodTrimmed(vector, -25));
509 CHECK_EQ(123456789012345e-39, StrtodTrimmed(vector, -39));
510
511 CHECK_EQ(0.0, StrtodTrimmedChar("", 1324));
512 CHECK_EQ(0.0, StrtodTrimmedChar("2", -324));
513 CHECK_EQ(4e-324, StrtodTrimmedChar("3", -324));
514 // It would be more readable to put non-zero literals on the left side (i.e.
515 // CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
516 // they are truncated to zero.
517 CHECK_EQ(0.0, StrtodTrimmedChar("1", -325));
518 CHECK_EQ(0.0, StrtodTrimmedChar("1", -325));
519
520 // It would be more readable to put the literals (and not Double::Infinity())
521 // on the left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
522 // complains that the floating constant exceeds range of 'double'.
523 CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("1", 309));
524 CHECK_EQ(1e308, StrtodTrimmedChar("1", 308));
525 CHECK_EQ(1234e305, StrtodTrimmedChar("1234", 305));
526 CHECK_EQ(1234e304, StrtodTrimmedChar("1234", 304));
527 CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("18", 307));
528 CHECK_EQ(17e307, StrtodTrimmedChar("17", 307));
529
530 CHECK_EQ(1.7976931348623157E+308, StrtodTrimmedChar("17976931348623157", 292));
531 CHECK_EQ(1.7976931348623158E+308, StrtodTrimmedChar("17976931348623158", 292));
532 CHECK_EQ(Double::Infinity(), StrtodTrimmedChar("17976931348623159", 292));
533
534 // The following number is the result of 89255.0/1e-22. Both floating-point
535 // numbers can be accurately represented with doubles. However on Linux,x86
536 // the floating-point stack is set to 80bits and the double-rounding
537 // introduces an error.
538 CHECK_EQ(89255e-22, StrtodTrimmedChar("89255", -22));
539
540 // Some random values.
541 CHECK_EQ(358416272e-33, StrtodTrimmedChar("358416272", -33));
542 CHECK_EQ(104110013277974872254e-225,
543 StrtodTrimmedChar("104110013277974872254", -225));
544
545 CHECK_EQ(123456789e108, StrtodTrimmedChar("123456789", 108));
546 CHECK_EQ(123456789e109, StrtodTrimmedChar("123456789", 109));
547 CHECK_EQ(123456789e110, StrtodTrimmedChar("123456789", 110));
548 CHECK_EQ(123456789e111, StrtodTrimmedChar("123456789", 111));
549 CHECK_EQ(123456789e112, StrtodTrimmedChar("123456789", 112));
550 CHECK_EQ(123456789e113, StrtodTrimmedChar("123456789", 113));
551 CHECK_EQ(123456789e114, StrtodTrimmedChar("123456789", 114));
552 CHECK_EQ(123456789e115, StrtodTrimmedChar("123456789", 115));
553 CHECK_EQ(1234567890123456789012345e108,
554 StrtodTrimmedChar("1234567890123456789012345", 108));
555 CHECK_EQ(1234567890123456789012345e109,
556 StrtodTrimmedChar("1234567890123456789012345", 109));
557 CHECK_EQ(1234567890123456789012345e110,
558 StrtodTrimmedChar("1234567890123456789012345", 110));
559 CHECK_EQ(1234567890123456789012345e111,
560 StrtodTrimmedChar("1234567890123456789012345", 111));
561 CHECK_EQ(1234567890123456789012345e112,
562 StrtodTrimmedChar("1234567890123456789012345", 112));
563 CHECK_EQ(1234567890123456789012345e113,
564 StrtodTrimmedChar("1234567890123456789012345", 113));
565 CHECK_EQ(1234567890123456789012345e114,
566 StrtodTrimmedChar("1234567890123456789012345", 114));
567 CHECK_EQ(1234567890123456789012345e115,
568 StrtodTrimmedChar("1234567890123456789012345", 115));
569
570 CHECK_EQ(1234567890123456789052345e108,
571 StrtodTrimmedChar("1234567890123456789052345", 108));
572 CHECK_EQ(1234567890123456789052345e109,
573 StrtodTrimmedChar("1234567890123456789052345", 109));
574 CHECK_EQ(1234567890123456789052345e110,
575 StrtodTrimmedChar("1234567890123456789052345", 110));
576 CHECK_EQ(1234567890123456789052345e111,
577 StrtodTrimmedChar("1234567890123456789052345", 111));
578 CHECK_EQ(1234567890123456789052345e112,
579 StrtodTrimmedChar("1234567890123456789052345", 112));
580 CHECK_EQ(1234567890123456789052345e113,
581 StrtodTrimmedChar("1234567890123456789052345", 113));
582 CHECK_EQ(1234567890123456789052345e114,
583 StrtodTrimmedChar("1234567890123456789052345", 114));
584 CHECK_EQ(1234567890123456789052345e115,
585 StrtodTrimmedChar("1234567890123456789052345", 115));
586
587 // Boundary cases. Boundaries themselves should round to even.
588 //
589 // 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928
590 // next: 72057594037927936
591 // boundary: 72057594037927932 should round up.
592 CHECK_EQ(72057594037927928.0, StrtodTrimmedChar("72057594037927928", 0));
593 CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("72057594037927936", 0));
594 CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("72057594037927932", 0));
595 CHECK_EQ(72057594037927928.0, StrtodTrimmedChar("7205759403792793199999", -5));
596 CHECK_EQ(72057594037927936.0, StrtodTrimmedChar("7205759403792793200001", -5));
597
598 // 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784
599 // next: 9223372036854775808
600 // boundary: 9223372036854775296 should round up.
601 CHECK_EQ(9223372036854774784.0, StrtodTrimmedChar("9223372036854774784", 0));
602 CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("9223372036854775808", 0));
603 CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("9223372036854775296", 0));
604 CHECK_EQ(9223372036854774784.0, StrtodTrimmedChar("922337203685477529599999", -5));
605 CHECK_EQ(9223372036854775808.0, StrtodTrimmedChar("922337203685477529600001", -5));
606
607 // 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384
608 // next: 10141204801825835211973625643008
609 // boundary: 10141204801825834649023672221696 should round up.
610 CHECK_EQ(10141204801825834086073718800384.0,
611 StrtodTrimmedChar("10141204801825834086073718800384", 0));
612 CHECK_EQ(10141204801825835211973625643008.0,
613 StrtodTrimmedChar("10141204801825835211973625643008", 0));
614 CHECK_EQ(10141204801825835211973625643008.0,
615 StrtodTrimmedChar("10141204801825834649023672221696", 0));
616 CHECK_EQ(10141204801825834086073718800384.0,
617 StrtodTrimmedChar("1014120480182583464902367222169599999", -5));
618 CHECK_EQ(10141204801825835211973625643008.0,
619 StrtodTrimmedChar("1014120480182583464902367222169600001", -5));
620
621 // 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808
622 // next: 5708990770823839524233143877797980545530986496
623 // boundary: 5708990770823839207320493820740630171355185152
624 // The boundary should round up.
625 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
626 StrtodTrimmedChar("5708990770823838890407843763683279797179383808", 0));
627 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
628 StrtodTrimmedChar("5708990770823839524233143877797980545530986496", 0));
629 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
630 StrtodTrimmedChar("5708990770823839207320493820740630171355185152", 0));
631 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
632 StrtodTrimmedChar("5708990770823839207320493820740630171355185151999", -3));
633 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
634 StrtodTrimmedChar("5708990770823839207320493820740630171355185152001", -3));
635
636 // The following test-cases got some public attention in early 2011 when they
637 // sent Java and PHP into an infinite loop.
638 CHECK_EQ(2.225073858507201e-308, StrtodTrimmedChar("22250738585072011", -324));
639 CHECK_EQ(2.22507385850720138309e-308,
640 StrtodTrimmedChar("22250738585072011360574097967091319759348195463516456480"
641 "23426109724822222021076945516529523908135087914149158913"
642 "03962110687008643869459464552765720740782062174337998814"
643 "10632673292535522868813721490129811224514518898490572223"
644 "07285255133155755015914397476397983411801999323962548289"
645 "01710708185069063066665599493827577257201576306269066333"
646 "26475653000092458883164330377797918696120494973903778297"
647 "04905051080609940730262937128958950003583799967207254304"
648 "36028407889577179615094551674824347103070260914462157228"
649 "98802581825451803257070188608721131280795122334262883686"
650 "22321503775666622503982534335974568884423900265498198385"
651 "48794829220689472168983109969836584681402285424333066033"
652 "98508864458040010349339704275671864433837704860378616227"
653 "71738545623065874679014086723327636718751", -1076));
654 }
655
656
TEST(Strtof)657 TEST(Strtof) {
658 Vector<const char> vector;
659
660 vector = StringToVector("0");
661 CHECK_EQ(0.0f, Strtof(vector, 1));
662 CHECK_EQ(0.0f, Strtof(vector, 2));
663 CHECK_EQ(0.0f, Strtof(vector, -2));
664 CHECK_EQ(0.0f, Strtof(vector, -999));
665 CHECK_EQ(0.0f, Strtof(vector, +999));
666
667 vector = StringToVector("1");
668 CHECK_EQ(1.0f, Strtof(vector, 0));
669 CHECK_EQ(10.0f, Strtof(vector, 1));
670 CHECK_EQ(100.0f, Strtof(vector, 2));
671 CHECK_EQ(1e20f, Strtof(vector, 20));
672 CHECK_EQ(1e22f, Strtof(vector, 22));
673 CHECK_EQ(1e23f, Strtof(vector, 23));
674 CHECK_EQ(1e35f, Strtof(vector, 35));
675 CHECK_EQ(1e36f, Strtof(vector, 36));
676 CHECK_EQ(1e37f, Strtof(vector, 37));
677 CHECK_EQ(1e-1f, Strtof(vector, -1));
678 CHECK_EQ(1e-2f, Strtof(vector, -2));
679 CHECK_EQ(1e-5f, Strtof(vector, -5));
680 CHECK_EQ(1e-20f, Strtof(vector, -20));
681 CHECK_EQ(1e-22f, Strtof(vector, -22));
682 CHECK_EQ(1e-23f, Strtof(vector, -23));
683 CHECK_EQ(1e-25f, Strtof(vector, -25));
684 CHECK_EQ(1e-39f, Strtof(vector, -39));
685
686 vector = StringToVector("2");
687 CHECK_EQ(2.0f, Strtof(vector, 0));
688 CHECK_EQ(20.0f, Strtof(vector, 1));
689 CHECK_EQ(200.0f, Strtof(vector, 2));
690 CHECK_EQ(2e20f, Strtof(vector, 20));
691 CHECK_EQ(2e22f, Strtof(vector, 22));
692 CHECK_EQ(2e23f, Strtof(vector, 23));
693 CHECK_EQ(2e35f, Strtof(vector, 35));
694 CHECK_EQ(2e36f, Strtof(vector, 36));
695 CHECK_EQ(2e37f, Strtof(vector, 37));
696 CHECK_EQ(2e-1f, Strtof(vector, -1));
697 CHECK_EQ(2e-2f, Strtof(vector, -2));
698 CHECK_EQ(2e-5f, Strtof(vector, -5));
699 CHECK_EQ(2e-20f, Strtof(vector, -20));
700 CHECK_EQ(2e-22f, Strtof(vector, -22));
701 CHECK_EQ(2e-23f, Strtof(vector, -23));
702 CHECK_EQ(2e-25f, Strtof(vector, -25));
703 CHECK_EQ(2e-39f, Strtof(vector, -39));
704
705 vector = StringToVector("9");
706 CHECK_EQ(9.0f, Strtof(vector, 0));
707 CHECK_EQ(90.0f, Strtof(vector, 1));
708 CHECK_EQ(900.0f, Strtof(vector, 2));
709 CHECK_EQ(9e20f, Strtof(vector, 20));
710 CHECK_EQ(9e22f, Strtof(vector, 22));
711 CHECK_EQ(9e23f, Strtof(vector, 23));
712 CHECK_EQ(9e35f, Strtof(vector, 35));
713 CHECK_EQ(9e36f, Strtof(vector, 36));
714 CHECK_EQ(9e37f, Strtof(vector, 37));
715 CHECK_EQ(9e-1f, Strtof(vector, -1));
716 CHECK_EQ(9e-2f, Strtof(vector, -2));
717 CHECK_EQ(9e-5f, Strtof(vector, -5));
718 CHECK_EQ(9e-20f, Strtof(vector, -20));
719 CHECK_EQ(9e-22f, Strtof(vector, -22));
720 CHECK_EQ(9e-23f, Strtof(vector, -23));
721 CHECK_EQ(9e-25f, Strtof(vector, -25));
722 CHECK_EQ(9e-39f, Strtof(vector, -39));
723
724 vector = StringToVector("12345");
725 CHECK_EQ(12345.0f, Strtof(vector, 0));
726 CHECK_EQ(123450.0f, Strtof(vector, 1));
727 CHECK_EQ(1234500.0f, Strtof(vector, 2));
728 CHECK_EQ(12345e20f, Strtof(vector, 20));
729 CHECK_EQ(12345e22f, Strtof(vector, 22));
730 CHECK_EQ(12345e23f, Strtof(vector, 23));
731 CHECK_EQ(12345e30f, Strtof(vector, 30));
732 CHECK_EQ(12345e31f, Strtof(vector, 31));
733 CHECK_EQ(12345e32f, Strtof(vector, 32));
734 CHECK_EQ(12345e-1f, Strtof(vector, -1));
735 CHECK_EQ(12345e-2f, Strtof(vector, -2));
736 CHECK_EQ(12345e-5f, Strtof(vector, -5));
737 CHECK_EQ(12345e-20f, Strtof(vector, -20));
738 CHECK_EQ(12345e-22f, Strtof(vector, -22));
739 CHECK_EQ(12345e-23f, Strtof(vector, -23));
740 CHECK_EQ(12345e-25f, Strtof(vector, -25));
741 CHECK_EQ(12345e-39f, Strtof(vector, -39));
742
743 vector = StringToVector("12345678901234");
744 CHECK_EQ(12345678901234.0f, Strtof(vector, 0));
745 CHECK_EQ(123456789012340.0f, Strtof(vector, 1));
746 CHECK_EQ(1234567890123400.0f, Strtof(vector, 2));
747 CHECK_EQ(12345678901234e20f, Strtof(vector, 20));
748 CHECK_EQ(12345678901234e22f, Strtof(vector, 22));
749 CHECK_EQ(12345678901234e23f, Strtof(vector, 23));
750 CHECK_EQ(12345678901234e-1f, Strtof(vector, -1));
751 CHECK_EQ(12345678901234e-2f, Strtof(vector, -2));
752 CHECK_EQ(12345678901234e-5f, Strtof(vector, -5));
753 CHECK_EQ(12345678901234e-20f, Strtof(vector, -20));
754 CHECK_EQ(12345678901234e-22f, Strtof(vector, -22));
755 CHECK_EQ(12345678901234e-23f, Strtof(vector, -23));
756 CHECK_EQ(12345678901234e-25f, Strtof(vector, -25));
757 CHECK_EQ(12345678901234e-39f, Strtof(vector, -39));
758
759 vector = StringToVector("123456789012345");
760 CHECK_EQ(123456789012345.0f, Strtof(vector, 0));
761 CHECK_EQ(1234567890123450.0f, Strtof(vector, 1));
762 CHECK_EQ(12345678901234500.0f, Strtof(vector, 2));
763 CHECK_EQ(123456789012345e20f, Strtof(vector, 20));
764 CHECK_EQ(123456789012345e22f, Strtof(vector, 22));
765 CHECK_EQ(123456789012345e23f, Strtof(vector, 23));
766 CHECK_EQ(123456789012345e-1f, Strtof(vector, -1));
767 CHECK_EQ(123456789012345e-2f, Strtof(vector, -2));
768 CHECK_EQ(123456789012345e-5f, Strtof(vector, -5));
769 CHECK_EQ(123456789012345e-20f, Strtof(vector, -20));
770 CHECK_EQ(123456789012345e-22f, Strtof(vector, -22));
771 CHECK_EQ(123456789012345e-23f, Strtof(vector, -23));
772 CHECK_EQ(123456789012345e-25f, Strtof(vector, -25));
773 CHECK_EQ(123456789012345e-39f, Strtof(vector, -39));
774
775 CHECK_EQ(0.0f, StrtofChar("0", 12345));
776 CHECK_EQ(0.0f, StrtofChar("", 1324));
777 CHECK_EQ(0.0f, StrtofChar("000000000", 123));
778 CHECK_EQ(0.0f, StrtofChar("2", -324));
779 CHECK_EQ(1e-45f, StrtofChar("1", -45));
780 // It would be more readable to put non-zero literals on the left side (i.e.
781 // CHECK_EQ(1e-46, StrtofChar("1", -45))), but then Gcc complains that
782 // they are truncated to zero.
783 CHECK_EQ(0.0f, StrtofChar("1", -46));
784 CHECK_EQ(0.0f, StrtofChar("1", -47));
785 CHECK_EQ(1e-45f, StrtofChar("1", -45));
786 CHECK_EQ(1e-45f, StrtofChar("8", -46));
787 CHECK_EQ(0.0f, StrtofChar("200000", -51));
788 CHECK_EQ(100000e-50f, StrtofChar("100000", -50));
789 CHECK_EQ(0.0f, StrtofChar("100000", -51));
790 CHECK_EQ(0.0f, StrtofChar("900000", -52));
791 CHECK_EQ(0.0f, StrtofChar("000000001", -47));
792 CHECK_EQ(0.0f, StrtofChar("000000001", -47));
793 CHECK_EQ(0.0f, StrtofChar("00000000200000", -51));
794 CHECK_EQ(800000e-50f, StrtofChar("000000800000", -50));
795 CHECK_EQ(0.0f, StrtofChar("00000000100000", -51));
796 CHECK_EQ(1e-45f, StrtofChar("00000000900000", -51));
797
798 // It would be more readable to put the literals (and not Double::Infinity())
799 // on the left side (i.e. CHECK_EQ(3e38, StrtofChar("3", 38))), but then Gcc
800 // complains that the floating constant exceeds range of 'double'.
801 CHECK_EQ(Single::Infinity(), StrtofChar("3", 39));
802 CHECK_EQ(3e38f, StrtofChar("3", 38));
803 CHECK_EQ(3401e35f, StrtofChar("3401", 35));
804 CHECK_EQ(3401e34f, StrtofChar("3401", 34));
805 CHECK_EQ(Single::Infinity(), StrtofChar("3410", 35));
806 CHECK_EQ(34e37f, StrtofChar("34", 37));
807 CHECK_EQ(Single::Infinity(), StrtofChar("0000001", 39));
808 CHECK_EQ(3401e35f, StrtofChar("0000003401", 35));
809 CHECK_EQ(3401e34f, StrtofChar("0000003401", 34));
810 CHECK_EQ(Single::Infinity(), StrtofChar("0000003410", 35));
811 CHECK_EQ(34e37f, StrtofChar("00000034", 37));
812 CHECK_EQ(1e38f, StrtofChar("100000", 33));
813 CHECK_EQ(3401e35f, StrtofChar("340100000", 30));
814 CHECK_EQ(3401e34f, StrtofChar("340100000", 29));
815 CHECK_EQ(Single::Infinity(), StrtofChar("341000000", 30));
816 CHECK_EQ(34e37f, StrtofChar("3400000", 32));
817 CHECK_EQ(1e38f, StrtofChar("00000100000", 33));
818 CHECK_EQ(3401e35f, StrtofChar("00000340100000", 30));
819 CHECK_EQ(3401e34f, StrtofChar("00000340100000", 29));
820 CHECK_EQ(Single::Infinity(), StrtofChar("00000341000000", 30));
821 CHECK_EQ(34e37f, StrtofChar("000003400000", 32));
822 CHECK_EQ(3.4028234e+38f, StrtofChar("34028235676", 28));
823 CHECK_EQ(3.4028234e+38f, StrtofChar("34028235677", 28));
824 CHECK_EQ(Single::Infinity(), StrtofChar("34028235678", 28));
825
826 // The following number is the result of 89255.0/1e-22. Both floating-point
827 // numbers can be accurately represented with doubles. However on Linux,x86
828 // the floating-point stack is set to 80bits and the double-rounding
829 // introduces an error.
830 CHECK_EQ(89255e-22f, StrtofChar("89255", -22));
831
832 // Boundary cases. Boundaries themselves should round to even.
833 //
834 // 0x4f012334 = 2166567936
835 // next: 2166568192
836 // boundary: 2166568064 should round down.
837 CHECK_EQ(2166567936.0f, StrtofChar("2166567936", 0));
838 CHECK_EQ(2166568192.0f, StrtofChar("2166568192", 0));
839 CHECK_EQ(2166567936.0f, StrtofChar("2166568064", 0));
840 CHECK_EQ(2166567936.0f, StrtofChar("216656806399999", -5));
841 CHECK_EQ(2166568192.0f, StrtofChar("216656806400001", -5));
842 // Verify that we don't double round.
843 // Get the boundary of the boundary.
844 CHECK_EQ(2.1665680640000002384185791015625e9, 2166568064.0);
845 // Visual Studio gets this wrong and believes that these two numbers are the
846 // same doubles. We want to test our conversion and not the compiler. We
847 // therefore disable the check.
848 #ifndef _MSC_VER
849 CHECK(2.16656806400000023841857910156251e9 != 2166568064.0);
850 #endif
851 CHECK_EQ(2166568192.0f, StrtofChar("21665680640000002384185791015625", -22));
852
853 // 0x4fffffff = 8589934080
854 // next: 8589934592
855 // boundary: 8589934336 should round up.
856 CHECK_EQ(8589934080.0f, StrtofChar("8589934080", 0));
857 CHECK_EQ(8589934592.0f, StrtofChar("8589934592", 0));
858 CHECK_EQ(8589934592.0f, StrtofChar("8589934336", 0));
859 CHECK_EQ(8589934080.0f, StrtofChar("858993433599999", -5));
860 CHECK_EQ(8589934592.0f, StrtofChar("858993433600001", -5));
861 // Verify that we don't double round.
862 // Get the boundary of the boundary.
863 // Visual Studio gets this wrong. To avoid failing tests because of a broken
864 // compiler we disable the following two tests. They were only testing the
865 // compiler. The real test is still active.
866 #ifndef _MSC_VER
867 CHECK_EQ(8.589934335999999523162841796875e+09, 8589934336.0);
868 CHECK(8.5899343359999995231628417968749e+09 != 8589934336.0);
869 #endif
870 CHECK_EQ(8589934080.0f, StrtofChar("8589934335999999523162841796875", -21));
871
872 // 0x4f000000 = 2147483648
873 // next: 2147483904
874 // boundary: 2147483776 should round down.
875 CHECK_EQ(2147483648.0f, StrtofChar("2147483648", 0));
876 CHECK_EQ(2147483904.0f, StrtofChar("2147483904", 0));
877 CHECK_EQ(2147483648.0f, StrtofChar("2147483776", 0));
878 CHECK_EQ(2147483648.0f, StrtofChar("214748377599999", -5));
879 CHECK_EQ(2147483904.0f, StrtofChar("214748377600001", -5));
880
881 }
882
TEST(StrtofTrimmed)883 TEST(StrtofTrimmed) {
884 Vector<const char> vector;
885
886 vector = StringToVector("1");
887 CHECK_EQ(1.0f, StrtofTrimmed(vector, 0));
888 CHECK_EQ(10.0f, StrtofTrimmed(vector, 1));
889 CHECK_EQ(100.0f, StrtofTrimmed(vector, 2));
890 CHECK_EQ(1e20f, StrtofTrimmed(vector, 20));
891 CHECK_EQ(1e22f, StrtofTrimmed(vector, 22));
892 CHECK_EQ(1e23f, StrtofTrimmed(vector, 23));
893 CHECK_EQ(1e35f, StrtofTrimmed(vector, 35));
894 CHECK_EQ(1e36f, StrtofTrimmed(vector, 36));
895 CHECK_EQ(1e37f, StrtofTrimmed(vector, 37));
896 CHECK_EQ(1e-1f, StrtofTrimmed(vector, -1));
897 CHECK_EQ(1e-2f, StrtofTrimmed(vector, -2));
898 CHECK_EQ(1e-5f, StrtofTrimmed(vector, -5));
899 CHECK_EQ(1e-20f, StrtofTrimmed(vector, -20));
900 CHECK_EQ(1e-22f, StrtofTrimmed(vector, -22));
901 CHECK_EQ(1e-23f, StrtofTrimmed(vector, -23));
902 CHECK_EQ(1e-25f, StrtofTrimmed(vector, -25));
903 CHECK_EQ(1e-39f, StrtofTrimmed(vector, -39));
904
905 vector = StringToVector("2");
906 CHECK_EQ(2.0f, StrtofTrimmed(vector, 0));
907 CHECK_EQ(20.0f, StrtofTrimmed(vector, 1));
908 CHECK_EQ(200.0f, StrtofTrimmed(vector, 2));
909 CHECK_EQ(2e20f, StrtofTrimmed(vector, 20));
910 CHECK_EQ(2e22f, StrtofTrimmed(vector, 22));
911 CHECK_EQ(2e23f, StrtofTrimmed(vector, 23));
912 CHECK_EQ(2e35f, StrtofTrimmed(vector, 35));
913 CHECK_EQ(2e36f, StrtofTrimmed(vector, 36));
914 CHECK_EQ(2e37f, StrtofTrimmed(vector, 37));
915 CHECK_EQ(2e-1f, StrtofTrimmed(vector, -1));
916 CHECK_EQ(2e-2f, StrtofTrimmed(vector, -2));
917 CHECK_EQ(2e-5f, StrtofTrimmed(vector, -5));
918 CHECK_EQ(2e-20f, StrtofTrimmed(vector, -20));
919 CHECK_EQ(2e-22f, StrtofTrimmed(vector, -22));
920 CHECK_EQ(2e-23f, StrtofTrimmed(vector, -23));
921 CHECK_EQ(2e-25f, StrtofTrimmed(vector, -25));
922 CHECK_EQ(2e-39f, StrtofTrimmed(vector, -39));
923
924 vector = StringToVector("9");
925 CHECK_EQ(9.0f, StrtofTrimmed(vector, 0));
926 CHECK_EQ(90.0f, StrtofTrimmed(vector, 1));
927 CHECK_EQ(900.0f, StrtofTrimmed(vector, 2));
928 CHECK_EQ(9e20f, StrtofTrimmed(vector, 20));
929 CHECK_EQ(9e22f, StrtofTrimmed(vector, 22));
930 CHECK_EQ(9e23f, StrtofTrimmed(vector, 23));
931 CHECK_EQ(9e35f, StrtofTrimmed(vector, 35));
932 CHECK_EQ(9e36f, StrtofTrimmed(vector, 36));
933 CHECK_EQ(9e37f, StrtofTrimmed(vector, 37));
934 CHECK_EQ(9e-1f, StrtofTrimmed(vector, -1));
935 CHECK_EQ(9e-2f, StrtofTrimmed(vector, -2));
936 CHECK_EQ(9e-5f, StrtofTrimmed(vector, -5));
937 CHECK_EQ(9e-20f, StrtofTrimmed(vector, -20));
938 CHECK_EQ(9e-22f, StrtofTrimmed(vector, -22));
939 CHECK_EQ(9e-23f, StrtofTrimmed(vector, -23));
940 CHECK_EQ(9e-25f, StrtofTrimmed(vector, -25));
941 CHECK_EQ(9e-39f, StrtofTrimmed(vector, -39));
942
943 vector = StringToVector("12345");
944 CHECK_EQ(12345.0f, StrtofTrimmed(vector, 0));
945 CHECK_EQ(123450.0f, StrtofTrimmed(vector, 1));
946 CHECK_EQ(1234500.0f, StrtofTrimmed(vector, 2));
947 CHECK_EQ(12345e20f, StrtofTrimmed(vector, 20));
948 CHECK_EQ(12345e22f, StrtofTrimmed(vector, 22));
949 CHECK_EQ(12345e23f, StrtofTrimmed(vector, 23));
950 CHECK_EQ(12345e30f, StrtofTrimmed(vector, 30));
951 CHECK_EQ(12345e31f, StrtofTrimmed(vector, 31));
952 CHECK_EQ(12345e32f, StrtofTrimmed(vector, 32));
953 CHECK_EQ(12345e-1f, StrtofTrimmed(vector, -1));
954 CHECK_EQ(12345e-2f, StrtofTrimmed(vector, -2));
955 CHECK_EQ(12345e-5f, StrtofTrimmed(vector, -5));
956 CHECK_EQ(12345e-20f, StrtofTrimmed(vector, -20));
957 CHECK_EQ(12345e-22f, StrtofTrimmed(vector, -22));
958 CHECK_EQ(12345e-23f, StrtofTrimmed(vector, -23));
959 CHECK_EQ(12345e-25f, StrtofTrimmed(vector, -25));
960 CHECK_EQ(12345e-39f, StrtofTrimmed(vector, -39));
961
962 vector = StringToVector("12345678901234");
963 CHECK_EQ(12345678901234.0f, StrtofTrimmed(vector, 0));
964 CHECK_EQ(123456789012340.0f, StrtofTrimmed(vector, 1));
965 CHECK_EQ(1234567890123400.0f, StrtofTrimmed(vector, 2));
966 CHECK_EQ(12345678901234e20f, StrtofTrimmed(vector, 20));
967 CHECK_EQ(12345678901234e22f, StrtofTrimmed(vector, 22));
968 CHECK_EQ(12345678901234e23f, StrtofTrimmed(vector, 23));
969 CHECK_EQ(12345678901234e-1f, StrtofTrimmed(vector, -1));
970 CHECK_EQ(12345678901234e-2f, StrtofTrimmed(vector, -2));
971 CHECK_EQ(12345678901234e-5f, StrtofTrimmed(vector, -5));
972 CHECK_EQ(12345678901234e-20f, StrtofTrimmed(vector, -20));
973 CHECK_EQ(12345678901234e-22f, StrtofTrimmed(vector, -22));
974 CHECK_EQ(12345678901234e-23f, StrtofTrimmed(vector, -23));
975 CHECK_EQ(12345678901234e-25f, StrtofTrimmed(vector, -25));
976 CHECK_EQ(12345678901234e-39f, StrtofTrimmed(vector, -39));
977
978 vector = StringToVector("123456789012345");
979 CHECK_EQ(123456789012345.0f, StrtofTrimmed(vector, 0));
980 CHECK_EQ(1234567890123450.0f, StrtofTrimmed(vector, 1));
981 CHECK_EQ(12345678901234500.0f, StrtofTrimmed(vector, 2));
982 CHECK_EQ(123456789012345e20f, StrtofTrimmed(vector, 20));
983 CHECK_EQ(123456789012345e22f, StrtofTrimmed(vector, 22));
984 CHECK_EQ(123456789012345e23f, StrtofTrimmed(vector, 23));
985 CHECK_EQ(123456789012345e-1f, StrtofTrimmed(vector, -1));
986 CHECK_EQ(123456789012345e-2f, StrtofTrimmed(vector, -2));
987 CHECK_EQ(123456789012345e-5f, StrtofTrimmed(vector, -5));
988 CHECK_EQ(123456789012345e-20f, StrtofTrimmed(vector, -20));
989 CHECK_EQ(123456789012345e-22f, StrtofTrimmed(vector, -22));
990 CHECK_EQ(123456789012345e-23f, StrtofTrimmed(vector, -23));
991 CHECK_EQ(123456789012345e-25f, StrtofTrimmed(vector, -25));
992 CHECK_EQ(123456789012345e-39f, StrtofTrimmed(vector, -39));
993
994 CHECK_EQ(0.0f, StrtofTrimmedChar("", 1324));
995 CHECK_EQ(0.0f, StrtofTrimmedChar("2", -324));
996 CHECK_EQ(1e-45f, StrtofTrimmedChar("1", -45));
997 // It would be more readable to put non-zero literals on the left side (i.e.
998 // CHECK_EQ(1e-46, StrtofChar("1", -45))), but then Gcc complains that
999 // they are truncated to zero.
1000 CHECK_EQ(0.0f, StrtofTrimmedChar("1", -46));
1001 CHECK_EQ(0.0f, StrtofTrimmedChar("1", -47));
1002 CHECK_EQ(1e-45f, StrtofTrimmedChar("1", -45));
1003 CHECK_EQ(1e-45f, StrtofTrimmedChar("8", -46));
1004
1005 // It would be more readable to put the literals (and not Double::Infinity())
1006 // on the left side (i.e. CHECK_EQ(3e38, StrtofChar("3", 38))), but then Gcc
1007 // complains that the floating constant exceeds range of 'double'.
1008 CHECK_EQ(Single::Infinity(), StrtofTrimmedChar("3", 39));
1009 CHECK_EQ(3e38f, StrtofTrimmedChar("3", 38));
1010 CHECK_EQ(3401e35f, StrtofTrimmedChar("3401", 35));
1011 CHECK_EQ(3401e34f, StrtofTrimmedChar("3401", 34));
1012 CHECK_EQ(34e37f, StrtofTrimmedChar("34", 37));
1013 CHECK_EQ(3.4028234e+38f, StrtofTrimmedChar("34028235676", 28));
1014 CHECK_EQ(3.4028234e+38f, StrtofTrimmedChar("34028235677", 28));
1015 CHECK_EQ(Single::Infinity(), StrtofTrimmedChar("34028235678", 28));
1016
1017 // The following number is the result of 89255.0/1e-22. Both floating-point
1018 // numbers can be accurately represented with doubles. However on Linux,x86
1019 // the floating-point stack is set to 80bits and the double-rounding
1020 // introduces an error.
1021 CHECK_EQ(89255e-22f, StrtofTrimmedChar("89255", -22));
1022
1023 // Boundary cases. Boundaries themselves should round to even.
1024 //
1025 // 0x4f012334 = 2166567936
1026 // next: 2166568192
1027 // boundary: 2166568064 should round down.
1028 CHECK_EQ(2166567936.0f, StrtofTrimmedChar("2166567936", 0));
1029 CHECK_EQ(2166568192.0f, StrtofTrimmedChar("2166568192", 0));
1030 CHECK_EQ(2166567936.0f, StrtofTrimmedChar("2166568064", 0));
1031 CHECK_EQ(2166567936.0f, StrtofTrimmedChar("216656806399999", -5));
1032 CHECK_EQ(2166568192.0f, StrtofTrimmedChar("216656806400001", -5));
1033 // Verify that we don't double round.
1034 // Get the boundary of the boundary.
1035 CHECK_EQ(2.1665680640000002384185791015625e9, 2166568064.0);
1036 // Visual Studio gets this wrong and believes that these two numbers are the
1037 // same doubles. We want to test our conversion and not the compiler. We
1038 // therefore disable the check.
1039 #ifndef _MSC_VER
1040 CHECK(2.16656806400000023841857910156251e9 != 2166568064.0);
1041 #endif
1042 CHECK_EQ(2166568192.0f, StrtofTrimmedChar("21665680640000002384185791015625", -22));
1043
1044 // 0x4fffffff = 8589934080
1045 // next: 8589934592
1046 // boundary: 8589934336 should round up.
1047 CHECK_EQ(8589934592.0f, StrtofTrimmedChar("8589934592", 0));
1048 CHECK_EQ(8589934592.0f, StrtofTrimmedChar("8589934336", 0));
1049 CHECK_EQ(8589934080.0f, StrtofTrimmedChar("858993433599999", -5));
1050 CHECK_EQ(8589934592.0f, StrtofTrimmedChar("858993433600001", -5));
1051 // Verify that we don't double round.
1052 // Get the boundary of the boundary.
1053 // Visual Studio gets this wrong. To avoid failing tests because of a broken
1054 // compiler we disable the following two tests. They were only testing the
1055 // compiler. The real test is still active.
1056 #ifndef _MSC_VER
1057 CHECK_EQ(8.589934335999999523162841796875e+09, 8589934336.0);
1058 CHECK(8.5899343359999995231628417968749e+09 != 8589934336.0);
1059 #endif
1060 CHECK_EQ(8589934080.0f, StrtofTrimmedChar("8589934335999999523162841796875", -21));
1061
1062 // 0x4f000000 = 2147483648
1063 // next: 2147483904
1064 // boundary: 2147483776 should round down.
1065 CHECK_EQ(2147483648.0f, StrtofTrimmedChar("2147483648", 0));
1066 CHECK_EQ(2147483904.0f, StrtofTrimmedChar("2147483904", 0));
1067 CHECK_EQ(2147483648.0f, StrtofTrimmedChar("2147483776", 0));
1068 CHECK_EQ(2147483648.0f, StrtofTrimmedChar("214748377599999", -5));
1069 CHECK_EQ(2147483904.0f, StrtofTrimmedChar("214748377600001", -5));
1070 }
1071
CompareBignumToDiyFp(const Bignum & bignum_digits,int bignum_exponent,DiyFp diy_fp)1072 static int CompareBignumToDiyFp(const Bignum& bignum_digits,
1073 int bignum_exponent,
1074 DiyFp diy_fp) {
1075 Bignum bignum;
1076 bignum.AssignBignum(bignum_digits);
1077 Bignum other;
1078 other.AssignUInt64(diy_fp.f());
1079 if (bignum_exponent >= 0) {
1080 bignum.MultiplyByPowerOfTen(bignum_exponent);
1081 } else {
1082 other.MultiplyByPowerOfTen(-bignum_exponent);
1083 }
1084 if (diy_fp.e() >= 0) {
1085 other.ShiftLeft(diy_fp.e());
1086 } else {
1087 bignum.ShiftLeft(-diy_fp.e());
1088 }
1089 return Bignum::Compare(bignum, other);
1090 }
1091
1092
CheckDouble(Vector<const char> buffer,int exponent,double to_check)1093 static bool CheckDouble(Vector<const char> buffer,
1094 int exponent,
1095 double to_check) {
1096 DiyFp lower_boundary;
1097 DiyFp upper_boundary;
1098 Bignum input_digits;
1099 input_digits.AssignDecimalString(buffer);
1100 if (to_check == 0.0) {
1101 const double kMinDouble = 4e-324;
1102 // Check that the buffer*10^exponent < (0 + kMinDouble)/2.
1103 Double d(kMinDouble);
1104 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1105 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) <= 0;
1106 }
1107 if (to_check == Double::Infinity()) {
1108 const double kMaxDouble = 1.7976931348623157e308;
1109 // Check that the buffer*10^exponent >= boundary between kMaxDouble and inf.
1110 Double d(kMaxDouble);
1111 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1112 return CompareBignumToDiyFp(input_digits, exponent, upper_boundary) >= 0;
1113 }
1114 Double d(to_check);
1115 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1116 if ((d.Significand() & 1) == 0) {
1117 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) >= 0 &&
1118 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) <= 0;
1119 } else {
1120 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) > 0 &&
1121 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) < 0;
1122 }
1123 }
1124
1125
1126 // Copied from v8.cc and adapted to make the function deterministic.
DeterministicRandom()1127 static uint32_t DeterministicRandom() {
1128 // Random number generator using George Marsaglia's MWC algorithm.
1129 static uint32_t hi = 0;
1130 static uint32_t lo = 0;
1131
1132 // Initialization values don't have any special meaning. (They are the result
1133 // of two calls to random().)
1134 if (hi == 0) hi = 0xbfe166e7;
1135 if (lo == 0) lo = 0x64d1c3c9;
1136
1137 // Mix the bits.
1138 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
1139 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
1140 return (hi << 16) + (lo & 0xFFFF);
1141 }
1142
1143
1144 static const int kBufferSize = 1024;
1145 static const int kShortStrtodRandomCount = 2;
1146 static const int kLargeStrtodRandomCount = 2;
1147
TEST(RandomStrtod)1148 TEST(RandomStrtod) {
1149 char buffer[kBufferSize];
1150 for (int length = 1; length < 15; length++) {
1151 for (int i = 0; i < kShortStrtodRandomCount; ++i) {
1152 int pos = 0;
1153 for (int j = 0; j < length; ++j) {
1154 buffer[pos++] = DeterministicRandom() % 10 + '0';
1155 }
1156 int exponent = DeterministicRandom() % (25*2 + 1) - 25 - length;
1157 buffer[pos] = '\0';
1158 Vector<const char> vector(buffer, pos);
1159 double strtod_result = Strtod(vector, exponent);
1160 CHECK(CheckDouble(vector, exponent, strtod_result));
1161 }
1162 }
1163 for (int length = 15; length < 800; length += 2) {
1164 for (int i = 0; i < kLargeStrtodRandomCount; ++i) {
1165 int pos = 0;
1166 for (int j = 0; j < length; ++j) {
1167 buffer[pos++] = DeterministicRandom() % 10 + '0';
1168 }
1169 int exponent = DeterministicRandom() % (308*2 + 1) - 308 - length;
1170 buffer[pos] = '\0';
1171 Vector<const char> vector(buffer, pos);
1172 double strtod_result = Strtod(vector, exponent);
1173 CHECK(CheckDouble(vector, exponent, strtod_result));
1174 }
1175 }
1176 }
1177
1178
CheckFloat(Vector<const char> buffer,int exponent,float to_check)1179 static bool CheckFloat(Vector<const char> buffer,
1180 int exponent,
1181 float to_check) {
1182 DiyFp lower_boundary;
1183 DiyFp upper_boundary;
1184 Bignum input_digits;
1185 input_digits.AssignDecimalString(buffer);
1186 if (to_check == 0.0) {
1187 const float kMinFloat = 1e-45f;
1188 // Check that the buffer*10^exponent < (0 + kMinFloat)/2.
1189 Single s(kMinFloat);
1190 s.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1191 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) <= 0;
1192 }
1193 if (to_check == static_cast<float>(Double::Infinity())) {
1194 const float kMaxFloat = 3.4028234e38f;
1195 // Check that the buffer*10^exponent >= boundary between kMaxFloat and inf.
1196 Single s(kMaxFloat);
1197 s.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1198 return CompareBignumToDiyFp(input_digits, exponent, upper_boundary) >= 0;
1199 }
1200 Single s(to_check);
1201 s.NormalizedBoundaries(&lower_boundary, &upper_boundary);
1202 if ((s.Significand() & 1) == 0) {
1203 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) >= 0 &&
1204 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) <= 0;
1205 } else {
1206 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) > 0 &&
1207 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) < 0;
1208 }
1209 }
1210
1211
1212 static const int kShortStrtofRandomCount = 2;
1213 static const int kLargeStrtofRandomCount = 2;
1214
TEST(RandomStrtof)1215 TEST(RandomStrtof) {
1216 char buffer[kBufferSize];
1217 for (int length = 1; length < 15; length++) {
1218 for (int i = 0; i < kShortStrtofRandomCount; ++i) {
1219 int pos = 0;
1220 for (int j = 0; j < length; ++j) {
1221 buffer[pos++] = DeterministicRandom() % 10 + '0';
1222 }
1223 int exponent = DeterministicRandom() % (5*2 + 1) - 5 - length;
1224 buffer[pos] = '\0';
1225 Vector<const char> vector(buffer, pos);
1226 float strtof_result = Strtof(vector, exponent);
1227 CHECK(CheckFloat(vector, exponent, strtof_result));
1228 }
1229 }
1230 for (int length = 15; length < 800; length += 2) {
1231 for (int i = 0; i < kLargeStrtofRandomCount; ++i) {
1232 int pos = 0;
1233 for (int j = 0; j < length; ++j) {
1234 buffer[pos++] = DeterministicRandom() % 10 + '0';
1235 }
1236 int exponent = DeterministicRandom() % (38*2 + 1) - 38 - length;
1237 buffer[pos] = '\0';
1238 Vector<const char> vector(buffer, pos);
1239 float strtof_result = Strtof(vector, exponent);
1240 CHECK(CheckFloat(vector, exponent, strtof_result));
1241 }
1242 }
1243 }
1244