v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-conversions.cc
Go to the documentation of this file.
1 // Copyright 2011 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 
10 using namespace v8::internal;
11 
12 
13 TEST(Hex) {
14  UnicodeCache uc;
15  CHECK_EQ(0.0, StringToDouble(&uc, "0x0", ALLOW_HEX | ALLOW_OCTALS));
16  CHECK_EQ(0.0, StringToDouble(&uc, "0X0", ALLOW_HEX | ALLOW_OCTALS));
17  CHECK_EQ(1.0, StringToDouble(&uc, "0x1", ALLOW_HEX | ALLOW_OCTALS));
18  CHECK_EQ(16.0, StringToDouble(&uc, "0x10", ALLOW_HEX | ALLOW_OCTALS));
19  CHECK_EQ(255.0, StringToDouble(&uc, "0xff", ALLOW_HEX | ALLOW_OCTALS));
20  CHECK_EQ(175.0, StringToDouble(&uc, "0xAF", ALLOW_HEX | ALLOW_OCTALS));
21 
22  CHECK_EQ(0.0, StringToDouble(&uc, "0x0", ALLOW_HEX));
23  CHECK_EQ(0.0, StringToDouble(&uc, "0X0", ALLOW_HEX));
24  CHECK_EQ(1.0, StringToDouble(&uc, "0x1", ALLOW_HEX));
25  CHECK_EQ(16.0, StringToDouble(&uc, "0x10", ALLOW_HEX));
26  CHECK_EQ(255.0, StringToDouble(&uc, "0xff", ALLOW_HEX));
27  CHECK_EQ(175.0, StringToDouble(&uc, "0xAF", ALLOW_HEX));
28 }
29 
30 
31 TEST(Octal) {
32  UnicodeCache uc;
33  CHECK_EQ(0.0, StringToDouble(&uc, "0", ALLOW_HEX | ALLOW_OCTALS));
34  CHECK_EQ(0.0, StringToDouble(&uc, "00", ALLOW_HEX | ALLOW_OCTALS));
35  CHECK_EQ(1.0, StringToDouble(&uc, "01", ALLOW_HEX | ALLOW_OCTALS));
36  CHECK_EQ(7.0, StringToDouble(&uc, "07", ALLOW_HEX | ALLOW_OCTALS));
37  CHECK_EQ(8.0, StringToDouble(&uc, "010", ALLOW_HEX | ALLOW_OCTALS));
38  CHECK_EQ(63.0, StringToDouble(&uc, "077", ALLOW_HEX | ALLOW_OCTALS));
39 
40  CHECK_EQ(0.0, StringToDouble(&uc, "0", ALLOW_HEX));
41  CHECK_EQ(0.0, StringToDouble(&uc, "00", ALLOW_HEX));
42  CHECK_EQ(1.0, StringToDouble(&uc, "01", ALLOW_HEX));
43  CHECK_EQ(7.0, StringToDouble(&uc, "07", ALLOW_HEX));
44  CHECK_EQ(10.0, StringToDouble(&uc, "010", ALLOW_HEX));
45  CHECK_EQ(77.0, StringToDouble(&uc, "077", ALLOW_HEX));
46 
47  const double x = 010000000000; // Power of 2, no rounding errors.
48  CHECK_EQ(x * x * x * x * x, StringToDouble(&uc, "01" "0000000000" "0000000000"
49  "0000000000" "0000000000" "0000000000", ALLOW_OCTALS));
50 }
51 
52 
53 TEST(MalformedOctal) {
54  UnicodeCache uc;
55  CHECK_EQ(8.0, StringToDouble(&uc, "08", ALLOW_HEX | ALLOW_OCTALS));
56  CHECK_EQ(81.0, StringToDouble(&uc, "081", ALLOW_HEX | ALLOW_OCTALS));
57  CHECK_EQ(78.0, StringToDouble(&uc, "078", ALLOW_HEX | ALLOW_OCTALS));
58 
63 
64  CHECK_EQ(8.7, StringToDouble(&uc, "08.7", ALLOW_HEX | ALLOW_OCTALS));
65  CHECK_EQ(8e7, StringToDouble(&uc, "08e7", ALLOW_HEX | ALLOW_OCTALS));
66 
67  CHECK_EQ(0.001, StringToDouble(&uc, "0.001", ALLOW_HEX | ALLOW_OCTALS));
68  CHECK_EQ(0.713, StringToDouble(&uc, "0.713", ALLOW_HEX | ALLOW_OCTALS));
69 
70  CHECK_EQ(8.0, StringToDouble(&uc, "08", ALLOW_HEX));
71  CHECK_EQ(81.0, StringToDouble(&uc, "081", ALLOW_HEX));
72  CHECK_EQ(78.0, StringToDouble(&uc, "078", ALLOW_HEX));
73 
74  CHECK_EQ(7.7, StringToDouble(&uc, "07.7", ALLOW_HEX));
75  CHECK_EQ(7.8, StringToDouble(&uc, "07.8", ALLOW_HEX));
76  CHECK_EQ(7e8, StringToDouble(&uc, "07e8", ALLOW_HEX));
77  CHECK_EQ(7e7, StringToDouble(&uc, "07e7", ALLOW_HEX));
78 
79  CHECK_EQ(8.7, StringToDouble(&uc, "08.7", ALLOW_HEX));
80  CHECK_EQ(8e7, StringToDouble(&uc, "08e7", ALLOW_HEX));
81 
82  CHECK_EQ(0.001, StringToDouble(&uc, "0.001", ALLOW_HEX));
83  CHECK_EQ(0.713, StringToDouble(&uc, "0.713", ALLOW_HEX));
84 }
85 
86 
87 TEST(TrailingJunk) {
88  UnicodeCache uc;
90  CHECK_EQ(63.0,
92  CHECK_EQ(10.0,
94  CHECK_EQ(10.0,
96 }
97 
98 
99 TEST(NonStrDecimalLiteral) {
100  UnicodeCache uc;
104  CHECK_EQ(0.0, StringToDouble(&uc, "", NO_FLAGS));
105  CHECK_EQ(0.0, StringToDouble(&uc, " ", NO_FLAGS));
106 }
107 
108 TEST(IntegerStrLiteral) {
109  UnicodeCache uc;
110  CHECK_EQ(0.0, StringToDouble(&uc, "0.0", NO_FLAGS));
111  CHECK_EQ(0.0, StringToDouble(&uc, "0", NO_FLAGS));
112  CHECK_EQ(0.0, StringToDouble(&uc, "00", NO_FLAGS));
113  CHECK_EQ(0.0, StringToDouble(&uc, "000", NO_FLAGS));
114  CHECK_EQ(1.0, StringToDouble(&uc, "1", NO_FLAGS));
115  CHECK_EQ(-1.0, StringToDouble(&uc, "-1", NO_FLAGS));
116  CHECK_EQ(-1.0, StringToDouble(&uc, " -1 ", NO_FLAGS));
117  CHECK_EQ(1.0, StringToDouble(&uc, " +1 ", NO_FLAGS));
118  CHECK(isnan(StringToDouble(&uc, " - 1 ", NO_FLAGS)));
119  CHECK(isnan(StringToDouble(&uc, " + 1 ", NO_FLAGS)));
120 
121  CHECK_EQ(0.0, StringToDouble(&uc, "0e0", ALLOW_HEX | ALLOW_OCTALS));
122  CHECK_EQ(0.0, StringToDouble(&uc, "0e1", ALLOW_HEX | ALLOW_OCTALS));
123  CHECK_EQ(0.0, StringToDouble(&uc, "0e-1", ALLOW_HEX | ALLOW_OCTALS));
124  CHECK_EQ(0.0, StringToDouble(&uc, "0e-100000", ALLOW_HEX | ALLOW_OCTALS));
125  CHECK_EQ(0.0, StringToDouble(&uc, "0e+100000", ALLOW_HEX | ALLOW_OCTALS));
126  CHECK_EQ(0.0, StringToDouble(&uc, "0.", ALLOW_HEX | ALLOW_OCTALS));
127 }
128 
129 TEST(LongNumberStr) {
130  UnicodeCache uc;
131  CHECK_EQ(1e10, StringToDouble(&uc, "1" "0000000000", NO_FLAGS));
132  CHECK_EQ(1e20, StringToDouble(&uc, "1" "0000000000" "0000000000", NO_FLAGS));
133 
134  CHECK_EQ(1e60, StringToDouble(&uc, "1" "0000000000" "0000000000" "0000000000"
135  "0000000000" "0000000000" "0000000000", NO_FLAGS));
136 
137  CHECK_EQ(1e-2, StringToDouble(&uc, "." "0" "1", NO_FLAGS));
138  CHECK_EQ(1e-11, StringToDouble(&uc, "." "0000000000" "1", NO_FLAGS));
139  CHECK_EQ(1e-21, StringToDouble(&uc, "." "0000000000" "0000000000" "1",
140  NO_FLAGS));
141 
142  CHECK_EQ(1e-61, StringToDouble(&uc, "." "0000000000" "0000000000" "0000000000"
143  "0000000000" "0000000000" "0000000000" "1", NO_FLAGS));
144 
145 
146  // x = 24414062505131248.0 and y = 24414062505131252.0 are representable in
147  // double. Check chat z = (x + y) / 2 is rounded to x...
148  CHECK_EQ(24414062505131248.0,
149  StringToDouble(&uc, "24414062505131250.0", NO_FLAGS));
150 
151  // ... and z = (x + y) / 2 + delta is rounded to y.
152  CHECK_EQ(24414062505131252.0,
153  StringToDouble(&uc, "24414062505131250.000000001", NO_FLAGS));
154 }
155 
156 
157 TEST(MaximumSignificantDigits) {
158  UnicodeCache uc;
159  char num[] =
160  "4.4501477170144020250819966727949918635852426585926051135169509"
161  "122872622312493126406953054127118942431783801370080830523154578"
162  "251545303238277269592368457430440993619708911874715081505094180"
163  "604803751173783204118519353387964161152051487413083163272520124"
164  "606023105869053620631175265621765214646643181420505164043632222"
165  "668006474326056011713528291579642227455489682133472873831754840"
166  "341397809846934151055619529382191981473003234105366170879223151"
167  "087335413188049110555339027884856781219017754500629806224571029"
168  "581637117459456877330110324211689177656713705497387108207822477"
169  "584250967061891687062782163335299376138075114200886249979505279"
170  "101870966346394401564490729731565935244123171539810221213221201"
171  "847003580761626016356864581135848683152156368691976240370422601"
172  "6998291015625000000000000000000000000000000000e-308";
173 
174  CHECK_EQ(4.4501477170144017780491e-308, StringToDouble(&uc, num, NO_FLAGS));
175 
176  // Changes the result of strtod (at least in glibc implementation).
177  num[sizeof(num) - 8] = '1';
178 
179  CHECK_EQ(4.4501477170144022721148e-308, StringToDouble(&uc, num, NO_FLAGS));
180 }
181 
182 TEST(MinimumExponent) {
183  UnicodeCache uc;
184  // Same test but with different point-position.
185  char num[] =
186  "445014771701440202508199667279499186358524265859260511351695091"
187  "228726223124931264069530541271189424317838013700808305231545782"
188  "515453032382772695923684574304409936197089118747150815050941806"
189  "048037511737832041185193533879641611520514874130831632725201246"
190  "060231058690536206311752656217652146466431814205051640436322226"
191  "680064743260560117135282915796422274554896821334728738317548403"
192  "413978098469341510556195293821919814730032341053661708792231510"
193  "873354131880491105553390278848567812190177545006298062245710295"
194  "816371174594568773301103242116891776567137054973871082078224775"
195  "842509670618916870627821633352993761380751142008862499795052791"
196  "018709663463944015644907297315659352441231715398102212132212018"
197  "470035807616260163568645811358486831521563686919762403704226016"
198  "998291015625000000000000000000000000000000000e-1108";
199 
200  CHECK_EQ(4.4501477170144017780491e-308, StringToDouble(&uc, num, NO_FLAGS));
201 
202  // Changes the result of strtod (at least in glibc implementation).
203  num[sizeof(num) - 8] = '1';
204 
205  CHECK_EQ(4.4501477170144022721148e-308, StringToDouble(&uc, num, NO_FLAGS));
206 }
207 
208 
209 TEST(MaximumExponent) {
210  UnicodeCache uc;
211  char num[] = "0.16e309";
212 
213  CHECK_EQ(1.59999999999999997765e+308, StringToDouble(&uc, num, NO_FLAGS));
214 }
215 
216 
217 TEST(ExponentNumberStr) {
218  UnicodeCache uc;
219  CHECK_EQ(1e1, StringToDouble(&uc, "1e1", NO_FLAGS));
220  CHECK_EQ(1e1, StringToDouble(&uc, "1e+1", NO_FLAGS));
221  CHECK_EQ(1e-1, StringToDouble(&uc, "1e-1", NO_FLAGS));
222  CHECK_EQ(1e100, StringToDouble(&uc, "1e+100", NO_FLAGS));
223  CHECK_EQ(1e-100, StringToDouble(&uc, "1e-100", NO_FLAGS));
224  CHECK_EQ(1e-106, StringToDouble(&uc, ".000001e-100", NO_FLAGS));
225 }
226 
227 class OneBit1: public BitField<uint32_t, 0, 1> {};
228 class OneBit2: public BitField<uint32_t, 7, 1> {};
229 class EightBit1: public BitField<uint32_t, 0, 8> {};
230 class EightBit2: public BitField<uint32_t, 13, 8> {};
231 
233  uint32_t x;
234 
235  // One bit bit field can hold values 0 and 1.
236  CHECK(!OneBit1::is_valid(static_cast<uint32_t>(-1)));
237  CHECK(!OneBit2::is_valid(static_cast<uint32_t>(-1)));
238  for (int i = 0; i < 2; i++) {
240  x = OneBit1::encode(i);
241  CHECK_EQ(i, OneBit1::decode(x));
242 
244  x = OneBit2::encode(i);
245  CHECK_EQ(i, OneBit2::decode(x));
246  }
249 
250  // Eight bit bit field can hold values from 0 tp 255.
251  CHECK(!EightBit1::is_valid(static_cast<uint32_t>(-1)));
252  CHECK(!EightBit2::is_valid(static_cast<uint32_t>(-1)));
253  for (int i = 0; i < 256; i++) {
255  x = EightBit1::encode(i);
258  x = EightBit2::encode(i);
260  }
261  CHECK(!EightBit1::is_valid(256));
262  CHECK(!EightBit2::is_valid(256));
263 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
static uint32_t encode(uint32_tvalue)
Definition: utils.h:262
#define CHECK(condition)
Definition: checks.h:56
int isnan(double x)
double StringToDouble(UnicodeCache *unicode_cache, const char *str, int flags, double empty_string_val)
Definition: conversions.cc:41
static uint32_t decode(uint32_t value)
Definition: utils.h:273
static double nan_value()
static bool is_valid(uint32_tvalue)
Definition: utils.h:257