1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25    'This test checks a few Number.toFixed cases, including ' +
26    '<a href="https://bugs.webkit.org/show_bug.cgi?id=5307">5307: Number.toFixed does not round 0.5 up</a>' +
27    ' and ' +
28    '<a href="https://bugs.webkit.org/show_bug.cgi?id=5308">5308: Number.toFixed does not include leading zero</a>' +
29    '.');
30
31shouldBe("(0).toFixed(0)", "'0'");
32
33shouldBe("(0.49).toFixed(0)", "'0'");
34shouldBe("(0.5).toFixed(0)", "'1'");
35shouldBe("(0.51).toFixed(0)", "'1'");
36
37shouldBe("(-0.49).toFixed(0)", "'-0'");
38shouldBe("(-0.5).toFixed(0)", "'-1'");
39shouldBe("(-0.51).toFixed(0)", "'-1'");
40
41shouldBe("(0).toFixed(1)", "'0.0'");
42
43shouldBe("(0.449).toFixed(1)", "'0.4'");
44shouldBe("(0.45).toFixed(1)", "'0.5'");
45shouldBe("(0.451).toFixed(1)", "'0.5'");
46shouldBe("(0.5).toFixed(1)", "'0.5'");
47shouldBe("(0.549).toFixed(1)", "'0.5'");
48shouldBe("(0.55).toFixed(1)", "'0.6'");
49shouldBe("(0.551).toFixed(1)", "'0.6'");
50
51shouldBe("(-0.449).toFixed(1)", "'-0.4'");
52shouldBe("(-0.45).toFixed(1)", "'-0.5'");
53shouldBe("(-0.451).toFixed(1)", "'-0.5'");
54shouldBe("(-0.5).toFixed(1)", "'-0.5'");
55shouldBe("(-0.549).toFixed(1)", "'-0.5'");
56shouldBe("(-0.55).toFixed(1)", "'-0.6'");
57shouldBe("(-0.551).toFixed(1)", "'-0.6'");
58
59var posInf = 1/0;
60var negInf = -1/0;
61var nan = 0/0;
62
63// From Acid3, http://bugs.webkit.org/show_bug.cgi?id=16640
64shouldBeEqualToString("(0.0).toFixed(4)", "0.0000");
65shouldBeEqualToString("(-0.0).toFixed(4)", "0.0000");
66shouldBeEqualToString("(0.0).toFixed()", "0");
67shouldBeEqualToString("(-0.0).toFixed()", "0");
68
69// From http://bugs.webkit.org/show_bug.cgi?id=5258
70shouldBeEqualToString("(1234.567).toFixed()", "1235");
71shouldBeEqualToString("(1234.567).toFixed(0)", "1235");
72// 0 equivilents
73shouldBeEqualToString("(1234.567).toFixed(null)", "1235");
74shouldBeEqualToString("(1234.567).toFixed(false)", "1235");
75shouldBeEqualToString("(1234.567).toFixed('foo')", "1235");
76shouldBeEqualToString("(1234.567).toFixed(nan)", "1235"); // nan is treated like 0
77
78shouldBeEqualToString("(1234.567).toFixed(1)", "1234.6");
79shouldBeEqualToString("(1234.567).toFixed(true)", "1234.6"); // just like 1
80shouldBeEqualToString("(1234.567).toFixed('1')", "1234.6"); // just like 1
81
82shouldBeEqualToString("(1234.567).toFixed(2)", "1234.57");
83shouldBeEqualToString("(1234.567).toFixed(2.9)", "1234.57");
84shouldBeEqualToString("(1234.567).toFixed(5)", "1234.56700");
85shouldBeEqualToString("(1234.567).toFixed(20)", "1234.56700000000000727596");
86
87// SpiderMonkey allows precision values -20 to 100, the spec only allows 0 to 20
88shouldThrow("(1234.567).toFixed(21)");
89shouldThrow("(1234.567).toFixed(100)");
90shouldThrow("(1234.567).toFixed(101)");
91shouldThrow("(1234.567).toFixed(-1)");
92shouldThrow("(1234.567).toFixed(-4)");
93shouldThrow("(1234.567).toFixed(-5)");
94shouldThrow("(1234.567).toFixed(-20)");
95shouldThrow("(1234.567).toFixed(-21)");
96
97shouldThrow("(1234.567).toFixed(posInf)");
98shouldThrow("(1234.567).toFixed(negInf)");
99
100shouldBeEqualToString("posInf.toFixed()", "Infinity");
101shouldBeEqualToString("negInf.toFixed()", "-Infinity");
102shouldBeEqualToString("nan.toFixed()", "NaN");
103