js/src/tests/ecma/GlobalObject/15.1.2.2-1.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial