Home | History | Annotate | Download | only in webkit
      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('Tests for the parseInt function.');
     25 
     26 // Simple hex & dec integer values.
     27 shouldBe("parseInt('123')", '123');
     28 shouldBe("parseInt('123x4')", '123');
     29 shouldBe("parseInt('-123')", '-123');
     30 shouldBe("parseInt('0x123')", '0x123');
     31 shouldBe("parseInt('0x123x4')", '0x123');
     32 shouldBe("parseInt('-0x123x4')", '-0x123');
     33 shouldBe("parseInt('-')", 'Number.NaN');
     34 shouldBe("parseInt('0x')", 'Number.NaN');
     35 shouldBe("parseInt('-0x')", 'Number.NaN');
     36 
     37 // These call default to base 10, unless radix is explicitly 16.
     38 shouldBe("parseInt('123', undefined)", '123');
     39 shouldBe("parseInt('123', null)", '123');
     40 shouldBe("parseInt('123', 0)", '123');
     41 shouldBe("parseInt('123', 10)", '123');
     42 shouldBe("parseInt('123', 16)", '0x123');
     43 // These call default to base 16, unless radix is explicitly 10.
     44 shouldBe("parseInt('0x123', undefined)", '0x123');
     45 shouldBe("parseInt('0x123', null)", '0x123');
     46 shouldBe("parseInt('0x123', 0)", '0x123');
     47 shouldBe("parseInt('0x123', 10)", '0');
     48 shouldBe("parseInt('0x123', 16)", '0x123');
     49 
     50 // Test edge cases for the Number.toString exponential ranges.
     51 shouldBe("parseInt(Math.pow(10, 20))", '100000000000000000000');
     52 shouldBe("parseInt(Math.pow(10, 21))", '1');
     53 shouldBe("parseInt(Math.pow(10, -6))", '0');
     54 shouldBe("parseInt(Math.pow(10, -7))", '1');
     55 shouldBe("parseInt(-Math.pow(10, 20))", '-100000000000000000000');
     56 shouldBe("parseInt(-Math.pow(10, 21))", '-1');
     57 shouldBe("parseInt(-Math.pow(10, -6))", '-0');
     58 shouldBe("parseInt(-Math.pow(10, -7))", '-1');
     59 
     60 // Test correct handling for -0.
     61 shouldBe("parseInt('0')", '0');
     62 shouldBe("parseInt('-0')", '-0');
     63 shouldBe("parseInt(0)", '0');
     64 shouldBe("parseInt(-0)", '0');
     65 
     66 // Test edge cases of our optimized int handling.
     67 shouldBe("parseInt(2147483647)", '2147483647');
     68 shouldBe("parseInt(2147483648)", '2147483648');
     69 shouldBe("parseInt('2147483647')", '2147483647');
     70 shouldBe("parseInt('2147483648')", '2147483648');
     71 
     72 // Add test cases where the ToString/ToInt32 conversions throw.
     73 var state;
     74 var throwingRadix = { valueOf: function(){ state = "throwingRadix"; throw null; } };
     75 var throwingString = { toString: function(){ state = "throwingString"; throw null; } };
     76 shouldBe("state = null; try { parseInt('123', throwingRadix); } catch (e) {} state;", '"throwingRadix"');
     77 shouldBe("state = null; try { parseInt(throwingString, throwingRadix); } catch (e) {} state;", '"throwingString"');
     78