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