summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/global/parseInt-01.js
blob: 0417086697e916212b593789d8e48fac0b65d047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

//-----------------------------------------------------------------------------
var BUGNUMBER = 577536;
var summary = "ES5 15.1.2.2 parseInt(string, radix)";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

var str, radix;
var upvar;

/* 1. Let inputString be ToString(string). */

assertEq(parseInt({ toString: function() { return "17" } }, 10), 17);

upvar = 0;
str = { get toString() { upvar++; return function() { upvar++; return "12345"; } } };
assertEq(parseInt(str, 10), 12345);
assertEq(upvar, 2);


/*
 * 2. Let S be a newly created substring of inputString consisting of the first
 *    character that is not a StrWhiteSpaceChar and all characters following
 *    that character. (In other words, remove leading white space.)
 */

var ws =
  ["\t", "\v", "\f", " ", "\xA0", "\uFEFF",
     "\u2004", "\u3000", // a few Unicode whitespaces
   "\r", "\n", "\u2028", "\u2029"];

str = "8675309";
for (var i = 0, sz = ws.length; i < sz; i++)
{
  assertEq(parseInt(ws[i] + str, 10), 8675309);
  for (var j = 0, sz = ws.length; j < sz; j++)
  {
    assertEq(parseInt(ws[i] + ws[j] + str, 10), 8675309,
             ws[i].charCodeAt(0).toString(16) + ", " +
             ws[j].charCodeAt(0).toString(16));
  }
}


/*
 * 3. Let sign be 1.
 * 4. If S is not empty and the first character of S is a minus sign -, let
 *    sign be −1.
 */
str = "5552368";
assertEq(parseInt("-" + str, 10), -parseInt(str, 10));
assertEq(parseInt(" -" + str, 10), -parseInt(str, 10));
assertEq(parseInt("-", 10), NaN);
assertEq(parseInt("", 10), NaN);
assertEq(parseInt("-0", 10), -0);


/*
 * 5. If S is not empty and the first character of S is a plus sign + or a
 *    minus sign -, then remove the first character from S.
 */
assertEq(parseInt("+12345", 10), 12345);
assertEq(parseInt(" +12345", 10), 12345);
assertEq(parseInt("-12345", 10), -12345);
assertEq(parseInt(" -12345", 10), -12345);


/*
 * 6.  Let R = ToInt32(radix).
 */

upvar = "";
str =
  { toString: function() { if (!upvar) upvar = "string"; return "42"; } };
radix =
  { toString: function() { if (!upvar) upvar = "radix"; return "10"; } };

assertEq(parseInt(str, radix), 42);
assertEq(upvar, "string");

assertEq(parseInt("123", null), 123);
assertEq(parseInt("123", undefined), 123);
assertEq(parseInt("123", NaN), 123);
assertEq(parseInt("123", -0), 123);
assertEq(parseInt("10", 72057594037927950), 16);
assertEq(parseInt("10", -4294967292), 4);
assertEq(parseInt("0x10", 1e308), 16);
assertEq(parseInt("10", 1e308), 10);
assertEq(parseInt("10", { valueOf: function() { return 16; } }), 16);


/*
 * 7.  Let stripPrefix be true.
 * 8.  If R ≠ 0, then
 *     a. If R < 2 or R > 36, then return NaN.
 *     b. If R ≠ 16, let stripPrefix be false.
 * 9.  Else, R = 0
 *     a. Let R = 10.
 * 10. If stripPrefix is true, then
 *     a. If the length of S is at least 2 and the first two characters of S
 *     are either “0x” or “0X”, then remove the first two characters from S and
 *     let R = 16.
 */
var vs = ["1", "51", "917", "2343", "99963"];
for (var i = 0, sz = vs.length; i < sz; i++)
  assertEq(parseInt(vs[i], 0), parseInt(vs[i], 10), "bad " + vs[i]);

assertEq(parseInt("0x10"), 16);
assertEq(parseInt("0x10", 0), 16);
assertEq(parseInt("0x10", 16), 16);
assertEq(parseInt("0x10", 8), 0);
assertEq(parseInt("-0x10", 16), -16);

assertEq(parseInt("5", 1), NaN);
assertEq(parseInt("5", 37), NaN);
assertEq(parseInt("5", { valueOf: function() { return -1; } }), NaN);


/*
 * 11. If S contains any character that is not a radix-R digit, then let Z be
 *     the substring of S consisting of all characters before the first such
 *     character; otherwise, let Z be S.
 * 12. If Z is empty, return NaN.
 */
assertEq(parseInt(""), NaN);
assertEq(parseInt("ohai"), NaN);
assertEq(parseInt("0xohai"), NaN);
assertEq(parseInt("-ohai"), NaN);
assertEq(parseInt("+ohai"), NaN);
assertEq(parseInt(" ohai"), NaN);

assertEq(parseInt("0xaohai"), 10);
assertEq(parseInt("hohai", 18), 17);


/*
 * 13. Let mathInt be the mathematical integer value that is represented by Z
 *     in radix-R notation, using the letters A-Z and a-z for digits with
 *     values 10 through 35. (However, if R is 10 and Z contains more than 20
 *     significant digits, every significant digit after the 20th may be
 *     replaced by a 0 digit, at the option of the implementation; and if R is
 *     not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-
 *     dependent approximation to the mathematical integer value that is
 *     represented by Z in radix-R notation.)
 * 14. Let number be the Number value for mathInt.
 * 15. Return sign × number.
 */
assertEq(parseInt("ohai", 36), 1142154);
assertEq(parseInt("0ohai", 36), 1142154);
assertEq(parseInt("00ohai", 36), 1142154);
assertEq(parseInt("A", 16), 10);
assertEq(parseInt("0A", 16), 10);
assertEq(parseInt("00A", 16), 10);
assertEq(parseInt("A", 17), 10);
assertEq(parseInt("0A", 17), 10);
assertEq(parseInt("00A", 17), 10);


/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("All tests passed!");