]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/GlobalObject/15.1.2.5-3.js
1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
23 File Name: 15.1.2.5-3.js
24 ECMA Section: 15.1.2.5 Function properties of the global object
28 This tests the cases where one of the four characters following "%u" is
29 not a hexidecimal character, or one of the two characters following "%"
30 or "%u" is not a hexidecimal character.
32 The unescape function computes a new version of a string value in which
33 each escape sequences of the sort that might be introduced by the escape
34 function is replaced with the character that it represents.
36 When the unescape function is called with one argument string, the
37 following steps are taken:
39 1. Call ToString(string).
40 2. Compute the number of characters in Result(1).
41 3. Let R be the empty string.
43 5. If k equals Result(2), return R.
44 6. Let c be the character at position k within Result(1).
45 7. If c is not %, go to step 18.
46 8. If k is greater than Result(2)-6, go to step 14.
47 9. If the character at position k+1 within result(1) is not u, go to step
49 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
50 Result(1) are not all hexadecimal digits, go to step 14.
51 11. Let c be the character whose Unicode encoding is the integer represented
52 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
56 14. If k is greater than Result(2)-3, go to step 18.
57 15. If the two characters at positions k+1 and k+2 within Result(1) are not
58 both hexadecimal digits, go to step 18.
59 16. Let c be the character whose Unicode encoding is the integer represented
60 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
63 18. Let R be a new string value computed by concatenating the previous value
67 Author: christine@netscape.com
72 var SECTION
= "15.1.2.5-3";
73 var VERSION
= "ECMA_1";
75 var TITLE
= "unescape(string)";
77 writeHeaderToLog( SECTION
+ " "+ TITLE
);
79 var testcases
= getTestCases();
83 function getTestCases() {
84 var array
= new Array();
87 for ( var CHARCODE
= 0, NONHEXCHARCODE
= 0; CHARCODE
< 256; CHARCODE
++, NONHEXCHARCODE
++ ) {
88 NONHEXCHARCODE
= getNextNonHexCharCode( NONHEXCHARCODE
);
90 array
[item
++] = new TestCase( SECTION
,
91 "unescape( %"+ (ToHexString(CHARCODE
)).substring(0,1) +
92 String
.fromCharCode( NONHEXCHARCODE
) +" )" +
93 "[where last character is String.fromCharCode("+NONHEXCHARCODE
+")]",
94 "%"+(ToHexString(CHARCODE
)).substring(0,1)+
95 String
.fromCharCode( NONHEXCHARCODE
),
96 unescape( "%" + (ToHexString(CHARCODE
)).substring(0,1)+
97 String
.fromCharCode( NONHEXCHARCODE
) ) );
99 for ( var CHARCODE
= 0, NONHEXCHARCODE
= 0; CHARCODE
< 256; CHARCODE
++, NONHEXCHARCODE
++ ) {
100 NONHEXCHARCODE
= getNextNonHexCharCode( NONHEXCHARCODE
);
102 array
[item
++] = new TestCase( SECTION
,
103 "unescape( %u"+ (ToHexString(CHARCODE
)).substring(0,1) +
104 String
.fromCharCode( NONHEXCHARCODE
) +" )" +
105 "[where last character is String.fromCharCode("+NONHEXCHARCODE
+")]",
106 "%u"+(ToHexString(CHARCODE
)).substring(0,1)+
107 String
.fromCharCode( NONHEXCHARCODE
),
108 unescape( "%u" + (ToHexString(CHARCODE
)).substring(0,1)+
109 String
.fromCharCode( NONHEXCHARCODE
) ) );
112 for ( var CHARCODE
= 0, NONHEXCHARCODE
= 0 ; CHARCODE
< 65536; CHARCODE
+= 54321, NONHEXCHARCODE
++ ) {
113 NONHEXCHARCODE
= getNextNonHexCharCode( NONHEXCHARCODE
);
115 array
[item
++] = new TestCase( SECTION
,
116 "unescape( %u"+ (ToUnicodeString(CHARCODE
)).substring(0,3) +
117 String
.fromCharCode( NONHEXCHARCODE
) +" )" +
118 "[where last character is String.fromCharCode("+NONHEXCHARCODE
+")]",
120 String
.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE
)).substring(0,2))) +
121 (ToUnicodeString(CHARCODE
)).substring(2,3) +
122 String
.fromCharCode( NONHEXCHARCODE
),
124 unescape( "%" + (ToUnicodeString(CHARCODE
)).substring(0,3)+
125 String
.fromCharCode( NONHEXCHARCODE
) ) );
130 function getNextNonHexCharCode( n
) {
131 for ( ; n
< Math
.pow(2,16); n
++ ) {
132 if ( ( n
== 43 || n
== 45 || n
== 46 || n
== 47 ||
133 (n
>= 71 && n
<= 90) || (n
>= 103 && n
<= 122) ||
134 n
== 64 || n
== 95 ) ) {
137 n
= ( n
> 122 ) ? 0 : n
;
142 function ToUnicodeString( n
) {
143 var string
= ToHexString(n
);
145 for ( var PAD
= (4 - string
.length
); PAD
> 0; PAD
-- ) {
146 string
= "0" + string
;
151 function ToHexString( n
) {
152 var hex
= new Array();
154 for ( var mag
= 1; Math
.pow(16,mag
) <= n
; mag
++ ) {
158 for ( index
= 0, mag
-= 1; mag
> 0; index
++, mag
-- ) {
159 hex
[index
] = Math
.floor( n
/ Math
.pow(16,mag
) );
160 n
-= Math
.pow(16,mag
) * Math
.floor( n
/Math
.pow(16,mag
) );
163 hex
[hex
.length
] = n
% 16;
167 for ( var index
= 0 ; index
< hex
.length
; index
++ ) {
168 switch ( hex
[index
] ) {
188 string
+= hex
[index
];
192 if ( string
.length
== 1 ) {
193 string
= "0" + string
;
198 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
199 testcases
[tc
].passed
= writeTestCaseResult(
200 testcases
[tc
].expect
,
201 testcases
[tc
].actual
,
202 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
);
203 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
206 return ( testcases
);