summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma/GlobalObject/15.1.2.2-1.js
blob: 5e6a57f369f6f382bec1a9406f51897170e8d485 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


/**
   File Name:          15.1.2.2-1.js
   ECMA Section:       15.1.2.2 Function properties of the global object
   parseInt( string, radix )

   Description:

   The parseInt function produces an integer value dictated by intepretation
   of the contents of the string argument according to the specified radix.

   When the parseInt function is called, the following steps are taken:

   1.   Call ToString(string).
   2.   Compute a substring of Result(1) consisting of the leftmost character
   that is not a StrWhiteSpaceChar and all characters to the right of
   that character. (In other words, remove leading whitespace.)
   3.   Let sign be 1.
   4.   If Result(2) is not empty and the first character of Result(2) is a
   minus sign -, let sign be -1.
   5.   If Result(2) is not empty and the first character of Result(2) is a
   plus sign + or a minus sign -, then Result(5) is the substring of
   Result(2) produced by removing the first character; otherwise, Result(5)
   is Result(2).
   6.   If the radix argument is not supplied, go to step 12.
   7.   Call ToInt32(radix).
   8.   If Result(7) is zero, go to step 12; otherwise, if Result(7) < 2 or
   Result(7) > 36, return NaN.
   9.   Let R be Result(7).
   10.   If R = 16 and the length of Result(5) is at least 2 and the first two
   characters of Result(5) are either "0x" or "0X", let S be the substring
   of Result(5) consisting of all but the first two characters; otherwise,
   let S be Result(5).
   11.   Go to step 22.
   12.   If Result(5) is empty or the first character of Result(5) is not 0,
   go to step 20.
   13.   If the length of Result(5) is at least 2 and the second character of
   Result(5) is x or X, go to step 17.
   14.   Let R be 8.
   15.   Let S be Result(5).
   16.   Go to step 22.
   17.   Let R be 16.
   18.   Let S be the substring of Result(5) consisting of all but the first
   two characters.
   19.   Go to step 22.
   20.   Let R be 10.
   21.   Let S be Result(5).
   22.   If S contains any character that is not a radix-R digit, then let Z be
   the substring of S consisting of all characters to the left of the
   leftmost such character; otherwise, let Z be S.
   23.   If Z is empty, return NaN.
   24.   Compute the mathematical integer value that is represented by Z in
   radix-R notation. (But if R is 10 and Z contains more than 20
   significant digits, every 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 Result(24) may be an implementation-dependent
   approximation to the mathematical integer value that is represented
   by Z in radix-R notation.)
   25.   Compute the number value for Result(24).
   26.   Return sign Result(25).

   Note that parseInt may interpret only a leading portion of the string as
   an integer value; it ignores any characters that cannot be interpreted as
   part of the notation of an integer, and no indication is given that any
   such characters were ignored.

   Author:             christine@netscape.com
   Date:               28 october 1997

*/
var SECTION = "15.1.2.2-1";
var VERSION = "ECMA_1";
var TITLE   = "parseInt(string, radix)";
var BUGNUMBER = "none";

startTest();

writeHeaderToLog( SECTION + " "+ TITLE);

var HEX_STRING = "0x0";
var HEX_VALUE = 0;

new TestCase( SECTION, 
	      "parseInt.length",     
	      2,     
	      parseInt.length );

new TestCase( SECTION, 
	      "parseInt.length = 0; parseInt.length",    
	      2,     
	      eval("parseInt.length = 0; parseInt.length") );

new TestCase( SECTION, 
	      "var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS",   "",
	      eval("var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS") );

new TestCase( SECTION, 
	      "parseInt()",      
	      NaN,   
	      parseInt() );

new TestCase( SECTION, 
	      "parseInt('')",    
	      NaN,   
	      parseInt("") );

new TestCase( SECTION, 
	      "parseInt('','')", 
	      NaN,   
	      parseInt("","") );

new TestCase( SECTION,
	      "parseInt(\"     0xabcdef     ",
	      11259375,
	      parseInt( "      0xabcdef     " ));

new TestCase( SECTION,
	      "parseInt(\"     0XABCDEF     ",
	      11259375,
	      parseInt( "      0XABCDEF     " ) );

new TestCase( SECTION,
	      "parseInt( 0xabcdef )",
	      11259375,
	      parseInt( "0xabcdef") );

new TestCase( SECTION,
	      "parseInt( 0XABCDEF )",
	      11259375,
	      parseInt( "0XABCDEF") );

for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', null)",    HEX_VALUE,  parseInt(HEX_STRING,null) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', void 0)",    HEX_VALUE,  parseInt(HEX_STRING, void 0) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}

// a few tests with spaces

