1 description('Tests for the parseInt function.');
3 // Simple hex & dec integer values.
4 shouldBe("parseInt('123')", '123');
5 shouldBe("parseInt('123x4')", '123');
6 shouldBe("parseInt('-123')", '-123');
7 shouldBe("parseInt('0x123')", '0x123');
8 shouldBe("parseInt('0x123x4')", '0x123');
9 shouldBe("parseInt('-0x123x4')", '-0x123');
10 shouldBe("parseInt('-')", 'Number.NaN');
11 shouldBe("parseInt('0x')", 'Number.NaN');
12 shouldBe("parseInt('-0x')", 'Number.NaN');
14 // These call default to base 10, unless radix is explicitly 16.
15 shouldBe("parseInt('123', undefined)", '123');
16 shouldBe("parseInt('123', null)", '123');
17 shouldBe("parseInt('123', 0)", '123');
18 shouldBe("parseInt('123', 10)", '123');
19 shouldBe("parseInt('123', 16)", '0x123');
20 // These call default to base 16, unless radix is explicitly 10.
21 shouldBe("parseInt('0x123', undefined)", '0x123');
22 shouldBe("parseInt('0x123', null)", '0x123');
23 shouldBe("parseInt('0x123', 0)", '0x123');
24 shouldBe("parseInt('0x123', 10)", '0');
25 shouldBe("parseInt('0x123', 16)", '0x123');
27 // Test edge cases for the Number.toString exponential ranges.
28 shouldBe("parseInt(Math.pow(10, 20))", '100000000000000000000');
29 shouldBe("parseInt(Math.pow(10, 21))", '1');
30 shouldBe("parseInt(Math.pow(10, -6))", '0');
31 shouldBe("parseInt(Math.pow(10, -7))", '1');
32 shouldBe("parseInt(-Math.pow(10, 20))", '-100000000000000000000');
33 shouldBe("parseInt(-Math.pow(10, 21))", '-1');
34 shouldBe("parseInt(-Math.pow(10, -6))", '-0');
35 shouldBe("parseInt(-Math.pow(10, -7))", '-1');
37 // Test correct handling for -0.
38 shouldBe("parseInt('0')", '0');
39 shouldBe("parseInt('-0')", '-0');
40 shouldBe("parseInt(0)", '0');
41 shouldBe("parseInt(-0)", '0');
43 // Test edge cases of our optimized int handling.
44 shouldBe("parseInt(2147483647)", '2147483647');
45 shouldBe("parseInt(2147483648)", '2147483648');
46 shouldBe("parseInt('2147483647')", '2147483647');
47 shouldBe("parseInt('2147483648')", '2147483648');
49 // Add test cases where the ToString/ToInt32 conversions throw.
51 var throwingRadix = { valueOf: function(){ state = "throwingRadix"; throw null; } };
52 var throwingString = { toString: function(){ state = "throwingString"; throw null; } };
53 shouldBe("state = null; try { parseInt('123', throwingRadix); } catch (e) {} state;", '"throwingRadix"');
54 shouldBe("state = null; try { parseInt(throwingString, throwingRadix); } catch (e) {} state;", '"throwingString"');