OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 |
| 3 #include <stdlib.h> |
| 4 |
| 5 #include "v8.h" |
| 6 |
| 7 #include "platform.h" |
| 8 #include "cctest.h" |
| 9 #include "diy_fp.h" |
| 10 #include "double.h" |
| 11 #include "grisu3.h" |
| 12 |
| 13 #include "test-dtoa.h" |
| 14 |
| 15 using namespace v8::internal; |
| 16 |
| 17 TEST(DoubleExtremes) { |
| 18 char buffer[kBufferSize]; |
| 19 int length; |
| 20 int sign; |
| 21 int point; |
| 22 bool status; |
| 23 double min_double = 5e-324; |
| 24 status = grisu3(min_double, buffer, &sign, &length, &point); |
| 25 CHECK(status); |
| 26 CHECK_EQ(0, sign); |
| 27 CHECK_EQ("5", buffer); |
| 28 CHECK_EQ(-323, point); |
| 29 |
| 30 double max_double = 1.7976931348623157e308; |
| 31 status = grisu3(max_double, buffer, &sign, &length, &point); |
| 32 CHECK(status); |
| 33 CHECK_EQ(0, sign); |
| 34 CHECK_EQ("17976931348623157", buffer); |
| 35 CHECK_EQ(309, point); |
| 36 } |
| 37 |
| 38 |
| 39 TEST(DoubleTestFunctions) { |
| 40 char buffer[kBufferSize]; |
| 41 |
| 42 strncpy(buffer, "12345", kBufferSize); |
| 43 CHECK(IsCorrect(123.45, buffer, 0, 5, 3)); |
| 44 strncpy(buffer, "12345", kBufferSize); |
| 45 CHECK(IsCorrect(1.2345, buffer, 0, 5, 1)); |
| 46 strncpy(buffer, "12345", kBufferSize); |
| 47 CHECK(!IsCorrect(1.2344, buffer, 0, 5, 1)); |
| 48 strncpy(buffer, "12345", kBufferSize); |
| 49 CHECK(!IsCorrect(1.2345, buffer, 0, 5, 2)); |
| 50 strncpy(buffer, "12345", kBufferSize); |
| 51 CHECK(!IsCorrect(1.2345, buffer, 0, 4, 1)); |
| 52 |
| 53 strncpy(buffer, "1234", kBufferSize); |
| 54 CHECK(IsRounded(123.44, buffer, 0, 4, 3)); |
| 55 strncpy(buffer, "1234", kBufferSize); |
| 56 CHECK(!IsRounded(123.4500000000001, buffer, 0, 4, 3)); |
| 57 strncpy(buffer, "1234", kBufferSize); |
| 58 CHECK(IsRounded(123.44999999, buffer, 0, 4, 3)); |
| 59 strncpy(buffer, "1234", kBufferSize); |
| 60 CHECK(IsRounded(123.44999999, buffer, 0, 3, 3)); |
| 61 |
| 62 strncpy(buffer, "1234567000000000000000000001", kBufferSize); |
| 63 CHECK(IsShortest(123.45, buffer, 0, 5, 3)); |
| 64 strncpy(buffer, "1234567000000000000000000001", kBufferSize); |
| 65 CHECK(IsShortest(123.4567, buffer, 0, 7, 3)); |
| 66 strncpy(buffer, "1234567000000000000000000001", kBufferSize); |
| 67 CHECK(!IsShortest(123.4567, buffer, 0, strlen(buffer), 3)); |
| 68 |
| 69 strncpy(buffer, "123456699999999999999999999999999999", kBufferSize); |
| 70 CHECK(!IsShortest(123.4567, buffer, 0, strlen(buffer), 3)); |
| 71 strncpy(buffer, "123456699999999999999999999999999999", kBufferSize); |
| 72 CHECK(IsShortest(123.456, buffer, 0, 6, 3)); |
| 73 } |
| 74 |
| 75 |
| 76 TEST(VariousDoubles) { |
| 77 char buffer[kBufferSize]; |
| 78 int sign; |
| 79 int length; |
| 80 int point; |
| 81 int status; |
| 82 status = grisu3(4294967272.0, buffer, &sign, &length, &point); |
| 83 CHECK(status); |
| 84 CHECK_EQ(0, sign); |
| 85 CHECK_EQ("4294967272", buffer); |
| 86 CHECK_EQ(10, point); |
| 87 |
| 88 status = grisu3(4.1855804968213567e298, buffer, &sign, &length, &point); |
| 89 CHECK(status); |
| 90 CHECK_EQ(0, sign); |
| 91 CHECK_EQ("4185580496821357", buffer); |
| 92 CHECK_EQ(299, point); |
| 93 |
| 94 status = grisu3(5.5626846462680035e-309, buffer, &sign, &length, &point); |
| 95 CHECK(status); |
| 96 CHECK_EQ(0, sign); |
| 97 CHECK_EQ("5562684646268003", buffer); |
| 98 CHECK_EQ(-308, point); |
| 99 |
| 100 status = grisu3(2147483648.0, buffer, &sign, &length, &point); |
| 101 CHECK(status); |
| 102 CHECK_EQ(0, sign); |
| 103 CHECK_EQ("2147483648", buffer); |
| 104 CHECK_EQ(10, point); |
| 105 |
| 106 status = grisu3(3.5844466002796428e+298, buffer, &sign, &length, &point); |
| 107 if (status) { // Not all grisu3 variants manage to compute this number. |
| 108 CHECK_EQ("35844466002796428", buffer); |
| 109 CHECK_EQ(0, sign); |
| 110 CHECK_EQ(299, point); |
| 111 } |
| 112 |
| 113 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); |
| 114 double v = Double(smallest_normal64).value(); |
| 115 status = grisu3(v, buffer, &sign, &length, &point); |
| 116 if (status) { |
| 117 CHECK_EQ(0, sign); |
| 118 CHECK(IsCorrect(v, buffer, 0, length, point)); |
| 119 CHECK(IsRounded(v, buffer, 0, length, point)); |
| 120 CHECK(IsShortest(v, buffer, 0, length, point)); |
| 121 } |
| 122 |
| 123 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); |
| 124 v = Double(largest_denormal64).value(); |
| 125 status = grisu3(v, buffer, &sign, &length, &point); |
| 126 if (status) { |
| 127 CHECK_EQ(0, sign); |
| 128 CHECK(IsCorrect(v, buffer, 0, length, point)); |
| 129 CHECK(IsRounded(v, buffer, 0, length, point)); |
| 130 CHECK(IsShortest(v, buffer, 0, length, point)); |
| 131 } |
| 132 } |
| 133 |
| 134 |
| 135 static double random_double() { |
| 136 uint64_t double64 = 0; |
| 137 for (int i = 0; i < 8; i++) { |
| 138 double64 <<= 8; |
| 139 double64 += rand() % 256; // NOLINT |
| 140 } |
| 141 return Double(double64).value(); |
| 142 } |
| 143 |
| 144 |
| 145 TEST(RandomDoubles) { |
| 146 // For a more thorough testing increase the iteration count. |
| 147 // We also check kGrisu3MaximalLength in here. |
| 148 const int kIterationCount = 100000; |
| 149 int succeeded = 0; |
| 150 int total = 0; |
| 151 char buffer[kBufferSize]; |
| 152 int length; |
| 153 int sign; |
| 154 int point; |
| 155 bool needed_max_length = false; |
| 156 |
| 157 for (int i = 0; i < kIterationCount; ++i) { |
| 158 double v = random_double(); |
| 159 if (v != v) continue; // NaN |
| 160 if (v == 0.0) continue; |
| 161 if (v < 0) v = -v; |
| 162 total++; |
| 163 int status = grisu3(v, buffer, &sign, &length, &point); |
| 164 CHECK_GE(kGrisu3MaximalLength, length); |
| 165 if (length == kGrisu3MaximalLength) needed_max_length = true; |
| 166 if (!status) continue; |
| 167 succeeded++; |
| 168 CHECK(IsCorrect(v, buffer, 0, length, point)); |
| 169 CHECK(IsRounded(v, buffer, 0, length, point)); |
| 170 CHECK(IsShortest(v, buffer, 0, length, point)); |
| 171 } |
| 172 CHECK_GT(succeeded*1.0/total, 0.99); |
| 173 CHECK(needed_max_length); |
| 174 } |
OLD | NEW |