* support-test-*.cs: Rename from test-*-p2.cs.
[mono.git] / mcs / class / Microsoft.JScript / Test / Mozilla / ecma / GlobalObject / 15.1.2.4.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 /**
40    File Name:          15.1.2.4.js
41    ECMA Section:       15.1.2.4  Function properties of the global object
42    escape( string )
43
44    Description:
45    The escape function computes a new version of a string value in which
46    certain characters have been replaced by a hexadecimal escape sequence.
47    The result thus contains no special characters that might have special
48    meaning within a URL.
49
50    For characters whose Unicode encoding is 0xFF or less, a two-digit
51    escape sequence of the form %xx is used in accordance with RFC1738.
52    For characters whose Unicode encoding is greater than 0xFF, a four-
53    digit escape sequence of the form %uxxxx is used.
54
55    When the escape 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.  Get the character at position k within Result(1).
64    7.  If Result(6) is one of the 69 nonblank ASCII characters
65    ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
66    0123456789 @*_+-./, go to step 14.
67    8.  Compute the 16-bit unsigned integer that is the Unicode character
68    encoding of Result(6).
69    9.  If Result(8), is less than 256, go to step 12.
70    10.  Let S be a string containing six characters "%uwxyz" where wxyz are
71    four hexadecimal digits encoding the value of Result(8).
72    11.  Go to step 15.
73    12.  Let S be a string containing three characters "%xy" where xy are two
74    hexadecimal digits encoding the value of Result(8).
75    13.  Go to step 15.
76    14.  Let S be a string containing the single character Result(6).
77    15.  Let R be a new string value computed by concatenating the previous value
78    of R and S.
79    16.  Increase k by 1.
80    17.  Go to step 5.
81
82    Author:             christine@netscape.com
83    Date:               28 october 1997
84
85 */
86 var SECTION = "15.1.2.4";
87 var VERSION = "ECMA_1";
88 startTest();
89 var TITLE   = "escape(string)";
90
91 writeHeaderToLog( SECTION + " "+ TITLE);
92
93 new TestCase( SECTION, "escape.length",         1,          escape.length );
94 new TestCase( SECTION, "escape.length = null; escape.length",   1,  eval("escape.length = null; escape.length") );
95 new TestCase( SECTION, "delete escape.length",                  false,  delete escape.length );
96 new TestCase( SECTION, "delete escape.length; escape.length",   1,      eval("delete escape.length; escape.length") );
97 new TestCase( SECTION, "var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS",    "",    eval("var MYPROPS=''; for ( var p in escape ) { MYPROPS+= p}; MYPROPS") );
98
99 new TestCase( SECTION, "escape()",              "undefined",    escape() );
100 new TestCase( SECTION, "escape('')",            "",             escape('') );
101 new TestCase( SECTION, "escape( null )",        "null",         escape(null) );
102 new TestCase( SECTION, "escape( void 0 )",      "undefined",    escape(void 0) );
103 new TestCase( SECTION, "escape( true )",        "true",         escape( true ) );
104 new TestCase( SECTION, "escape( false )",       "false",        escape( false ) );
105
106 new TestCase( SECTION, "escape( new Boolean(true) )",   "true", escape(new Boolean(true)) );
107 new TestCase( SECTION, "escape( new Boolean(false) )",  "false",    escape(new Boolean(false)) );
108
109 new TestCase( SECTION, "escape( Number.NaN  )",                 "NaN",      escape(Number.NaN) );
110 new TestCase( SECTION, "escape( -0 )",                          "0",        escape( -0 ) );
111 new TestCase( SECTION, "escape( 'Infinity' )",                  "Infinity", escape( "Infinity" ) );
112 new TestCase( SECTION, "escape( Number.POSITIVE_INFINITY )",    "Infinity", escape( Number.POSITIVE_INFINITY ) );
113 new TestCase( SECTION, "escape( Number.NEGATIVE_INFINITY )",    "-Infinity", escape( Number.NEGATIVE_INFINITY ) );
114
115 var ASCII_TEST_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
116
117 new TestCase( SECTION, "escape( " +ASCII_TEST_STRING+" )",    ASCII_TEST_STRING,  escape( ASCII_TEST_STRING ) );
118
119 // ASCII value less than
120
121 for ( var CHARCODE = 0; CHARCODE < 32; CHARCODE++ ) {
122   new TestCase( SECTION,
123                 "escape(String.fromCharCode("+CHARCODE+"))",
124                 "%"+ToHexString(CHARCODE),
125                 escape(String.fromCharCode(CHARCODE))  );
126 }
127 for ( var CHARCODE = 128; CHARCODE < 256; CHARCODE++ ) {
128   new TestCase( SECTION,
129                 "escape(String.fromCharCode("+CHARCODE+"))",
130                 "%"+ToHexString(CHARCODE),
131                 escape(String.fromCharCode(CHARCODE))  );
132 }
133
134 for ( var CHARCODE = 256; CHARCODE < 1024; CHARCODE++ ) {
135   new TestCase( SECTION,
136                 "escape(String.fromCharCode("+CHARCODE+"))",
137                 "%u"+ ToUnicodeString(CHARCODE),
138                 escape(String.fromCharCode(CHARCODE))  );
139 }
140 for ( var CHARCODE = 65500; CHARCODE < 65536; CHARCODE++ ) {
141   new TestCase( SECTION,
142                 "escape(String.fromCharCode("+CHARCODE+"))",
143                 "%u"+ ToUnicodeString(CHARCODE),
144                 escape(String.fromCharCode(CHARCODE))  );
145 }
146
147 test();
148
149 function ToUnicodeString( n ) {
150   var string = ToHexString(n);
151
152   for ( var PAD = (4 - string.length ); PAD > 0; PAD-- ) {
153     string = "0" + string;
154   }
155
156   return string;
157 }
158 function ToHexString( n ) {
159   var hex = new Array();
160
161   for ( var mag = 1; Math.pow(16,mag) <= n ; mag++ ) {
162     ;
163   }
164
165   for ( index = 0, mag -= 1; mag > 0; index++, mag-- ) {
166     hex[index] = Math.floor( n / Math.pow(16,mag) );
167     n -= Math.pow(16,mag) * Math.floor( n/Math.pow(16,mag) );
168   }
169
170   hex[hex.length] = n % 16;
171
172   var string ="";
173
174   for ( var index = 0 ; index < hex.length ; index++ ) {
175     switch ( hex[index] ) {
176     case 10:
177       string += "A";
178       break;
179     case 11:
180       string += "B";
181       break;
182     case 12:
183       string += "C";
184       break;
185     case 13:
186       string += "D";
187       break;
188     case 14:
189       string += "E";
190       break;
191     case 15:
192       string += "F";
193       break;
194     default:
195       string += hex[index];
196     }
197   }
198
199   if ( string.length == 1 ) {
200     string = "0" + string;
201   }
202   return string;
203 }