Debian Codegraph @

rhino/testsrc/tests/ecma/GlobalObject/15.1.2.2-1.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = '15.1.2.2-1.js';
7
8 /**
9 File Name: 15.1.2.2-1.js
10 ECMA Section: 15.1.2.2 Function properties of the global object
11 parseInt( string, radix )
12
13 Description:
14
15 The parseInt function produces an integer value dictated by intepretation
16 of the contents of the string argument according to the specified radix.
17
18 When the parseInt function is called, the following steps are taken:
19
20 1. Call ToString(string).
21 2. Compute a substring of Result(1) consisting of the leftmost character
22 that is not a StrWhiteSpaceChar and all characters to the right of
23 that character. (In other words, remove leading whitespace.)
24 3. Let sign be 1.
25 4. If Result(2) is not empty and the first character of Result(2) is a
26 minus sign -, let sign be -1.
27 5. If Result(2) is not empty and the first character of Result(2) is a
28 plus sign + or a minus sign -, then Result(5) is the substring of
29 Result(2) produced by removing the first character; otherwise, Result(5)
30 is Result(2).
31 6. If the radix argument is not supplied, go to step 12.
32 7. Call ToInt32(radix).
33 8. If Result(7) is zero, go to step 12; otherwise, if Result(7) < 2 or
34 Result(7) > 36, return NaN.
35 9. Let R be Result(7).
36 10. If R = 16 and the length of Result(5) is at least 2 and the first two
37 characters of Result(5) are either "0x" or "0X", let S be the substring
38 of Result(5) consisting of all but the first two characters; otherwise,
39 let S be Result(5).
40 11. Go to step 22.
41 12. If Result(5) is empty or the first character of Result(5) is not 0,
42 go to step 20.
43 13. If the length of Result(5) is at least 2 and the second character of
44 Result(5) is x or X, go to step 17.
45 14. Let R be 8.
46 15. Let S be Result(5).
47 16. Go to step 22.
48 17. Let R be 16.
49 18. Let S be the substring of Result(5) consisting of all but the first
50 two characters.
51 19. Go to step 22.
52 20. Let R be 10.
53 21. Let S be Result(5).
54 22. If S contains any character that is not a radix-R digit, then let Z be
55 the substring of S consisting of all characters to the left of the
56 leftmost such character; otherwise, let Z be S.
57 23. If Z is empty, return NaN.
58 24. Compute the mathematical integer value that is represented by Z in
59 radix-R notation. (But if R is 10 and Z contains more than 20
60 significant digits, every digit after the 20th may be replaced by a 0
61 digit, at the option of the implementation; and if R is not 2, 4, 8,
62 10, 16, or 32, then Result(24) may be an implementation-dependent
63 approximation to the mathematical integer value that is represented
64 by Z in radix-R notation.)
65 25. Compute the number value for Result(24).
66 26. Return sign Result(25).
67
68 Note that parseInt may interpret only a leading portion of the string as
69 an integer value; it ignores any characters that cannot be interpreted as
70 part of the notation of an integer, and no indication is given that any
71 such characters were ignored.
72
73 Author: [email protected]
74 Date: 28 october 1997
75
76 */
77 var SECTION = "15.1.2.2-1";
78 var VERSION = "ECMA_1";
79 var TITLE = "parseInt(string, radix)";
80 var BUGNUMBER = "none";
81
82 startTest();
83
84 writeHeaderToLog( SECTION + " "+ TITLE);
85
86 var HEX_STRING = "0x0";
87 var HEX_VALUE = 0;
88
89 new TestCase( SECTION,
90 "parseInt.length",
91 2,
92 parseInt.length );
93
94 new TestCase( SECTION,
95 "parseInt.length = 0; parseInt.length",
96 2,
97 eval("parseInt.length = 0; parseInt.length") );
98
99 new TestCase( SECTION,
100 "var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS", "prototype",
101 eval("var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS") );
102
103 new TestCase( SECTION,
104 "delete parseInt.length",
105 false,
106 delete parseInt.length );
107
108 new TestCase( SECTION,
109 "delete parseInt.length; parseInt.length",
110 2,
111 eval("delete parseInt.length; parseInt.length") );
112
113 new TestCase( SECTION,
114 "parseInt.length = null; parseInt.length",
115 2,
116 eval("parseInt.length = null; parseInt.length") );
117
118 new TestCase( SECTION,
119 "parseInt()",
120 NaN,
121 parseInt() );
122
123 new TestCase( SECTION,
124 "parseInt('')",
125 NaN,
126 parseInt("") );
127
128 new TestCase( SECTION,
129 "parseInt('','')",
130 NaN,
131 parseInt("","") );
132
133 new TestCase( SECTION,
134 "parseInt(\" 0xabcdef ",
135 11259375,
136 parseInt( " 0xabcdef " ));
137
138 new TestCase( SECTION,
139 "parseInt(\" 0XABCDEF ",
140 11259375,
141 parseInt( " 0XABCDEF " ) );
142
143 new TestCase( SECTION,
144 "parseInt( 0xabcdef )",
145 11259375,
146 parseInt( "0xabcdef") );
147
148 new TestCase( SECTION,
149 "parseInt( 0XABCDEF )",
150 11259375,
151 parseInt( "0XABCDEF") );
152
153 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
154 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
155 HEX_VALUE += Math.pow(16,POWER)*15;
156 }
157 for ( HEX_STRING = "0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
158 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
159 HEX_VALUE += Math.pow(16,POWER)*15;
160 }
161 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
162 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
163 HEX_VALUE += Math.pow(16,POWER)*15;
164 }
165 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
166 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
167 HEX_VALUE += Math.pow(16,POWER)*15;
168 }
169 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
170 new TestCase( SECTION, "parseInt("+HEX_STRING+",null)", HEX_VALUE, parseInt(HEX_STRING,null) );
171 HEX_VALUE += Math.pow(16,POWER)*15;
172 }
173 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
174 new TestCase( SECTION, "parseInt("+HEX_STRING+", void 0)", HEX_VALUE, parseInt(HEX_STRING, void 0) );
175 HEX_VALUE += Math.pow(16,POWER)*15;
176 }
177
178 // a few tests with spaces
179
180 for ( var space = " ", HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0;
181 POWER < 15;
182 POWER++, HEX_STRING = HEX_STRING +"f", space += " ")
183 {
184 new TestCase( SECTION, "parseInt("+space+HEX_STRING+space+", void 0)", HEX_VALUE, parseInt(space+HEX_STRING+space, void 0) );
185 HEX_VALUE += Math.pow(16,POWER)*15;
186 }
187
188 // a few tests with negative numbers
189 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
190 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
191 HEX_VALUE -= Math.pow(16,POWER)*15;
192 }
193
194 // we should stop parsing when we get to a value that is not a numeric literal for the type we expect
195
196 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
197 new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)", HEX_VALUE, parseInt(HEX_STRING+"g",16) );
198 HEX_VALUE += Math.pow(16,POWER)*15;
199 }
200 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
201 new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)", HEX_VALUE, parseInt(HEX_STRING+"G",16) );
202 HEX_VALUE += Math.pow(16,POWER)*15;
203 }
204
205 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
206 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
207 HEX_VALUE -= Math.pow(16,POWER)*15;
208 }
209 for ( HEX_STRING = "-0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
210 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
211 HEX_VALUE -= Math.pow(16,POWER)*15;
212 }
213 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
214 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
215 HEX_VALUE -= Math.pow(16,POWER)*15;
216 }
217 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
218 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
219 HEX_VALUE -= Math.pow(16,POWER)*15;
220 }
221
222 // let us do some octal tests. numbers that start with 0 and do not provid a radix should
223 // default to using "0" as a radix.
224
225 var OCT_STRING = "0";
226 var OCT_VALUE = 0;
227
228 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
229 new TestCase( SECTION, "parseInt("+OCT_STRING+")", OCT_VALUE, parseInt(OCT_STRING) );
230 OCT_VALUE += Math.pow(8,POWER)*7;
231 }
232
233 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
234 new TestCase( SECTION, "parseInt("+OCT_STRING+")", OCT_VALUE, parseInt(OCT_STRING) );
235 OCT_VALUE -= Math.pow(8,POWER)*7;
236 }
237
238 // should get the same results as above if we provid the radix of 8 (or 010)
239
240 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
241 new TestCase( SECTION, "parseInt("+OCT_STRING+",8)", OCT_VALUE, parseInt(OCT_STRING,8) );
242 OCT_VALUE += Math.pow(8,POWER)*7;
243 }
244 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
245 new TestCase( SECTION, "parseInt("+OCT_STRING+",010)", OCT_VALUE, parseInt(OCT_STRING,010) );
246 OCT_VALUE -= Math.pow(8,POWER)*7;
247 }
248
249 // we shall stop parsing digits when we get one that isn't a numeric literal of the type we think
250 // it should be.
251 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
252 new TestCase( SECTION, "parseInt("+OCT_STRING+"8,8)", OCT_VALUE, parseInt(OCT_STRING+"8",8) );
253 OCT_VALUE += Math.pow(8,POWER)*7;
254 }
255 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
256 new TestCase( SECTION, "parseInt("+OCT_STRING+"8,010)", OCT_VALUE, parseInt(OCT_STRING+"8",010) );
257 OCT_VALUE -= Math.pow(8,POWER)*7;
258 }
259
260 new TestCase( SECTION,
261 "parseInt( '0x' )",
262 NaN,
263 parseInt("0x") );
264
265 new TestCase( SECTION,
266 "parseInt( '0X' )",
267 NaN,
268 parseInt("0X") );
269
270 new TestCase( SECTION,
271 "parseInt( '11111111112222222222' )",
272 11111111112222222222,
273 parseInt("11111111112222222222") );
274
275 new TestCase( SECTION,
276 "parseInt( '111111111122222222223' )",
277 111111111122222222220,
278 parseInt("111111111122222222223") );
279
280 new TestCase( SECTION,
281 "parseInt( '11111111112222222222',10 )",
282 11111111112222222222,
283 parseInt("11111111112222222222",10) );
284
285 new TestCase( SECTION,
286 "parseInt( '111111111122222222223',10 )",
287 111111111122222222220,
288 parseInt("111111111122222222223",10) );
289
290 new TestCase( SECTION,
291 "parseInt( '01234567890', -1 )",
292 Number.NaN,
293 parseInt("01234567890",-1) );
294
295 new TestCase( SECTION,
296 "parseInt( '01234567890', 0 )",
297 Number.NaN,
298 parseInt("01234567890",1) );
299
300 new TestCase( SECTION,
301 "parseInt( '01234567890', 1 )",
302 Number.NaN,
303 parseInt("01234567890",1) );
304
305 new TestCase( SECTION,
306 "parseInt( '01234567890', 2 )",
307 1,
308 parseInt("01234567890",2) );
309
310 new TestCase( SECTION,
311 "parseInt( '01234567890', 3 )",
312 5,
313 parseInt("01234567890",3) );
314
315 new TestCase( SECTION,
316 "parseInt( '01234567890', 4 )",
317 27,
318 parseInt("01234567890",4) );
319
320 new TestCase( SECTION,
321 "parseInt( '01234567890', 5 )",
322 194,
323 parseInt("01234567890",5) );
324
325 new TestCase( SECTION,
326 "parseInt( '01234567890', 6 )",
327 1865,
328 parseInt("01234567890",6) );
329
330 new TestCase( SECTION,
331 "parseInt( '01234567890', 7 )",
332 22875,
333 parseInt("01234567890",7) );
334
335 new TestCase( SECTION,
336 "parseInt( '01234567890', 8 )",
337 342391,
338 parseInt("01234567890",8) );
339
340 new TestCase( SECTION,
341 "parseInt( '01234567890', 9 )",
342 6053444,
343 parseInt("01234567890",9) );
344
345 new TestCase( SECTION,
346 "parseInt( '01234567890', 10 )",
347 1234567890,
348 parseInt("01234567890",10) );
349
350 // need more test cases with hex radix
351
352 new TestCase( SECTION,
353 "parseInt( '1234567890', '0xa')",
354 1234567890,
355 parseInt("1234567890","0xa") );
356
357 new TestCase( SECTION,
358 "parseInt( '012345', 11 )",
359 17715,
360 parseInt("012345",11) );
361
362 new TestCase( SECTION,
363 "parseInt( '012345', 35 )",
364 1590195,
365 parseInt("012345",35) );
366
367 new TestCase( SECTION,
368 "parseInt( '012345', 36 )",
369 1776965,
370 parseInt("012345",36) );
371
372 new TestCase( SECTION,
373 "parseInt( '012345', 37 )",
374 Number.NaN,
375 parseInt("012345",37) );
376
377 test();