js/src/tests/ecma/GlobalObject/15.1.2.5-2.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/GlobalObject/15.1.2.5-2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          15.1.2.5-2.js
    1.12 +   ECMA Section:       15.1.2.5  Function properties of the global object
    1.13 +   unescape( string )
    1.14 +   Description:
    1.15 +
    1.16 +   This tests the cases where there are fewer than 4 characters following "%u",
    1.17 +   or fewer than 2 characters following "%" or "%u".
    1.18 +
    1.19 +   The unescape function computes a new version of a string value in which
    1.20 +   each escape sequences of the sort that might be introduced by the escape
    1.21 +   function is replaced with the character that it represents.
    1.22 +
    1.23 +   When the unescape function is called with one argument string, the
    1.24 +   following steps are taken:
    1.25 +
    1.26 +   1.  Call ToString(string).
    1.27 +   2.  Compute the number of characters in Result(1).
    1.28 +   3.  Let R be the empty string.
    1.29 +   4.  Let k be 0.
    1.30 +   5.  If k equals Result(2), return R.
    1.31 +   6.  Let c be the character at position k within Result(1).
    1.32 +   7.  If c is not %, go to step 18.
    1.33 +   8.  If k is greater than Result(2)-6, go to step 14.
    1.34 +   9.  If the character at position k+1 within result(1) is not u, go to step
    1.35 +   14.
    1.36 +   10. If the four characters at positions k+2, k+3, k+4, and k+5 within
    1.37 +   Result(1) are not all hexadecimal digits, go to step 14.
    1.38 +   11. Let c be the character whose Unicode encoding is the integer represented
    1.39 +   by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
    1.40 +   within Result(1).
    1.41 +   12. Increase k by 5.
    1.42 +   13. Go to step 18.
    1.43 +   14. If k is greater than Result(2)-3, go to step 18.
    1.44 +   15. If the two characters at positions k+1 and k+2 within Result(1) are not
    1.45 +   both hexadecimal digits, go to step 18.
    1.46 +   16. Let c be the character whose Unicode encoding is the integer represented
    1.47 +   by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
    1.48 +   within Result(1).
    1.49 +   17. Increase k by 2.
    1.50 +   18. Let R be a new string value computed by concatenating the previous value
    1.51 +   of R and c.
    1.52 +   19. Increase k by 1.
    1.53 +   20. Go to step 5.
    1.54 +   Author:             christine@netscape.com
    1.55 +   Date:               28 october 1997
    1.56 +*/
    1.57 +
    1.58 +var SECTION = "15.1.2.5-2";
    1.59 +var VERSION = "ECMA_1";
    1.60 +startTest();
    1.61 +var TITLE   = "unescape(string)";
    1.62 +
    1.63 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.64 +
    1.65 +// since there is only one character following "%", no conversion should occur.
    1.66 +
    1.67 +for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE += 16 ) {
    1.68 +  new TestCase( SECTION,
    1.69 +		"unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +" )",
    1.70 +		"%"+(ToHexString(CHARCODE)).substring(0,1),
    1.71 +		unescape( "%" + (ToHexString(CHARCODE)).substring(0,1) )  );
    1.72 +}
    1.73 +
    1.74 +// since there is only one character following "%u", no conversion should occur.
    1.75 +
    1.76 +for ( var CHARCODE = 0; CHARCODE < 256; CHARCODE +=16 ) {
    1.77 +  new TestCase( SECTION,
    1.78 +		"unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +" )",
    1.79 +		"%u"+(ToHexString(CHARCODE)).substring(0,1),
    1.80 +		unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1) )  );
    1.81 +}
    1.82 +
    1.83 +
    1.84 +// three char unicode string.  no conversion should occur
    1.85 +
    1.86 +for ( var CHARCODE = 1024; CHARCODE < 65536; CHARCODE+= 1234 ) {
    1.87 +  new TestCase
    1.88 +    (   SECTION,
    1.89 +	"unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3)+ " )",
    1.90 +
    1.91 +	"%u"+(ToUnicodeString(CHARCODE)).substring(0,3),
    1.92 +	unescape( "%u"+(ToUnicodeString(CHARCODE)).substring(0,3) )
    1.93 +      );
    1.94 +}
    1.95 +
    1.96 +test();
    1.97 +
    1.98 +function ToUnicodeString( n ) {
    1.99 +  var string = ToHexString(n);
   1.100 +
   1.101 +  for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
   1.102 +    string = "0" + string;
   1.103 +  }
   1.104 +
   1.105 +  return string;
   1.106 +}
   1.107 +function ToHexString( n ) {
   1.108 +  var hex = new Array();
   1.109 +
   1.110 +  for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
   1.111 +    ;
   1.112 +  }
   1.113 +
   1.114 +  for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
   1.115 +    hex[index] = Math.floor( n / Math.pow(16,mag) );
   1.116 +    n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
   1.117 +  }
   1.118 +
   1.119 +  hex[hex.length] = n % 16;
   1.120 +
   1.121 +  var string ="";
   1.122 +
   1.123 +  for ( var index = 0 ; index < hex.length ; index++ ) {
   1.124 +    switch ( hex[index] ) {
   1.125 +    case 10:
   1.126 +      string += "A";
   1.127 +      break;
   1.128 +    case 11:
   1.129 +      string += "B";
   1.130 +      break;
   1.131 +    case 12:
   1.132 +      string += "C";
   1.133 +      break;
   1.134 +    case 13:
   1.135 +      string += "D";
   1.136 +      break;
   1.137 +    case 14:
   1.138 +      string += "E";
   1.139 +      break;
   1.140 +    case 15:
   1.141 +      string += "F";
   1.142 +      break;
   1.143 +    default:
   1.144 +      string += hex[index];
   1.145 +    }
   1.146 +  }
   1.147 +
   1.148 +  if ( string.length == 1 ) {
   1.149 +    string = "0" + string;
   1.150 +  }
   1.151 +  return string;
   1.152 +}

mercurial