Reviewed by Sam.
[WebKit-https.git] / LayoutTests / fast / js / resources / number-toprecision.js
1 description(
2     'This test checks a few Number.toPrecision cases, including ' +
3     '<a href="http://bugs.webkit.org/show_bug.cgi?id=15145">15145: (0.999).toPrecision(1) returns incorrect result</a>' +
4     '.');
5
6 shouldBeEqualToString("(0.999).toPrecision(1)", "1");
7 shouldBeEqualToString("(0.999).toPrecision(2)", "1.0");
8 shouldBeEqualToString("(0.999).toPrecision(3)", "0.999");
9
10 var posInf = 1/0;
11 var negInf = -1/0;
12 var nan = 0/0;
13
14 shouldBeEqualToString("(0.0).toPrecision(4)", "0.000");
15 shouldBeEqualToString("(-0.0).toPrecision(4)", "0.000");
16 shouldBeEqualToString("(0.0).toPrecision()", "0");
17 shouldBeEqualToString("(-0.0).toPrecision()", "0");
18 shouldBeEqualToString("(1234.567).toPrecision()", "1234.567");
19 shouldThrow("(1234.567).toPrecision(0)");
20 shouldThrow("(1234.567).toPrecision(null)"); // just like 0
21 shouldThrow("(1234.567).toPrecision(false)"); // just like 0
22 shouldThrow("(1234.567).toPrecision('foo')"); // just like 0
23 shouldThrow("(1234.567).toPrecision(-1)");
24 shouldBeEqualToString("(1234.567).toPrecision(1)", "1e+3");
25 shouldBeEqualToString("(1234.567).toPrecision(true)", "1e+3"); // just like 1
26 shouldBeEqualToString("(1234.567).toPrecision('1')", "1e+3"); // just like 1
27 shouldBeEqualToString("(1234.567).toPrecision(2)", "1.2e+3");
28 shouldBeEqualToString("(1234.567).toPrecision(2.9)", "1.2e+3");
29 shouldBeEqualToString("(1234.567).toPrecision(5)", "1234.6");
30 shouldBeEqualToString("(1234.567).toPrecision(21)", "1234.56700000000000728");
31 // SpiderMonkey allows precision values 1 to 100, we only allow 1 to 21 currently
32 shouldBeEqualToString("(1234.567).toPrecision(22)", "1234.567000000000007276");
33 shouldBeEqualToString("(1234.567).toPrecision(100)", "1234.567000000000007275957614183425903320312500000000000000000000000000000000000000000000000000000000");
34 shouldThrow("(1234.567).toPrecision(101)");
35
36 shouldThrow("(1234.567).toPrecision(posInf)");
37 shouldThrow("(1234.567).toPrecision(negInf)");
38 shouldThrow("(1234.567).toPrecision(nan)"); // nan is treated like 0
39
40 shouldBeEqualToString("posInf.toPrecision()", "Infinity");
41 shouldBeEqualToString("negInf.toPrecision()", "-Infinity");
42 shouldBeEqualToString("nan.toPrecision()", "NaN");
43
44 var successfullyParsed = true;