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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/GlobalObject/15.1.2.2-1.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,378 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          15.1.2.2-1.js
    1.12 +   ECMA Section:       15.1.2.2 Function properties of the global object
    1.13 +   parseInt( string, radix )
    1.14 +
    1.15 +   Description:
    1.16 +
    1.17 +   The parseInt function produces an integer value dictated by intepretation
    1.18 +   of the contents of the string argument according to the specified radix.
    1.19 +
    1.20 +   When the parseInt function is called, the following steps are taken:
    1.21 +
    1.22 +   1.   Call ToString(string).
    1.23 +   2.   Compute a substring of Result(1) consisting of the leftmost character
    1.24 +   that is not a StrWhiteSpaceChar and all characters to the right of
    1.25 +   that character. (In other words, remove leading whitespace.)
    1.26 +   3.   Let sign be 1.
    1.27 +   4.   If Result(2) is not empty and the first character of Result(2) is a
    1.28 +   minus sign -, let sign be -1.
    1.29 +   5.   If Result(2) is not empty and the first character of Result(2) is a
    1.30 +   plus sign + or a minus sign -, then Result(5) is the substring of
    1.31 +   Result(2) produced by removing the first character; otherwise, Result(5)
    1.32 +   is Result(2).
    1.33 +   6.   If the radix argument is not supplied, go to step 12.
    1.34 +   7.   Call ToInt32(radix).
    1.35 +   8.   If Result(7) is zero, go to step 12; otherwise, if Result(7) < 2 or
    1.36 +   Result(7) > 36, return NaN.
    1.37 +   9.   Let R be Result(7).
    1.38 +   10.   If R = 16 and the length of Result(5) is at least 2 and the first two
    1.39 +   characters of Result(5) are either "0x" or "0X", let S be the substring
    1.40 +   of Result(5) consisting of all but the first two characters; otherwise,
    1.41 +   let S be Result(5).
    1.42 +   11.   Go to step 22.
    1.43 +   12.   If Result(5) is empty or the first character of Result(5) is not 0,
    1.44 +   go to step 20.
    1.45 +   13.   If the length of Result(5) is at least 2 and the second character of
    1.46 +   Result(5) is x or X, go to step 17.
    1.47 +   14.   Let R be 8.
    1.48 +   15.   Let S be Result(5).
    1.49 +   16.   Go to step 22.
    1.50 +   17.   Let R be 16.
    1.51 +   18.   Let S be the substring of Result(5) consisting of all but the first
    1.52 +   two characters.
    1.53 +   19.   Go to step 22.
    1.54 +   20.   Let R be 10.
    1.55 +   21.   Let S be Result(5).
    1.56 +   22.   If S contains any character that is not a radix-R digit, then let Z be
    1.57 +   the substring of S consisting of all characters to the left of the
    1.58 +   leftmost such character; otherwise, let Z be S.
    1.59 +   23.   If Z is empty, return NaN.
    1.60 +   24.   Compute the mathematical integer value that is represented by Z in
    1.61 +   radix-R notation. (But if R is 10 and Z contains more than 20
    1.62 +   significant digits, every digit after the 20th may be replaced by a 0
    1.63 +   digit, at the option of the implementation; and if R is not 2, 4, 8,
    1.64 +   10, 16, or 32, then Result(24) may be an implementation-dependent
    1.65 +   approximation to the mathematical integer value that is represented
    1.66 +   by Z in radix-R notation.)
    1.67 +   25.   Compute the number value for Result(24).
    1.68 +   26.   Return sign Result(25).
    1.69 +
    1.70 +   Note that parseInt may interpret only a leading portion of the string as
    1.71 +   an integer value; it ignores any characters that cannot be interpreted as
    1.72 +   part of the notation of an integer, and no indication is given that any
    1.73 +   such characters were ignored.
    1.74 +
    1.75 +   Author:             christine@netscape.com
    1.76 +   Date:               28 october 1997
    1.77 +
    1.78 +*/
    1.79 +var SECTION = "15.1.2.2-1";
    1.80 +var VERSION = "ECMA_1";
    1.81 +var TITLE   = "parseInt(string, radix)";
    1.82 +var BUGNUMBER = "none";
    1.83 +
    1.84 +startTest();
    1.85 +
    1.86 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.87 +
    1.88 +var HEX_STRING = "0x0";
    1.89 +var HEX_VALUE = 0;
    1.90 +
    1.91 +new TestCase( SECTION, 
    1.92 +	      "parseInt.length",     
    1.93 +	      2,     
    1.94 +	      parseInt.length );
    1.95 +
    1.96 +new TestCase( SECTION, 
    1.97 +	      "parseInt.length = 0; parseInt.length",    
    1.98 +	      2,     
    1.99 +	      eval("parseInt.length = 0; parseInt.length") );
   1.100 +
   1.101 +new TestCase( SECTION, 
   1.102 +	      "var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS",   "",
   1.103 +	      eval("var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS") );
   1.104 +
   1.105 +new TestCase( SECTION, 
   1.106 +	      "delete parseInt.length",  
   1.107 +	      false, 
   1.108 +	      delete parseInt.length );
   1.109 +
   1.110 +new TestCase( SECTION, 
   1.111 +	      "delete parseInt.length; parseInt.length", 
   1.112 +	      2, 
   1.113 +	      eval("delete parseInt.length; parseInt.length") );
   1.114 +
   1.115 +new TestCase( SECTION, 
   1.116 +	      "parseInt.length = null; parseInt.length", 
   1.117 +	      2, 
   1.118 +	      eval("parseInt.length = null; parseInt.length") );
   1.119 +
   1.120 +new TestCase( SECTION, 
   1.121 +	      "parseInt()",      
   1.122 +	      NaN,   
   1.123 +	      parseInt() );
   1.124 +
   1.125 +new TestCase( SECTION, 
   1.126 +	      "parseInt('')",    
   1.127 +	      NaN,   
   1.128 +	      parseInt("") );
   1.129 +
   1.130 +new TestCase( SECTION, 
   1.131 +	      "parseInt('','')", 
   1.132 +	      NaN,   
   1.133 +	      parseInt("","") );
   1.134 +
   1.135 +new TestCase( SECTION,
   1.136 +	      "parseInt(\"     0xabcdef     ",
   1.137 +	      11259375,
   1.138 +	      parseInt( "      0xabcdef     " ));
   1.139 +
   1.140 +new TestCase( SECTION,
   1.141 +	      "parseInt(\"     0XABCDEF     ",
   1.142 +	      11259375,
   1.143 +	      parseInt( "      0XABCDEF     " ) );
   1.144 +
   1.145 +new TestCase( SECTION,
   1.146 +	      "parseInt( 0xabcdef )",
   1.147 +	      11259375,
   1.148 +	      parseInt( "0xabcdef") );
   1.149 +
   1.150 +new TestCase( SECTION,
   1.151 +	      "parseInt( 0XABCDEF )",
   1.152 +	      11259375,
   1.153 +	      parseInt( "0XABCDEF") );
   1.154 +
   1.155 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.156 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
   1.157 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.158 +}
   1.159 +for ( HEX_STRING = "0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.160 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
   1.161 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.162 +}
   1.163 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.164 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
   1.165 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.166 +}
   1.167 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.168 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
   1.169 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.170 +}
   1.171 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.172 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', null)",    HEX_VALUE,  parseInt(HEX_STRING,null) );
   1.173 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.174 +}
   1.175 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.176 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', void 0)",    HEX_VALUE,  parseInt(HEX_STRING, void 0) );
   1.177 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.178 +}
   1.179 +
   1.180 +// a few tests with spaces
   1.181 +
   1.182 +for ( var space = " ", HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0;
   1.183 +      POWER < 15;
   1.184 +      POWER++, HEX_STRING = HEX_STRING +"f", space += " ")
   1.185 +{
   1.186 +  new TestCase( SECTION, "parseInt('"+space+HEX_STRING+space+"', void 0)",    HEX_VALUE,  parseInt(space+HEX_STRING+space, void 0) );
   1.187 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.188 +}
   1.189 +
   1.190 +new TestCase(SECTION, "parseInt(BOM + '123', 10)", 123, parseInt("\uFEFF" + "123", 10));
   1.191 +
   1.192 +// a few tests with negative numbers
   1.193 +for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.194 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
   1.195 +  HEX_VALUE -= Math.pow(16,POWER)*15;
   1.196 +}
   1.197 +
   1.198 +// we should stop parsing when we get to a value that is not a numeric literal for the type we expect
   1.199 +
   1.200 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.201 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"g', 16)",    HEX_VALUE,  parseInt(HEX_STRING+"g",16) );
   1.202 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.203 +}
   1.204 +for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.205 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"G', 16)",    HEX_VALUE,  parseInt(HEX_STRING+"G",16) );
   1.206 +  HEX_VALUE += Math.pow(16,POWER)*15;
   1.207 +}
   1.208 +
   1.209 +for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.210 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
   1.211 +  HEX_VALUE -= Math.pow(16,POWER)*15;
   1.212 +}
   1.213 +for ( HEX_STRING = "-0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.214 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"')",    HEX_VALUE,  parseInt(HEX_STRING) );
   1.215 +  HEX_VALUE -= Math.pow(16,POWER)*15;
   1.216 +}
   1.217 +for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.218 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
   1.219 +  HEX_VALUE -= Math.pow(16,POWER)*15;
   1.220 +}
   1.221 +for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
   1.222 +  new TestCase( SECTION, "parseInt('"+HEX_STRING+"', 16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
   1.223 +  HEX_VALUE -= Math.pow(16,POWER)*15;
   1.224 +}
   1.225 +
   1.226 +// Numbers that start with 0 and do not provide a radix should use 10 as radix
   1.227 +// per ES5, not octal (as it was in ES3).
   1.228 +
   1.229 +var OCT_STRING = "0";
   1.230 +var OCT_VALUE = 0;
   1.231 +
   1.232 +for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.233 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"')",    OCT_VALUE,  parseInt(OCT_STRING) );
   1.234 +  OCT_VALUE += Math.pow(10,POWER)*7;
   1.235 +}
   1.236 +
   1.237 +for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.238 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"')",    OCT_VALUE,  parseInt(OCT_STRING) );
   1.239 +  OCT_VALUE -= Math.pow(10,POWER)*7;
   1.240 +}
   1.241 +
   1.242 +// should get octal-based results if we provid the radix of 8 (or 010)
   1.243 +
   1.244 +for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.245 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"', 8)",    OCT_VALUE,  parseInt(OCT_STRING,8) );
   1.246 +  OCT_VALUE += Math.pow(8,POWER)*7;
   1.247 +}
   1.248 +for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.249 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"', 010)",    OCT_VALUE,  parseInt(OCT_STRING,010) );
   1.250 +  OCT_VALUE -= Math.pow(8,POWER)*7;
   1.251 +}
   1.252 +
   1.253 +// we shall stop parsing digits when we get one that isn't a numeric literal of the type we think
   1.254 +// it should be.
   1.255 +for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.256 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"8', 8)",    OCT_VALUE,  parseInt(OCT_STRING+"8",8) );
   1.257 +  OCT_VALUE += Math.pow(8,POWER)*7;
   1.258 +}
   1.259 +for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
   1.260 +  new TestCase( SECTION, "parseInt('"+OCT_STRING+"8', 010)",    OCT_VALUE,  parseInt(OCT_STRING+"8",010) );
   1.261 +  OCT_VALUE -= Math.pow(8,POWER)*7;
   1.262 +}
   1.263 +
   1.264 +new TestCase( SECTION,
   1.265 +	      "parseInt( '0x' )",             
   1.266 +	      NaN,       
   1.267 +	      parseInt("0x") );
   1.268 +
   1.269 +new TestCase( SECTION,
   1.270 +	      "parseInt( '0X' )",             
   1.271 +	      NaN,       
   1.272 +	      parseInt("0X") );
   1.273 +
   1.274 +new TestCase( SECTION,
   1.275 +	      "parseInt( '11111111112222222222' )",   
   1.276 +	      11111111112222222222,  
   1.277 +	      parseInt("11111111112222222222") );
   1.278 +
   1.279 +new TestCase( SECTION,
   1.280 +	      "parseInt( '111111111122222222223' )",   
   1.281 +	      111111111122222222220,  
   1.282 +	      parseInt("111111111122222222223") );
   1.283 +
   1.284 +new TestCase( SECTION,
   1.285 +	      "parseInt( '11111111112222222222',10 )",   
   1.286 +	      11111111112222222222,  
   1.287 +	      parseInt("11111111112222222222",10) );
   1.288 +
   1.289 +new TestCase( SECTION,
   1.290 +	      "parseInt( '111111111122222222223',10 )",   
   1.291 +	      111111111122222222220,  
   1.292 +	      parseInt("111111111122222222223",10) );
   1.293 +
   1.294 +new TestCase( SECTION,
   1.295 +	      "parseInt( '01234567890', -1 )", 
   1.296 +	      Number.NaN,   
   1.297 +	      parseInt("01234567890",-1) );
   1.298 +
   1.299 +new TestCase( SECTION,
   1.300 +	      "parseInt( '01234567890', 0 )", 
   1.301 +	      1234567890,
   1.302 +	      parseInt("01234567890", 0) );
   1.303 +
   1.304 +new TestCase( SECTION,
   1.305 +	      "parseInt( '01234567890', 1 )", 
   1.306 +	      Number.NaN,    
   1.307 +	      parseInt("01234567890",1) );
   1.308 +
   1.309 +new TestCase( SECTION,
   1.310 +	      "parseInt( '01234567890', 2 )", 
   1.311 +	      1,             
   1.312 +	      parseInt("01234567890",2) );
   1.313 +
   1.314 +new TestCase( SECTION,
   1.315 +	      "parseInt( '01234567890', 3 )", 
   1.316 +	      5,             
   1.317 +	      parseInt("01234567890",3) );
   1.318 +
   1.319 +new TestCase( SECTION,
   1.320 +	      "parseInt( '01234567890', 4 )", 
   1.321 +	      27,            
   1.322 +	      parseInt("01234567890",4) );
   1.323 +
   1.324 +new TestCase( SECTION,
   1.325 +	      "parseInt( '01234567890', 5 )", 
   1.326 +	      194,           
   1.327 +	      parseInt("01234567890",5) );
   1.328 +
   1.329 +new TestCase( SECTION,
   1.330 +	      "parseInt( '01234567890', 6 )", 
   1.331 +	      1865,          
   1.332 +	      parseInt("01234567890",6) );
   1.333 +
   1.334 +new TestCase( SECTION,
   1.335 +	      "parseInt( '01234567890', 7 )", 
   1.336 +	      22875,         
   1.337 +	      parseInt("01234567890",7) );
   1.338 +
   1.339 +new TestCase( SECTION,
   1.340 +	      "parseInt( '01234567890', 8 )", 
   1.341 +	      342391,        
   1.342 +	      parseInt("01234567890",8) );
   1.343 +
   1.344 +new TestCase( SECTION,
   1.345 +	      "parseInt( '01234567890', 9 )", 
   1.346 +	      6053444,       
   1.347 +	      parseInt("01234567890",9) );
   1.348 +
   1.349 +new TestCase( SECTION,
   1.350 +	      "parseInt( '01234567890', 10 )",
   1.351 +	      1234567890,
   1.352 +	      parseInt("01234567890",10) );
   1.353 +
   1.354 +// need more test cases with hex radix
   1.355 +
   1.356 +new TestCase( SECTION,
   1.357 +	      "parseInt( '1234567890', '0xa')",
   1.358 +	      1234567890,
   1.359 +	      parseInt("1234567890","0xa") );
   1.360 +
   1.361 +new TestCase( SECTION,
   1.362 +	      "parseInt( '012345', 11 )",     
   1.363 +	      17715,         
   1.364 +	      parseInt("012345",11) );
   1.365 +
   1.366 +new TestCase( SECTION,
   1.367 +	      "parseInt( '012345', 35 )",     
   1.368 +	      1590195,       
   1.369 +	      parseInt("012345",35) );
   1.370 +
   1.371 +new TestCase( SECTION,
   1.372 +	      "parseInt( '012345', 36 )",     
   1.373 +	      1776965,       
   1.374 +	      parseInt("012345",36) );
   1.375 +
   1.376 +new TestCase( SECTION,
   1.377 +	      "parseInt( '012345', 37 )",     
   1.378 +	      Number.NaN,    
   1.379 +	      parseInt("012345",37) );
   1.380 +
   1.381 +test();

mercurial