• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/
5 *
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.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
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
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          15.1.2.5-1.js
24    ECMA Section:       15.1.2.5  Function properties of the global object
25                        unescape( string )
26
27    Description:
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.
31
32    When the unescape function is called with one argument string, the
33    following steps are taken:
34
35    1.  Call ToString(string).
36    2.  Compute the number of characters in Result(1).
37    3.  Let R be the empty string.
38    4.  Let k be 0.
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
44        14.
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
49        within Result(1).
50    12. Increase k by 5.
51    13. Go to step 18.
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
57        within Result(1).
58    17. Increase k by 2.
59    18. Let R be a new string value computed by concatenating the previous value
60        of R and c.
61    19. Increase k by 1.
62    20. Go to step 5.
63    Author:             christine@netscape.com
64    Date:               28 october 1997
65*/
66
67    var SECTION = "15.1.2.5-1";
68    var VERSION = "ECMA_1";
69    startTest();
70    var TITLE   = "unescape(string)";
71
72    writeHeaderToLog( SECTION + " "+ TITLE);
73
74    var testcases = getTestCases();
75
76    test();
77
78function getTestCases() {
79    var array = new Array();
80    var item = 0;
81
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") );
87
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 ) );
94
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)) );
97
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 ) );
103
104    var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
105
106    array[item++] = new TestCase( SECTION, "unescape( " +ASCII_TEST_STRING+" )",    ASCII_TEST_STRING,  unescape( ASCII_TEST_STRING ) );
107
108    // escaped chars with ascii values less than 256
109
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) )  );
115    }
116
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) )  );
123    }
124/*
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) )  );
130    }
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) )  );
136    }
137*/
138    return ( array );
139}
140
141function ToUnicodeString( n ) {
142    var string = ToHexString(n);
143
144    for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
145        string = "0" + string;
146    }
147
148    return string;
149}
150function ToHexString( n ) {
151    var hex = new Array();
152
153    for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
154        ;
155    }
156
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) );
160    }
161
162    hex[hex.length] = n % 16;
163
164    var string ="";
165
166    for ( var index = 0 ; index < hex.length ; index++ ) {
167        switch ( hex[index] ) {
168            case 10:
169                string += "A";
170                break;
171            case 11:
172                string += "B";
173                break;
174            case 12:
175                string += "C";
176                break;
177            case 13:
178                string += "D";
179                break;
180            case 14:
181                string += "E";
182                break;
183            case 15:
184                string += "F";
185                break;
186            default:
187                string += hex[index];
188        }
189    }
190
191    if ( string.length == 1 ) {
192        string = "0" + string;
193    }
194    return string;
195}
196function 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 );
202
203        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
204    }
205    stopTest();
206    return ( testcases );
207}
208