1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/GlobalObject/15.1.2.5-1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,172 @@ 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.5-1.js 1.12 + ECMA Section: 15.1.2.5 Function properties of the global object 1.13 + unescape( string ) 1.14 + 1.15 + Description: 1.16 + The unescape function computes a new version of a string value in which 1.17 + each escape sequences of the sort that might be introduced by the escape 1.18 + function is replaced with the character that it represents. 1.19 + 1.20 + When the unescape function is called with one argument string, the 1.21 + following steps are taken: 1.22 + 1.23 + 1. Call ToString(string). 1.24 + 2. Compute the number of characters in Result(1). 1.25 + 3. Let R be the empty string. 1.26 + 4. Let k be 0. 1.27 + 5. If k equals Result(2), return R. 1.28 + 6. Let c be the character at position k within Result(1). 1.29 + 7. If c is not %, go to step 18. 1.30 + 8. If k is greater than Result(2)-6, go to step 14. 1.31 + 9. If the character at position k+1 within result(1) is not u, go to step 1.32 + 14. 1.33 + 10. If the four characters at positions k+2, k+3, k+4, and k+5 within 1.34 + Result(1) are not all hexadecimal digits, go to step 14. 1.35 + 11. Let c be the character whose Unicode encoding is the integer represented 1.36 + by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5 1.37 + within Result(1). 1.38 + 12. Increase k by 5. 1.39 + 13. Go to step 18. 1.40 + 14. If k is greater than Result(2)-3, go to step 18. 1.41 + 15. If the two characters at positions k+1 and k+2 within Result(1) are not 1.42 + both hexadecimal digits, go to step 18. 1.43 + 16. Let c be the character whose Unicode encoding is the integer represented 1.44 + by two zeroes plus the two hexadecimal digits at positions k+1 and k+2 1.45 + within Result(1). 1.46 + 17. Increase k by 2. 1.47 + 18. Let R be a new string value computed by concatenating the previous value 1.48 + of R and c. 1.49 + 19. Increase k by 1. 1.50 + 20. Go to step 5. 1.51 + Author: christine@netscape.com 1.52 + Date: 28 october 1997 1.53 +*/ 1.54 + 1.55 +var SECTION = "15.1.2.5-1"; 1.56 +var VERSION = "ECMA_1"; 1.57 +startTest(); 1.58 +var TITLE = "unescape(string)"; 1.59 + 1.60 +writeHeaderToLog( SECTION + " "+ TITLE); 1.61 + 1.62 +new TestCase( SECTION, "unescape.length", 1, unescape.length ); 1.63 +new TestCase( SECTION, "unescape.length = null; unescape.length", 1, eval("unescape.length=null; unescape.length") ); 1.64 +new TestCase( SECTION, "delete unescape.length", false, delete unescape.length ); 1.65 +new TestCase( SECTION, "delete unescape.length; unescape.length", 1, eval("delete unescape.length; unescape.length") ); 1.66 +new TestCase( SECTION, "var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS", "", eval("var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS") ); 1.67 + 1.68 +new TestCase( SECTION, "unescape()", "undefined", unescape() ); 1.69 +new TestCase( SECTION, "unescape('')", "", unescape('') ); 1.70 +new TestCase( SECTION, "unescape( null )", "null", unescape(null) ); 1.71 +new TestCase( SECTION, "unescape( void 0 )", "undefined", unescape(void 0) ); 1.72 +new TestCase( SECTION, "unescape( true )", "true", unescape( true ) ); 1.73 +new TestCase( SECTION, "unescape( false )", "false", unescape( false ) ); 1.74 + 1.75 +new TestCase( SECTION, "unescape( new Boolean(true) )", "true", unescape(new Boolean(true)) ); 1.76 +new TestCase( SECTION, "unescape( new Boolean(false) )", "false", unescape(new Boolean(false)) ); 1.77 + 1.78 +new TestCase( SECTION, "unescape( Number.NaN )", "NaN", unescape(Number.NaN) ); 1.79 +new TestCase( SECTION, "unescape( -0 )", "0", unescape( -0 ) ); 1.80 +new TestCase( SECTION, "unescape( 'Infinity' )", "Infinity", unescape( "Infinity" ) ); 1.81 +new TestCase( SECTION, "unescape( Number.POSITIVE_INFINITY )", "Infinity", unescape( Number.POSITIVE_INFINITY ) ); 1.82 +new TestCase( SECTION, "unescape( Number.NEGATIVE_INFINITY )", "-Infinity", unescape( Number.NEGATIVE_INFINITY ) ); 1.83 + 1.84 +var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./"; 1.85 + 1.86 +new TestCase( SECTION, "unescape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, unescape( ASCII_TEST_STRING ) ); 1.87 + 1.88 +// escaped chars with ascii values less than 256 1.89 + 1.90 +for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) { 1.91 + new TestCase( SECTION, 1.92 + "unescape( %"+ ToHexString(CHARCODE)+" )", 1.93 + String.fromCharCode(CHARCODE), 1.94 + unescape( "%" + ToHexString(CHARCODE) ) ); 1.95 +} 1.96 + 1.97 +// unicode chars represented by two hex digits 1.98 +for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) { 1.99 + new TestCase( SECTION, 1.100 + "unescape( %u"+ ToHexString(CHARCODE)+" )", 1.101 + "%u"+ToHexString(CHARCODE), 1.102 + unescape( "%u" + ToHexString(CHARCODE) ) ); 1.103 +} 1.104 +/* 1.105 + for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) { 1.106 + new TestCase( SECTION, 1.107 + "unescape( %u"+ ToUnicodeString(CHARCODE)+" )", 1.108 + String.fromCharCode(CHARCODE), 1.109 + unescape( "%u" + ToUnicodeString(CHARCODE) ) ); 1.110 + } 1.111 + for ( var CHARCODE = 256; CHARCODE < 65536; CHARCODE+= 333 ) { 1.112 + new TestCase( SECTION, 1.113 + "unescape( %u"+ ToUnicodeString(CHARCODE)+" )", 1.114 + String.fromCharCode(CHARCODE), 1.115 + unescape( "%u" + ToUnicodeString(CHARCODE) ) ); 1.116 + } 1.117 +*/ 1.118 + 1.119 +test(); 1.120 + 1.121 +function ToUnicodeString( n ) { 1.122 + var string = ToHexString(n); 1.123 + 1.124 + for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) { 1.125 + string = "0" + string; 1.126 + } 1.127 + 1.128 + return string; 1.129 +} 1.130 +function ToHexString( n ) { 1.131 + var hex = new Array(); 1.132 + 1.133 + for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) { 1.134 + ; 1.135 + } 1.136 + 1.137 + for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) { 1.138 + hex[index] = Math.floor( n / Math.pow(16,mag) ); 1.139 + n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) ); 1.140 + } 1.141 + 1.142 + hex[hex.length] = n % 16; 1.143 + 1.144 + var string =""; 1.145 + 1.146 + for ( var index = 0 ; index < hex.length ; index++ ) { 1.147 + switch ( hex[index] ) { 1.148 + case 10: 1.149 + string += "A"; 1.150 + break; 1.151 + case 11: 1.152 + string += "B"; 1.153 + break; 1.154 + case 12: 1.155 + string += "C"; 1.156 + break; 1.157 + case 13: 1.158 + string += "D"; 1.159 + break; 1.160 + case 14: 1.161 + string += "E"; 1.162 + break; 1.163 + case 15: 1.164 + string += "F"; 1.165 + break; 1.166 + default: 1.167 + string += hex[index]; 1.168 + } 1.169 + } 1.170 + 1.171 + if ( string.length == 1 ) { 1.172 + string = "0" + string; 1.173 + } 1.174 + return string; 1.175 +}