~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/lang/js/test/ecma/GlobalObject/15.1.2.5-3.js

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla Communicator client code, released
 
16
 * March 31, 1998.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Netscape Communications Corporation.
 
20
 * Portions created by the Initial Developer are Copyright (C) 1998
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the MPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the MPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
/**
 
39
   File Name:          15.1.2.5-3.js
 
40
   ECMA Section:       15.1.2.5  Function properties of the global object
 
41
   unescape( string )
 
42
 
 
43
   Description:
 
44
   This tests the cases where one of the four characters following "%u" is
 
45
   not a hexidecimal character, or one of the two characters following "%"
 
46
   or "%u" is not a hexidecimal character.
 
47
 
 
48
   The unescape function computes a new version of a string value in which
 
49
   each escape sequences of the sort that might be introduced by the escape
 
50
   function is replaced with the character that it represents.
 
51
 
 
52
   When the unescape function is called with one argument string, the
 
53
   following steps are taken:
 
54
 
 
55
   1.  Call ToString(string).
 
56
   2.  Compute the number of characters in Result(1).
 
57
   3.  Let R be the empty string.
 
58
   4.  Let k be 0.
 
59
   5.  If k equals Result(2), return R.
 
60
   6.  Let c be the character at position k within Result(1).
 
61
   7.  If c is not %, go to step 18.
 
62
   8.  If k is greater than Result(2)-6, go to step 14.
 
63
   9.  If the character at position k+1 within result(1) is not u, go to step
 
64
   14.
 
65
   10. If the four characters at positions k+2, k+3, k+4, and k+5 within
 
66
   Result(1) are not all hexadecimal digits, go to step 14.
 
67
   11. Let c be the character whose Unicode encoding is the integer represented
 
68
   by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
 
69
   within Result(1).
 
70
   12. Increase k by 5.
 
71
   13. Go to step 18.
 
72
   14. If k is greater than Result(2)-3, go to step 18.
 
73
   15. If the two characters at positions k+1 and k+2 within Result(1) are not
 
74
   both hexadecimal digits, go to step 18.
 
75
   16. Let c be the character whose Unicode encoding is the integer represented
 
76
   by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
 
77
   within Result(1).
 
78
   17. Increase k by 2.
 
79
   18. Let R be a new string value computed by concatenating the previous value
 
80
   of R and c.
 
81
   19. Increase k by 1.
 
82
   20. Go to step 5.
 
83
   Author:             christine@netscape.com
 
84
   Date:               28 october 1997
 
85
*/
 
86
 
 
87
 
 
88
var SECTION = "15.1.2.5-3";
 
89
var VERSION = "ECMA_1";
 
90
startTest();
 
91
var TITLE   = "unescape(string)";
 
92
 
 
93
writeHeaderToLog( SECTION + " "+ TITLE);
 
94
 
 
95
for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
 
96
  NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
 
97
 
 
98
  new TestCase( SECTION,
 
99
                "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +
 
100
                String.fromCharCode( NONHEXCHARCODE ) +" )" +
 
101
                "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
 
102
                "%"+(ToHexString(CHARCODE)).substring(0,1)+
 
103
                String.fromCharCode( NONHEXCHARCODE ),
 
104
                unescape( "%" + (ToHexString(CHARCODE)).substring(0,1)+
 
105
                          String.fromCharCode( NONHEXCHARCODE ) )  );
 
106
}
 
107
for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
 
108
  NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
 
109
 
 
110
  new TestCase( SECTION,
 
111
                "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +
 
112
                String.fromCharCode( NONHEXCHARCODE ) +" )" +
 
113
                "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
 
114
                "%u"+(ToHexString(CHARCODE)).substring(0,1)+
 
115
                String.fromCharCode( NONHEXCHARCODE ),
 
116
                unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1)+
 
117
                          String.fromCharCode( NONHEXCHARCODE ) )  );
 
118
}
 
119
 
 
120
for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
 
121
  NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
 
122
 
 
123
  new TestCase( SECTION,
 
124
                "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3) +
 
125
                String.fromCharCode( NONHEXCHARCODE ) +" )" +
 
126
                "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
 
127
 
 
128
                String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
 
129
                (ToUnicodeString(CHARCODE)).substring(2,3) +
 
130
                String.fromCharCode( NONHEXCHARCODE ),
 
131
 
 
132
                unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
 
133
                          String.fromCharCode( NONHEXCHARCODE ) )  );
 
134
}
 
135
 
 
136
test();
 
137
 
 
138
function getNextNonHexCharCode( n ) {
 
139
  for (  ; n < Math.pow(2,16); n++ ) {
 
140
    if ( (  n == 43 || n == 45 || n == 46 || n == 47 ||
 
141
            (n >= 71 && n <= 90) || (n >= 103 && n <= 122) ||
 
142
            n == 64 || n == 95 ) ) {
 
143
      break;
 
144
    } else {
 
145
      n = ( n > 122 ) ? 0 : n;
 
146
    }
 
147
  }
 
148
  return n;
 
149
}
 
150
function ToUnicodeString( n ) {
 
151
  var string = ToHexString(n);
 
152
 
 
153
  for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
 
154
    string = "0" + string;
 
155
  }
 
156
 
 
157
  return string;
 
158
}
 
159
function ToHexString( n ) {
 
160
  var hex = new Array();
 
161
 
 
162
  for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
 
163
    ;
 
164
  }
 
165
 
 
166
  for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
 
167
    hex[index] = Math.floor( n / Math.pow(16,mag) );
 
168
    n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
 
169
  }
 
170
 
 
171
  hex[hex.length] = n % 16;
 
172
 
 
173
  var string ="";
 
174
 
 
175
  for ( var index = 0 ; index < hex.length ; index++ ) {
 
176
    switch ( hex[index] ) {
 
177
    case 10:
 
178
      string += "A";
 
179
      break;
 
180
    case 11:
 
181
      string += "B";
 
182
      break;
 
183
    case 12:
 
184
      string += "C";
 
185
      break;
 
186
    case 13:
 
187
      string += "D";
 
188
      break;
 
189
    case 14:
 
190
      string += "E";
 
191
      break;
 
192
    case 15:
 
193
      string += "F";
 
194
      break;
 
195
    default:
 
196
      string += hex[index];
 
197
    }
 
198
  }
 
199
 
 
200
  if ( string.length == 1 ) {
 
201
    string = "0" + string;
 
202
  }
 
203
  return string;
 
204
}