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