OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2010 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 "v8.h" | |
31 | |
32 #include "bignum-dtoa.h" | |
33 | |
34 #include "cctest.h" | |
35 #include "double.h" | |
36 #include "gay-fixed.h" | |
37 #include "gay-precision.h" | |
38 #include "gay-shortest.h" | |
39 #include "platform.h" | |
40 | |
41 using namespace v8::internal; | |
42 | |
43 | |
44 // Removes trailing '0' digits. | |
William Hesse
2010/11/15 15:48:30
// Can return the empty string if all digits are 0
Florian Loitsch
2010/11/16 14:32:06
Done.
| |
45 static void TrimRepresentation(Vector<char> representation) { | |
46 int len = strlen(representation.start()); | |
47 int i; | |
48 for (i = len - 1; i >= 0; --i) { | |
49 if (representation[i] != '0') break; | |
50 } | |
51 representation[i + 1] = '\0'; | |
52 } | |
53 | |
54 | |
55 static const int kBufferSize = 100; | |
56 | |
57 | |
58 TEST(BignumDtoaVariousDoubles) { | |
59 char buffer_container[kBufferSize]; | |
60 Vector<char> buffer(buffer_container, kBufferSize); | |
61 int length; | |
62 int point; | |
63 | |
64 BignumDtoa(1.0, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
65 CHECK_EQ("1", buffer.start()); | |
66 CHECK_EQ(1, point); | |
67 | |
68 BignumDtoa(1.0, BIGNUM_DTOA_FIXED, 3, buffer, &length, &point); | |
69 CHECK_GE(3, length - point); | |
70 TrimRepresentation(buffer); | |
71 CHECK_EQ("1", buffer.start()); | |
72 CHECK_EQ(1, point); | |
73 | |
74 BignumDtoa(1.0, BIGNUM_DTOA_PRECISION, 3, buffer, &length, &point); | |
75 CHECK_GE(3, length); | |
76 TrimRepresentation(buffer); | |
77 CHECK_EQ("1", buffer.start()); | |
78 CHECK_EQ(1, point); | |
79 | |
80 BignumDtoa(1.5, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
81 CHECK_EQ("15", buffer.start()); | |
82 CHECK_EQ(1, point); | |
83 | |
84 BignumDtoa(1.5, BIGNUM_DTOA_FIXED, 10, buffer, &length, &point); | |
85 CHECK_GE(10, length - point); | |
86 TrimRepresentation(buffer); | |
87 CHECK_EQ("15", buffer.start()); | |
88 CHECK_EQ(1, point); | |
89 | |
90 BignumDtoa(1.5, BIGNUM_DTOA_PRECISION, 10, buffer, &length, &point); | |
91 CHECK_GE(10, length); | |
92 TrimRepresentation(buffer); | |
93 CHECK_EQ("15", buffer.start()); | |
94 CHECK_EQ(1, point); | |
95 | |
96 double min_double = 5e-324; | |
97 BignumDtoa(min_double, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
98 CHECK_EQ("5", buffer.start()); | |
99 CHECK_EQ(-323, point); | |
100 | |
101 BignumDtoa(min_double, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point); | |
102 CHECK_GE(5, length - point); | |
103 TrimRepresentation(buffer); | |
104 CHECK_EQ("", buffer.start()); | |
105 | |
106 BignumDtoa(min_double, BIGNUM_DTOA_PRECISION, 5, buffer, &length, &point); | |
107 CHECK_GE(5, length); | |
108 TrimRepresentation(buffer); | |
109 CHECK_EQ("49407", buffer.start()); | |
110 CHECK_EQ(-323, point); | |
111 | |
112 double max_double = 1.7976931348623157e308; | |
113 BignumDtoa(max_double, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
114 CHECK_EQ("17976931348623157", buffer.start()); | |
115 CHECK_EQ(309, point); | |
116 | |
117 BignumDtoa(max_double, BIGNUM_DTOA_PRECISION, 7, buffer, &length, &point); | |
118 CHECK_GE(7, length); | |
119 TrimRepresentation(buffer); | |
120 CHECK_EQ("1797693", buffer.start()); | |
121 CHECK_EQ(309, point); | |
122 | |
123 BignumDtoa(4294967272.0, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
124 CHECK_EQ("4294967272", buffer.start()); | |
125 CHECK_EQ(10, point); | |
126 | |
127 BignumDtoa(4294967272.0, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point); | |
128 CHECK_EQ("429496727200000", buffer.start()); | |
129 CHECK_EQ(10, point); | |
130 | |
131 | |
132 BignumDtoa(4294967272.0, BIGNUM_DTOA_PRECISION, 14, buffer, &length, &point); | |
133 CHECK_GE(14, length); | |
134 TrimRepresentation(buffer); | |
135 CHECK_EQ("4294967272", buffer.start()); | |
136 CHECK_EQ(10, point); | |
137 | |
138 BignumDtoa(4.1855804968213567e298, BIGNUM_DTOA_SHORTEST, 0, | |
139 buffer, &length, &point); | |
140 CHECK_EQ("4185580496821357", buffer.start()); | |
141 CHECK_EQ(299, point); | |
142 | |
143 BignumDtoa(4.1855804968213567e298, BIGNUM_DTOA_PRECISION, 20, | |
144 buffer, &length, &point); | |
145 CHECK_GE(20, length); | |
146 TrimRepresentation(buffer); | |
147 CHECK_EQ("41855804968213567225", buffer.start()); | |
148 CHECK_EQ(299, point); | |
149 | |
150 BignumDtoa(5.5626846462680035e-309, BIGNUM_DTOA_SHORTEST, 0, | |
151 buffer, &length, &point); | |
152 CHECK_EQ("5562684646268003", buffer.start()); | |
153 CHECK_EQ(-308, point); | |
154 | |
155 BignumDtoa(5.5626846462680035e-309, BIGNUM_DTOA_PRECISION, 1, | |
156 buffer, &length, &point); | |
157 CHECK_GE(1, length); | |
158 TrimRepresentation(buffer); | |
159 CHECK_EQ("6", buffer.start()); | |
160 CHECK_EQ(-308, point); | |
161 | |
162 BignumDtoa(2147483648.0, BIGNUM_DTOA_SHORTEST, 0, | |
163 buffer, &length, &point); | |
164 CHECK_EQ("2147483648", buffer.start()); | |
165 CHECK_EQ(10, point); | |
166 | |
167 | |
168 BignumDtoa(2147483648.0, BIGNUM_DTOA_FIXED, 2, | |
169 buffer, &length, &point); | |
170 CHECK_GE(2, length - point); | |
171 TrimRepresentation(buffer); | |
172 CHECK_EQ("2147483648", buffer.start()); | |
173 CHECK_EQ(10, point); | |
174 | |
175 BignumDtoa(2147483648.0, BIGNUM_DTOA_PRECISION, 5, | |
176 buffer, &length, &point); | |
177 CHECK_GE(5, length); | |
178 TrimRepresentation(buffer); | |
179 CHECK_EQ("21475", buffer.start()); | |
180 CHECK_EQ(10, point); | |
181 | |
182 BignumDtoa(3.5844466002796428e+298, BIGNUM_DTOA_SHORTEST, 0, | |
183 buffer, &length, &point); | |
184 CHECK_EQ("35844466002796428", buffer.start()); | |
185 CHECK_EQ(299, point); | |
186 | |
187 BignumDtoa(3.5844466002796428e+298, BIGNUM_DTOA_PRECISION, 10, | |
188 buffer, &length, &point); | |
189 CHECK_GE(10, length); | |
190 TrimRepresentation(buffer); | |
191 CHECK_EQ("35844466", buffer.start()); | |
192 CHECK_EQ(299, point); | |
193 | |
194 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); | |
195 double v = Double(smallest_normal64).value(); | |
196 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
197 CHECK_EQ("22250738585072014", buffer.start()); | |
198 CHECK_EQ(-307, point); | |
199 | |
200 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point); | |
201 CHECK_GE(20, length); | |
202 TrimRepresentation(buffer); | |
203 CHECK_EQ("22250738585072013831", buffer.start()); | |
204 CHECK_EQ(-307, point); | |
205 | |
206 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); | |
207 v = Double(largest_denormal64).value(); | |
208 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
209 CHECK_EQ("2225073858507201", buffer.start()); | |
210 CHECK_EQ(-307, point); | |
211 | |
212 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point); | |
213 CHECK_GE(20, length); | |
214 TrimRepresentation(buffer); | |
215 CHECK_EQ("2225073858507200889", buffer.start()); | |
216 CHECK_EQ(-307, point); | |
217 | |
218 BignumDtoa(4128420500802942e-24, BIGNUM_DTOA_SHORTEST, 0, | |
219 buffer, &length, &point); | |
220 CHECK_EQ("4128420500802942", buffer.start()); | |
221 CHECK_EQ(-8, point); | |
222 | |
223 v = 3.9292015898194142585311918e-10; | |
224 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
225 CHECK_EQ("39292015898194143", buffer.start()); | |
226 | |
227 v = 4194304.0; | |
228 BignumDtoa(v, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point); | |
229 CHECK_GE(5, length - point); | |
230 TrimRepresentation(buffer); | |
231 CHECK_EQ("4194304", buffer.start()); | |
232 | |
233 v = 3.3161339052167390562200598e-237; | |
234 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 19, buffer, &length, &point); | |
235 CHECK_GE(19, length); | |
236 TrimRepresentation(buffer); | |
237 CHECK_EQ("3316133905216739056", buffer.start()); | |
238 CHECK_EQ(-236, point); | |
239 | |
240 v = 7.9885183916008099497815232e+191; | |
241 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 4, buffer, &length, &point); | |
242 CHECK_GE(4, length); | |
243 TrimRepresentation(buffer); | |
244 CHECK_EQ("7989", buffer.start()); | |
245 CHECK_EQ(192, point); | |
246 | |
247 v = 1.0000000000000012800000000e+17; | |
248 BignumDtoa(v, BIGNUM_DTOA_FIXED, 1, buffer, &length, &point); | |
249 CHECK_GE(1, length - point); | |
250 TrimRepresentation(buffer); | |
251 CHECK_EQ("100000000000000128", buffer.start()); | |
252 CHECK_EQ(18, point); | |
253 } | |
254 | |
255 | |
William Hesse
2010/11/15 15:48:30
Are these tests taken from the Gay strtod implemen
Florian Loitsch
2010/11/16 14:32:06
They could be merged, but the numbers from Gay are
| |
256 TEST(BignumDtoaGayShortest) { | |
257 char buffer_container[kBufferSize]; | |
258 Vector<char> buffer(buffer_container, kBufferSize); | |
259 int length; | |
260 int point; | |
261 | |
262 Vector<const PrecomputedShortest> precomputed = | |
263 PrecomputedShortestRepresentations(); | |
264 for (int i = 0; i < precomputed.length(); ++i) { | |
265 const PrecomputedShortest current_test = precomputed[i]; | |
266 double v = current_test.v; | |
267 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point); | |
268 CHECK_EQ(current_test.decimal_point, point); | |
269 CHECK_EQ(current_test.representation, buffer.start()); | |
270 } | |
271 } | |
272 | |
273 | |
274 TEST(BignumDtoaGayFixed) { | |
275 char buffer_container[kBufferSize]; | |
276 Vector<char> buffer(buffer_container, kBufferSize); | |
277 int length; | |
278 int point; | |
279 | |
280 Vector<const PrecomputedFixed> precomputed = | |
281 PrecomputedFixedRepresentations(); | |
282 for (int i = 0; i < precomputed.length(); ++i) { | |
283 const PrecomputedFixed current_test = precomputed[i]; | |
284 double v = current_test.v; | |
285 int number_digits = current_test.number_digits; | |
286 BignumDtoa(v, BIGNUM_DTOA_FIXED, number_digits, buffer, &length, &point); | |
287 CHECK_EQ(current_test.decimal_point, point); | |
288 CHECK_GE(number_digits, length - point); | |
289 TrimRepresentation(buffer); | |
290 CHECK_EQ(current_test.representation, buffer.start()); | |
291 } | |
292 } | |
293 | |
294 | |
295 TEST(BignumDtoaGayPrecision) { | |
296 char buffer_container[kBufferSize]; | |
297 Vector<char> buffer(buffer_container, kBufferSize); | |
298 int length; | |
299 int point; | |
300 | |
301 Vector<const PrecomputedPrecision> precomputed = | |
302 PrecomputedPrecisionRepresentations(); | |
303 for (int i = 0; i < precomputed.length(); ++i) { | |
304 const PrecomputedPrecision current_test = precomputed[i]; | |
305 double v = current_test.v; | |
306 int number_digits = current_test.number_digits; | |
307 BignumDtoa(v, BIGNUM_DTOA_PRECISION, number_digits, | |
308 buffer, &length, &point); | |
309 CHECK_EQ(current_test.decimal_point, point); | |
310 CHECK_GE(number_digits, length); | |
311 TrimRepresentation(buffer); | |
312 CHECK_EQ(current_test.representation, buffer.start()); | |
313 } | |
314 } | |
OLD | NEW |