Home | History | Annotate | Download | only in GlobalObject
      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-3.js
     24     ECMA Section:       15.1.2.5  Function properties of the global object
     25                         unescape( string )
     26 
     27     Description:
     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.
     31 
     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.
     35 
     36     When the unescape function is called with one argument string, the
     37     following steps are taken:
     38 
     39     1.  Call ToString(string).
     40     2.  Compute the number of characters in Result(1).
     41     3.  Let R be the empty string.
     42     4.  Let k be 0.
     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
     48         14.
     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
     53         within Result(1).
     54     12. Increase k by 5.
     55     13. Go to step 18.
     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
     61         within Result(1).
     62     17. Increase k by 2.
     63     18. Let R be a new string value computed by concatenating the previous value
     64         of R and c.
     65     19. Increase k by 1.
     66     20. Go to step 5.
     67     Author:             christine (at) netscape.com
     68     Date:               28 october 1997
     69 */
     70 
     71 
     72     var SECTION = "15.1.2.5-3";
     73     var VERSION = "ECMA_1";
     74     startTest();
     75     var TITLE   = "unescape(string)";
     76 
     77     writeHeaderToLog( SECTION + " "+ TITLE);
     78 
     79     var testcases = getTestCases();
     80 
     81     test();
     82 
     83 function getTestCases() {
     84     var array = new Array();
     85     var item = 0;
     86 
     87     for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
     88         NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
     89 
     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 ) )  );
     98     }
     99     for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
    100         NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
    101 
    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 ) )  );
    110     }
    111 
    112     for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
    113         NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
    114 
    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+")]",
    119 
    120                             String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
    121                             (ToUnicodeString(CHARCODE)).substring(2,3) +
    122                                 String.fromCharCode( NONHEXCHARCODE ),
    123 
    124                             unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
    125                                 String.fromCharCode( NONHEXCHARCODE ) )  );
    126     }
    127 
    128     return ( array );
    129 }
    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 ) ) {
    135             break;
    136         } else {
    137             n = ( n > 122 ) ? 0 : n;
    138         }
    139     }
    140     return n;
    141 }
    142 function ToUnicodeString( n ) {
    143     var string = ToHexString(n);
    144 
    145     for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
    146         string = "0" + string;
    147     }
    148 
    149     return string;
    150 }
    151 function ToHexString( n ) {
    152     var hex = new Array();
    153 
    154     for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
    155         ;
    156     }
    157 
    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) );
    161     }
    162 
    163     hex[hex.length] = n % 16;
    164 
    165     var string ="";
    166 
    167     for ( var index = 0 ; index < hex.length ; index++ ) {
    168         switch ( hex[index] ) {
    169             case 10:
    170                 string += "A";
    171                 break;
    172             case 11:
    173                 string += "B";
    174                 break;
    175             case 12:
    176                 string += "C";
    177                 break;
    178             case 13:
    179                 string += "D";
    180                 break;
    181             case 14:
    182                 string += "E";
    183                 break;
    184             case 15:
    185                 string += "F";
    186                 break;
    187             default:
    188                 string += hex[index];
    189         }
    190     }
    191 
    192     if ( string.length == 1 ) {
    193         string = "0" + string;
    194     }
    195     return string;
    196 }
    197 function test() {
    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 ";
    204     }
    205     stopTest();
    206     return ( testcases );
    207 }
    208