for ( var space = " ", HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0;
      POWER < 15;
      POWER++, HEX_STRING = HEX_STRING +"f", space += " ")
{
  new TestCase( SECTION, "parseInt('"+space+HEX_STRING+space+"', void 0)",    HEX_VALUE,  parseInt(space+HEX_STRING+space, void 0) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}

new TestCase(SECTION, "parseInt(BOM + '123', 10)", 123, parseInt("\uFEFF" + "123", 10));

// a few tests with negative numbers
for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
  HEX_VALUE -= Math.pow(16,POWER)*15;
}

// we should stop parsing when we get to a value that is not a numeric literal for the type we expect

for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"g', 16)",    HEX_VALUE,  parseInt(HEX_STRING+"g",16) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"G', 16)",    HEX_VALUE,  parseInt(HEX_STRING+"G",16) );
  HEX_VALUE += Math.pow(16,POWER)*15;
}

for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
  HEX_VALUE -= Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "-0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
  HEX_VALUE -= Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
  HEX_VALUE -= Math.pow(16,POWER)*15;
}
for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
  HEX_VALUE -= Math.pow(16,POWER)*15;
}

// Numbers that start with 0 and do not provide a radix should use 10 as radix
// per ES5, not octal (as it was in ES3).

var OCT_STRING = "0";
var OCT_VALUE = 0;

for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"')",    OCT_VALUE,  parseInt(OCT_STRING) );
  OCT_VALUE += Math.pow(10,POWER)*7;
}

for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"')",    OCT_VALUE,  parseInt(OCT_STRING) );
  OCT_VALUE -= Math.pow(10,POWER)*7;
}

// should get octal-based results if we provid the radix of 8 (or 010)

for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"', 8)",    OCT_VALUE,  parseInt(OCT_STRING,8) );
  OCT_VALUE += Math.pow(8,POWER)*7;
}
for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"', 010)",    OCT_VALUE,  parseInt(OCT_STRING,010) );
  OCT_VALUE -= Math.pow(8,POWER)*7;
}

// we shall stop parsing digits when we get one that isn't a numeric literal of the type we think
// it should be.
for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"8', 8)",    OCT_VALUE,  parseInt(OCT_STRING+"8",8) );
  OCT_VALUE += Math.pow(8,POWER)*7;
}
for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
  new TestCase( SECTION, "parseInt('"+OCT_STRING+"8', 010)",    OCT_VALUE,  parseInt(OCT_STRING+"8",010) );
  OCT_VALUE -= Math.pow(8,POWER)*7;
}

new TestCase( SECTION,
	      "parseInt( '0x' )",             
	      NaN,       
	      parseInt("0x") );

new TestCase( SECTION,
	      "parseInt( '0X' )",             
	      NaN,       
	      parseInt("0X") );

new TestCase( SECTION,
	      "parseInt( '11111111112222222222' )",   
	      11111111112222222222,  
	      parseInt("11111111112222222222") );

new TestCase( SECTION,
	      "parseInt( '111111111122222222223' )",   
	      111111111122222222220,  
	      parseInt("111111111122222222223") );

new TestCase( SECTION,
	      "parseInt( '11111111112222222222',10 )",   
	      11111111112222222222,  
	      parseInt("11111111112222222222",10) );

new TestCase( SECTION,
	      "parseInt( '111111111122222222223',10 )",   
	      111111111122222222220,  
	      parseInt("111111111122222222223",10) );

new TestCase( SECTION,
	      "parseInt( '01234567890', -1 )", 
	      Number.NaN,   
	      parseInt("01234567890",-1) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 0 )", 
	      1234567890,
	      parseInt("01234567890", 0) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 1 )", 
	      Number.NaN,    
	      parseInt("01234567890",1) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 2 )", 
	      1,             
	      parseInt("01234567890",2) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 3 )", 
	      5,             
	      parseInt("01234567890",3) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 4 )", 
	      27,            
	      parseInt("01234567890",4) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 5 )", 
	      194,           
	      parseInt("01234567890",5) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 6 )", 
	      1865,          
	      parseInt("01234567890",6) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 7 )", 
	      22875,         
	      parseInt("01234567890",7) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 8 )", 
	      342391,        
	      parseInt("01234567890",8) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 9 )", 
	      6053444,       
	      parseInt("01234567890",9) );

new TestCase( SECTION,
	      "parseInt( '01234567890', 10 )",
	      1234567890,
	      parseInt("01234567890",10) );

// need more test cases with hex radix

new TestCase( SECTION,
	      "parseInt( '1234567890', '0xa')",
	      1234567890,
	      parseInt("1234567890","0xa") );

new TestCase( SECTION,
	      "parseInt( '012345', 11 )",     
	      17715,         
	      parseInt("012345",11) );

new TestCase( SECTION,
	      "parseInt( '012345', 35 )",     
	      1590195,       
	      parseInt("012345",35) );

new TestCase( SECTION,
	      "parseInt( '012345', 36 )",     
	      1776965,       
	      parseInt("012345",36) );

new TestCase( SECTION,
	      "parseInt( '012345', 37 )",     
	      Number.NaN,    
	      parseInt("012345",37) );

test();