Debian Codegraph @

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