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

Side by Side Diff: test/cctest/test-strtod.cc

Issue 3519017: Weed out extreme exponents in strtod. (Closed)
Patch Set: Remove unnecessary include from test-strtod.cc. Created 10 years, 2 months 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
« no previous file with comments | « src/strtod.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 2
3 #include <stdlib.h> 3 #include <stdlib.h>
4 4
5 #include "v8.h" 5 #include "v8.h"
6 6
7 #include "platform.h"
8 #include "cctest.h" 7 #include "cctest.h"
9 #include "strtod.h" 8 #include "strtod.h"
10 9
11 using namespace v8::internal; 10 using namespace v8::internal;
12 11
13 static const int kBufferSize = 100; 12 static Vector<const char> StringToVector(const char* str) {
13 return Vector<const char>(str, StrLength(str));
14 }
15
16
17 static double StrtodChar(const char* str, int exponent) {
18 return Strtod(StringToVector(str), exponent);
19 }
14 20
15 21
16 TEST(Strtod) { 22 TEST(Strtod) {
17 char buffer_array[kBufferSize]; 23 Vector<const char> vector;
18 Vector<char> buffer(buffer_array, kBufferSize);
19 Vector<char> vector;
20 24
21 OS::StrNCpy(buffer, "0", kBufferSize); 25 vector = StringToVector("0");
22 vector = Vector<char>(buffer_array, strlen(buffer_array));
23 CHECK_EQ(0.0, Strtod(vector, 1)); 26 CHECK_EQ(0.0, Strtod(vector, 1));
24 CHECK_EQ(0.0, Strtod(vector, 2)); 27 CHECK_EQ(0.0, Strtod(vector, 2));
25 CHECK_EQ(0.0, Strtod(vector, -2)); 28 CHECK_EQ(0.0, Strtod(vector, -2));
26 CHECK_EQ(0.0, Strtod(vector, -999)); 29 CHECK_EQ(0.0, Strtod(vector, -999));
27 CHECK_EQ(0.0, Strtod(vector, +999)); 30 CHECK_EQ(0.0, Strtod(vector, +999));
28 31
29 OS::StrNCpy(buffer, "1", kBufferSize); 32 vector = StringToVector("1");
30 vector = Vector<char>(buffer_array, strlen(buffer_array));
31 CHECK_EQ(1.0, Strtod(vector, 0)); 33 CHECK_EQ(1.0, Strtod(vector, 0));
32 CHECK_EQ(10.0, Strtod(vector, 1)); 34 CHECK_EQ(10.0, Strtod(vector, 1));
33 CHECK_EQ(100.0, Strtod(vector, 2)); 35 CHECK_EQ(100.0, Strtod(vector, 2));
34 CHECK_EQ(1e20, Strtod(vector, 20)); 36 CHECK_EQ(1e20, Strtod(vector, 20));
35 CHECK_EQ(1e22, Strtod(vector, 22)); 37 CHECK_EQ(1e22, Strtod(vector, 22));
36 CHECK_EQ(1e23, Strtod(vector, 23)); 38 CHECK_EQ(1e23, Strtod(vector, 23));
37 CHECK_EQ(1e35, Strtod(vector, 35)); 39 CHECK_EQ(1e35, Strtod(vector, 35));
38 CHECK_EQ(1e36, Strtod(vector, 36)); 40 CHECK_EQ(1e36, Strtod(vector, 36));
39 CHECK_EQ(1e37, Strtod(vector, 37)); 41 CHECK_EQ(1e37, Strtod(vector, 37));
40 CHECK_EQ(1e-1, Strtod(vector, -1)); 42 CHECK_EQ(1e-1, Strtod(vector, -1));
41 CHECK_EQ(1e-2, Strtod(vector, -2)); 43 CHECK_EQ(1e-2, Strtod(vector, -2));
42 CHECK_EQ(1e-5, Strtod(vector, -5)); 44 CHECK_EQ(1e-5, Strtod(vector, -5));
43 CHECK_EQ(1e-20, Strtod(vector, -20)); 45 CHECK_EQ(1e-20, Strtod(vector, -20));
44 CHECK_EQ(1e-22, Strtod(vector, -22)); 46 CHECK_EQ(1e-22, Strtod(vector, -22));
45 CHECK_EQ(1e-23, Strtod(vector, -23)); 47 CHECK_EQ(1e-23, Strtod(vector, -23));
46 CHECK_EQ(1e-25, Strtod(vector, -25)); 48 CHECK_EQ(1e-25, Strtod(vector, -25));
47 CHECK_EQ(1e-39, Strtod(vector, -39)); 49 CHECK_EQ(1e-39, Strtod(vector, -39));
48 50
49 OS::StrNCpy(buffer, "2", kBufferSize); 51 vector = StringToVector("2");
50 vector = Vector<char>(buffer_array, strlen(buffer_array));
51 CHECK_EQ(2.0, Strtod(vector, 0)); 52 CHECK_EQ(2.0, Strtod(vector, 0));
52 CHECK_EQ(20.0, Strtod(vector, 1)); 53 CHECK_EQ(20.0, Strtod(vector, 1));
53 CHECK_EQ(200.0, Strtod(vector, 2)); 54 CHECK_EQ(200.0, Strtod(vector, 2));
54 CHECK_EQ(2e20, Strtod(vector, 20)); 55 CHECK_EQ(2e20, Strtod(vector, 20));
55 CHECK_EQ(2e22, Strtod(vector, 22)); 56 CHECK_EQ(2e22, Strtod(vector, 22));
56 CHECK_EQ(2e23, Strtod(vector, 23)); 57 CHECK_EQ(2e23, Strtod(vector, 23));
57 CHECK_EQ(2e35, Strtod(vector, 35)); 58 CHECK_EQ(2e35, Strtod(vector, 35));
58 CHECK_EQ(2e36, Strtod(vector, 36)); 59 CHECK_EQ(2e36, Strtod(vector, 36));
59 CHECK_EQ(2e37, Strtod(vector, 37)); 60 CHECK_EQ(2e37, Strtod(vector, 37));
60 CHECK_EQ(2e-1, Strtod(vector, -1)); 61 CHECK_EQ(2e-1, Strtod(vector, -1));
61 CHECK_EQ(2e-2, Strtod(vector, -2)); 62 CHECK_EQ(2e-2, Strtod(vector, -2));
62 CHECK_EQ(2e-5, Strtod(vector, -5)); 63 CHECK_EQ(2e-5, Strtod(vector, -5));
63 CHECK_EQ(2e-20, Strtod(vector, -20)); 64 CHECK_EQ(2e-20, Strtod(vector, -20));
64 CHECK_EQ(2e-22, Strtod(vector, -22)); 65 CHECK_EQ(2e-22, Strtod(vector, -22));
65 CHECK_EQ(2e-23, Strtod(vector, -23)); 66 CHECK_EQ(2e-23, Strtod(vector, -23));
66 CHECK_EQ(2e-25, Strtod(vector, -25)); 67 CHECK_EQ(2e-25, Strtod(vector, -25));
67 CHECK_EQ(2e-39, Strtod(vector, -39)); 68 CHECK_EQ(2e-39, Strtod(vector, -39));
68 69
69 OS::StrNCpy(buffer, "9", kBufferSize); 70 vector = StringToVector("9");
70 vector = Vector<char>(buffer_array, strlen(buffer_array));
71 CHECK_EQ(9.0, Strtod(vector, 0)); 71 CHECK_EQ(9.0, Strtod(vector, 0));
72 CHECK_EQ(90.0, Strtod(vector, 1)); 72 CHECK_EQ(90.0, Strtod(vector, 1));
73 CHECK_EQ(900.0, Strtod(vector, 2)); 73 CHECK_EQ(900.0, Strtod(vector, 2));
74 CHECK_EQ(9e20, Strtod(vector, 20)); 74 CHECK_EQ(9e20, Strtod(vector, 20));
75 CHECK_EQ(9e22, Strtod(vector, 22)); 75 CHECK_EQ(9e22, Strtod(vector, 22));
76 CHECK_EQ(9e23, Strtod(vector, 23)); 76 CHECK_EQ(9e23, Strtod(vector, 23));
77 CHECK_EQ(9e35, Strtod(vector, 35)); 77 CHECK_EQ(9e35, Strtod(vector, 35));
78 CHECK_EQ(9e36, Strtod(vector, 36)); 78 CHECK_EQ(9e36, Strtod(vector, 36));
79 CHECK_EQ(9e37, Strtod(vector, 37)); 79 CHECK_EQ(9e37, Strtod(vector, 37));
80 CHECK_EQ(9e-1, Strtod(vector, -1)); 80 CHECK_EQ(9e-1, Strtod(vector, -1));
81 CHECK_EQ(9e-2, Strtod(vector, -2)); 81 CHECK_EQ(9e-2, Strtod(vector, -2));
82 CHECK_EQ(9e-5, Strtod(vector, -5)); 82 CHECK_EQ(9e-5, Strtod(vector, -5));
83 CHECK_EQ(9e-20, Strtod(vector, -20)); 83 CHECK_EQ(9e-20, Strtod(vector, -20));
84 CHECK_EQ(9e-22, Strtod(vector, -22)); 84 CHECK_EQ(9e-22, Strtod(vector, -22));
85 CHECK_EQ(9e-23, Strtod(vector, -23)); 85 CHECK_EQ(9e-23, Strtod(vector, -23));
86 CHECK_EQ(9e-25, Strtod(vector, -25)); 86 CHECK_EQ(9e-25, Strtod(vector, -25));
87 CHECK_EQ(9e-39, Strtod(vector, -39)); 87 CHECK_EQ(9e-39, Strtod(vector, -39));
88 88
89 OS::StrNCpy(buffer, "12345", kBufferSize); 89 vector = StringToVector("12345");
90 vector = Vector<char>(buffer_array, strlen(buffer_array));
91 CHECK_EQ(12345.0, Strtod(vector, 0)); 90 CHECK_EQ(12345.0, Strtod(vector, 0));
92 CHECK_EQ(123450.0, Strtod(vector, 1)); 91 CHECK_EQ(123450.0, Strtod(vector, 1));
93 CHECK_EQ(1234500.0, Strtod(vector, 2)); 92 CHECK_EQ(1234500.0, Strtod(vector, 2));
94 CHECK_EQ(12345e20, Strtod(vector, 20)); 93 CHECK_EQ(12345e20, Strtod(vector, 20));
95 CHECK_EQ(12345e22, Strtod(vector, 22)); 94 CHECK_EQ(12345e22, Strtod(vector, 22));
96 CHECK_EQ(12345e23, Strtod(vector, 23)); 95 CHECK_EQ(12345e23, Strtod(vector, 23));
97 CHECK_EQ(12345e30, Strtod(vector, 30)); 96 CHECK_EQ(12345e30, Strtod(vector, 30));
98 CHECK_EQ(12345e31, Strtod(vector, 31)); 97 CHECK_EQ(12345e31, Strtod(vector, 31));
99 CHECK_EQ(12345e32, Strtod(vector, 32)); 98 CHECK_EQ(12345e32, Strtod(vector, 32));
100 CHECK_EQ(12345e35, Strtod(vector, 35)); 99 CHECK_EQ(12345e35, Strtod(vector, 35));
101 CHECK_EQ(12345e36, Strtod(vector, 36)); 100 CHECK_EQ(12345e36, Strtod(vector, 36));
102 CHECK_EQ(12345e37, Strtod(vector, 37)); 101 CHECK_EQ(12345e37, Strtod(vector, 37));
103 CHECK_EQ(12345e-1, Strtod(vector, -1)); 102 CHECK_EQ(12345e-1, Strtod(vector, -1));
104 CHECK_EQ(12345e-2, Strtod(vector, -2)); 103 CHECK_EQ(12345e-2, Strtod(vector, -2));
105 CHECK_EQ(12345e-5, Strtod(vector, -5)); 104 CHECK_EQ(12345e-5, Strtod(vector, -5));
106 CHECK_EQ(12345e-20, Strtod(vector, -20)); 105 CHECK_EQ(12345e-20, Strtod(vector, -20));
107 CHECK_EQ(12345e-22, Strtod(vector, -22)); 106 CHECK_EQ(12345e-22, Strtod(vector, -22));
108 CHECK_EQ(12345e-23, Strtod(vector, -23)); 107 CHECK_EQ(12345e-23, Strtod(vector, -23));
109 CHECK_EQ(12345e-25, Strtod(vector, -25)); 108 CHECK_EQ(12345e-25, Strtod(vector, -25));
110 CHECK_EQ(12345e-39, Strtod(vector, -39)); 109 CHECK_EQ(12345e-39, Strtod(vector, -39));
111 110
112 OS::StrNCpy(buffer, "12345678901234", kBufferSize); 111 vector = StringToVector("12345678901234");
113 vector = Vector<char>(buffer_array, strlen(buffer_array));
114 CHECK_EQ(12345678901234.0, Strtod(vector, 0)); 112 CHECK_EQ(12345678901234.0, Strtod(vector, 0));
115 CHECK_EQ(123456789012340.0, Strtod(vector, 1)); 113 CHECK_EQ(123456789012340.0, Strtod(vector, 1));
116 CHECK_EQ(1234567890123400.0, Strtod(vector, 2)); 114 CHECK_EQ(1234567890123400.0, Strtod(vector, 2));
117 CHECK_EQ(12345678901234e20, Strtod(vector, 20)); 115 CHECK_EQ(12345678901234e20, Strtod(vector, 20));
118 CHECK_EQ(12345678901234e22, Strtod(vector, 22)); 116 CHECK_EQ(12345678901234e22, Strtod(vector, 22));
119 CHECK_EQ(12345678901234e23, Strtod(vector, 23)); 117 CHECK_EQ(12345678901234e23, Strtod(vector, 23));
120 CHECK_EQ(12345678901234e30, Strtod(vector, 30)); 118 CHECK_EQ(12345678901234e30, Strtod(vector, 30));
121 CHECK_EQ(12345678901234e31, Strtod(vector, 31)); 119 CHECK_EQ(12345678901234e31, Strtod(vector, 31));
122 CHECK_EQ(12345678901234e32, Strtod(vector, 32)); 120 CHECK_EQ(12345678901234e32, Strtod(vector, 32));
123 CHECK_EQ(12345678901234e35, Strtod(vector, 35)); 121 CHECK_EQ(12345678901234e35, Strtod(vector, 35));
124 CHECK_EQ(12345678901234e36, Strtod(vector, 36)); 122 CHECK_EQ(12345678901234e36, Strtod(vector, 36));
125 CHECK_EQ(12345678901234e37, Strtod(vector, 37)); 123 CHECK_EQ(12345678901234e37, Strtod(vector, 37));
126 CHECK_EQ(12345678901234e-1, Strtod(vector, -1)); 124 CHECK_EQ(12345678901234e-1, Strtod(vector, -1));
127 CHECK_EQ(12345678901234e-2, Strtod(vector, -2)); 125 CHECK_EQ(12345678901234e-2, Strtod(vector, -2));
128 CHECK_EQ(12345678901234e-5, Strtod(vector, -5)); 126 CHECK_EQ(12345678901234e-5, Strtod(vector, -5));
129 CHECK_EQ(12345678901234e-20, Strtod(vector, -20)); 127 CHECK_EQ(12345678901234e-20, Strtod(vector, -20));
130 CHECK_EQ(12345678901234e-22, Strtod(vector, -22)); 128 CHECK_EQ(12345678901234e-22, Strtod(vector, -22));
131 CHECK_EQ(12345678901234e-23, Strtod(vector, -23)); 129 CHECK_EQ(12345678901234e-23, Strtod(vector, -23));
132 CHECK_EQ(12345678901234e-25, Strtod(vector, -25)); 130 CHECK_EQ(12345678901234e-25, Strtod(vector, -25));
133 CHECK_EQ(12345678901234e-39, Strtod(vector, -39)); 131 CHECK_EQ(12345678901234e-39, Strtod(vector, -39));
134 132
135 OS::StrNCpy(buffer, "123456789012345", kBufferSize); 133 vector = StringToVector("123456789012345");
136 vector = Vector<char>(buffer_array, strlen(buffer_array));
137 CHECK_EQ(123456789012345.0, Strtod(vector, 0)); 134 CHECK_EQ(123456789012345.0, Strtod(vector, 0));
138 CHECK_EQ(1234567890123450.0, Strtod(vector, 1)); 135 CHECK_EQ(1234567890123450.0, Strtod(vector, 1));
139 CHECK_EQ(12345678901234500.0, Strtod(vector, 2)); 136 CHECK_EQ(12345678901234500.0, Strtod(vector, 2));
140 CHECK_EQ(123456789012345e20, Strtod(vector, 20)); 137 CHECK_EQ(123456789012345e20, Strtod(vector, 20));
141 CHECK_EQ(123456789012345e22, Strtod(vector, 22)); 138 CHECK_EQ(123456789012345e22, Strtod(vector, 22));
142 CHECK_EQ(123456789012345e23, Strtod(vector, 23)); 139 CHECK_EQ(123456789012345e23, Strtod(vector, 23));
143 CHECK_EQ(123456789012345e35, Strtod(vector, 35)); 140 CHECK_EQ(123456789012345e35, Strtod(vector, 35));
144 CHECK_EQ(123456789012345e36, Strtod(vector, 36)); 141 CHECK_EQ(123456789012345e36, Strtod(vector, 36));
145 CHECK_EQ(123456789012345e37, Strtod(vector, 37)); 142 CHECK_EQ(123456789012345e37, Strtod(vector, 37));
146 CHECK_EQ(123456789012345e39, Strtod(vector, 39)); 143 CHECK_EQ(123456789012345e39, Strtod(vector, 39));
147 CHECK_EQ(123456789012345e-1, Strtod(vector, -1)); 144 CHECK_EQ(123456789012345e-1, Strtod(vector, -1));
148 CHECK_EQ(123456789012345e-2, Strtod(vector, -2)); 145 CHECK_EQ(123456789012345e-2, Strtod(vector, -2));
149 CHECK_EQ(123456789012345e-5, Strtod(vector, -5)); 146 CHECK_EQ(123456789012345e-5, Strtod(vector, -5));
150 CHECK_EQ(123456789012345e-20, Strtod(vector, -20)); 147 CHECK_EQ(123456789012345e-20, Strtod(vector, -20));
151 CHECK_EQ(123456789012345e-22, Strtod(vector, -22)); 148 CHECK_EQ(123456789012345e-22, Strtod(vector, -22));
152 CHECK_EQ(123456789012345e-23, Strtod(vector, -23)); 149 CHECK_EQ(123456789012345e-23, Strtod(vector, -23));
153 CHECK_EQ(123456789012345e-25, Strtod(vector, -25)); 150 CHECK_EQ(123456789012345e-25, Strtod(vector, -25));
154 CHECK_EQ(123456789012345e-39, Strtod(vector, -39)); 151 CHECK_EQ(123456789012345e-39, Strtod(vector, -39));
152
153 CHECK_EQ(0.0, StrtodChar("0", 12345));
154 CHECK_EQ(0.0, StrtodChar("", 1324));
155 CHECK_EQ(0.0, StrtodChar("000000000", 123));
156 CHECK_EQ(0.0, StrtodChar("2", -324));
157 CHECK_EQ(3e-324, StrtodChar("3", -324));
158 // It would be more readable to put non-zero literals on the left side (i.e.
159 // CHECK_EQ(1e-325, StrtodChar("1", -325))), but then Gcc complains that
160 // they are truncated to zero.
161 CHECK_EQ(0.0, StrtodChar("1", -325));
162 CHECK_EQ(0.0, StrtodChar("1", -325));
163 CHECK_EQ(0.0, StrtodChar("20000", -328));
164 CHECK_EQ(30000e-328, StrtodChar("30000", -328));
165 CHECK_EQ(0.0, StrtodChar("10000", -329));
166 CHECK_EQ(0.0, StrtodChar("90000", -329));
167 CHECK_EQ(0.0, StrtodChar("000000001", -325));
168 CHECK_EQ(0.0, StrtodChar("000000001", -325));
169 CHECK_EQ(0.0, StrtodChar("0000000020000", -328));
170 CHECK_EQ(30000e-328, StrtodChar("00000030000", -328));
171 CHECK_EQ(0.0, StrtodChar("0000000010000", -329));
172 CHECK_EQ(0.0, StrtodChar("0000000090000", -329));
173
174 // It would be more readable to put the literals (and not V8_INFINITY) on the
175 // left side (i.e. CHECK_EQ(1e309, StrtodChar("1", 309))), but then Gcc
176 // complains that the floating constant exceeds range of 'double'.
177 CHECK_EQ(V8_INFINITY, StrtodChar("1", 309));
178 CHECK_EQ(1e308, StrtodChar("1", 308));
179 CHECK_EQ(1234e305, StrtodChar("1234", 305));
180 CHECK_EQ(1234e304, StrtodChar("1234", 304));
181 CHECK_EQ(V8_INFINITY, StrtodChar("18", 307));
182 CHECK_EQ(17e307, StrtodChar("17", 307));
183 CHECK_EQ(V8_INFINITY, StrtodChar("0000001", 309));
184 CHECK_EQ(1e308, StrtodChar("00000001", 308));
185 CHECK_EQ(1234e305, StrtodChar("00000001234", 305));
186 CHECK_EQ(1234e304, StrtodChar("000000001234", 304));
187 CHECK_EQ(V8_INFINITY, StrtodChar("0000000018", 307));
188 CHECK_EQ(17e307, StrtodChar("0000000017", 307));
189 CHECK_EQ(V8_INFINITY, StrtodChar("1000000", 303));
190 CHECK_EQ(1e308, StrtodChar("100000", 303));
191 CHECK_EQ(1234e305, StrtodChar("123400000", 300));
192 CHECK_EQ(1234e304, StrtodChar("123400000", 299));
193 CHECK_EQ(V8_INFINITY, StrtodChar("180000000", 300));
194 CHECK_EQ(17e307, StrtodChar("170000000", 300));
195 CHECK_EQ(V8_INFINITY, StrtodChar("00000001000000", 303));
196 CHECK_EQ(1e308, StrtodChar("000000000000100000", 303));
197 CHECK_EQ(1234e305, StrtodChar("00000000123400000", 300));
198 CHECK_EQ(1234e304, StrtodChar("0000000123400000", 299));
199 CHECK_EQ(V8_INFINITY, StrtodChar("00000000180000000", 300));
200 CHECK_EQ(17e307, StrtodChar("00000000170000000", 300));
155 } 201 }
OLDNEW
« no previous file with comments | « src/strtod.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698