michael@0: // Any copyright is dedicated to the Public Domain. michael@0: // http://creativecommons.org/licenses/publicdomain/ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 886949; michael@0: var summary = "ES6 (draft May 2013) 15.7.3.9 Number.parseInt(string, radix)"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: var str, radix; michael@0: var upvar; michael@0: michael@0: /* 1. Let inputString be ToString(string). */ michael@0: michael@0: assertEq(Number.parseInt({ toString: function() { return "17" } }, 10), 17); michael@0: michael@0: upvar = 0; michael@0: str = { get toString() { upvar++; return function() { upvar++; return "12345"; } } }; michael@0: assertEq(Number.parseInt(str, 10), 12345); michael@0: assertEq(upvar, 2); michael@0: michael@0: michael@0: /* michael@0: * 2. Let S be a newly created substring of inputString consisting of the first michael@0: * character that is not a StrWhiteSpaceChar and all characters following michael@0: * that character. (In other words, remove leading white space.) michael@0: */ michael@0: michael@0: var ws = michael@0: ["\t", "\v", "\f", " ", "\xA0", "\uFEFF", michael@0: "\u2004", "\u3000", // a few Unicode whitespaces michael@0: "\r", "\n", "\u2028", "\u2029"]; michael@0: michael@0: str = "8675309"; michael@0: for (var i = 0, sz = ws.length; i < sz; i++) michael@0: { michael@0: assertEq(Number.parseInt(ws[i] + str, 10), 8675309); michael@0: for (var j = 0, sz = ws.length; j < sz; j++) michael@0: { michael@0: assertEq(Number.parseInt(ws[i] + ws[j] + str, 10), 8675309, michael@0: ws[i].charCodeAt(0).toString(16) + ", " + michael@0: ws[j].charCodeAt(0).toString(16)); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * 3. Let sign be 1. michael@0: * 4. If S is not empty and the first character of S is a minus sign -, let michael@0: * sign be −1. michael@0: */ michael@0: str = "5552368"; michael@0: assertEq(Number.parseInt("-" + str, 10), -Number.parseInt(str, 10)); michael@0: assertEq(Number.parseInt(" -" + str, 10), -Number.parseInt(str, 10)); michael@0: assertEq(Number.parseInt("-", 10), NaN); michael@0: assertEq(Number.parseInt("", 10), NaN); michael@0: assertEq(Number.parseInt("-0", 10), -0); michael@0: michael@0: michael@0: /* michael@0: * 5. If S is not empty and the first character of S is a plus sign + or a michael@0: * minus sign -, then remove the first character from S. michael@0: */ michael@0: assertEq(Number.parseInt("+12345", 10), 12345); michael@0: assertEq(Number.parseInt(" +12345", 10), 12345); michael@0: assertEq(Number.parseInt("-12345", 10), -12345); michael@0: assertEq(Number.parseInt(" -12345", 10), -12345); michael@0: michael@0: michael@0: /* michael@0: * 6. Let R = ToInt32(radix). michael@0: */ michael@0: michael@0: upvar = ""; michael@0: str = michael@0: { toString: function() { if (!upvar) upvar = "string"; return "42"; } }; michael@0: radix = michael@0: { toString: function() { if (!upvar) upvar = "radix"; return "10"; } }; michael@0: michael@0: assertEq(Number.parseInt(str, radix), 42); michael@0: assertEq(upvar, "string"); michael@0: michael@0: assertEq(Number.parseInt("123", null), 123); michael@0: assertEq(Number.parseInt("123", undefined), 123); michael@0: assertEq(Number.parseInt("123", NaN), 123); michael@0: assertEq(Number.parseInt("123", -0), 123); michael@0: assertEq(Number.parseInt("10", 72057594037927950), 16); michael@0: assertEq(Number.parseInt("10", -4294967292), 4); michael@0: assertEq(Number.parseInt("0x10", 1e308), 16); michael@0: assertEq(Number.parseInt("10", 1e308), 10); michael@0: assertEq(Number.parseInt("10", { valueOf: function() { return 16; } }), 16); michael@0: michael@0: michael@0: /* michael@0: * 7. Let stripPrefix be true. michael@0: * 8. If R ≠ 0, then michael@0: * a. If R < 2 or R > 36, then return NaN. michael@0: * b. If R ≠ 16, let stripPrefix be false. michael@0: * 9. Else, R = 0 michael@0: * a. Let R = 10. michael@0: * 10. If stripPrefix is true, then michael@0: * a. If the length of S is at least 2 and the first two characters of S michael@0: * are either “0x” or “0X”, then remove the first two characters from S and michael@0: * let R = 16. michael@0: */ michael@0: var vs = ["1", "51", "917", "2343", "99963"]; michael@0: for (var i = 0, sz = vs.length; i < sz; i++) michael@0: assertEq(Number.parseInt(vs[i], 0), Number.parseInt(vs[i], 10), "bad " + vs[i]); michael@0: michael@0: assertEq(Number.parseInt("0x10"), 16); michael@0: assertEq(Number.parseInt("0x10", 0), 16); michael@0: assertEq(Number.parseInt("0x10", 16), 16); michael@0: assertEq(Number.parseInt("0x10", 8), 0); michael@0: assertEq(Number.parseInt("-0x10", 16), -16); michael@0: michael@0: assertEq(Number.parseInt("5", 1), NaN); michael@0: assertEq(Number.parseInt("5", 37), NaN); michael@0: assertEq(Number.parseInt("5", { valueOf: function() { return -1; } }), NaN); michael@0: michael@0: michael@0: /* michael@0: * 11. If S contains any character that is not a radix-R digit, then let Z be michael@0: * the substring of S consisting of all characters before the first such michael@0: * character; otherwise, let Z be S. michael@0: * 12. If Z is empty, return NaN. michael@0: */ michael@0: assertEq(Number.parseInt(""), NaN); michael@0: assertEq(Number.parseInt("ohai"), NaN); michael@0: assertEq(Number.parseInt("0xohai"), NaN); michael@0: assertEq(Number.parseInt("-ohai"), NaN); michael@0: assertEq(Number.parseInt("+ohai"), NaN); michael@0: assertEq(Number.parseInt(" ohai"), NaN); michael@0: michael@0: assertEq(Number.parseInt("0xaohai"), 10); michael@0: assertEq(Number.parseInt("hohai", 18), 17); michael@0: michael@0: michael@0: /* michael@0: * 13. Let mathInt be the mathematical integer value that is represented by Z michael@0: * in radix-R notation, using the letters A-Z and a-z for digits with michael@0: * values 10 through 35. (However, if R is 10 and Z contains more than 20 michael@0: * significant digits, every significant digit after the 20th may be michael@0: * replaced by a 0 digit, at the option of the implementation; and if R is michael@0: * not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation- michael@0: * dependent approximation to the mathematical integer value that is michael@0: * represented by Z in radix-R notation.) michael@0: * 14. Let number be the Number value for mathInt. michael@0: * 15. Return sign × number. michael@0: */ michael@0: assertEq(Number.parseInt("ohai", 36), 1142154); michael@0: assertEq(Number.parseInt("0ohai", 36), 1142154); michael@0: assertEq(Number.parseInt("00ohai", 36), 1142154); michael@0: assertEq(Number.parseInt("A", 16), 10); michael@0: assertEq(Number.parseInt("0A", 16), 10); michael@0: assertEq(Number.parseInt("00A", 16), 10); michael@0: assertEq(Number.parseInt("A", 17), 10); michael@0: assertEq(Number.parseInt("0A", 17), 10); michael@0: assertEq(Number.parseInt("00A", 17), 10); michael@0: michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");