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 "gay_shortest.h" | |
12 #include "grisu3.h" | |
13 | |
14 using namespace v8::internal; | |
15 | |
16 static const int kBufferSize = 100; | |
17 | |
18 TEST(GrisuVariousDoubles) { | |
19 char buffer[kBufferSize]; | |
20 int sign; | |
21 int length; | |
22 int point; | |
23 int status; | |
24 | |
25 double min_double = 5e-324; | |
26 status = grisu3(min_double, buffer, &sign, &length, &point); | |
27 CHECK(status); | |
28 CHECK_EQ(0, sign); | |
29 CHECK_EQ("5", buffer); | |
30 CHECK_EQ(-323, point); | |
31 | |
32 double max_double = 1.7976931348623157e308; | |
33 status = grisu3(max_double, buffer, &sign, &length, &point); | |
34 CHECK(status); | |
35 CHECK_EQ(0, sign); | |
36 CHECK_EQ("17976931348623157", buffer); | |
37 CHECK_EQ(309, point); | |
38 | |
39 status = grisu3(4294967272.0, buffer, &sign, &length, &point); | |
40 CHECK(status); | |
41 CHECK_EQ(0, sign); | |
42 CHECK_EQ("4294967272", buffer); | |
43 CHECK_EQ(10, point); | |
44 | |
45 status = grisu3(4.1855804968213567e298, buffer, &sign, &length, &point); | |
46 CHECK(status); | |
47 CHECK_EQ(0, sign); | |
48 CHECK_EQ("4185580496821357", buffer); | |
49 CHECK_EQ(299, point); | |
50 | |
51 status = grisu3(5.5626846462680035e-309, buffer, &sign, &length, &point); | |
52 CHECK(status); | |
53 CHECK_EQ(0, sign); | |
54 CHECK_EQ("5562684646268003", buffer); | |
55 CHECK_EQ(-308, point); | |
56 | |
57 status = grisu3(2147483648.0, buffer, &sign, &length, &point); | |
58 CHECK(status); | |
59 CHECK_EQ(0, sign); | |
60 CHECK_EQ("2147483648", buffer); | |
61 CHECK_EQ(10, point); | |
62 | |
63 status = grisu3(3.5844466002796428e+298, buffer, &sign, &length, &point); | |
64 if (status) { // Not all grisu3 variants manage to compute this number. | |
65 CHECK_EQ("35844466002796428", buffer); | |
66 CHECK_EQ(0, sign); | |
67 CHECK_EQ(299, point); | |
68 } | |
69 | |
70 uint64_t smallest_normal64 = V8_2PART_UINT64_C(0x00100000, 00000000); | |
71 double v = Double(smallest_normal64).value(); | |
72 status = grisu3(v, buffer, &sign, &length, &point); | |
73 if (status) { | |
74 CHECK_EQ(0, sign); | |
75 CHECK_EQ("22250738585072014", buffer); | |
76 CHECK_EQ(-307, point); | |
77 } | |
78 | |
79 uint64_t largest_denormal64 = V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF); | |
80 v = Double(largest_denormal64).value(); | |
81 status = grisu3(v, buffer, &sign, &length, &point); | |
82 if (status) { | |
83 CHECK_EQ(0, sign); | |
84 CHECK_EQ("2225073858507201", buffer); | |
85 CHECK_EQ(-307, point); | |
86 } | |
87 } | |
88 | |
89 | |
90 TEST(GrisuGayShortest) { | |
91 char buffer[kBufferSize]; | |
92 bool status; | |
93 int sign; | |
94 int length; | |
95 int point; | |
96 int succeeded = 0; | |
97 int total = 0; | |
98 bool needed_max_length = false; | |
99 | |
100 Vector<const GayShortest> precomputed = PrecomputedShortestRepresentations(); | |
101 for (int i = 0; i < precomputed.length(); ++i) { | |
102 const GayShortest current_test = precomputed[i]; | |
103 total++; | |
104 double v = current_test.v; | |
105 status = grisu3(v, buffer, &sign, &length, &point); | |
106 CHECK_GE(kGrisu3MaximalLength, length); | |
107 if (!status) continue; | |
108 if (length == kGrisu3MaximalLength) needed_max_length = true; | |
109 succeeded++; | |
110 CHECK_EQ(0, sign); // All precomputed numbers are positive. | |
111 CHECK_EQ(current_test.decimal_point, point); | |
112 CHECK_EQ(current_test.representation, buffer); | |
113 } | |
114 CHECK_GT(succeeded*1.0/total, 0.99); | |
115 CHECK(needed_max_length); | |
116 } | |
OLD | NEW |