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