1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// ----------------------------------------------------------------------
29// toString
30function testToString(a, b) {
31  assertEquals(a, b.toString());
32}
33
34function testToStringP(a, b, c) {
35  assertEquals(a, b.toString(c));
36}
37
38testToString("NaN", (NaN));
39testToString("Infinity", (1/0));
40testToString("-Infinity", (-1/0));
41testToString("0", (0));
42testToString("9", (9));
43testToString("90", (90));
44testToString("90.12", (90.12));
45testToString("0.1", (0.1));
46testToString("0.01", (0.01));
47testToString("0.0123", (0.0123));
48testToString("111111111111111110000", (111111111111111111111));
49testToString("1.1111111111111111e+21", (1111111111111111111111));
50testToString("1.1111111111111111e+22", (11111111111111111111111));
51testToString("0.00001", (0.00001));
52testToString("0.000001", (0.000001));
53testToString("1e-7", (0.0000001));
54testToString("1.2e-7", (0.00000012));
55testToString("1.23e-7", (0.000000123));
56testToString("1e-8", (0.00000001));
57testToString("1.2e-8", (0.000000012));
58testToString("1.23e-8", (0.0000000123));
59
60testToString("0", (-0));
61testToString("-9", (-9));
62testToString("-90", (-90));
63testToString("-90.12", (-90.12));
64testToString("-0.1", (-0.1));
65testToString("-0.01", (-0.01));
66testToString("-0.0123", (-0.0123));
67testToString("-111111111111111110000", (-111111111111111111111));
68testToString("-1.1111111111111111e+21", (-1111111111111111111111));
69testToString("-1.1111111111111111e+22", (-11111111111111111111111));
70testToString("-0.00001", (-0.00001));
71testToString("-0.000001", (-0.000001));
72testToString("-1e-7", (-0.0000001));
73testToString("-1.2e-7", (-0.00000012));
74testToString("-1.23e-7", (-0.000000123));
75testToString("-1e-8", (-0.00000001));
76testToString("-1.2e-8", (-0.000000012));
77testToString("-1.23e-8", (-0.0000000123));
78
79testToString("1000", (1000));
80testToString("0.00001", (0.00001));
81testToString("1000000000000000100", (1000000000000000128));
82testToString("1e+21", (1000000000000000012800));
83testToString("-1e+21", (-1000000000000000012800));
84testToString("1e-7", (0.0000001));
85testToString("-1e-7", (-0.0000001));
86testToString("1.0000000000000001e+21", (1000000000000000128000));
87testToString("0.000001", (0.000001));
88testToString("1e-7", (0.0000001));
89
90testToStringP("NaN", (NaN), 16);
91testToStringP("Infinity", (1/0), 16);
92testToStringP("-Infinity", (-1/0), 16);
93testToStringP("0", (0), 16);
94testToStringP("9", (9), 16);
95testToStringP("5a", (90), 16);
96testToStringP("5a.1eb851eb852", (90.12), 16);
97testToStringP("0.1999999999999a", (0.1), 16);
98testToStringP("0.028f5c28f5c28f6", (0.01), 16);
99testToStringP("0.032617c1bda511a", (0.0123), 16);
100testToStringP("605f9f6dd18bc8000", (111111111111111111111), 16);
101testToStringP("3c3bc3a4a2f75c0000", (1111111111111111111111), 16);
102testToStringP("25a55a46e5da9a00000", (11111111111111111111111), 16);
103testToStringP("0.0000a7c5ac471b4788", (0.00001), 16);
104testToStringP("0.000010c6f7a0b5ed8d", (0.000001), 16);
105testToStringP("0.000001ad7f29abcaf48", (0.0000001), 16);
106testToStringP("0.000002036565348d256", (0.00000012), 16);
107testToStringP("0.0000021047ee22aa466", (0.000000123), 16);
108testToStringP("0.0000002af31dc4611874", (0.00000001), 16);
109testToStringP("0.000000338a23b87483be", (0.000000012), 16);
110testToStringP("0.00000034d3fe36aaa0a2", (0.0000000123), 16);
111
112testToStringP("0", (-0), 16);
113testToStringP("-9", (-9), 16);
114testToStringP("-5a", (-90), 16);
115testToStringP("-5a.1eb851eb852", (-90.12), 16);
116testToStringP("-0.1999999999999a", (-0.1), 16);
117testToStringP("-0.028f5c28f5c28f6", (-0.01), 16);
118testToStringP("-0.032617c1bda511a", (-0.0123), 16);
119testToStringP("-605f9f6dd18bc8000", (-111111111111111111111), 16);
120testToStringP("-3c3bc3a4a2f75c0000", (-1111111111111111111111), 16);
121testToStringP("-25a55a46e5da9a00000", (-11111111111111111111111), 16);
122testToStringP("-0.0000a7c5ac471b4788", (-0.00001), 16);
123testToStringP("-0.000010c6f7a0b5ed8d", (-0.000001), 16);
124testToStringP("-0.000001ad7f29abcaf48", (-0.0000001), 16);
125testToStringP("-0.000002036565348d256", (-0.00000012), 16);
126testToStringP("-0.0000021047ee22aa466", (-0.000000123), 16);
127testToStringP("-0.0000002af31dc4611874", (-0.00000001), 16);
128testToStringP("-0.000000338a23b87483be", (-0.000000012), 16);
129testToStringP("-0.00000034d3fe36aaa0a2", (-0.0000000123), 16);
130
131testToString("4294967296", Math.pow(2,32));
132testToStringP("ffffffff", (Math.pow(2,32)-1), 16);
133testToStringP("11111111111111111111111111111111", (Math.pow(2,32)-1), 2);
134testToStringP("5yc1z", (10000007), 36);
135testToStringP("0", (0), 36);
136testToStringP("0", (0), 16);
137testToStringP("0", (0), 10);
138testToStringP("0", (0), 8);
139testToStringP("0", (0), 2);
140testToStringP("100000000000000000000000000000000", Math.pow(2,32), 2);
141testToStringP("100000000000000000000000000000001", (Math.pow(2,32) + 1), 2);
142testToStringP("100000000000080", (0x100000000000081), 16);
143testToStringP("1000000000000100", (-(-'0x1000000000000081')), 16);
144testToStringP("1000000000000000", (-(-'0x1000000000000080')), 16);
145testToStringP("1000000000000000", (-(-'0x100000000000007F')), 16);
146testToStringP("100000000000000000000000000000000000000000000000010000000", (0x100000000000081), 2);
147testToStringP("-11111111111111111111111111111111", (-(Math.pow(2,32)-1)), 2);
148testToStringP("-5yc1z", (-10000007), 36);
149testToStringP("-100000000000000000000000000000000", (-Math.pow(2,32)), 2);
150testToStringP("-100000000000000000000000000000001", (-(Math.pow(2,32) + 1)), 2);
151testToStringP("-100000000000080", (-0x100000000000081), 16);
152testToStringP("-100000000000000000000000000000000000000000000000010000000", (-0x100000000000081), 2);
153testToStringP("8.8", (8.5), 16);
154testToStringP("-8.8", (-8.5), 16);
155
156// ----------------------------------------------------------------------
157// toFixed
158function testToFixed(a, b, c) {
159  assertEquals(a, b.toFixed(c));
160}
161
162testToFixed("NaN", (NaN), (2));
163testToFixed("Infinity", (1/0), (2));
164testToFixed("-Infinity", (-1/0), (2));
165
166testToFixed("1.1111111111111111e+21", (1111111111111111111111), (8));
167testToFixed("0.1", (0.1), (1));
168testToFixed("0.10", (0.1), (2));
169testToFixed("0.100", (0.1), (3));
170testToFixed("0.01", (0.01), (2));
171testToFixed("0.010", (0.01), (3));
172testToFixed("0.0100", (0.01), (4));
173testToFixed("0.00", (0.001), (2));
174testToFixed("0.001", (0.001), (3));
175testToFixed("0.0010", (0.001), (4));
176testToFixed("1.0000", (1), (4));
177testToFixed("1.0", (1), (1));
178testToFixed("1", (1), (0));
179testToFixed("12", (12), (0));
180testToFixed("1", (1.1), (0));
181testToFixed("12", (12.1), (0));
182testToFixed("1", (1.12), (0));
183testToFixed("12", (12.12), (0));
184testToFixed("0.0000006", (0.0000006), (7));
185testToFixed("0.00000006", (0.00000006), (8));
186testToFixed("0.000000060", (0.00000006), (9));
187testToFixed("0.0000000600", (0.00000006), (10));
188testToFixed("0", (0), (0));
189testToFixed("0.0", (0), (1));
190testToFixed("0.00", (0), (2));
191
192testToFixed("-1.1111111111111111e+21", (-1111111111111111111111), (8));
193testToFixed("-0.1", (-0.1), (1));
194testToFixed("-0.10", (-0.1), (2));
195testToFixed("-0.100", (-0.1), (3));
196testToFixed("-0.01", (-0.01), (2));
197testToFixed("-0.010", (-0.01), (3));
198testToFixed("-0.0100", (-0.01), (4));
199testToFixed("-0.00", (-0.001), (2));
200testToFixed("-0.001", (-0.001), (3));
201testToFixed("-0.0010", (-0.001), (4));
202testToFixed("-1.0000", (-1), (4));
203testToFixed("-1.0", (-1), (1));
204testToFixed("-1", (-1), (0));
205testToFixed("-1", (-1.1), (0));
206testToFixed("-12", (-12.1), (0));
207testToFixed("-1", (-1.12), (0));
208testToFixed("-12", (-12.12), (0));
209testToFixed("-0.0000006", (-0.0000006), (7));
210testToFixed("-0.00000006", (-0.00000006), (8));
211testToFixed("-0.000000060", (-0.00000006), (9));
212testToFixed("-0.0000000600", (-0.00000006), (10));
213testToFixed("0", (-0), (0));
214testToFixed("0.0", (-0), (1));
215testToFixed("0.00", (-0), (2));
216
217testToFixed("0.00001", (0.00001), (5));
218testToFixed("0.00000000000000000010", (0.0000000000000000001), (20));
219testToFixed("0.00001000000000000", (0.00001), (17));
220testToFixed("1.00000000000000000", (1), (17));
221testToFixed("100000000000000128.0", (100000000000000128), (1));
222testToFixed("10000000000000128.00", (10000000000000128), (2));
223testToFixed("10000000000000128.00000000000000000000", (10000000000000128), (20));
224testToFixed("-42.000", (-42), (3));
225testToFixed("-0.00000000000000000010", (-0.0000000000000000001), (20));
226testToFixed("0.12312312312312299889", (0.123123123123123), (20));
227
228assertEquals("-1000000000000000128", (-1000000000000000128).toFixed());
229assertEquals("0", (0).toFixed());
230assertEquals("1000000000000000128", (1000000000000000128).toFixed());
231assertEquals("1000", (1000).toFixed());
232assertEquals("0", (0.00001).toFixed());
233// Test that we round up even when the last digit generated is even.
234// dtoa does not do this in its original form.
235assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)");
236assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)");
237assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)");
238// This is bizare, but Spidermonkey and KJS behave the same.
239assertEquals("234.2040", (234.20405).toFixed(4), "234.2040.toFixed(4)");
240assertEquals("234.2041", (234.2040506).toFixed(4));
241
242// ----------------------------------------------------------------------
243// toExponential
244function testToExponential(a, b) {
245  assertEquals(a, b.toExponential());
246}
247
248function testToExponentialP(a, b, c) {
249  assertEquals(a, b.toExponential(c));
250}
251
252testToExponential("1e+0", (1));
253testToExponential("1.1e+1", (11));
254testToExponential("1.12e+2", (112));
255testToExponential("1e-1", (0.1));
256testToExponential("1.1e-1", (0.11));
257testToExponential("1.12e-1", (0.112));
258testToExponential("-1e+0", (-1));
259testToExponential("-1.1e+1", (-11));
260testToExponential("-1.12e+2", (-112));
261testToExponential("-1e-1", (-0.1));
262testToExponential("-1.1e-1", (-0.11));
263testToExponential("-1.12e-1", (-0.112));
264testToExponential("0e+0", (0));
265testToExponential("1.12356e-4", (0.000112356));
266testToExponential("-1.12356e-4", (-0.000112356));
267
268testToExponentialP("1e+0", (1), (0));
269testToExponentialP("1e+1", (11), (0));
270testToExponentialP("1e+2", (112), (0));
271testToExponentialP("1.0e+0", (1), (1));
272testToExponentialP("1.1e+1", (11), (1));
273testToExponentialP("1.1e+2", (112), (1));
274testToExponentialP("1.00e+0", (1), (2));
275testToExponentialP("1.10e+1", (11), (2));
276testToExponentialP("1.12e+2", (112), (2));
277testToExponentialP("1.000e+0", (1), (3));
278testToExponentialP("1.100e+1", (11), (3));
279testToExponentialP("1.120e+2", (112), (3));
280testToExponentialP("1e-1", (0.1), (0));
281testToExponentialP("1e-1", (0.11), (0));
282testToExponentialP("1e-1", (0.112), (0));
283testToExponentialP("1.0e-1", (0.1), (1));
284testToExponentialP("1.1e-1", (0.11), (1));
285testToExponentialP("1.1e-1", (0.112), (1));
286testToExponentialP("1.00e-1", (0.1), (2));
287testToExponentialP("1.10e-1", (0.11), (2));
288testToExponentialP("1.12e-1", (0.112), (2));
289testToExponentialP("1.000e-1", (0.1), (3));
290testToExponentialP("1.100e-1", (0.11), (3));
291testToExponentialP("1.120e-1", (0.112), (3));
292
293testToExponentialP("-1e+0", (-1), (0));
294testToExponentialP("-1e+1", (-11), (0));
295testToExponentialP("-1e+2", (-112), (0));
296testToExponentialP("-1.0e+0", (-1), (1));
297testToExponentialP("-1.1e+1", (-11), (1));
298testToExponentialP("-1.1e+2", (-112), (1));
299testToExponentialP("-1.00e+0", (-1), (2));
300testToExponentialP("-1.10e+1", (-11), (2));
301testToExponentialP("-1.12e+2", (-112), (2));
302testToExponentialP("-1.000e+0", (-1), (3));
303testToExponentialP("-1.100e+1", (-11), (3));
304testToExponentialP("-1.120e+2", (-112), (3));
305testToExponentialP("-1e-1", (-0.1), (0));
306testToExponentialP("-1e-1", (-0.11), (0));
307testToExponentialP("-1e-1", (-0.112), (0));
308testToExponentialP("-1.0e-1", (-0.1), (1));
309testToExponentialP("-1.1e-1", (-0.11), (1));
310testToExponentialP("-1.1e-1", (-0.112), (1));
311testToExponentialP("-1.00e-1", (-0.1), (2));
312testToExponentialP("-1.10e-1", (-0.11), (2));
313testToExponentialP("-1.12e-1", (-0.112), (2));
314testToExponentialP("-1.000e-1", (-0.1), (3));
315testToExponentialP("-1.100e-1", (-0.11), (3));
316testToExponentialP("-1.120e-1", (-0.112), (3));
317
318testToExponentialP("NaN", (NaN), (2));
319testToExponentialP("Infinity", (Infinity), (2));
320testToExponentialP("-Infinity", (-Infinity), (2));
321testToExponentialP("1e+0", (1), (0));
322testToExponentialP("0.00e+0", (0), (2));
323testToExponentialP("1e+1", (11.2356), (0));
324testToExponentialP("1.1236e+1", (11.2356), (4));
325testToExponentialP("1.1236e-4", (0.000112356), (4));
326testToExponentialP("-1.1236e-4", (-0.000112356), (4));
327
328// ----------------------------------------------------------------------
329// toPrecision
330function testToPrecision(a, b, c) {
331  assertEquals(a, b.toPrecision(c));
332}
333
334testToPrecision("NaN", (NaN), (1));
335testToPrecision("Infinity", (Infinity), (2));
336testToPrecision("-Infinity", (-Infinity), (2));
337testToPrecision("0.000555000000000000", (0.000555), (15));
338testToPrecision("5.55000000000000e-7", (0.000000555), (15));
339testToPrecision("-5.55000000000000e-7", (-0.000000555), (15));
340testToPrecision("1e+8", (123456789), (1));
341testToPrecision("123456789", (123456789), (9));
342testToPrecision("1.2345679e+8", (123456789), (8));
343testToPrecision("1.234568e+8", (123456789), (7));
344testToPrecision("-1.234568e+8", (-123456789), (7));
345testToPrecision("-1.2e-9", Number(-.0000000012345), (2));
346testToPrecision("-1.2e-8", Number(-.000000012345), (2));
347testToPrecision("-1.2e-7", Number(-.00000012345), (2));
348testToPrecision("-0.0000012", Number(-.0000012345), (2));
349testToPrecision("-0.000012", Number(-.000012345), (2));
350testToPrecision("-0.00012", Number(-.00012345), (2));
351testToPrecision("-0.0012", Number(-.0012345), (2));
352testToPrecision("-0.012", Number(-.012345), (2));
353testToPrecision("-0.12", Number(-.12345), (2));
354testToPrecision("-1.2", Number(-1.2345), (2));
355testToPrecision("-12", Number(-12.345), (2));
356testToPrecision("-1.2e+2", Number(-123.45), (2));
357testToPrecision("-1.2e+3", Number(-1234.5), (2));
358testToPrecision("-1.2e+4", Number(-12345), (2));
359testToPrecision("-1.235e+4", Number(-12345.67), (4));
360testToPrecision("-1.234e+4", Number(-12344.67), (4));
361// Test that we round up even when the last digit generated is even.
362// dtoa does not do this in its original form.
363assertEquals("1.3", 1.25.toPrecision(2), "1.25.toPrecision(2)");
364assertEquals("1.4", 1.35.toPrecision(2), "1.35.toPrecision(2)");
365