blob: 952f57cf3b99a0d6a5b7002b9c3dde20e0647d1d [file] [log] [blame]
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002// 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.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000027
28#include <stdlib.h>
29
30#include "v8.h"
31
ager@chromium.org01fe7df2010-11-10 11:59:11 +000032#include "bignum.h"
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000033#include "cctest.h"
ager@chromium.org01fe7df2010-11-10 11:59:11 +000034#include "diy-fp.h"
35#include "double.h"
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000036#include "strtod.h"
37
38using namespace v8::internal;
39
vegorov@chromium.org42841962010-10-18 11:18:59 +000040static Vector<const char> StringToVector(const char* str) {
41 return Vector<const char>(str, StrLength(str));
42}
43
44
45static double StrtodChar(const char* str, int exponent) {
46 return Strtod(StringToVector(str), exponent);
47}
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000048
49
50TEST(Strtod) {
vegorov@chromium.org42841962010-10-18 11:18:59 +000051 Vector<const char> vector;
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000052
vegorov@chromium.org42841962010-10-18 11:18:59 +000053 vector = StringToVector("0");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000054 CHECK_EQ(0.0, Strtod(vector, 1));
55 CHECK_EQ(0.0, Strtod(vector, 2));
56 CHECK_EQ(0.0, Strtod(vector, -2));
57 CHECK_EQ(0.0, Strtod(vector, -999));
58 CHECK_EQ(0.0, Strtod(vector, +999));
59
vegorov@chromium.org42841962010-10-18 11:18:59 +000060 vector = StringToVector("1");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000061 CHECK_EQ(1.0, Strtod(vector, 0));
62 CHECK_EQ(10.0, Strtod(vector, 1));
63 CHECK_EQ(100.0, Strtod(vector, 2));
64 CHECK_EQ(1e20, Strtod(vector, 20));
65 CHECK_EQ(1e22, Strtod(vector, 22));
66 CHECK_EQ(1e23, Strtod(vector, 23));
67 CHECK_EQ(1e35, Strtod(vector, 35));
68 CHECK_EQ(1e36, Strtod(vector, 36));
69 CHECK_EQ(1e37, Strtod(vector, 37));
70 CHECK_EQ(1e-1, Strtod(vector, -1));
71 CHECK_EQ(1e-2, Strtod(vector, -2));
72 CHECK_EQ(1e-5, Strtod(vector, -5));
73 CHECK_EQ(1e-20, Strtod(vector, -20));
74 CHECK_EQ(1e-22, Strtod(vector, -22));
75 CHECK_EQ(1e-23, Strtod(vector, -23));
76 CHECK_EQ(1e-25, Strtod(vector, -25));
77 CHECK_EQ(1e-39, Strtod(vector, -39));
78
vegorov@chromium.org42841962010-10-18 11:18:59 +000079 vector = StringToVector("2");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000080 CHECK_EQ(2.0, Strtod(vector, 0));
81 CHECK_EQ(20.0, Strtod(vector, 1));
82 CHECK_EQ(200.0, Strtod(vector, 2));
83 CHECK_EQ(2e20, Strtod(vector, 20));
84 CHECK_EQ(2e22, Strtod(vector, 22));
85 CHECK_EQ(2e23, Strtod(vector, 23));
86 CHECK_EQ(2e35, Strtod(vector, 35));
87 CHECK_EQ(2e36, Strtod(vector, 36));
88 CHECK_EQ(2e37, Strtod(vector, 37));
89 CHECK_EQ(2e-1, Strtod(vector, -1));
90 CHECK_EQ(2e-2, Strtod(vector, -2));
91 CHECK_EQ(2e-5, Strtod(vector, -5));
92 CHECK_EQ(2e-20, Strtod(vector, -20));
93 CHECK_EQ(2e-22, Strtod(vector, -22));
94 CHECK_EQ(2e-23, Strtod(vector, -23));
95 CHECK_EQ(2e-25, Strtod(vector, -25));
96 CHECK_EQ(2e-39, Strtod(vector, -39));
97
vegorov@chromium.org42841962010-10-18 11:18:59 +000098 vector = StringToVector("9");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000099 CHECK_EQ(9.0, Strtod(vector, 0));
100 CHECK_EQ(90.0, Strtod(vector, 1));
101 CHECK_EQ(900.0, Strtod(vector, 2));
102 CHECK_EQ(9e20, Strtod(vector, 20));
103 CHECK_EQ(9e22, Strtod(vector, 22));
104 CHECK_EQ(9e23, Strtod(vector, 23));
105 CHECK_EQ(9e35, Strtod(vector, 35));
106 CHECK_EQ(9e36, Strtod(vector, 36));
107 CHECK_EQ(9e37, Strtod(vector, 37));
108 CHECK_EQ(9e-1, Strtod(vector, -1));
109 CHECK_EQ(9e-2, Strtod(vector, -2));
110 CHECK_EQ(9e-5, Strtod(vector, -5));
111 CHECK_EQ(9e-20, Strtod(vector, -20));
112 CHECK_EQ(9e-22, Strtod(vector, -22));
113 CHECK_EQ(9e-23, Strtod(vector, -23));
114 CHECK_EQ(9e-25, Strtod(vector, -25));
115 CHECK_EQ(9e-39, Strtod(vector, -39));
116
vegorov@chromium.org42841962010-10-18 11:18:59 +0000117 vector = StringToVector("12345");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000118 CHECK_EQ(12345.0, Strtod(vector, 0));
119 CHECK_EQ(123450.0, Strtod(vector, 1));
120 CHECK_EQ(1234500.0, Strtod(vector, 2));
121 CHECK_EQ(12345e20, Strtod(vector, 20));
122 CHECK_EQ(12345e22, Strtod(vector, 22));
123 CHECK_EQ(12345e23, Strtod(vector, 23));
124 CHECK_EQ(12345e30, Strtod(vector, 30));
125 CHECK_EQ(12345e31, Strtod(vector, 31));
126 CHECK_EQ(12345e32, Strtod(vector, 32));
127 CHECK_EQ(12345e35, Strtod(vector, 35));
128 CHECK_EQ(12345e36, Strtod(vector, 36));
129 CHECK_EQ(12345e37, Strtod(vector, 37));
130 CHECK_EQ(12345e-1, Strtod(vector, -1));
131 CHECK_EQ(12345e-2, Strtod(vector, -2));
132 CHECK_EQ(12345e-5, Strtod(vector, -5));
133 CHECK_EQ(12345e-20, Strtod(vector, -20));
134 CHECK_EQ(12345e-22, Strtod(vector, -22));
135 CHECK_EQ(12345e-23, Strtod(vector, -23));
136 CHECK_EQ(12345e-25, Strtod(vector, -25));
137 CHECK_EQ(12345e-39, Strtod(vector, -39));
138
vegorov@chromium.org42841962010-10-18 11:18:59 +0000139 vector = StringToVector("12345678901234");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000140 CHECK_EQ(12345678901234.0, Strtod(vector, 0));
141 CHECK_EQ(123456789012340.0, Strtod(vector, 1));
142 CHECK_EQ(1234567890123400.0, Strtod(vector, 2));
143 CHECK_EQ(12345678901234e20, Strtod(vector, 20));
144 CHECK_EQ(12345678901234e22, Strtod(vector, 22));
145 CHECK_EQ(12345678901234e23, Strtod(vector, 23));
146 CHECK_EQ(12345678901234e30, Strtod(vector, 30));
147 CHECK_EQ(12345678901234e31, Strtod(vector, 31));
148 CHECK_EQ(12345678901234e32, Strtod(vector, 32));
149 CHECK_EQ(12345678901234e35, Strtod(vector, 35));
150 CHECK_EQ(12345678901234e36, Strtod(vector, 36));
151 CHECK_EQ(12345678901234e37, Strtod(vector, 37));
152 CHECK_EQ(12345678901234e-1, Strtod(vector, -1));
153 CHECK_EQ(12345678901234e-2, Strtod(vector, -2));
154 CHECK_EQ(12345678901234e-5, Strtod(vector, -5));
155 CHECK_EQ(12345678901234e-20, Strtod(vector, -20));
156 CHECK_EQ(12345678901234e-22, Strtod(vector, -22));
157 CHECK_EQ(12345678901234e-23, Strtod(vector, -23));
158 CHECK_EQ(12345678901234e-25, Strtod(vector, -25));
159 CHECK_EQ(12345678901234e-39, Strtod(vector, -39));
160
vegorov@chromium.org42841962010-10-18 11:18:59 +0000161 vector = StringToVector("123456789012345");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000162 CHECK_EQ(123456789012345.0, Strtod(vector, 0));
163 CHECK_EQ(1234567890123450.0, Strtod(vector, 1));
164 CHECK_EQ(12345678901234500.0, Strtod(vector, 2));
165 CHECK_EQ(123456789012345e20, Strtod(vector, 20));
166 CHECK_EQ(123456789012345e22, Strtod(vector, 22));
167 CHECK_EQ(123456789012345e23, Strtod(vector, 23));
168 CHECK_EQ(123456789012345e35, Strtod(vector, 35));
169 CHECK_EQ(123456789012345e36, Strtod(vector, 36));
170 CHECK_EQ(123456789012345e37, Strtod(vector, 37));
171 CHECK_EQ(123456789012345e39, Strtod(vector, 39));
172 CHECK_EQ(123456789012345e-1, Strtod(vector, -1));
173 CHECK_EQ(123456789012345e-2, Strtod(vector, -2));
174 CHECK_EQ(123456789012345e-5, Strtod(vector, -5));
175 CHECK_EQ(123456789012345e-20, Strtod(vector, -20));
176 CHECK_EQ(123456789012345e-22, Strtod(vector, -22));
177 CHECK_EQ(123456789012345e-23, Strtod(vector, -23));
178 CHECK_EQ(123456789012345e-25, Strtod(vector, -25));
179 CHECK_EQ(123456789012345e-39, Strtod(vector, -39));
vegorov@chromium.org42841962010-10-18 11:18:59 +0000180
181 CHECK_EQ(0.0, StrtodChar("0", 12345));
182 CHECK_EQ(0.0, StrtodChar("", 1324));
183 CHECK_EQ(0.0, StrtodChar("000000000", 123));
184 CHECK_EQ(0.0, StrtodChar("2", -324));
185 CHECK_EQ(4e-324, StrtodChar("3", -324));
186 // It would be more readable to put non-zero literals on the left side (i.e.
187 // CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
188 // they are truncated to zero.
189 CHECK_EQ(0.0, StrtodChar("1", -325));
190 CHECK_EQ(0.0, StrtodChar("1", -325));
191 CHECK_EQ(0.0, StrtodChar("20000", -328));
192 CHECK_EQ(40000e-328, StrtodChar("30000", -328));
193 CHECK_EQ(0.0, StrtodChar("10000", -329));
194 CHECK_EQ(0.0, StrtodChar("90000", -329));
195 CHECK_EQ(0.0, StrtodChar("000000001", -325));
196 CHECK_EQ(0.0, StrtodChar("000000001", -325));
197 CHECK_EQ(0.0, StrtodChar("0000000020000", -328));
198 CHECK_EQ(40000e-328, StrtodChar("00000030000", -328));
199 CHECK_EQ(0.0, StrtodChar("0000000010000", -329));
200 CHECK_EQ(0.0, StrtodChar("0000000090000", -329));
201
202 // It would be more readable to put the literals (and not V8_INFINITY) on the
203 // left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
204 // complains that the floating constant exceeds range of 'double'.
205 CHECK_EQ(V8_INFINITY, StrtodChar("1", 309));
206 CHECK_EQ(1e308, StrtodChar("1", 308));
207 CHECK_EQ(1234e305, StrtodChar("1234", 305));
208 CHECK_EQ(1234e304, StrtodChar("1234", 304));
209 CHECK_EQ(V8_INFINITY, StrtodChar("18", 307));
210 CHECK_EQ(17e307, StrtodChar("17", 307));
211 CHECK_EQ(V8_INFINITY, StrtodChar("0000001", 309));
212 CHECK_EQ(1e308, StrtodChar("00000001", 308));
213 CHECK_EQ(1234e305, StrtodChar("00000001234", 305));
214 CHECK_EQ(1234e304, StrtodChar("000000001234", 304));
215 CHECK_EQ(V8_INFINITY, StrtodChar("0000000018", 307));
216 CHECK_EQ(17e307, StrtodChar("0000000017", 307));
217 CHECK_EQ(V8_INFINITY, StrtodChar("1000000", 303));
218 CHECK_EQ(1e308, StrtodChar("100000", 303));
219 CHECK_EQ(1234e305, StrtodChar("123400000", 300));
220 CHECK_EQ(1234e304, StrtodChar("123400000", 299));
221 CHECK_EQ(V8_INFINITY, StrtodChar("180000000", 300));
222 CHECK_EQ(17e307, StrtodChar("170000000", 300));
223 CHECK_EQ(V8_INFINITY, StrtodChar("00000001000000", 303));
224 CHECK_EQ(1e308, StrtodChar("000000000000100000", 303));
225 CHECK_EQ(1234e305, StrtodChar("00000000123400000", 300));
226 CHECK_EQ(1234e304, StrtodChar("0000000123400000", 299));
227 CHECK_EQ(V8_INFINITY, StrtodChar("00000000180000000", 300));
228 CHECK_EQ(17e307, StrtodChar("00000000170000000", 300));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000229 CHECK_EQ(1.7976931348623157E+308, StrtodChar("17976931348623157", 292));
230 CHECK_EQ(1.7976931348623158E+308, StrtodChar("17976931348623158", 292));
231 CHECK_EQ(V8_INFINITY, StrtodChar("17976931348623159", 292));
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000232
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000233 // The following number is the result of 89255.0/1e22. Both floating-point
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000234 // numbers can be accurately represented with doubles. However on Linux,x86
235 // the floating-point stack is set to 80bits and the double-rounding
236 // introduces an error.
237 CHECK_EQ(89255e-22, StrtodChar("89255", -22));
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000238
239 // Some random values.
240 CHECK_EQ(358416272e-33, StrtodChar("358416272", -33));
lrn@chromium.org303ada72010-10-27 09:33:13 +0000241 CHECK_EQ(104110013277974872254e-225,
242 StrtodChar("104110013277974872254", -225));
243
244 CHECK_EQ(123456789e108, StrtodChar("123456789", 108));
245 CHECK_EQ(123456789e109, StrtodChar("123456789", 109));
246 CHECK_EQ(123456789e110, StrtodChar("123456789", 110));
247 CHECK_EQ(123456789e111, StrtodChar("123456789", 111));
248 CHECK_EQ(123456789e112, StrtodChar("123456789", 112));
249 CHECK_EQ(123456789e113, StrtodChar("123456789", 113));
250 CHECK_EQ(123456789e114, StrtodChar("123456789", 114));
251 CHECK_EQ(123456789e115, StrtodChar("123456789", 115));
252
253 CHECK_EQ(1234567890123456789012345e108,
254 StrtodChar("1234567890123456789012345", 108));
255 CHECK_EQ(1234567890123456789012345e109,
256 StrtodChar("1234567890123456789012345", 109));
257 CHECK_EQ(1234567890123456789012345e110,
258 StrtodChar("1234567890123456789012345", 110));
259 CHECK_EQ(1234567890123456789012345e111,
260 StrtodChar("1234567890123456789012345", 111));
261 CHECK_EQ(1234567890123456789012345e112,
262 StrtodChar("1234567890123456789012345", 112));
263 CHECK_EQ(1234567890123456789012345e113,
264 StrtodChar("1234567890123456789012345", 113));
265 CHECK_EQ(1234567890123456789012345e114,
266 StrtodChar("1234567890123456789012345", 114));
267 CHECK_EQ(1234567890123456789012345e115,
268 StrtodChar("1234567890123456789012345", 115));
269
270 CHECK_EQ(1234567890123456789052345e108,
271 StrtodChar("1234567890123456789052345", 108));
272 CHECK_EQ(1234567890123456789052345e109,
273 StrtodChar("1234567890123456789052345", 109));
274 CHECK_EQ(1234567890123456789052345e110,
275 StrtodChar("1234567890123456789052345", 110));
276 CHECK_EQ(1234567890123456789052345e111,
277 StrtodChar("1234567890123456789052345", 111));
278 CHECK_EQ(1234567890123456789052345e112,
279 StrtodChar("1234567890123456789052345", 112));
280 CHECK_EQ(1234567890123456789052345e113,
281 StrtodChar("1234567890123456789052345", 113));
282 CHECK_EQ(1234567890123456789052345e114,
283 StrtodChar("1234567890123456789052345", 114));
284 CHECK_EQ(1234567890123456789052345e115,
285 StrtodChar("1234567890123456789052345", 115));
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000286
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000287 CHECK_EQ(5.445618932859895e-255,
288 StrtodChar("5445618932859895362967233318697132813618813095743952975"
289 "4392982234069699615600475529427176366709107287468930197"
290 "8628345413991790019316974825934906752493984055268219809"
291 "5012176093045431437495773903922425632551857520884625114"
292 "6241265881735209066709685420744388526014389929047617597"
293 "0302268848374508109029268898695825171158085457567481507"
294 "4162979705098246243690189880319928315307816832576838178"
295 "2563074014542859888710209237525873301724479666744537857"
296 "9026553346649664045621387124193095870305991178772256504"
297 "4368663670643970181259143319016472430928902201239474588"
298 "1392338901353291306607057623202353588698746085415097902"
299 "6640064319118728664842287477491068264828851624402189317"
300 "2769161449825765517353755844373640588822904791244190695"
301 "2998382932630754670573838138825217065450843010498555058"
302 "88186560731", -1035));
303
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000304 // Boundary cases. Boundaries themselves should round to even.
305 //
306 // 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928
307 // next: 72057594037927936
308 // boundary: 72057594037927932 should round up.
309 CHECK_EQ(72057594037927928.0, StrtodChar("72057594037927928", 0));
310 CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927936", 0));
311 CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927932", 0));
312 CHECK_EQ(72057594037927928.0, StrtodChar("7205759403792793199999", -5));
313 CHECK_EQ(72057594037927936.0, StrtodChar("7205759403792793200001", -5));
314
315 // 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784
316 // next: 9223372036854775808
317 // boundary: 9223372036854775296 should round up.
318 CHECK_EQ(9223372036854774784.0, StrtodChar("9223372036854774784", 0));
319 CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775808", 0));
320 CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775296", 0));
321 CHECK_EQ(9223372036854774784.0, StrtodChar("922337203685477529599999", -5));
322 CHECK_EQ(9223372036854775808.0, StrtodChar("922337203685477529600001", -5));
323
324 // 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384
325 // next: 10141204801825835211973625643008
326 // boundary: 10141204801825834649023672221696 should round up.
327 CHECK_EQ(10141204801825834086073718800384.0,
328 StrtodChar("10141204801825834086073718800384", 0));
329 CHECK_EQ(10141204801825835211973625643008.0,
330 StrtodChar("10141204801825835211973625643008", 0));
331 CHECK_EQ(10141204801825835211973625643008.0,
332 StrtodChar("10141204801825834649023672221696", 0));
333 CHECK_EQ(10141204801825834086073718800384.0,
334 StrtodChar("1014120480182583464902367222169599999", -5));
335 CHECK_EQ(10141204801825835211973625643008.0,
336 StrtodChar("1014120480182583464902367222169600001", -5));
337
338 // 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808
339 // next: 5708990770823839524233143877797980545530986496
340 // boundary: 5708990770823839207320493820740630171355185152
341 // The boundary should round up.
342 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
343 StrtodChar("5708990770823838890407843763683279797179383808", 0));
344 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
345 StrtodChar("5708990770823839524233143877797980545530986496", 0));
346 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
347 StrtodChar("5708990770823839207320493820740630171355185152", 0));
348 CHECK_EQ(5708990770823838890407843763683279797179383808.0,
349 StrtodChar("5708990770823839207320493820740630171355185151999", -3));
350 CHECK_EQ(5708990770823839524233143877797980545530986496.0,
351 StrtodChar("5708990770823839207320493820740630171355185152001", -3));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000352
353 // The following test-cases got some public attention in early 2011 when they
354 // sent Java and PHP into an infinite loop.
355 CHECK_EQ(2.225073858507201e-308, StrtodChar("22250738585072011", -324));
356 CHECK_EQ(2.22507385850720138309e-308,
357 StrtodChar("22250738585072011360574097967091319759348195463516456480"
358 "23426109724822222021076945516529523908135087914149158913"
359 "03962110687008643869459464552765720740782062174337998814"
360 "10632673292535522868813721490129811224514518898490572223"
361 "07285255133155755015914397476397983411801999323962548289"
362 "01710708185069063066665599493827577257201576306269066333"
363 "26475653000092458883164330377797918696120494973903778297"
364 "04905051080609940730262937128958950003583799967207254304"
365 "36028407889577179615094551674824347103070260914462157228"
366 "98802581825451803257070188608721131280795122334262883686"
367 "22321503775666622503982534335974568884423900265498198385"
368 "48794829220689472168983109969836584681402285424333066033"
369 "98508864458040010349339704275671864433837704860378616227"
370 "71738545623065874679014086723327636718751", -1076));
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000371}
372
373
374static int CompareBignumToDiyFp(const Bignum& bignum_digits,
375 int bignum_exponent,
376 DiyFp diy_fp) {
377 Bignum bignum;
378 bignum.AssignBignum(bignum_digits);
379 Bignum other;
380 other.AssignUInt64(diy_fp.f());
381 if (bignum_exponent >= 0) {
382 bignum.MultiplyByPowerOfTen(bignum_exponent);
383 } else {
384 other.MultiplyByPowerOfTen(-bignum_exponent);
385 }
386 if (diy_fp.e() >= 0) {
387 other.ShiftLeft(diy_fp.e());
388 } else {
389 bignum.ShiftLeft(-diy_fp.e());
390 }
391 return Bignum::Compare(bignum, other);
392}
393
394
395static bool CheckDouble(Vector<const char> buffer,
396 int exponent,
397 double to_check) {
398 DiyFp lower_boundary;
399 DiyFp upper_boundary;
400 Bignum input_digits;
401 input_digits.AssignDecimalString(buffer);
402 if (to_check == 0.0) {
403 const double kMinDouble = 4e-324;
404 // Check that the buffer*10^exponent < (0 + kMinDouble)/2.
405 Double d(kMinDouble);
406 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
407 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) <= 0;
408 }
409 if (to_check == V8_INFINITY) {
410 const double kMaxDouble = 1.7976931348623157e308;
411 // Check that the buffer*10^exponent >= boundary between kMaxDouble and inf.
412 Double d(kMaxDouble);
413 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
414 return CompareBignumToDiyFp(input_digits, exponent, upper_boundary) >= 0;
415 }
416 Double d(to_check);
417 d.NormalizedBoundaries(&lower_boundary, &upper_boundary);
418 if ((d.Significand() & 1) == 0) {
419 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) >= 0 &&
420 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) <= 0;
421 } else {
422 return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) > 0 &&
423 CompareBignumToDiyFp(input_digits, exponent, upper_boundary) < 0;
424 }
425}
426
427
428// Copied from v8.cc and adapted to make the function deterministic.
429static uint32_t DeterministicRandom() {
430 // Random number generator using George Marsaglia's MWC algorithm.
431 static uint32_t hi = 0;
432 static uint32_t lo = 0;
433
434 // Initialization values don't have any special meaning. (They are the result
435 // of two calls to random().)
436 if (hi == 0) hi = 0xbfe166e7;
437 if (lo == 0) lo = 0x64d1c3c9;
438
439 // Mix the bits.
440 hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
441 lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
442 return (hi << 16) + (lo & 0xFFFF);
443}
444
445
446static const int kBufferSize = 1024;
447static const int kShortStrtodRandomCount = 2;
448static const int kLargeStrtodRandomCount = 2;
449
450TEST(RandomStrtod) {
451 char buffer[kBufferSize];
452 for (int length = 1; length < 15; length++) {
453 for (int i = 0; i < kShortStrtodRandomCount; ++i) {
454 int pos = 0;
455 for (int j = 0; j < length; ++j) {
456 buffer[pos++] = random() % 10 + '0';
457 }
458 int exponent = DeterministicRandom() % (25*2 + 1) - 25 - length;
459 buffer[pos] = '\0';
460 Vector<const char> vector(buffer, pos);
461 double strtod_result = Strtod(vector, exponent);
462 CHECK(CheckDouble(vector, exponent, strtod_result));
463 }
464 }
465 for (int length = 15; length < 800; length += 2) {
466 for (int i = 0; i < kLargeStrtodRandomCount; ++i) {
467 int pos = 0;
468 for (int j = 0; j < length; ++j) {
469 buffer[pos++] = random() % 10 + '0';
470 }
471 int exponent = DeterministicRandom() % (308*2 + 1) - 308 - length;
472 buffer[pos] = '\0';
473 Vector<const char> vector(buffer, pos);
474 double strtod_result = Strtod(vector, exponent);
475 CHECK(CheckDouble(vector, exponent, strtod_result));
476 }
477 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000478}