Index: test/cctest/test-strtod.cc |
diff --git a/test/cctest/test-strtod.cc b/test/cctest/test-strtod.cc |
index 56b26ea1eb90f8526431bcfb8cd4ef473dd52695..34c474ff16e51e2a4076fe15a1334df095e3a8b9 100644 |
--- a/test/cctest/test-strtod.cc |
+++ b/test/cctest/test-strtod.cc |
@@ -4,7 +4,10 @@ |
#include "v8.h" |
+#include "bignum.h" |
#include "cctest.h" |
+#include "diy-fp.h" |
+#include "double.h" |
#include "strtod.h" |
using namespace v8::internal; |
@@ -202,11 +205,14 @@ TEST(Strtod) { |
CHECK_EQ(1.7976931348623158E+308, StrtodChar("17976931348623158", 292)); |
CHECK_EQ(V8_INFINITY, StrtodChar("17976931348623159", 292)); |
- // The following number is the result of 89255.0/1e-22. Both floating-point |
+ // The following number is the result of 89255.0/1e22. Both floating-point |
// numbers can be accurately represented with doubles. However on Linux,x86 |
// the floating-point stack is set to 80bits and the double-rounding |
// introduces an error. |
CHECK_EQ(89255e-22, StrtodChar("89255", -22)); |
+ |
+ // Some random values. |
+ CHECK_EQ(358416272e-33, StrtodChar("358416272", -33)); |
CHECK_EQ(104110013277974872254e-225, |
StrtodChar("104110013277974872254", -225)); |
@@ -252,4 +258,158 @@ TEST(Strtod) { |
StrtodChar("1234567890123456789052345", 114)); |
CHECK_EQ(1234567890123456789052345e115, |
StrtodChar("1234567890123456789052345", 115)); |
+ |
+ // Boundary cases. |
+ // 0x1FFFFFFFFFFFF * 2^3 = 72057594037927928 |
+ // next: 72057594037927936 |
+ // boundary: 72057594037927932 |
+ CHECK_EQ(72057594037927928.0, StrtodChar("72057594037927928", 0)); |
+ CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927936", 0)); |
+ CHECK_EQ(72057594037927936.0, StrtodChar("72057594037927932", 0)); |
+ CHECK_EQ(72057594037927928.0, StrtodChar("7205759403792793199999", -5)); |
+ CHECK_EQ(72057594037927936.0, StrtodChar("7205759403792793200001", -5)); |
+ |
+ // 0x1FFFFFFFFFFFF * 2^10 = 9223372036854774784 |
+ // next: 9223372036854775808 |
+ // boundary: 9223372036854775296 |
+ CHECK_EQ(9223372036854774784.0, StrtodChar("9223372036854774784", 0)); |
+ CHECK_EQ(9223372036854775808.0, StrtodChar("9223372036854775808", 0)); |
+ CHECK_EQ(9223372036854775296.0, StrtodChar("9223372036854775296", 0)); |
+ CHECK_EQ(9223372036854774784.0, StrtodChar("922337203685477529599999", -5)); |
+ CHECK_EQ(9223372036854775808.0, StrtodChar("922337203685477529600001", -5)); |
+ |
+ // 0x1FFFFFFFFFFFF * 2^50 = 10141204801825834086073718800384 |
+ // next: 10141204801825835211973625643008 |
+ // boundary: 10141204801825834649023672221696 |
+ CHECK_EQ(10141204801825834086073718800384.0, |
+ StrtodChar("10141204801825834086073718800384", 0)); |
+ CHECK_EQ(10141204801825835211973625643008.0, |
+ StrtodChar("10141204801825835211973625643008", 0)); |
+ CHECK_EQ(10141204801825834649023672221696.0, |
+ StrtodChar("10141204801825834649023672221696", 0)); |
+ CHECK_EQ(10141204801825834086073718800384.0, |
+ StrtodChar("1014120480182583464902367222169599999", -5)); |
+ CHECK_EQ(10141204801825835211973625643008.0, |
+ StrtodChar("1014120480182583464902367222169600001", -5)); |
+ |
+ // 0x1FFFFFFFFFFFF * 2^99 = 5708990770823838890407843763683279797179383808 |
+ // next: 5708990770823839524233143877797980545530986496 |
+ // boundary: 5708990770823839207320493820740630171355185152 |
+ CHECK_EQ(5708990770823838890407843763683279797179383808.0, |
+ StrtodChar("5708990770823838890407843763683279797179383808", 0)); |
+ CHECK_EQ(5708990770823839524233143877797980545530986496.0, |
+ StrtodChar("5708990770823839524233143877797980545530986496", 0)); |
+ CHECK_EQ(5708990770823839207320493820740630171355185152.0, |
+ StrtodChar("5708990770823839207320493820740630171355185152", 0)); |
+ CHECK_EQ(5708990770823838890407843763683279797179383808.0, |
+ StrtodChar("5708990770823839207320493820740630171355185151999", -3)); |
+ CHECK_EQ(5708990770823839524233143877797980545530986496.0, |
+ StrtodChar("5708990770823839207320493820740630171355185152001", -3)); |
+} |
+ |
+ |
+static int CompareBignumToDiyFp(const Bignum& bignum_digits, |
+ int bignum_exponent, |
+ DiyFp diy_fp) { |
+ Bignum bignum; |
+ bignum.AssignBignum(bignum_digits); |
+ Bignum other; |
+ other.AssignUInt64(diy_fp.f()); |
+ if (bignum_exponent >= 0) { |
+ bignum.MultiplyByPowerOfTen(bignum_exponent); |
+ } else { |
+ other.MultiplyByPowerOfTen(-bignum_exponent); |
+ } |
+ if (diy_fp.e() >= 0) { |
+ other.ShiftLeft(diy_fp.e()); |
+ } else { |
+ bignum.ShiftLeft(-diy_fp.e()); |
+ } |
+ return Bignum::Compare(bignum, other); |
+} |
+ |
+ |
+static bool CheckDouble(Vector<const char> buffer, |
+ int exponent, |
+ double to_check) { |
+ DiyFp lower_boundary; |
+ DiyFp upper_boundary; |
+ Bignum input_digits; |
+ input_digits.AssignDecimalString(buffer); |
+ if (to_check == 0.0) { |
+ const double kMinDouble = 4e-324; |
+ // Check that the buffer*10^exponent < (0 + kMinDouble)/2. |
+ Double d(kMinDouble); |
+ d.NormalizedBoundaries(&lower_boundary, &upper_boundary); |
+ return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) <= 0; |
+ } |
+ if (to_check == V8_INFINITY) { |
+ const double kMaxDouble = 1.7976931348623157e308; |
+ // Check that the buffer*10^exponent >= boundary between kMaxDouble and inf. |
+ Double d(kMaxDouble); |
+ d.NormalizedBoundaries(&lower_boundary, &upper_boundary); |
+ return CompareBignumToDiyFp(input_digits, exponent, upper_boundary) >= 0; |
+ } |
+ Double d(to_check); |
+ d.NormalizedBoundaries(&lower_boundary, &upper_boundary); |
+ if ((d.Significand() & 1) == 0) { |
+ return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) >= 0 && |
+ CompareBignumToDiyFp(input_digits, exponent, upper_boundary) <= 0; |
+ } else { |
+ return CompareBignumToDiyFp(input_digits, exponent, lower_boundary) > 0 && |
+ CompareBignumToDiyFp(input_digits, exponent, upper_boundary) < 0; |
+ } |
+} |
+ |
+ |
+// Copied from v8.cc and adapted to make the function deterministic. |
+static uint32_t DeterministicRandom() { |
+ // Random number generator using George Marsaglia's MWC algorithm. |
+ static uint32_t hi = 0; |
+ static uint32_t lo = 0; |
+ |
+ // Initialization values don't have any special meaning. (They are the result |
+ // of two calls to random().) |
+ if (hi == 0) hi = 0xbfe166e7; |
+ if (lo == 0) lo = 0x64d1c3c9; |
+ |
+ // Mix the bits. |
+ hi = 36969 * (hi & 0xFFFF) + (hi >> 16); |
+ lo = 18273 * (lo & 0xFFFF) + (lo >> 16); |
+ return (hi << 16) + (lo & 0xFFFF); |
+} |
+ |
+ |
+static const int kBufferSize = 1024; |
+static const int kShortStrtodRandomCount = 2; |
+static const int kLargeStrtodRandomCount = 2; |
+ |
+TEST(RandomStrtod) { |
+ char buffer[kBufferSize]; |
+ for (int length = 1; length < 15; length++) { |
+ for (int i = 0; i < kShortStrtodRandomCount; ++i) { |
+ int pos = 0; |
+ for (int j = 0; j < length; ++j) { |
+ buffer[pos++] = random() % 10 + '0'; |
+ } |
+ int exponent = DeterministicRandom() % (25*2 + 1) - 25 - length; |
+ buffer[pos] = '\0'; |
+ Vector<const char> vector(buffer, pos); |
+ double strtod_result = Strtod(vector, exponent); |
+ CHECK(CheckDouble(vector, exponent, strtod_result)); |
+ } |
+ } |
+ for (int length = 15; length < 800; length += 2) { |
+ for (int i = 0; i < kLargeStrtodRandomCount; ++i) { |
+ int pos = 0; |
+ for (int j = 0; j < length; ++j) { |
+ buffer[pos++] = random() % 10 + '0'; |
+ } |
+ int exponent = DeterministicRandom() % (308*2 + 1) - 308 - length; |
+ buffer[pos] = '\0'; |
+ Vector<const char> vector(buffer, pos); |
+ double strtod_result = Strtod(vector, exponent); |
+ CHECK(CheckDouble(vector, exponent, strtod_result)); |
+ } |
+ } |
} |