• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "double-conversion/double-conversion.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 
39 
40 using namespace double_conversion;
41 
42 
43 enum DtoaMode {
44   SHORTEST,
45   SHORTEST_SINGLE,
46   FIXED,
47   PRECISION
48 };
49 
DoubleToAscii(double v,DtoaMode test_mode,int requested_digits,Vector<char> buffer,bool * sign,int * length,int * point)50 static void DoubleToAscii(double v, DtoaMode test_mode, int requested_digits,
51                           Vector<char> buffer, bool* sign, int* length,
52                           int* point) {
53   DoubleToStringConverter::DtoaMode mode = DoubleToStringConverter::SHORTEST;
54   switch (test_mode) {
55     case SHORTEST: mode = DoubleToStringConverter::SHORTEST; break;
56     case SHORTEST_SINGLE:
57         mode = DoubleToStringConverter::SHORTEST_SINGLE;
58         break;
59     case FIXED: mode = DoubleToStringConverter::FIXED; break;
60     case PRECISION: mode = DoubleToStringConverter::PRECISION; break;
61   }
62   DoubleToStringConverter::DoubleToAscii(v, mode, requested_digits,
63                                  buffer.start(), buffer.length(),
64                                  sign, length, point);
65 }
66 
67 // Removes trailing '0' digits.
TrimRepresentation(Vector<char> representation)68 static void TrimRepresentation(Vector<char> representation) {
69   int len = strlen(representation.start());
70   int i;
71   for (i = len - 1; i >= 0; --i) {
72     if (representation[i] != '0') break;
73   }
74   representation[i + 1] = '\0';
75 }
76 
77 
78 static const int kBufferSize = 100;
79 
80 
TEST(DtoaVariousDoubles)81 TEST(DtoaVariousDoubles) {
82   char buffer_container[kBufferSize];
83   Vector<char> buffer(buffer_container, kBufferSize);
84   int length;
85   int point;
86   bool sign;
87 
88   DoubleToAscii(0.0, SHORTEST, 0, buffer, &sign, &length, &point);
89   CHECK_EQ("0", buffer.start());
90   CHECK_EQ(1, point);
91 
92   DoubleToAscii(0.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
93   CHECK_EQ("0", buffer.start());
94   CHECK_EQ(1, point);
95 
96   DoubleToAscii(0.0, FIXED, 2, buffer, &sign, &length, &point);
97   CHECK_EQ(1, length);
98   CHECK_EQ("0", buffer.start());
99   CHECK_EQ(1, point);
100 
101   DoubleToAscii(0.0, PRECISION, 3, buffer, &sign, &length, &point);
102   CHECK_EQ(1, length);
103   CHECK_EQ("0", buffer.start());
104   CHECK_EQ(1, point);
105 
106   DoubleToAscii(1.0, SHORTEST, 0, buffer, &sign, &length, &point);
107   CHECK_EQ("1", buffer.start());
108   CHECK_EQ(1, point);
109 
110   DoubleToAscii(1.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
111   CHECK_EQ("1", buffer.start());
112   CHECK_EQ(1, point);
113 
114   DoubleToAscii(1.0, FIXED, 3, buffer, &sign, &length, &point);
115   CHECK_GE(3, length - point);
116   TrimRepresentation(buffer);
117   CHECK_EQ("1", buffer.start());
118   CHECK_EQ(1, point);
119 
120   DoubleToAscii(1.0, PRECISION, 3, buffer, &sign, &length, &point);
121   CHECK_GE(3, length);
122   TrimRepresentation(buffer);
123   CHECK_EQ("1", buffer.start());
124   CHECK_EQ(1, point);
125 
126   DoubleToAscii(1.5, SHORTEST, 0, buffer, &sign, &length, &point);
127   CHECK_EQ("15", buffer.start());
128   CHECK_EQ(1, point);
129 
130   DoubleToAscii(1.5f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
131   CHECK_EQ("15", buffer.start());
132   CHECK_EQ(1, point);
133 
134   DoubleToAscii(1.5, FIXED, 10, buffer, &sign, &length, &point);
135   CHECK_GE(10, length - point);
136   TrimRepresentation(buffer);
137   CHECK_EQ("15", buffer.start());
138   CHECK_EQ(1, point);
139 
140   DoubleToAscii(1.5, PRECISION, 10, buffer, &sign, &length, &point);
141   CHECK_GE(10, length);
142   TrimRepresentation(buffer);
143   CHECK_EQ("15", buffer.start());
144   CHECK_EQ(1, point);
145 
146   double min_double = 5e-324;
147   DoubleToAscii(min_double, SHORTEST, 0, buffer, &sign, &length, &point);
148   CHECK_EQ("5", buffer.start());
149   CHECK_EQ(-323, point);
150 
151   float min_float = 1e-45f;
152   DoubleToAscii(min_float, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
153   CHECK_EQ("1", buffer.start());
154   CHECK_EQ(-44, point);
155 
156   DoubleToAscii(min_double, FIXED, 5, buffer, &sign, &length, &point);
157   CHECK_GE(5, length - point);
158   TrimRepresentation(buffer);
159   CHECK_EQ("", buffer.start());
160   CHECK_GE(-5, point);
161 
162   DoubleToAscii(min_double, PRECISION, 5, buffer, &sign, &length, &point);
163   CHECK_GE(5, length);
164   TrimRepresentation(buffer);
165   CHECK_EQ("49407", buffer.start());
166   CHECK_EQ(-323, point);
167 
168   double max_double = 1.7976931348623157e308;
169   DoubleToAscii(max_double, SHORTEST, 0, buffer, &sign, &length, &point);
170   CHECK_EQ("17976931348623157", buffer.start());
171   CHECK_EQ(309, point);
172 
173   float max_float = 3.4028234e38f;
174   DoubleToAscii(max_float, SHORTEST_SINGLE, 0,
175                 buffer, &sign, &length, &point);
176   CHECK_EQ("34028235", buffer.start());
177   CHECK_EQ(39, point);
178 
179   DoubleToAscii(max_double, PRECISION, 7, buffer, &sign, &length, &point);
180   CHECK_GE(7, length);
181   TrimRepresentation(buffer);
182   CHECK_EQ("1797693", buffer.start());
183   CHECK_EQ(309, point);
184 
185   DoubleToAscii(4294967272.0, SHORTEST, 0, buffer, &sign, &length, &point);
186   CHECK_EQ("4294967272", buffer.start());
187   CHECK_EQ(10, point);
188 
189   DoubleToAscii(4294967272.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
190   CHECK_EQ("42949673", buffer.start());
191   CHECK_EQ(10, point);
192 
193   DoubleToAscii(4294967272.0, FIXED, 5, buffer, &sign, &length, &point);
194   CHECK_GE(5, length - point);
195   TrimRepresentation(buffer);
196   CHECK_EQ("4294967272", buffer.start());
197   CHECK_EQ(10, point);
198 
199   DoubleToAscii(4294967272.0, PRECISION, 14,
200                 buffer, &sign, &length, &point);
201   CHECK_GE(14, length);
202   TrimRepresentation(buffer);
203   CHECK_EQ("4294967272", buffer.start());
204   CHECK_EQ(10, point);
205 
206   DoubleToAscii(4.1855804968213567e298, SHORTEST, 0,
207                 buffer, &sign, &length, &point);
208   CHECK_EQ("4185580496821357", buffer.start());
209   CHECK_EQ(299, point);
210 
211   DoubleToAscii(4.1855804968213567e298, PRECISION, 20,
212                 buffer, &sign, &length, &point);
213   CHECK_GE(20, length);
214   TrimRepresentation(buffer);
215   CHECK_EQ("41855804968213567225", buffer.start());
216   CHECK_EQ(299, point);
217 
218   DoubleToAscii(5.5626846462680035e-309, SHORTEST, 0,
219                 buffer, &sign, &length, &point);
220   CHECK_EQ("5562684646268003", buffer.start());
221   CHECK_EQ(-308, point);
222 
223   DoubleToAscii(5.5626846462680035e-309, PRECISION, 1,
224                 buffer, &sign, &length, &point);
225   CHECK_GE(1, length);
226   TrimRepresentation(buffer);
227   CHECK_EQ("6", buffer.start());
228   CHECK_EQ(-308, point);
229 
230   DoubleToAscii(-2147483648.0, SHORTEST, 0,
231                 buffer, &sign, &length, &point);
232   CHECK_EQ(1, sign);
233   CHECK_EQ("2147483648", buffer.start());
234   CHECK_EQ(10, point);
235 
236   DoubleToAscii(-2147483648.0, SHORTEST_SINGLE, 0,
237                 buffer, &sign, &length, &point);
238   CHECK_EQ(1, sign);
239   CHECK_EQ("21474836", buffer.start());
240   CHECK_EQ(10, point);
241 
242 
243   DoubleToAscii(-2147483648.0, FIXED, 2, buffer, &sign, &length, &point);
244   CHECK_GE(2, length - point);
245   TrimRepresentation(buffer);
246   CHECK_EQ(1, sign);
247   CHECK_EQ("2147483648", buffer.start());
248   CHECK_EQ(10, point);
249 
250   DoubleToAscii(-2147483648.0, PRECISION, 5,
251                 buffer, &sign, &length, &point);
252   CHECK_GE(5, length);
253   TrimRepresentation(buffer);
254   CHECK_EQ(1, sign);
255   CHECK_EQ("21475", buffer.start());
256   CHECK_EQ(10, point);
257 
258   DoubleToAscii(-3.5844466002796428e+298, SHORTEST, 0,
259                 buffer, &sign, &length, &point);
260   CHECK_EQ(1, sign);
261   CHECK_EQ("35844466002796428", buffer.start());
262   CHECK_EQ(299, point);
263 
264   DoubleToAscii(-3.5844466002796428e+298, PRECISION, 10,
265                 buffer, &sign, &length, &point);
266   CHECK_EQ(1, sign);
267   CHECK_GE(10, length);
268   TrimRepresentation(buffer);
269   CHECK_EQ("35844466", buffer.start());
270   CHECK_EQ(299, point);
271 
272   uint64_t smallest_normal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
273   double v = Double(smallest_normal64).value();
274   DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
275   CHECK_EQ("22250738585072014", buffer.start());
276   CHECK_EQ(-307, point);
277 
278   uint32_t smallest_normal32 = 0x00800000;
279   float f = Single(smallest_normal32).value();
280   DoubleToAscii(f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
281   CHECK_EQ("11754944", buffer.start());
282   CHECK_EQ(-37, point);
283 
284   DoubleToAscii(v, PRECISION, 20, buffer, &sign, &length, &point);
285   CHECK_GE(20, length);
286   TrimRepresentation(buffer);
287   CHECK_EQ("22250738585072013831", buffer.start());
288   CHECK_EQ(-307, point);
289 
290   uint64_t largest_denormal64 = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
291   v = Double(largest_denormal64).value();
292   DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
293   CHECK_EQ("2225073858507201", buffer.start());
294   CHECK_EQ(-307, point);
295 
296   uint32_t largest_denormal32 = 0x007FFFFF;
297   f = Single(largest_denormal32).value();
298   DoubleToAscii(f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
299   CHECK_EQ("11754942", buffer.start());
300   CHECK_EQ(-37, point);
301 
302   DoubleToAscii(v, PRECISION, 20, buffer, &sign, &length, &point);
303   CHECK_GE(20, length);
304   TrimRepresentation(buffer);
305   CHECK_EQ("2225073858507200889", buffer.start());
306   CHECK_EQ(-307, point);
307 
308   DoubleToAscii(4128420500802942e-24, SHORTEST, 0,
309                 buffer, &sign, &length, &point);
310   CHECK_EQ(0, sign);
311   CHECK_EQ("4128420500802942", buffer.start());
312   CHECK_EQ(-8, point);
313 
314   v = -3.9292015898194142585311918e-10;
315   DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
316   CHECK_EQ("39292015898194143", buffer.start());
317 
318   f = -3.9292015898194142585311918e-10f;
319   DoubleToAscii(f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
320   CHECK_EQ("39292017", buffer.start());
321 
322   v = 4194304.0;
323   DoubleToAscii(v, FIXED, 5, buffer, &sign, &length, &point);
324   CHECK_GE(5, length - point);
325   TrimRepresentation(buffer);
326   CHECK_EQ("4194304", buffer.start());
327 
328   v = 3.3161339052167390562200598e-237;
329   DoubleToAscii(v, PRECISION, 19, buffer, &sign, &length, &point);
330   CHECK_GE(19, length);
331   TrimRepresentation(buffer);
332   CHECK_EQ("3316133905216739056", buffer.start());
333   CHECK_EQ(-236, point);
334 }
335 
336 
TEST(DtoaSign)337 TEST(DtoaSign) {
338   char buffer_container[kBufferSize];
339   Vector<char> buffer(buffer_container, kBufferSize);
340   bool sign;
341   int length;
342   int point;
343 
344   DoubleToAscii(0.0, SHORTEST, 0, buffer, &sign, &length, &point);
345   CHECK(!sign);
346 
347   DoubleToAscii(-0.0, SHORTEST, 0, buffer, &sign, &length, &point);
348   CHECK(sign);
349 
350   DoubleToAscii(1.0, SHORTEST, 0, buffer, &sign, &length, &point);
351   CHECK(!sign);
352 
353   DoubleToAscii(-1.0, SHORTEST, 0, buffer, &sign, &length, &point);
354   CHECK(sign);
355 
356   DoubleToAscii(0.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
357   CHECK(!sign);
358 
359   DoubleToAscii(-0.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
360   CHECK(sign);
361 
362   DoubleToAscii(1.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
363   CHECK(!sign);
364 
365   DoubleToAscii(-1.0f, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
366   CHECK(sign);
367 
368   DoubleToAscii(0.0, PRECISION, 1, buffer, &sign, &length, &point);
369   CHECK(!sign);
370 
371   DoubleToAscii(-0.0, PRECISION, 1, buffer, &sign, &length, &point);
372   CHECK(sign);
373 
374   DoubleToAscii(1.0, PRECISION, 1, buffer, &sign, &length, &point);
375   CHECK(!sign);
376 
377   DoubleToAscii(-1.0, PRECISION, 1, buffer, &sign, &length, &point);
378   CHECK(sign);
379 
380   DoubleToAscii(0.0, FIXED, 1, buffer, &sign, &length, &point);
381   CHECK(!sign);
382 
383   DoubleToAscii(-0.0, FIXED, 1, buffer, &sign, &length, &point);
384   CHECK(sign);
385 
386   DoubleToAscii(1.0, FIXED, 1, buffer, &sign, &length, &point);
387   CHECK(!sign);
388 
389   DoubleToAscii(-1.0, FIXED, 1, buffer, &sign, &length, &point);
390   CHECK(sign);
391 }
392 
393 
TEST(DtoaCorners)394 TEST(DtoaCorners) {
395   char buffer_container[kBufferSize];
396   Vector<char> buffer(buffer_container, kBufferSize);
397   bool sign;
398   int length;
399   int point;
400 
401   DoubleToAscii(0.0, PRECISION, 0, buffer, &sign, &length, &point);
402   CHECK_EQ(0, length);
403   CHECK_EQ("", buffer.start());
404   CHECK(!sign);
405 
406   DoubleToAscii(1.0, PRECISION, 0, buffer, &sign, &length, &point);
407   CHECK_EQ(0, length);
408   CHECK_EQ("", buffer.start());
409   CHECK(!sign);
410 
411   DoubleToAscii(0.0, FIXED, 0, buffer, &sign, &length, &point);
412   CHECK_EQ(1, length);
413   CHECK_EQ("0", buffer.start());
414   CHECK(!sign);
415 
416   DoubleToAscii(1.0, FIXED, 0, buffer, &sign, &length, &point);
417   CHECK_EQ(1, length);
418   CHECK_EQ("1", buffer.start());
419   CHECK(!sign);
420 }
421 
422 
TEST(DtoaGayShortest)423 TEST(DtoaGayShortest) {
424   char buffer_container[kBufferSize];
425   Vector<char> buffer(buffer_container, kBufferSize);
426   bool sign;
427   int length;
428   int point;
429 
430   Vector<const PrecomputedShortest> precomputed =
431       PrecomputedShortestRepresentations();
432   for (int i = 0; i < precomputed.length(); ++i) {
433     const PrecomputedShortest current_test = precomputed[i];
434     double v = current_test.v;
435     DoubleToAscii(v, SHORTEST, 0, buffer, &sign, &length, &point);
436     CHECK(!sign);  // All precomputed numbers are positive.
437     CHECK_EQ(current_test.decimal_point, point);
438     CHECK_EQ(current_test.representation, buffer.start());
439   }
440 }
441 
442 
TEST(DtoaGayShortestSingle)443 TEST(DtoaGayShortestSingle) {
444   char buffer_container[kBufferSize];
445   Vector<char> buffer(buffer_container, kBufferSize);
446   bool sign;
447   int length;
448   int point;
449 
450   Vector<const PrecomputedShortestSingle> precomputed =
451       PrecomputedShortestSingleRepresentations();
452   for (int i = 0; i < precomputed.length(); ++i) {
453     const PrecomputedShortestSingle current_test = precomputed[i];
454     float v = current_test.v;
455     DoubleToAscii(v, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
456     CHECK(!sign);  // All precomputed numbers are positive.
457     CHECK_EQ(current_test.decimal_point, point);
458     CHECK_EQ(current_test.representation, buffer.start());
459   }
460 }
461 
462 
TEST(DtoaGayFixed)463 TEST(DtoaGayFixed) {
464   char buffer_container[kBufferSize];
465   Vector<char> buffer(buffer_container, kBufferSize);
466   bool sign;
467   int length;
468   int point;
469 
470   Vector<const PrecomputedFixed> precomputed =
471       PrecomputedFixedRepresentations();
472   for (int i = 0; i < precomputed.length(); ++i) {
473     const PrecomputedFixed current_test = precomputed[i];
474     double v = current_test.v;
475     int number_digits = current_test.number_digits;
476     DoubleToAscii(v, FIXED, number_digits, buffer, &sign, &length, &point);
477     CHECK(!sign);  // All precomputed numbers are positive.
478     CHECK_EQ(current_test.decimal_point, point);
479     CHECK_GE(number_digits, length - point);
480     TrimRepresentation(buffer);
481     CHECK_EQ(current_test.representation, buffer.start());
482   }
483 }
484 
485 
TEST(DtoaGayPrecision)486 TEST(DtoaGayPrecision) {
487   char buffer_container[kBufferSize];
488   Vector<char> buffer(buffer_container, kBufferSize);
489   bool sign;
490   int length;
491   int point;
492 
493   Vector<const PrecomputedPrecision> precomputed =
494       PrecomputedPrecisionRepresentations();
495   for (int i = 0; i < precomputed.length(); ++i) {
496     const PrecomputedPrecision current_test = precomputed[i];
497     double v = current_test.v;
498     int number_digits = current_test.number_digits;
499     DoubleToAscii(v, PRECISION, number_digits,
500                   buffer, &sign, &length, &point);
501     CHECK(!sign);  // All precomputed numbers are positive.
502     CHECK_EQ(current_test.decimal_point, point);
503     CHECK_GE(number_digits, length);
504     TrimRepresentation(buffer);
505     CHECK_EQ(current_test.representation, buffer.start());
506   }
507 }
508