1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 gTestfile = '15.1.2.2-1.js';
42 File Name: 15.1.2.2-1.js
43 ECMA Section: 15.1.2.2 Function properties of the global object
44 parseInt( string, radix )
48 The parseInt function produces an integer value dictated by intepretation
49 of the contents of the string argument according to the specified radix.
51 When the parseInt function is called, the following steps are taken:
53 1. Call ToString(string).
54 2. Compute a substring of Result(1) consisting of the leftmost character
55 that is not a StrWhiteSpaceChar and all characters to the right of
56 that character. (In other words, remove leading whitespace.)
58 4. If Result(2) is not empty and the first character of Result(2) is a
59 minus sign -, let sign be -1.
60 5. If Result(2) is not empty and the first character of Result(2) is a
61 plus sign + or a minus sign -, then Result(5) is the substring of
62 Result(2) produced by removing the first character; otherwise, Result(5)
64 6. If the radix argument is not supplied, go to step 12.
65 7. Call ToInt32(radix).
66 8. If Result(7) is zero, go to step 12; otherwise, if Result(7) < 2 or
67 Result(7) > 36, return NaN.
68 9. Let R be Result(7).
69 10. If R = 16 and the length of Result(5) is at least 2 and the first two
70 characters of Result(5) are either "0x" or "0X", let S be the substring
71 of Result(5) consisting of all but the first two characters; otherwise,
74 12. If Result(5) is empty or the first character of Result(5) is not 0,
76 13. If the length of Result(5) is at least 2 and the second character of
77 Result(5) is x or X, go to step 17.
79 15. Let S be Result(5).
82 18. Let S be the substring of Result(5) consisting of all but the first
86 21. Let S be Result(5).
87 22. If S contains any character that is not a radix-R digit, then let Z be
88 the substring of S consisting of all characters to the left of the
89 leftmost such character; otherwise, let Z be S.
90 23. If Z is empty, return NaN.
91 24. Compute the mathematical integer value that is represented by Z in
92 radix-R notation. (But if R is 10 and Z contains more than 20
93 significant digits, every digit after the 20th may be replaced by a 0
94 digit, at the option of the implementation; and if R is not 2, 4, 8,
95 10, 16, or 32, then Result(24) may be an implementation-dependent
96 approximation to the mathematical integer value that is represented
97 by Z in radix-R notation.)
98 25. Compute the number value for Result(24).
99 26. Return sign Result(25).
101 Note that parseInt may interpret only a leading portion of the string as
102 an integer value; it ignores any characters that cannot be interpreted as
103 part of the notation of an integer, and no indication is given that any
104 such characters were ignored.
106 Author: christine@netscape.com
107 Date: 28 october 1997
110 var SECTION = "15.1.2.2-1";
111 var VERSION = "ECMA_1";
112 var TITLE = "parseInt(string, radix)";
113 var BUGNUMBER = "none";
117 writeHeaderToLog( SECTION + " "+ TITLE);
119 var HEX_STRING = "0x0";
122 new TestCase( SECTION,
127 new TestCase( SECTION,
128 "parseInt.length = 0; parseInt.length",
130 eval("parseInt.length = 0; parseInt.length") );
132 new TestCase( SECTION,
133 "var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS", "prototype",
134 eval("var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS") );
136 new TestCase( SECTION,
137 "delete parseInt.length",
139 delete parseInt.length );
141 new TestCase( SECTION,
142 "delete parseInt.length; parseInt.length",
144 eval("delete parseInt.length; parseInt.length") );
146 new TestCase( SECTION,
147 "parseInt.length = null; parseInt.length",
149 eval("parseInt.length = null; parseInt.length") );
151 new TestCase( SECTION,
156 new TestCase( SECTION,
161 new TestCase( SECTION,
166 new TestCase( SECTION,
167 "parseInt(\" 0xabcdef ",
169 parseInt( " 0xabcdef " ));
171 new TestCase( SECTION,
172 "parseInt(\" 0XABCDEF ",
174 parseInt( " 0XABCDEF " ) );
176 new TestCase( SECTION,
177 "parseInt( 0xabcdef )",
179 parseInt( "0xabcdef") );
181 new TestCase( SECTION,
182 "parseInt( 0XABCDEF )",
184 parseInt( "0XABCDEF") );
186 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
187 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
188 HEX_VALUE += Math.pow(16,POWER)*15;
190 for ( HEX_STRING = "0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
191 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
192 HEX_VALUE += Math.pow(16,POWER)*15;
194 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
195 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
196 HEX_VALUE += Math.pow(16,POWER)*15;
198 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
199 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
200 HEX_VALUE += Math.pow(16,POWER)*15;
202 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
203 new TestCase( SECTION, "parseInt("+HEX_STRING+",null)", HEX_VALUE, parseInt(HEX_STRING,null) );
204 HEX_VALUE += Math.pow(16,POWER)*15;
206 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
207 new TestCase( SECTION, "parseInt("+HEX_STRING+", void 0)", HEX_VALUE, parseInt(HEX_STRING, void 0) );
208 HEX_VALUE += Math.pow(16,POWER)*15;
211 // a few tests with spaces
213 for ( var space = " ", HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0;
215 POWER++, HEX_STRING = HEX_STRING +"f", space += " ")
217 new TestCase( SECTION, "parseInt("+space+HEX_STRING+space+", void 0)", HEX_VALUE, parseInt(space+HEX_STRING+space, void 0) );
218 HEX_VALUE += Math.pow(16,POWER)*15;
221 // a few tests with negative numbers
222 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
223 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
224 HEX_VALUE -= Math.pow(16,POWER)*15;
227 // we should stop parsing when we get to a value that is not a numeric literal for the type we expect
229 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
230 new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)", HEX_VALUE, parseInt(HEX_STRING+"g",16) );
231 HEX_VALUE += Math.pow(16,POWER)*15;
233 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
234 new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)", HEX_VALUE, parseInt(HEX_STRING+"G",16) );
235 HEX_VALUE += Math.pow(16,POWER)*15;
238 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
239 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
240 HEX_VALUE -= Math.pow(16,POWER)*15;
242 for ( HEX_STRING = "-0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
243 new TestCase( SECTION, "parseInt("+HEX_STRING+")", HEX_VALUE, parseInt(HEX_STRING) );
244 HEX_VALUE -= Math.pow(16,POWER)*15;
246 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
247 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
248 HEX_VALUE -= Math.pow(16,POWER)*15;
250 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
251 new TestCase( SECTION, "parseInt("+HEX_STRING+",16)", HEX_VALUE, parseInt(HEX_STRING,16) );
252 HEX_VALUE -= Math.pow(16,POWER)*15;
255 // let us do some octal tests. numbers that start with 0 and do not provid a radix should
256 // default to using "0" as a radix.
258 var OCT_STRING = "0";
261 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
262 new TestCase( SECTION, "parseInt("+OCT_STRING+")", OCT_VALUE, parseInt(OCT_STRING) );
263 OCT_VALUE += Math.pow(8,POWER)*7;
266 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
267 new TestCase( SECTION, "parseInt("+OCT_STRING+")", OCT_VALUE, parseInt(OCT_STRING) );
268 OCT_VALUE -= Math.pow(8,POWER)*7;
271 // should get the same results as above if we provid the radix of 8 (or 010)
273 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
274 new TestCase( SECTION, "parseInt("+OCT_STRING+",8)", OCT_VALUE, parseInt(OCT_STRING,8) );
275 OCT_VALUE += Math.pow(8,POWER)*7;
277 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
278 new TestCase( SECTION, "parseInt("+OCT_STRING+",010)", OCT_VALUE, parseInt(OCT_STRING,010) );
279 OCT_VALUE -= Math.pow(8,POWER)*7;
282 // we shall stop parsing digits when we get one that isn't a numeric literal of the type we think
284 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
285 new TestCase( SECTION, "parseInt("+OCT_STRING+"8,8)", OCT_VALUE, parseInt(OCT_STRING+"8",8) );
286 OCT_VALUE += Math.pow(8,POWER)*7;
288 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
289 new TestCase( SECTION, "parseInt("+OCT_STRING+"8,010)", OCT_VALUE, parseInt(OCT_STRING+"8",010) );
290 OCT_VALUE -= Math.pow(8,POWER)*7;
293 new TestCase( SECTION,
298 new TestCase( SECTION,
303 new TestCase( SECTION,
304 "parseInt( '11111111112222222222' )",
305 11111111112222222222,
306 parseInt("11111111112222222222") );
308 new TestCase( SECTION,
309 "parseInt( '111111111122222222223' )",
310 111111111122222222220,
311 parseInt("111111111122222222223") );
313 new TestCase( SECTION,
314 "parseInt( '11111111112222222222',10 )",
315 11111111112222222222,
316 parseInt("11111111112222222222",10) );
318 new TestCase( SECTION,
319 "parseInt( '111111111122222222223',10 )",
320 111111111122222222220,
321 parseInt("111111111122222222223",10) );
323 new TestCase( SECTION,
324 "parseInt( '01234567890', -1 )",
326 parseInt("01234567890",-1) );
328 new TestCase( SECTION,
329 "parseInt( '01234567890', 0 )",
331 parseInt("01234567890",1) );
333 new TestCase( SECTION,
334 "parseInt( '01234567890', 1 )",
336 parseInt("01234567890",1) );
338 new TestCase( SECTION,
339 "parseInt( '01234567890', 2 )",
341 parseInt("01234567890",2) );
343 new TestCase( SECTION,
344 "parseInt( '01234567890', 3 )",
346 parseInt("01234567890",3) );
348 new TestCase( SECTION,
349 "parseInt( '01234567890', 4 )",
351 parseInt("01234567890",4) );
353 new TestCase( SECTION,
354 "parseInt( '01234567890', 5 )",
356 parseInt("01234567890",5) );
358 new TestCase( SECTION,
359 "parseInt( '01234567890', 6 )",
361 parseInt("01234567890",6) );
363 new TestCase( SECTION,
364 "parseInt( '01234567890', 7 )",
366 parseInt("01234567890",7) );
368 new TestCase( SECTION,
369 "parseInt( '01234567890', 8 )",
371 parseInt("01234567890",8) );
373 new TestCase( SECTION,
374 "parseInt( '01234567890', 9 )",
376 parseInt("01234567890",9) );
378 new TestCase( SECTION,
379 "parseInt( '01234567890', 10 )",
381 parseInt("01234567890",10) );
383 // need more test cases with hex radix
385 new TestCase( SECTION,
386 "parseInt( '1234567890', '0xa')",
388 parseInt("1234567890","0xa") );
390 new TestCase( SECTION,
391 "parseInt( '012345', 11 )",
393 parseInt("012345",11) );
395 new TestCase( SECTION,
396 "parseInt( '012345', 35 )",
398 parseInt("012345",35) );
400 new TestCase( SECTION,
401 "parseInt( '012345', 36 )",
403 parseInt("012345",36) );
405 new TestCase( SECTION,
406 "parseInt( '012345', 37 )",
408 parseInt("012345",37) );