Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / String / 15.5.4.6-2.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 /**
41    File Name:          15.5.4.6-1.js
42    ECMA Section:       15.5.4.6 String.prototype.indexOf( searchString, pos)
43    Description:        If the given searchString appears as a substring of the
44    result of converting this object to a string, at one or
45    more positions that are at or to the right of the
46    specified position, then the index of the leftmost such
47    position is returned; otherwise -1 is returned.  If
48    positionis undefined or not supplied, 0 is assumed, so
49    as to search all of the string.
50
51    When the indexOf method is called with two arguments,
52    searchString and pos, the following steps are taken:
53
54    1. Call ToString, giving it the this value as its
55    argument.
56    2. Call ToString(searchString).
57    3. Call ToInteger(position). (If position is undefined
58    or not supplied, this step produces the value 0).
59    4. Compute the number of characters in Result(1).
60    5. Compute min(max(Result(3), 0), Result(4)).
61    6. Compute the number of characters in the string that
62    is Result(2).
63    7. Compute the smallest possible integer k not smaller
64    than Result(5) such that k+Result(6) is not greater
65    than Result(4), and for all nonnegative integers j
66    less than Result(6), the character at position k+j
67    of Result(1) is the same as the character at position
68    j of Result(2); but if there is no such integer k,
69    then compute the value -1.
70    8. Return Result(7).
71
72    Note that the indexOf function is intentionally generic;
73    it does not require that its this value be a String object.
74    Therefore it can be transferred to other kinds of objects
75    for use as a method.
76
77    Author:             christine@netscape.com, pschwartau@netscape.com
78    Date:               02 October 1997
79    Modified:           14 July 2002
80    Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
81    ECMA-262 Ed.3  Section 15.5.4.7
82    The length property of the indexOf method is 1
83    *
84    */
85 var SECTION = "15.5.4.6-2";
86 var VERSION = "ECMA_1";
87 var TITLE   = "String.protoype.indexOf";
88 var BUGNUMBER="105721";
89
90 startTest();
91
92 writeHeaderToLog( SECTION + " "+ TITLE);
93
94
95 // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721
96
97 // regress http://scopus/bugsplat/show_bug.cgi?id=105721
98
99 new TestCase( SECTION,
100               "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()",   
101               GLOBAL, 
102               g().toString()
103   );
104
105
106 new TestCase( SECTION, "String.prototype.indexOf.length",                                               1,     String.prototype.indexOf.length );
107 new TestCase( SECTION, "String.prototype.indexOf.length = null; String.prototype.indexOf.length",       1,     eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") );
108 new TestCase( SECTION, "delete String.prototype.indexOf.length",                                        false,  delete String.prototype.indexOf.length );
109 new TestCase( SECTION, "delete String.prototype.indexOf.length; String.prototype.indexOf.length",       1,      eval("delete String.prototype.indexOf.length; String.prototype.indexOf.length") );
110
111 new TestCase( SECTION,
112               "var s = new String(); s.indexOf()",    
113               -1,    
114               eval("var s = new String(); s.indexOf()") );
115
116 // some Unicode tests.
117
118 // generate a test string.
119
120 var TEST_STRING = "";
121
122 for ( var u = 0x00A1; u <= 0x00FF; u++ ) {
123   TEST_STRING += String.fromCharCode( u );
124 }
125
126 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
127   new TestCase(   SECTION,
128                   "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )",
129                   i,
130                   TEST_STRING.indexOf( String.fromCharCode(u) ) );
131 }
132 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
133   new TestCase(   SECTION,
134                   "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )",
135                   i,
136                   TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) );
137 }
138
139
140
141 var foo = new MyObject('hello');
142
143 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo.indexOf("h")  );
144 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo.indexOf("e")  );
145 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
146 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
147 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo.indexOf("o")  );
148 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('X')", -1,  foo.indexOf("X")  );
149 new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf(5) ", -1,  foo.indexOf(5)  );
150
151 var boo = new MyObject(true);
152
153 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo.indexOf("t")  );
154 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo.indexOf("r")  );
155 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo.indexOf("u")  );
156 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo.indexOf("e")  );
157 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo.indexOf("true")  );
158 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo.indexOf("rue")  );
159 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo.indexOf("ue")  );
160 new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo.indexOf("oy")  );
161
162
163 var noo = new MyObject( Math.PI );
164 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo.indexOf('3')  );
165 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo.indexOf('.')  );
166 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
167 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo.indexOf('4')  );
168 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
169 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo.indexOf('5')  );
170 new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo.indexOf('9')  );
171
172 new TestCase( SECTION,
173               "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
174               0,
175               eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );
176
177 new TestCase( SECTION,
178               "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
179               3,
180               eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );
181
182 new TestCase( SECTION,
183               "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
184               0,
185               eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );
186
187 new TestCase( SECTION,
188               "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
189               2,
190               eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );
191
192 new TestCase( SECTION,
193               "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
194               0,
195               eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );
196
197 new TestCase( SECTION,
198               "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
199               -1,
200               eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );
201
202 new TestCase( SECTION,
203               "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
204               -1,
205               eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );
206
207 new TestCase( SECTION,
208               "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
209               0,
210               eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );
211
212 new TestCase( SECTION,
213               "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
214               1,
215               eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );
216
217 new TestCase( SECTION,
218               "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
219               0,
220               eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );
221
222 new TestCase( SECTION,
223               "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
224               1,
225               eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );
226
227 new TestCase( SECTION,
228               "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
229               8,
230               eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );
231
232 // new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively
233 new TestCase( SECTION,
234               "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')",
235               8,
236               eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );
237
238 test();
239
240 function f() {
241   return this;
242 }
243 function g() {
244   var h = f;
245   return h();
246 }
247
248 function MyObject (v) {
249   this.value      = v;
250   this.toString   = new Function ( "return this.value +\"\"");
251   this.indexOf     = String.prototype.indexOf;
252 }
253