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