Home | History | Annotate | Download | only in js
      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 
     24 description(
     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 
     31 shouldBe("(0).toFixed(0)", "'0'");
     32 
     33 shouldBe("(0.49).toFixed(0)", "'0'");
     34 shouldBe("(0.5).toFixed(0)", "'1'");
     35 shouldBe("(0.51).toFixed(0)", "'1'");
     36 
     37 shouldBe("(-0.49).toFixed(0)", "'-0'");
     38 shouldBe("(-0.5).toFixed(0)", "'-1'");
     39 shouldBe("(-0.51).toFixed(0)", "'-1'");
     40 
     41 shouldBe("(0).toFixed(1)", "'0.0'");
     42 
     43 shouldBe("(0.449).toFixed(1)", "'0.4'");
     44 shouldBe("(0.45).toFixed(1)", "'0.5'");
     45 shouldBe("(0.451).toFixed(1)", "'0.5'");
     46 shouldBe("(0.5).toFixed(1)", "'0.5'");
     47 shouldBe("(0.549).toFixed(1)", "'0.5'");
     48 shouldBe("(0.55).toFixed(1)", "'0.6'");
     49 shouldBe("(0.551).toFixed(1)", "'0.6'");
     50 
     51 shouldBe("(-0.449).toFixed(1)", "'-0.4'");
     52 shouldBe("(-0.45).toFixed(1)", "'-0.5'");
     53 shouldBe("(-0.451).toFixed(1)", "'-0.5'");
     54 shouldBe("(-0.5).toFixed(1)", "'-0.5'");
     55 shouldBe("(-0.549).toFixed(1)", "'-0.5'");
     56 shouldBe("(-0.55).toFixed(1)", "'-0.6'");
     57 shouldBe("(-0.551).toFixed(1)", "'-0.6'");
     58 
     59 var posInf = 1/0;
     60 var negInf = -1/0;
     61 var nan = 0/0;
     62 
     63 // From Acid3, http://bugs.webkit.org/show_bug.cgi?id=16640
     64 shouldBeEqualToString("(0.0).toFixed(4)", "0.0000");
     65 shouldBeEqualToString("(-0.0).toFixed(4)", "0.0000");
     66 shouldBeEqualToString("(0.0).toFixed()", "0");
     67 shouldBeEqualToString("(-0.0).toFixed()", "0");
     68 
     69 // From http://bugs.webkit.org/show_bug.cgi?id=5258
     70 shouldBeEqualToString("(1234.567).toFixed()", "1235");
     71 shouldBeEqualToString("(1234.567).toFixed(0)", "1235");
     72 // 0 equivilents
     73 shouldBeEqualToString("(1234.567).toFixed(null)", "1235");
     74 shouldBeEqualToString("(1234.567).toFixed(false)", "1235");
     75 shouldBeEqualToString("(1234.567).toFixed('foo')", "1235");
     76 shouldBeEqualToString("(1234.567).toFixed(nan)", "1235"); // nan is treated like 0
     77 
     78 shouldBeEqualToString("(1234.567).toFixed(1)", "1234.6");
     79 shouldBeEqualToString("(1234.567).toFixed(true)", "1234.6"); // just like 1
     80 shouldBeEqualToString("(1234.567).toFixed('1')", "1234.6"); // just like 1
     81 
     82 shouldBeEqualToString("(1234.567).toFixed(2)", "1234.57");
     83 shouldBeEqualToString("(1234.567).toFixed(2.9)", "1234.57");
     84 shouldBeEqualToString("(1234.567).toFixed(5)", "1234.56700");
     85 shouldBeEqualToString("(1234.567).toFixed(20)", "1234.56700000000000727596");
     86 
     87 // SpiderMonkey allows precision values -20 to 100, the spec only allows 0 to 20
     88 shouldThrow("(1234.567).toFixed(21)");
     89 shouldThrow("(1234.567).toFixed(100)");
     90 shouldThrow("(1234.567).toFixed(101)");
     91 shouldThrow("(1234.567).toFixed(-1)");
     92 shouldThrow("(1234.567).toFixed(-4)");
     93 shouldThrow("(1234.567).toFixed(-5)");
     94 shouldThrow("(1234.567).toFixed(-20)");
     95 shouldThrow("(1234.567).toFixed(-21)");
     96 
     97 shouldThrow("(1234.567).toFixed(posInf)");
     98 shouldThrow("(1234.567).toFixed(negInf)");
     99 
    100 shouldBeEqualToString("posInf.toFixed()", "Infinity");
    101 shouldBeEqualToString("negInf.toFixed()", "-Infinity");
    102 shouldBeEqualToString("nan.toFixed()", "NaN");
    103