1 // Any copyright is dedicated to the Public Domain.
2 // http://creativecommons.org/licenses/publicdomain/
4 //-----------------------------------------------------------------------------
5 var BUGNUMBER = 577536;
6 var summary = "ES5 15.1.2.2 parseInt(string, radix)";
8 print(BUGNUMBER + ": " + summary);
17 /* 1. Let inputString be ToString(string). */
19 assertEq(parseInt({ toString: function() { return "17" } }, 10), 17);
22 str = { get toString() { upvar++; return function() { upvar++; return "12345"; } } };
23 assertEq(parseInt(str, 10), 12345);
28 * 2. Let S be a newly created substring of inputString consisting of the first
29 * character that is not a StrWhiteSpaceChar and all characters following
30 * that character. (In other words, remove leading white space.)
34 ["\t", "\v", "\f", " ", "\xA0", "\uFEFF",
35 "\u2004", "\u3000", // a few Unicode whitespaces
36 "\r", "\n", "\u2028", "\u2029"];
39 for (var i = 0, sz = ws.length; i < sz; i++)
41 assertEq(parseInt(ws[i] + str, 10), 8675309);
42 for (var j = 0, sz = ws.length; j < sz; j++)
44 assertEq(parseInt(ws[i] + ws[j] + str, 10), 8675309,
45 ws[i].charCodeAt(0).toString(16) + ", " +
46 ws[j].charCodeAt(0).toString(16));
53 * 4. If S is not empty and the first character of S is a minus sign -, let
57 assertEq(parseInt("-" + str, 10), -parseInt(str, 10));
58 assertEq(parseInt(" -" + str, 10), -parseInt(str, 10));
59 assertEq(parseInt("-", 10), NaN);
60 assertEq(parseInt("", 10), NaN);
61 assertEq(parseInt("-0", 10), -0);
65 * 5. If S is not empty and the first character of S is a plus sign + or a
66 * minus sign -, then remove the first character from S.
68 assertEq(parseInt("+12345", 10), 12345);
69 assertEq(parseInt(" +12345", 10), 12345);
70 assertEq(parseInt("-12345", 10), -12345);
71 assertEq(parseInt(" -12345", 10), -12345);
75 * 6. Let R = ToInt32(radix).
80 { toString: function() { if (!upvar) upvar = "string"; return "42"; } };
82 { toString: function() { if (!upvar) upvar = "radix"; return "10"; } };
84 assertEq(parseInt(str, radix), 42);
85 assertEq(upvar, "string");
87 assertEq(parseInt("123", null), 123);
88 assertEq(parseInt("123", undefined), 123);
89 assertEq(parseInt("123", NaN), 123);
90 assertEq(parseInt("123", -0), 123);
91 assertEq(parseInt("10", 72057594037927950), 16);
92 assertEq(parseInt("10", -4294967292), 4);
93 assertEq(parseInt("0x10", 1e308), 16);
94 assertEq(parseInt("10", 1e308), 10);
95 assertEq(parseInt("10", { valueOf: function() { return 16; } }), 16);
99 * 7. Let stripPrefix be true.
101 * a. If R < 2 or R > 36, then return NaN.
102 * b. If R ≠ 16, let stripPrefix be false.
105 * 10. If stripPrefix is true, then
106 * a. If the length of S is at least 2 and the first two characters of S
107 * are either “0x” or “0X”, then remove the first two characters from S and
110 var vs = ["1", "51", "917", "2343", "99963"];
111 for (var i = 0, sz = vs.length; i < sz; i++)
112 assertEq(parseInt(vs[i], 0), parseInt(vs[i], 10), "bad " + vs[i]);
114 assertEq(parseInt("0x10"), 16);
115 assertEq(parseInt("0x10", 0), 16);
116 assertEq(parseInt("0x10", 16), 16);
117 assertEq(parseInt("0x10", 8), 0);
118 assertEq(parseInt("-0x10", 16), -16);
120 assertEq(parseInt("5", 1), NaN);
121 assertEq(parseInt("5", 37), NaN);
122 assertEq(parseInt("5", { valueOf: function() { return -1; } }), NaN);
126 * 11. If S contains any character that is not a radix-R digit, then let Z be
127 * the substring of S consisting of all characters before the first such
128 * character; otherwise, let Z be S.
129 * 12. If Z is empty, return NaN.
131 assertEq(parseInt(""), NaN);
132 assertEq(parseInt("ohai"), NaN);
133 assertEq(parseInt("0xohai"), NaN);
134 assertEq(parseInt("-ohai"), NaN);
135 assertEq(parseInt("+ohai"), NaN);
136 assertEq(parseInt(" ohai"), NaN);
138 assertEq(parseInt("0xaohai"), 10);
139 assertEq(parseInt("hohai", 18), 17);
143 * 13. Let mathInt be the mathematical integer value that is represented by Z
144 * in radix-R notation, using the letters A-Z and a-z for digits with
145 * values 10 through 35. (However, if R is 10 and Z contains more than 20
146 * significant digits, every significant digit after the 20th may be
147 * replaced by a 0 digit, at the option of the implementation; and if R is
148 * not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-
149 * dependent approximation to the mathematical integer value that is
150 * represented by Z in radix-R notation.)
151 * 14. Let number be the Number value for mathInt.
152 * 15. Return sign × number.
154 assertEq(parseInt("ohai", 36), 1142154);
155 assertEq(parseInt("0ohai", 36), 1142154);
156 assertEq(parseInt("00ohai", 36), 1142154);
157 assertEq(parseInt("A", 16), 10);
158 assertEq(parseInt("0A", 16), 10);
159 assertEq(parseInt("00A", 16), 10);
160 assertEq(parseInt("A", 17), 10);
161 assertEq(parseInt("0A", 17), 10);
162 assertEq(parseInt("00A", 17), 10);
165 /******************************************************************************/
167 if (typeof reportCompare === "function")
168 reportCompare(true, true);
170 print("All tests passed!");