Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: runtime/third_party/double-conversion/test/cctest/test-bignum-dtoa.cc

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

Powered by Google App Engine
This is Rietveld 408576698