1 // Copyright 2012 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-dtoa.h"
31
32 #include "cctest.h"
33 #include "gay-fixed.h"
34 #include "gay-precision.h"
35 #include "gay-shortest.h"
36 #include "gay-shortest-single.h"
37 #include "double-conversion/ieee.h"
38 #include "double-conversion/utils.h"
39
40 using namespace double_conversion;
41
42
43 // Removes trailing '0' digits.
44 // Can return the empty string if all digits are 0.
TrimRepresentation(Vector<char> representation)45 static void TrimRepresentation(Vector<char> representation) {
46 int len = static_cast<int>(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
TEST(BignumDtoaVariousDoubles)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 BignumDtoa(1e-23, BIGNUM_DTOA_SHORTEST, 0,
195 buffer, &length, &point);
196 CHECK_EQ("1", buffer.start());
197 CHECK_EQ(-22, point);
198
199 uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
200 double v = Double(smallest_normal64).value();
201 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
202 CHECK_EQ("22250738585072014", buffer.start());
203 CHECK_EQ(-307, point);
204
205 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point);
206 CHECK_GE(20, length);
207 TrimRepresentation(buffer);
208 CHECK_EQ("22250738585072013831", buffer.start());
209 CHECK_EQ(-307, point);
210
211 uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
212 v = Double(largest_denormal64).value();
213 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
214 CHECK_EQ("2225073858507201", buffer.start());
215 CHECK_EQ(-307, point);
216
217 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point);
218 CHECK_GE(20, length);
219 TrimRepresentation(buffer);
220 CHECK_EQ("2225073858507200889", buffer.start());
221 CHECK_EQ(-307, point);
222
223 BignumDtoa(4128420500802942e-24, BIGNUM_DTOA_SHORTEST, 0,
224 buffer, &length, &point);
225 CHECK_EQ("4128420500802942", buffer.start());
226 CHECK_EQ(-8, point);
227
228 v = 3.9292015898194142585311918e-10;
229 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
230 CHECK_EQ("39292015898194143", buffer.start());
231
232 v = 4194304.0;
233 BignumDtoa(v, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point);
234 CHECK_GE(5, length - point);
235 TrimRepresentation(buffer);
236 CHECK_EQ("4194304", buffer.start());
237
238 v = 3.3161339052167390562200598e-237;
239 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 19, buffer, &length, &point);
240 CHECK_GE(19, length);
241 TrimRepresentation(buffer);
242 CHECK_EQ("3316133905216739056", buffer.start());
243 CHECK_EQ(-236, point);
244
245 v = 7.9885183916008099497815232e+191;
246 BignumDtoa(v, BIGNUM_DTOA_PRECISION, 4, buffer, &length, &point);
247 CHECK_GE(4, length);
248 TrimRepresentation(buffer);
249 CHECK_EQ("7989", buffer.start());
250 CHECK_EQ(192, point);
251
252 v = 1.0000000000000012800000000e+17;
253 BignumDtoa(v, BIGNUM_DTOA_FIXED, 1, buffer, &length, &point);
254 CHECK_GE(1, length - point);
255 TrimRepresentation(buffer);
256 CHECK_EQ("100000000000000128", buffer.start());
257 CHECK_EQ(18, point);
258 }
259
260
TEST(BignumDtoaShortestVariousFloats)261 TEST(BignumDtoaShortestVariousFloats) {
262 char buffer_container[kBufferSize];
263 Vector<char> buffer(buffer_container, kBufferSize);
264 int length;
265 int point;
266
267 float min_float = 1e-45f;
268 BignumDtoa(min_float, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
269 buffer, &length, &point);
270 CHECK_EQ("1", buffer.start());
271 CHECK_EQ(-44, point);
272
273
274 float max_float = 3.4028234e38f;
275 BignumDtoa(max_float, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
276 buffer, &length, &point);
277 CHECK_EQ("34028235", buffer.start());
278 CHECK_EQ(39, point);
279
280 BignumDtoa(4294967272.0f, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
281 buffer, &length, &point);
282 CHECK_EQ("42949673", buffer.start());
283 CHECK_EQ(10, point);
284
285 BignumDtoa(3.32306998946228968226e+35f, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
286 buffer, &length, &point);
287 CHECK_EQ("332307", buffer.start());
288 CHECK_EQ(36, point);
289
290 BignumDtoa(1.2341e-41f, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
291 buffer, &length, &point);
292 CHECK_EQ("12341", buffer.start());
293 CHECK_EQ(-40, point);
294
295 BignumDtoa(3.3554432e7, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
296 buffer, &length, &point);
297 CHECK_EQ("33554432", buffer.start());
298 CHECK_EQ(8, point);
299
300 BignumDtoa(3.26494756798464e14f, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
301 buffer, &length, &point);
302 CHECK_EQ("32649476", buffer.start());
303 CHECK_EQ(15, point);
304
305 BignumDtoa(3.91132223637771935344e37f, BIGNUM_DTOA_SHORTEST_SINGLE, 0,
306 buffer, &length, &point);
307 CHECK_EQ("39113222", buffer.start());
308 CHECK_EQ(38, point);
309
310 uint32_t smallest_normal32 = 0x00800000;
311 double v = Single(smallest_normal32).value();
312 BignumDtoa(v, BIGNUM_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
313 CHECK_EQ("11754944", buffer.start());
314 CHECK_EQ(-37, point);
315
316 uint32_t largest_denormal32 = 0x007FFFFF;
317 v = Single(largest_denormal32).value();
318 BignumDtoa(v, BIGNUM_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
319 CHECK_EQ("11754942", buffer.start());
320 CHECK_EQ(-37, point);
321 }
322
323
TEST(BignumDtoaGayShortest)324 TEST(BignumDtoaGayShortest) {
325 char buffer_container[kBufferSize];
326 Vector<char> buffer(buffer_container, kBufferSize);
327 int length;
328 int point;
329
330 Vector<const PrecomputedShortest> precomputed =
331 PrecomputedShortestRepresentations();
332 for (int i = 0; i < precomputed.length(); ++i) {
333 const PrecomputedShortest current_test = precomputed[i];
334 double v = current_test.v;
335 BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
336 CHECK_EQ(current_test.decimal_point, point);
337 CHECK_EQ(current_test.representation, buffer.start());
338 }
339 }
340
341
TEST(BignumDtoaGayShortestSingle)342 TEST(BignumDtoaGayShortestSingle) {
343 char buffer_container[kBufferSize];
344 Vector<char> buffer(buffer_container, kBufferSize);
345 int length;
346 int point;
347
348 Vector<const PrecomputedShortestSingle> precomputed =
349 PrecomputedShortestSingleRepresentations();
350 for (int i = 0; i < precomputed.length(); ++i) {
351 const PrecomputedShortestSingle current_test = precomputed[i];
352 float v = current_test.v;
353 BignumDtoa(v, BIGNUM_DTOA_SHORTEST_SINGLE, 0, buffer, &length, &point);
354 CHECK_EQ(current_test.decimal_point, point);
355 CHECK_EQ(current_test.representation, buffer.start());
356 }
357 }
358
359
TEST(BignumDtoaGayFixed)360 TEST(BignumDtoaGayFixed) {
361 char buffer_container[kBufferSize];
362 Vector<char> buffer(buffer_container, kBufferSize);
363 int length;
364 int point;
365
366 Vector<const PrecomputedFixed> precomputed =
367 PrecomputedFixedRepresentations();
368 for (int i = 0; i < precomputed.length(); ++i) {
369 const PrecomputedFixed current_test = precomputed[i];
370 double v = current_test.v;
371 int number_digits = current_test.number_digits;
372 BignumDtoa(v, BIGNUM_DTOA_FIXED, number_digits, buffer, &length, &point);
373 CHECK_EQ(current_test.decimal_point, point);
374 CHECK_GE(number_digits, length - point);
375 TrimRepresentation(buffer);
376 CHECK_EQ(current_test.representation, buffer.start());
377 }
378 }
379
380
TEST(BignumDtoaGayPrecision)381 TEST(BignumDtoaGayPrecision) {
382 char buffer_container[kBufferSize];
383 Vector<char> buffer(buffer_container, kBufferSize);
384 int length;
385 int point;
386
387 Vector<const PrecomputedPrecision> precomputed =
388 PrecomputedPrecisionRepresentations();
389 for (int i = 0; i < precomputed.length(); ++i) {
390 const PrecomputedPrecision current_test = precomputed[i];
391 double v = current_test.v;
392 int number_digits = current_test.number_digits;
393 BignumDtoa(v, BIGNUM_DTOA_PRECISION, number_digits,
394 buffer, &length, &point);
395 CHECK_EQ(current_test.decimal_point, point);
396 CHECK_GE(number_digits, length);
397 TrimRepresentation(buffer);
398 CHECK_EQ(current_test.representation, buffer.start());
399 }
400 }
401