v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-bignum-dtoa.cc
Go to the documentation of this file.
1 // Copyright 2011 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.
45 // Can return the empty string if all digits are 0.
46 static void TrimRepresentation(Vector<char> representation) {
47  int len = StrLength(representation.start());
48  int i;
49  for (i = len - 1; i >= 0; --i) {
50  if (representation[i] != '0') break;
51  }
52  representation[i + 1] = '\0';
53 }
54 
55 
56 static const int kBufferSize = 100;
57 
58 
59 TEST(BignumDtoaVariousDoubles) {
60  char buffer_container[kBufferSize];
61  Vector<char> buffer(buffer_container, kBufferSize);
62  int length;
63  int point;
64 
65  BignumDtoa(1.0, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
66  CHECK_EQ("1", buffer.start());
67  CHECK_EQ(1, point);
68 
69  BignumDtoa(1.0, BIGNUM_DTOA_FIXED, 3, buffer, &length, &point);
70  CHECK_GE(3, length - point);
71  TrimRepresentation(buffer);
72  CHECK_EQ("1", buffer.start());
73  CHECK_EQ(1, point);
74 
75  BignumDtoa(1.0, BIGNUM_DTOA_PRECISION, 3, buffer, &length, &point);
76  CHECK_GE(3, length);
77  TrimRepresentation(buffer);
78  CHECK_EQ("1", buffer.start());
79  CHECK_EQ(1, point);
80 
81  BignumDtoa(1.5, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
82  CHECK_EQ("15", buffer.start());
83  CHECK_EQ(1, point);
84 
85  BignumDtoa(1.5, BIGNUM_DTOA_FIXED, 10, buffer, &length, &point);
86  CHECK_GE(10, length - point);
87  TrimRepresentation(buffer);
88  CHECK_EQ("15", buffer.start());
89  CHECK_EQ(1, point);
90 
91  BignumDtoa(1.5, BIGNUM_DTOA_PRECISION, 10, buffer, &length, &point);
92  CHECK_GE(10, length);
93  TrimRepresentation(buffer);
94  CHECK_EQ("15", buffer.start());
95  CHECK_EQ(1, point);
96 
97  double min_double = 5e-324;
98  BignumDtoa(min_double, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
99  CHECK_EQ("5", buffer.start());
100  CHECK_EQ(-323, point);
101 
102  BignumDtoa(min_double, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point);
103  CHECK_GE(5, length - point);
104  TrimRepresentation(buffer);
105  CHECK_EQ("", buffer.start());
106 
107  BignumDtoa(min_double, BIGNUM_DTOA_PRECISION, 5, buffer, &length, &point);
108  CHECK_GE(5, length);
109  TrimRepresentation(buffer);
110  CHECK_EQ("49407", buffer.start());
111  CHECK_EQ(-323, point);
112 
113  double max_double = 1.7976931348623157e308;
114  BignumDtoa(max_double, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
115  CHECK_EQ("17976931348623157", buffer.start());
116  CHECK_EQ(309, point);
117 
118  BignumDtoa(max_double, BIGNUM_DTOA_PRECISION, 7, buffer, &length, &point);
119  CHECK_GE(7, length);
120  TrimRepresentation(buffer);
121  CHECK_EQ("1797693", buffer.start());
122  CHECK_EQ(309, point);
123 
124  BignumDtoa(4294967272.0, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
125  CHECK_EQ("4294967272", buffer.start());
126  CHECK_EQ(10, point);
127 
128  BignumDtoa(4294967272.0, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point);
129  CHECK_EQ("429496727200000", buffer.start());
130  CHECK_EQ(10, point);
131 
132 
133  BignumDtoa(4294967272.0, BIGNUM_DTOA_PRECISION, 14, buffer, &length, &point);
134  CHECK_GE(14, length);
135  TrimRepresentation(buffer);
136  CHECK_EQ("4294967272", buffer.start());
137  CHECK_EQ(10, point);
138 
139  BignumDtoa(4.1855804968213567e298, BIGNUM_DTOA_SHORTEST, 0,
140  buffer, &length, &point);
141  CHECK_EQ("4185580496821357", buffer.start());
142  CHECK_EQ(299, point);
143 
144  BignumDtoa(4.1855804968213567e298, BIGNUM_DTOA_PRECISION, 20,
145  buffer, &length, &point);
146  CHECK_GE(20, length);
147  TrimRepresentation(buffer);
148  CHECK_EQ("41855804968213567225", buffer.start());
149  CHECK_EQ(299, point);
150 
151  BignumDtoa(5.5626846462680035e-309, BIGNUM_DTOA_SHORTEST, 0,
152  buffer, &length, &point);
153  CHECK_EQ("5562684646268003", buffer.start());
154  CHECK_EQ(-308, point);
155 
156  BignumDtoa(5.5626846462680035e-309, BIGNUM_DTOA_PRECISION, 1,
157  buffer, &length, &point);
158  CHECK_GE(1, length);
159  TrimRepresentation(buffer);
160  CHECK_EQ("6", buffer.start());
161  CHECK_EQ(-308, point);
162 
163  BignumDtoa(2147483648.0, BIGNUM_DTOA_SHORTEST, 0,
164  buffer, &length, &point);
165  CHECK_EQ("2147483648", buffer.start());
166  CHECK_EQ(10, point);
167 
168 
169  BignumDtoa(2147483648.0, BIGNUM_DTOA_FIXED, 2,
170  buffer, &length, &point);
171  CHECK_GE(2, length - point);
172  TrimRepresentation(buffer);
173  CHECK_EQ("2147483648", buffer.start());
174  CHECK_EQ(10, point);
175 
176  BignumDtoa(2147483648.0, BIGNUM_DTOA_PRECISION, 5,
177  buffer, &length, &point);
178  CHECK_GE(5, length);
179  TrimRepresentation(buffer);
180  CHECK_EQ("21475", buffer.start());
181  CHECK_EQ(10, point);
182 
183  BignumDtoa(3.5844466002796428e+298, BIGNUM_DTOA_SHORTEST, 0,
184  buffer, &length, &point);
185  CHECK_EQ("35844466002796428", buffer.start());
186  CHECK_EQ(299, point);
187 
188  BignumDtoa(3.5844466002796428e+298, BIGNUM_DTOA_PRECISION, 10,
189  buffer, &length, &point);
190  CHECK_GE(10, length);
191  TrimRepresentation(buffer);
192  CHECK_EQ("35844466", buffer.start());
193  CHECK_EQ(299, point);
194 
195  uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000);
196  double v = Double(smallest_normal64).value();
197  BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
198  CHECK_EQ("22250738585072014", buffer.start());
199  CHECK_EQ(-307, point);
200 
201  BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point);
202  CHECK_GE(20, length);
203  TrimRepresentation(buffer);
204  CHECK_EQ("22250738585072013831", buffer.start());
205  CHECK_EQ(-307, point);
206 
207  uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
208  v = Double(largest_denormal64).value();
209  BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
210  CHECK_EQ("2225073858507201", buffer.start());
211  CHECK_EQ(-307, point);
212 
213  BignumDtoa(v, BIGNUM_DTOA_PRECISION, 20, buffer, &length, &point);
214  CHECK_GE(20, length);
215  TrimRepresentation(buffer);
216  CHECK_EQ("2225073858507200889", buffer.start());
217  CHECK_EQ(-307, point);
218 
219  BignumDtoa(4128420500802942e-24, BIGNUM_DTOA_SHORTEST, 0,
220  buffer, &length, &point);
221  CHECK_EQ("4128420500802942", buffer.start());
222  CHECK_EQ(-8, point);
223 
224  v = 3.9292015898194142585311918e-10;
225  BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
226  CHECK_EQ("39292015898194143", buffer.start());
227 
228  v = 4194304.0;
229  BignumDtoa(v, BIGNUM_DTOA_FIXED, 5, buffer, &length, &point);
230  CHECK_GE(5, length - point);
231  TrimRepresentation(buffer);
232  CHECK_EQ("4194304", buffer.start());
233 
234  v = 3.3161339052167390562200598e-237;
235  BignumDtoa(v, BIGNUM_DTOA_PRECISION, 19, buffer, &length, &point);
236  CHECK_GE(19, length);
237  TrimRepresentation(buffer);
238  CHECK_EQ("3316133905216739056", buffer.start());
239  CHECK_EQ(-236, point);
240 
241  v = 7.9885183916008099497815232e+191;
242  BignumDtoa(v, BIGNUM_DTOA_PRECISION, 4, buffer, &length, &point);
243  CHECK_GE(4, length);
244  TrimRepresentation(buffer);
245  CHECK_EQ("7989", buffer.start());
246  CHECK_EQ(192, point);
247 
248  v = 1.0000000000000012800000000e+17;
249  BignumDtoa(v, BIGNUM_DTOA_FIXED, 1, buffer, &length, &point);
250  CHECK_GE(1, length - point);
251  TrimRepresentation(buffer);
252  CHECK_EQ("100000000000000128", buffer.start());
253  CHECK_EQ(18, point);
254 }
255 
256 
257 TEST(BignumDtoaGayShortest) {
258  char buffer_container[kBufferSize];
259  Vector<char> buffer(buffer_container, kBufferSize);
260  int length;
261  int point;
262 
265  for (int i = 0; i < precomputed.length(); ++i) {
266  const PrecomputedShortest current_test = precomputed[i];
267  double v = current_test.v;
268  BignumDtoa(v, BIGNUM_DTOA_SHORTEST, 0, buffer, &length, &point);
269  CHECK_EQ(current_test.decimal_point, point);
270  CHECK_EQ(current_test.representation, buffer.start());
271  }
272 }
273 
274 
275 TEST(BignumDtoaGayFixed) {
276  char buffer_container[kBufferSize];
277  Vector<char> buffer(buffer_container, kBufferSize);
278  int length;
279  int point;
280 
281  Vector<const PrecomputedFixed> precomputed =
283  for (int i = 0; i < precomputed.length(); ++i) {
284  const PrecomputedFixed current_test = precomputed[i];
285  double v = current_test.v;
286  int number_digits = current_test.number_digits;
287  BignumDtoa(v, BIGNUM_DTOA_FIXED, number_digits, buffer, &length, &point);
288  CHECK_EQ(current_test.decimal_point, point);
289  CHECK_GE(number_digits, length - point);
290  TrimRepresentation(buffer);
291  CHECK_EQ(current_test.representation, buffer.start());
292  }
293 }
294 
295 
296 TEST(BignumDtoaGayPrecision) {
297  char buffer_container[kBufferSize];
298  Vector<char> buffer(buffer_container, kBufferSize);
299  int length;
300  int point;
301 
304  for (int i = 0; i < precomputed.length(); ++i) {
305  const PrecomputedPrecision current_test = precomputed[i];
306  double v = current_test.v;
307  int number_digits = current_test.number_digits;
308  BignumDtoa(v, BIGNUM_DTOA_PRECISION, number_digits,
309  buffer, &length, &point);
310  CHECK_EQ(current_test.decimal_point, point);
311  CHECK_GE(number_digits, length);
312  TrimRepresentation(buffer);
313  CHECK_EQ(current_test.representation, buffer.start());
314  }
315 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
Vector< const PrecomputedShortest > PrecomputedShortestRepresentations()
Vector< const PrecomputedPrecision > PrecomputedPrecisionRepresentations()
Vector< const PrecomputedFixed > PrecomputedFixedRepresentations()
#define CHECK_GE(a, b)
Definition: checks.h:228
T * start() const
Definition: utils.h:389
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
Definition: bignum-dtoa.cc:92
int length() const
Definition: utils.h:383
#define V8_2PART_UINT64_C(a, b)
Definition: globals.h:202
int StrLength(const char *string)
Definition: utils.h:234
double value() const
Definition: double.h:178