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