]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/GlobalObject/15.1.2.5-1.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-1.js
24 ECMA Section: 15.1.2.5 Function properties of the global object
28 The unescape function computes a new version of a string value in which
29 each escape sequences of the sort that might be introduced by the escape
30 function is replaced with the character that it represents.
32 When the unescape function is called with one argument string, the
33 following steps are taken:
35 1. Call ToString(string).
36 2. Compute the number of characters in Result(1).
37 3. Let R be the empty string.
39 5. If k equals Result(2), return R.
40 6. Let c be the character at position k within Result(1).
41 7. If c is not %, go to step 18.
42 8. If k is greater than Result(2)-6, go to step 14.
43 9. If the character at position k+1 within result(1) is not u, go to step
45 10. If the four characters at positions k+2, k+3, k+4, and k+5 within
46 Result(1) are not all hexadecimal digits, go to step 14.
47 11. Let c be the character whose Unicode encoding is the integer represented
48 by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
52 14. If k is greater than Result(2)-3, go to step 18.
53 15. If the two characters at positions k+1 and k+2 within Result(1) are not
54 both hexadecimal digits, go to step 18.
55 16. Let c be the character whose Unicode encoding is the integer represented
56 by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
59 18. Let R be a new string value computed by concatenating the previous value
63 Author: christine@netscape.com
67 var SECTION
= "15.1.2.5-1" ;
68 var VERSION
= "ECMA_1" ;
70 var TITLE
= "unescape(string)" ;
72 writeHeaderToLog ( SECTION
+ " " + TITLE
);
74 var testcases
= getTestCases ();
78 function getTestCases () {
79 var array
= new Array ();
82 array
[ item
++] = new TestCase ( SECTION
, "unescape.length" , 1 , unescape
. length
);
83 array
[ item
++] = new TestCase ( SECTION
, "unescape.length = null; unescape.length" , 1 , eval ( "unescape.length=null; unescape.length" ) );
84 array
[ item
++] = new TestCase ( SECTION
, "delete unescape.length" , false , delete unescape
. length
);
85 array
[ item
++] = new TestCase ( SECTION
, "delete unescape.length; unescape.length" , 1 , eval ( "delete unescape.length; unescape.length" ) );
86 array
[ item
++] = new TestCase ( SECTION
, "var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS" , "" , eval ( "var MYPROPS=''; for ( var p in unescape ) { MYPROPS+= p }; MYPROPS" ) );
88 array
[ item
++] = new TestCase ( SECTION
, "unescape()" , "undefined" , unescape () );
89 array
[ item
++] = new TestCase ( SECTION
, "unescape('')" , "" , unescape ( '' ) );
90 array
[ item
++] = new TestCase ( SECTION
, "unescape( null )" , "null" , unescape ( null ) );
91 array
[ item
++] = new TestCase ( SECTION
, "unescape( void 0 )" , "undefined" , unescape ( void 0 ) );
92 array
[ item
++] = new TestCase ( SECTION
, "unescape( true )" , "true" , unescape ( true ) );
93 array
[ item
++] = new TestCase ( SECTION
, "unescape( false )" , "false" , unescape ( false ) );
95 array
[ item
++] = new TestCase ( SECTION
, "unescape( new Boolean(true) )" , "true" , unescape ( new Boolean ( true )) );
96 array
[ item
++] = new TestCase ( SECTION
, "unescape( new Boolean(false) )" , "false" , unescape ( new Boolean ( false )) );
98 array
[ item
++] = new TestCase ( SECTION
, "unescape( Number.NaN )" , "NaN" , unescape ( Number
. NaN
) );
99 array
[ item
++] = new TestCase ( SECTION
, "unescape( -0 )" , "0" , unescape ( - 0 ) );
100 array
[ item
++] = new TestCase ( SECTION
, "unescape( 'Infinity' )" , "Infinity" , unescape ( "Infinity" ) );
101 array
[ item
++] = new TestCase ( SECTION
, "unescape( Number.POSITIVE_INFINITY )" , "Infinity" , unescape ( Number
. POSITIVE_INFINITY
) );
102 array
[ item
++] = new TestCase ( SECTION
, "unescape( Number.NEGATIVE_INFINITY )" , "-Infinity" , unescape ( Number
. NEGATIVE_INFINITY
) );
104 var ASCII_TEST_STRING
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./" ;
106 array
[ item
++] = new TestCase ( SECTION
, "unescape( " + ASCII_TEST_STRING
+ " )" , ASCII_TEST_STRING
, unescape ( ASCII_TEST_STRING
) );
108 // escaped chars with ascii values less than 256
110 for ( var CHARCODE
= 0 ; CHARCODE
< 256 ; CHARCODE
++ ) {
111 array
[ item
++] = new TestCase ( SECTION
,
112 "unescape( %" + ToHexString ( CHARCODE
)+ " )" ,
113 String
. fromCharCode ( CHARCODE
),
114 unescape ( "%" + ToHexString ( CHARCODE
) ) );
117 // unicode chars represented by two hex digits
118 for ( var CHARCODE
= 0 ; CHARCODE
< 256 ; CHARCODE
++ ) {
119 array
[ item
++] = new TestCase ( SECTION
,
120 "unescape( %u" + ToHexString ( CHARCODE
)+ " )" ,
121 "%u" + ToHexString ( CHARCODE
),
122 unescape ( "%u" + ToHexString ( CHARCODE
) ) );
125 for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
126 array[item++] = new TestCase( SECTION,
127 "unescape( %u"+ ToUnicodeString(CHARCODE)+" )",
128 String.fromCharCode(CHARCODE),
129 unescape( "%u" + ToUnicodeString(CHARCODE) ) );
131 for ( var CHARCODE = 256; CHARCODE < 65536; CHARCODE+= 333 ) {
132 array[item++] = new TestCase( SECTION,
133 "unescape( %u"+ ToUnicodeString(CHARCODE)+" )",
134 String.fromCharCode(CHARCODE),
135 unescape( "%u" + ToUnicodeString(CHARCODE) ) );
141 function ToUnicodeString ( n
) {
142 var string
= ToHexString ( n
);
144 for ( var PAD
= ( 4 - string
. length
); PAD
> 0 ; PAD
-- ) {
145 string
= "0" + string
;
150 function ToHexString ( n
) {
151 var hex
= new Array ();
153 for ( var mag
= 1 ; Math
. pow ( 16 , mag
) <= n
; mag
++ ) {
157 for ( index
= 0 , mag
-= 1 ; mag
> 0 ; index
++, mag
-- ) {
158 hex
[ index
] = Math
. floor ( n
/ Math
. pow ( 16 , mag
) );
159 n
-= Math
. pow ( 16 , mag
) * Math
. floor ( n
/ Math
. pow ( 16 , mag
) );
162 hex
[ hex
. length
] = n
% 16 ;
166 for ( var index
= 0 ; index
< hex
. length
; index
++ ) {
167 switch ( hex
[ index
] ) {
187 string
+= hex
[ index
];
191 if ( string
. length
== 1 ) {
192 string
= "0" + string
;
196 function test ( array
) {
197 for ( tc
= 0 ; tc
< testcases
. length
; tc
++ ) {
198 testcases
[ tc
]. passed
= writeTestCaseResult (
199 testcases
[ tc
]. expect
,
200 testcases
[ tc
]. actual
,
201 testcases
[ tc
]. description
+ " = " + testcases
[ tc
]. actual
);
203 testcases
[ tc
]. reason
+= ( testcases
[ tc
]. passed
) ? "" : "wrong value " ;
206 return ( testcases
);