js/src/tests/ecma_6/Number/parseInt-01.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:d84923a2607b
1 // Any copyright is dedicated to the Public Domain.
2 // http://creativecommons.org/licenses/publicdomain/
3
4 //-----------------------------------------------------------------------------
5 var BUGNUMBER = 886949;
6 var summary = "ES6 (draft May 2013) 15.7.3.9 Number.parseInt(string, radix)";
7
8 print(BUGNUMBER + ": " + summary);
9
10 /**************
11 * BEGIN TEST *
12 **************/
13
14 var str, radix;
15 var upvar;
16
17 /* 1. Let inputString be ToString(string). */
18
19 assertEq(Number.parseInt({ toString: function() { return "17" } }, 10), 17);
20
21 upvar = 0;
22 str = { get toString() { upvar++; return function() { upvar++; return "12345"; } } };
23 assertEq(Number.parseInt(str, 10), 12345);
24 assertEq(upvar, 2);
25
26
27 /*
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.)
31 */
32
33 var ws =
34 ["\t", "\v", "\f", " ", "\xA0", "\uFEFF",
35 "\u2004", "\u3000", // a few Unicode whitespaces
36 "\r", "\n", "\u2028", "\u2029"];
37
38 str = "8675309";
39 for (var i = 0, sz = ws.length; i < sz; i++)
40 {
41 assertEq(Number.parseInt(ws[i] + str, 10), 8675309);
42 for (var j = 0, sz = ws.length; j < sz; j++)
43 {
44 assertEq(Number.parseInt(ws[i] + ws[j] + str, 10), 8675309,
45 ws[i].charCodeAt(0).toString(16) + ", " +
46 ws[j].charCodeAt(0).toString(16));
47 }
48 }
49
50
51 /*
52 * 3. Let sign be 1.
53 * 4. If S is not empty and the first character of S is a minus sign -, let
54 * sign be −1.
55 */
56 str = "5552368";
57 assertEq(Number.parseInt("-" + str, 10), -Number.parseInt(str, 10));
58 assertEq(Number.parseInt(" -" + str, 10), -Number.parseInt(str, 10));
59 assertEq(Number.parseInt("-", 10), NaN);
60 assertEq(Number.parseInt("", 10), NaN);
61 assertEq(Number.parseInt("-0", 10), -0);
62
63
64 /*
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.
67 */
68 assertEq(Number.parseInt("+12345", 10), 12345);
69 assertEq(Number.parseInt(" +12345", 10), 12345);
70 assertEq(Number.parseInt("-12345", 10), -12345);
71 assertEq(Number.parseInt(" -12345", 10), -12345);
72
73
74 /*
75 * 6. Let R = ToInt32(radix).
76 */
77
78 upvar = "";
79 str =
80 { toString: function() { if (!upvar) upvar = "string"; return "42"; } };
81 radix =
82 { toString: function() { if (!upvar) upvar = "radix"; return "10"; } };
83
84 assertEq(Number.parseInt(str, radix), 42);
85 assertEq(upvar, "string");
86
87 assertEq(Number.parseInt("123", null), 123);
88 assertEq(Number.parseInt("123", undefined), 123);
89 assertEq(Number.parseInt("123", NaN), 123);
90 assertEq(Number.parseInt("123", -0), 123);
91 assertEq(Number.parseInt("10", 72057594037927950), 16);
92 assertEq(Number.parseInt("10", -4294967292), 4);
93 assertEq(Number.parseInt("0x10", 1e308), 16);
94 assertEq(Number.parseInt("10", 1e308), 10);
95 assertEq(Number.parseInt("10", { valueOf: function() { return 16; } }), 16);
96
97
98 /*
99 * 7. Let stripPrefix be true.
100 * 8. If R ≠ 0, then
101 * a. If R < 2 or R > 36, then return NaN.
102 * b. If R ≠ 16, let stripPrefix be false.
103 * 9. Else, R = 0
104 * a. Let R = 10.
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
108 * let R = 16.
109 */
110 var vs = ["1", "51", "917", "2343", "99963"];
111 for (var i = 0, sz = vs.length; i < sz; i++)
112 assertEq(Number.parseInt(vs[i], 0), Number.parseInt(vs[i], 10), "bad " + vs[i]);
113
114 assertEq(Number.parseInt("0x10"), 16);
115 assertEq(Number.parseInt("0x10", 0), 16);
116 assertEq(Number.parseInt("0x10", 16), 16);
117 assertEq(Number.parseInt("0x10", 8), 0);
118 assertEq(Number.parseInt("-0x10", 16), -16);
119
120 assertEq(Number.parseInt("5", 1), NaN);
121 assertEq(Number.parseInt("5", 37), NaN);
122 assertEq(Number.parseInt("5", { valueOf: function() { return -1; } }), NaN);
123
124
125 /*
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.
130 */
131 assertEq(Number.parseInt(""), NaN);
132 assertEq(Number.parseInt("ohai"), NaN);
133 assertEq(Number.parseInt("0xohai"), NaN);
134 assertEq(Number.parseInt("-ohai"), NaN);
135 assertEq(Number.parseInt("+ohai"), NaN);
136 assertEq(Number.parseInt(" ohai"), NaN);
137
138 assertEq(Number.parseInt("0xaohai"), 10);
139 assertEq(Number.parseInt("hohai", 18), 17);
140
141
142 /*
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.
153 */
154 assertEq(Number.parseInt("ohai", 36), 1142154);
155 assertEq(Number.parseInt("0ohai", 36), 1142154);
156 assertEq(Number.parseInt("00ohai", 36), 1142154);
157 assertEq(Number.parseInt("A", 16), 10);
158 assertEq(Number.parseInt("0A", 16), 10);
159 assertEq(Number.parseInt("00A", 16), 10);
160 assertEq(Number.parseInt("A", 17), 10);
161 assertEq(Number.parseInt("0A", 17), 10);
162 assertEq(Number.parseInt("00A", 17), 10);
163
164
165 /******************************************************************************/
166
167 if (typeof reportCompare === "function")
168 reportCompare(true, true);
169
170 print("All tests passed!");

mercurial