Debian Codegraph @

rhino/testsrc/tests/ecma/GlobalObject/15.1.2.4.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = '15.1.2.4.js';
7
8 /**
9 File Name: 15.1.2.4.js
10 ECMA Section: 15.1.2.4 Function properties of the global object
11 escape( string )
12
13 Description:
14 The escape function computes a new version of a string value in which
15 certain characters have been replaced by a hexadecimal escape sequence.
16 The result thus contains no special characters that might have special
17 meaning within a URL.
18
19 For characters whose Unicode encoding is 0xFF or less, a two-digit
20 escape sequence of the form %xx is used in accordance with RFC1738.
21 For characters whose Unicode encoding is greater than 0xFF, a four-
22 digit escape sequence of the form %uxxxx is used.
23
24 When the escape function is called with one argument string, the
25 following steps are taken:
26
27 1. Call ToString(string).
28 2. Compute the number of characters in Result(1).
29 3. Let R be the empty string.
30 4. Let k be 0.
31 5. If k equals Result(2), return R.
32 6. Get the character at position k within Result(1).
33 7. If Result(6) is one of the 69 nonblank ASCII characters
34 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
35 0123456789 @*_+-./, go to step 14.
36 8. Compute the 16-bit unsigned integer that is the Unicode character
37 encoding of Result(6).
38 9. If Result(8), is less than 256, go to step 12.
39 10. Let S be a string containing six characters "%uwxyz" where wxyz are
40 four hexadecimal digits encoding the value of Result(8).
41 11. Go to step 15.
42 12. Let S be a string containing three characters "%xy" where xy are two
43 hexadecimal digits encoding the value of Result(8).
44 13. Go to step 15.
45 14. Let S be a string containing the single character Result(6).
46 15. Let R be a new string value computed by concatenating the previous value
47 of R and S.
48 16. Increase k by 1.
49 17. Go to step 5.
50
51 Author: [email protected]
52 Date: 28 october 1997
53
54 */
55 var SECTION = "15.1.2.4";
56 var VERSION = "ECMA_1";
57 startTest();
58 var TITLE = "escape(string)";
59
60 writeHeaderToLog( SECTION + " "+ TITLE);
61
62 new TestCase( SECTION, "escape.length", 1, escape.length );
63 new TestCase( SECTION, "escape.length = null; escape.length", 1, eval("escape.length = null; escape.length") );
64 new TestCase( SECTION, "delete escape.length", false, delete escape.length );
65 new TestCase( SECTION, "delete escape.length; escape.length", 1, eval("delete escape.length; escape.length") );
66 new TestCase( SECTION, "var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS", "prototype", eval("var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS") );
67
68 new TestCase( SECTION, "escape()", "undefined", escape() );
69 new TestCase( SECTION, "escape('')", "", escape('') );
70 new TestCase( SECTION, "escape( null )", "null", escape(null) );
71 new TestCase( SECTION, "escape( void 0 )", "undefined", escape(void 0) );
72 new TestCase( SECTION, "escape( true )", "true", escape( true ) );
73 new TestCase( SECTION, "escape( false )", "false", escape( false ) );
74
75 new TestCase( SECTION, "escape( new Boolean(true) )", "true", escape(new Boolean(true)) );
76 new TestCase( SECTION, "escape( new Boolean(false) )", "false", escape(new Boolean(false)) );
77
78 new TestCase( SECTION, "escape( Number.NaN )", "NaN", escape(Number.NaN) );
79 new TestCase( SECTION, "escape( -0 )", "0", escape( -0 ) );
80 new TestCase( SECTION, "escape( 'Infinity' )", "Infinity", escape( "Infinity" ) );
81 new TestCase( SECTION, "escape( Number.POSITIVE_INFINITY )", "Infinity", escape( Number.POSITIVE_INFINITY ) );
82 new TestCase( SECTION, "escape( Number.NEGATIVE_INFINITY )", "-Infinity", escape( Number.NEGATIVE_INFINITY ) );
83
84 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
85
86 new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, escape( ASCII_TEST_STRING ) );
87
88 // ASCII value less than
89
90 for ( var CHARCODE = 0; CHARCODE < 32; CHARCODE++ ) {
91 new TestCase( SECTION,
92 "escape(String.fromCharCode("+CHARCODE+"))",
93 "%"+ToHexString(CHARCODE),
94 escape(String.fromCharCode(CHARCODE)) );
95 }
96 for ( var CHARCODE = 128; CHARCODE < 256; CHARCODE++ ) {
97 new TestCase( SECTION,
98 "escape(String.fromCharCode("+CHARCODE+"))",
99 "%"+ToHexString(CHARCODE),
100 escape(String.fromCharCode(CHARCODE)) );
101 }
102
103 for ( var CHARCODE = 256; CHARCODE < 1024; CHARCODE++ ) {
104 new TestCase( SECTION,
105 "escape(String.fromCharCode("+CHARCODE+"))",
106 "%u"+ ToUnicodeString(CHARCODE),
107 escape(String.fromCharCode(CHARCODE)) );
108 }
109 for ( var CHARCODE = 65500; CHARCODE < 65536; CHARCODE++ ) {
110 new TestCase( SECTION,
111 "escape(String.fromCharCode("+CHARCODE+"))",
112 "%u"+ ToUnicodeString(CHARCODE),
113 escape(String.fromCharCode(CHARCODE)) );
114 }
115
116 test();
117
118 function ToUnicodeString( n ) {
119 var string = ToHexString(n);
120
121 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
122 string = "0" + string;
123 }
124
125 return string;
126 }
127 function ToHexString( n ) {
128 var hex = new Array();
129
130 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
131 ;
132 }
133
134 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
135 hex[index] = Math.floor( n / Math.pow(16,mag) );
136 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
137 }
138
139 hex[hex.length] = n % 16;
140
141 var string ="";
142
143 for ( var index = 0 ; index < hex.length ; index++ ) {
144 switch ( hex[index] ) {
145 case 10:
146 string += "A";
147 break;
148 case 11:
149 string += "B";
150 break;
151 case 12:
152 string += "C";
153 break;
154 case 13:
155 string += "D";
156 break;
157 case 14:
158 string += "E";
159 break;
160 case 15:
161 string += "F";
162 break;
163 default:
164 string += hex[index];
165 }
166 }
167
168 if ( string.length == 1 ) {
169 string = "0" + string;
170 }
171 return string;
172 }