2615d625313cf376b8cc1c13d24d9cc05d33cfd7
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / parserstress / tests / ecma / GlobalObject / 15.1.2.5-3.js
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 gTestfile = '15.1.2.5-3.js';
40
41 /**
42    File Name:          15.1.2.5-3.js
43    ECMA Section:       15.1.2.5  Function properties of the global object
44    unescape( string )
45
46    Description:
47    This tests the cases where one of the four characters following "%u" is
48    not a hexidecimal character, or one of the two characters following "%"
49    or "%u" is not a hexidecimal character.
50
51    The unescape function computes a new version of a string value in which
52    each escape sequences of the sort that might be introduced by the escape
53    function is replaced with the character that it represents.
54
55    When the unescape function is called with one argument string, the
56    following steps are taken:
57
58    1.  Call ToString(string).
59    2.  Compute the number of characters in Result(1).
60    3.  Let R be the empty string.
61    4.  Let k be 0.
62    5.  If k equals Result(2), return R.
63    6.  Let c be the character at position k within Result(1).
64    7.  If c is not %, go to step 18.
65    8.  If k is greater than Result(2)-6, go to step 14.
66    9.  If the character at position k+1 within result(1) is not u, go to step
67    14.
68    10. If the four characters at positions k+2, k+3, k+4, and k+5 within
69    Result(1) are not all hexadecimal digits, go to step 14.
70    11. Let c be the character whose Unicode encoding is the integer represented
71    by the four hexadecimal digits at positions k+2, k+3, k+4, and k+5
72    within Result(1).
73    12. Increase k by 5.
74    13. Go to step 18.
75    14. If k is greater than Result(2)-3, go to step 18.
76    15. If the two characters at positions k+1 and k+2 within Result(1) are not
77    both hexadecimal digits, go to step 18.
78    16. Let c be the character whose Unicode encoding is the integer represented
79    by two zeroes plus the two hexadecimal digits at positions k+1 and k+2
80    within Result(1).
81    17. Increase k by 2.
82    18. Let R be a new string value computed by concatenating the previous value
83    of R and c.
84    19. Increase k by 1.
85    20. Go to step 5.
86    Author:             christine@netscape.com
87    Date:               28 october 1997
88 */
89
90
91 var SECTION = "15.1.2.5-3";
92 var VERSION = "ECMA_1";
93 startTest();
94 var TITLE   = "unescape(string)";
95
96 writeHeaderToLog( SECTION + " "+ TITLE);
97
98 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
99   NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
100
101   new TestCase( SECTION,
102                 "unescape( %"+ (ToHexString(CHARCODE)).substring(0,1) +
103                 String.fromCharCode( NONHEXCHARCODE ) +" )" +
104                 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
105                 "%"+(ToHexString(CHARCODE)).substring(0,1)+
106                 String.fromCharCode( NONHEXCHARCODE ),
107                 unescape( "%" + (ToHexString(CHARCODE)).substring(0,1)+
108                           String.fromCharCode( NONHEXCHARCODE ) )  );
109 }
110 for ( var CHARCODE = 0, NONHEXCHARCODE = 0; CHARCODE < 256; CHARCODE++, NONHEXCHARCODE++ ) {
111   NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
112
113   new TestCase( SECTION,
114                 "unescape( %u"+ (ToHexString(CHARCODE)).substring(0,1) +
115                 String.fromCharCode( NONHEXCHARCODE ) +" )" +
116                 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
117                 "%u"+(ToHexString(CHARCODE)).substring(0,1)+
118                 String.fromCharCode( NONHEXCHARCODE ),
119                 unescape( "%u" + (ToHexString(CHARCODE)).substring(0,1)+
120                           String.fromCharCode( NONHEXCHARCODE ) )  );
121 }
122
123 for ( var CHARCODE = 0, NONHEXCHARCODE = 0 ; CHARCODE < 65536; CHARCODE+= 54321, NONHEXCHARCODE++ ) {
124   NONHEXCHARCODE = getNextNonHexCharCode( NONHEXCHARCODE );
125
126   new TestCase( SECTION,
127                 "unescape( %u"+ (ToUnicodeString(CHARCODE)).substring(0,3) +
128                 String.fromCharCode( NONHEXCHARCODE ) +" )" +
129                 "[where last character is String.fromCharCode("+NONHEXCHARCODE+")]",
130
131                 String.fromCharCode(eval("0x"+ (ToUnicodeString(CHARCODE)).substring(0,2))) +
132                 (ToUnicodeString(CHARCODE)).substring(2,3) +
133                 String.fromCharCode( NONHEXCHARCODE ),
134
135                 unescape( "%" + (ToUnicodeString(CHARCODE)).substring(0,3)+
136                           String.fromCharCode( NONHEXCHARCODE ) )  );
137 }
138
139 test();
140
141 function getNextNonHexCharCode( n ) {
142   for (  ; n < Math.pow(2,16); n++ ) {
143     if ( (  n == 43 || n == 45 || n == 46 || n == 47 ||
144             (n >= 71 && n <= 90) || (n >= 103 && n <= 122) ||
145             n == 64 || n == 95 ) ) {
146       break;
147     } else {
148       n = ( n > 122 ) ? 0 : n;
149     }
150   }
151   return n;
152 }
153 function ToUnicodeString( n ) {
154   var string = ToHexString(n);
155
156   for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
157     string = "0" + string;
158   }
159
160   return string;
161 }
162 function ToHexString( n ) {
163   var hex = new Array();
164
165   for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
166     ;
167   }
168
169   for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
170     hex[index] = Math.floor( n / Math.pow(16,mag) );
171     n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
172   }
173
174   hex[hex.length] = n % 16;
175
176   var string ="";
177
178   for ( var index = 0 ; index < hex.length ; index++ ) {
179     switch ( hex[index] ) {
180     case 10:
181       string += "A";
182       break;
183     case 11:
184       string += "B";
185       break;
186     case 12:
187       string += "C";
188       break;
189     case 13:
190       string += "D";
191       break;
192     case 14:
193       string += "E";
194       break;
195     case 15:
196       string += "F";
197       break;
198     default:
199       string += hex[index];
200     }
201   }
202
203   if ( string.length == 1 ) {
204     string = "0" + string;
205   }
206   return string;
207 }