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