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
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/
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
15 * The Original Code is Mozilla Communicator client code, released
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.
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.
37 * ***** END LICENSE BLOCK ***** */
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.
51 When the indexOf method is called with two arguments,
52 searchString and pos, the following steps are taken:
54 1. Call ToString, giving it the this value as its
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
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.
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
77 Author: christine@netscape.com, pschwartau@netscape.com
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
85 var SECTION = "15.5.4.6-2";
86 var VERSION = "ECMA_1";
87 var TITLE = "String.protoype.indexOf";
88 var BUGNUMBER="105721";
92 writeHeaderToLog( SECTION + " "+ TITLE);
95 // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721
97 // regress http://scopus/bugsplat/show_bug.cgi?id=105721
99 new TestCase( SECTION,
100 "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()",
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") );
111 new TestCase( SECTION,
112 "var s = new String(); s.indexOf()",
114 eval("var s = new String(); s.indexOf()") );
116 // some Unicode tests.
118 // generate a test string.
120 var TEST_STRING = "";
122 for ( var u = 0x00A1; u <= 0x00FF; u++ ) {
123 TEST_STRING += String.fromCharCode( u );
126 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
127 new TestCase( SECTION,
128 "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )",
130 TEST_STRING.indexOf( String.fromCharCode(u) ) );
132 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
133 new TestCase( SECTION,
134 "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )",
136 TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) );
141 var foo = new MyObject('hello');
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) );
151 var boo = new MyObject(true);
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") );
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') );
172 new TestCase( SECTION,
173 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
175 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );
177 new TestCase( SECTION,
178 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
180 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );
182 new TestCase( SECTION,
183 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
185 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );
187 new TestCase( SECTION,
188 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
190 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );
192 new TestCase( SECTION,
193 "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
195 eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );
197 new TestCase( SECTION,
198 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
200 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );
202 new TestCase( SECTION,
203 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
205 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );
207 new TestCase( SECTION,
208 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
210 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );
212 new TestCase( SECTION,
213 "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
215 eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );
217 new TestCase( SECTION,
218 "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
220 eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );
222 new TestCase( SECTION,
223 "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
225 eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );
227 new TestCase( SECTION,
228 "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
230 eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );
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')",
236 eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );
248 function MyObject (v) {
250 this.toString = new Function ( "return this.value +\"\"");
251 this.indexOf = String.prototype.indexOf;