Debian Codegraph @

rhino/testsrc/tests/ecma/GlobalObject/15.1.2.5-1.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.5-1.js';
7
8 /**
9 File Name: 15.1.2.5-1.js
10 ECMA Section: 15.1.2.5 Function properties of the global object
11 unescape( string )
12
13 Description:
14 The unescape function computes a new version of a string value in which
15 each escape sequences of the sort that might be introduced by the escape
16 function is replaced with the character that it represents.
17
18 When the unescape function is called with one argument string, the
19 following steps are taken:
20
21 1. Call ToString(string).
22 2. Compute the number of characters in Result(1).
23 3. Let R be the empty string.
24 4. Let k be 0.
25 5. If k equals Result(2), return R.
26 6. Let c be the character at position k within Result(1).
27 7. If c is not %, go to step 18.
28 8. If k is greater than Result(2)-6, go to step 14.
29 9. If the character at position k+1 within result(1) is not u, go to step
30 14.
31 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
32 Result(1) are not all hexadecimal digits, go to step 14.
33 11. Let c be the character whose Unicode encoding is the integer represented
34 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
35 within Result(1).
36 12. Increase k by 5.
37 13. Go to step 18.
38 14. If k is greater than Result(2)-3, go to step 18.
39 15. If the two characters at positions k+1 and k+2 within Result(1) are not
40 both hexadecimal digits, go to step 18.
41 16. Let c be the character whose Unicode encoding is the integer represented
42 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
43 within Result(1).
44 17. Increase k by 2.
45 18. Let R be a new string value computed by concatenating the previous value
46 of R and c.
47 19. Increase k by 1.
48 20. Go to step 5.
49 Author: [email protected]
50 Date: 28 october 1997
51 */
52
53 var SECTION = "15.1.2.5-1";
54 var VERSION = "ECMA_1";
55 startTest();
56 var TITLE = "unescape(string)";
57
58 writeHeaderToLog( SECTION + " "+ TITLE);
59
60 new TestCase( SECTION, "unescape.length", 1, unescape.length );
61 new TestCase( SECTION, "unescape.length = null; unescape.length", 1, eval("unescape.length=null; unescape.length") );
62 new TestCase( SECTION, "delete unescape.length", false, delete unescape.length );
63 new TestCase( SECTION, "delete unescape.length; unescape.length", 1, eval("delete unescape.length; unescape.length") );
64 new TestCase( SECTION, "var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS", "prototype", eval("var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS") );
65
66 new TestCase( SECTION, "unescape()", "undefined", unescape() );
67 new TestCase( SECTION, "unescape('')", "", unescape('') );
68 new TestCase( SECTION, "unescape( null )", "null", unescape(null) );
69 new TestCase( SECTION, "unescape( void 0 )", "undefined", unescape(void 0) );
70 new TestCase( SECTION, "unescape( true )", "true", unescape( true ) );
71 new TestCase( SECTION, "unescape( false )", "false", unescape( false ) );
72
73 new TestCase( SECTION, "unescape( new Boolean(true) )", "true", unescape(new Boolean(true)) );
74 new TestCase( SECTION, "unescape( new Boolean(false) )", "false", unescape(new Boolean(false)) );
75
76 new TestCase( SECTION, "unescape( Number.NaN )", "NaN", unescape(Number.NaN) );
77 new TestCase( SECTION, "unescape( -0 )", "0", unescape( -0 ) );
78 new TestCase( SECTION, "unescape( 'Infinity' )", "Infinity", unescape( "Infinity" ) );
79 new TestCase( SECTION, "unescape( Number.POSITIVE_INFINITY )", "Infinity", unescape( Number.POSITIVE_INFINITY ) );
80 new TestCase( SECTION, "unescape( Number.NEGATIVE_INFINITY )", "-Infinity", unescape( Number.NEGATIVE_INFINITY ) );
81
82 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
83
84 new TestCase( SECTION, "unescape( " +ASCII_TEST_STRING+" )", ASCII_TEST_STRING, unescape( ASCII_TEST_STRING ) );
85
86 // escaped chars with ascii values less than 256
87
88 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
89 new TestCase( SECTION,
90 "unescape( %"+ ToHexString(CHARCODE)+" )",
91 String.fromCharCode(CHARCODE),
92 unescape( "%" + ToHexString(CHARCODE) ) );
93 }
94
95 // unicode chars represented by two hex digits
96 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
97 new TestCase( SECTION,
98 "unescape( %u"+ ToHexString(CHARCODE)+" )",
99 "%u"+ToHexString(CHARCODE),
100 unescape( "%u" + ToHexString(CHARCODE) ) );
101 }
102 /*
103 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
104 new TestCase( SECTION,
105 "unescape( %u"+ ToUnicodeString(CHARCODE)+" )",
106 String.fromCharCode(CHARCODE),
107 unescape( "%u" + ToUnicodeString(CHARCODE) ) );
108 }
109 for ( var CHARCODE = 256; CHARCODE < 65536; CHARCODE+= 333 ) {
110 new TestCase( SECTION,
111 "unescape( %u"+ ToUnicodeString(CHARCODE)+" )",
112 String.fromCharCode(CHARCODE),
113 unescape( "%u" + ToUnicodeString(CHARCODE) ) );
114 }
115 */
116
117 test();
118
119 function ToUnicodeString( n ) {
120 var string = ToHexString(n);
121
122 for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
123 string = "0" + string;
124 }
125
126 return string;
127 }
128 function ToHexString( n ) {
129 var hex = new Array();
130
131 for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
132 ;
133 }
134
135 for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
136 hex[index] = Math.floor( n / Math.pow(16,mag) );
137 n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
138 }
139
140 hex[hex.length] = n % 16;
141
142 var string ="";
143
144 for ( var index = 0 ; index < hex.length ; index++ ) {
145 switch ( hex[index] ) {
146 case 10:
147 string += "A";
148 break;
149 case 11:
150 string += "B";
151 break;
152 case 12:
153 string += "C";
154 break;
155 case 13:
156 string += "D";
157 break;
158 case 14:
159 string += "E";
160 break;
161 case 15:
162 string += "F";
163 break;
164 default:
165 string += hex[index];
166 }
167 }
168
169 if ( string.length == 1 ) {
170 string = "0" + string;
171 }
172 return string;
173 }