1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8
File Name: 15.5.4.6-1.js
9
ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos)
10
Description: If the given searchString appears as a substring of the
11
result of converting this object to a string, at one or
12
more positions that are at or to the right of the
13
specified position, then the index of the leftmost such
14
position is returned; otherwise -1 is returned. If
15
positionis undefined or not supplied, 0 is assumed, so
16
as to search all of the string.
18
When the indexOf method is called with two arguments,
19
searchString and pos, the following steps are taken:
21
1. Call ToString, giving it the this value as its
23
2. Call ToString(searchString).
24
3. Call ToInteger(position). (If position is undefined
25
or not supplied, this step produces the value 0).
26
4. Compute the number of characters in Result(1).
27
5. Compute min(max(Result(3), 0), Result(4)).
28
6. Compute the number of characters in the string that
30
7. Compute the smallest possible integer k not smaller
31
than Result(5) such that k+Result(6) is not greater
32
than Result(4), and for all nonnegative integers j
33
less than Result(6), the character at position k+j
34
of Result(1) is the same as the character at position
35
j of Result(2); but if there is no such integer k,
36
then compute the value -1.
39
Note that the indexOf function is intentionally generic;
40
it does not require that its this value be a String object.
41
Therefore it can be transferred to other kinds of objects
44
Author: christine@netscape.com, pschwartau@netscape.com
46
Modified: 14 July 2002
47
Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
48
ECMA-262 Ed.3 Section 15.5.4.7
49
The length property of the indexOf method is 1
52
var SECTION = "15.5.4.6-2";
53
var VERSION = "ECMA_1";
54
var TITLE = "String.protoype.indexOf";
55
var BUGNUMBER="105721";
59
writeHeaderToLog( SECTION + " "+ TITLE);
62
// the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721
64
// regress http://scopus/bugsplat/show_bug.cgi?id=105721
66
new TestCase( SECTION,
67
"function f() { return this; }; function g() { var h = f; return h(); }; g().toString()",
73
new TestCase( SECTION, "String.prototype.indexOf.length", 1, String.prototype.indexOf.length );
74
new TestCase( SECTION, "String.prototype.indexOf.length = null; String.prototype.indexOf.length", 1, eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") );
75
new TestCase( SECTION, "delete String.prototype.indexOf.length", false, delete String.prototype.indexOf.length );
76
new TestCase( SECTION, "delete String.prototype.indexOf.length; String.prototype.indexOf.length", 1, eval("delete String.prototype.indexOf.length; String.prototype.indexOf.length") );
78
new TestCase( SECTION,
79
"var s = new String(); s.indexOf()",
81
eval("var s = new String(); s.indexOf()") );
83
// some Unicode tests.
85
// generate a test string.
89
for ( var u = 0x00A1; u <= 0x00FF; u++ ) {
90
TEST_STRING += String.fromCharCode( u );
93
for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
94
new TestCase( SECTION,
95
"TEST_STRING.indexOf( " + String.fromCharCode(u) + " )",
97
TEST_STRING.indexOf( String.fromCharCode(u) ) );
99
for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
100
new TestCase( SECTION,
101
"TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )",
103
TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) );
108
var foo = new MyObject('hello');
110
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo.indexOf("h") );
111
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo.indexOf("e") );
112
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l") );
113
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l") );
114
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo.indexOf("o") );
115
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('X')", -1, foo.indexOf("X") );
116
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf(5) ", -1, foo.indexOf(5) );
118
var boo = new MyObject(true);
120
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo.indexOf("t") );
121
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo.indexOf("r") );
122
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo.indexOf("u") );
123
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo.indexOf("e") );
124
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo.indexOf("true") );
125
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo.indexOf("rue") );
126
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo.indexOf("ue") );
127
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo.indexOf("oy") );
130
var noo = new MyObject( Math.PI );
131
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo.indexOf('3') );
132
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo.indexOf('.') );
133
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1') );
134
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo.indexOf('4') );
135
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1') );
136
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo.indexOf('5') );
137
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo.indexOf('9') );
139
new TestCase( SECTION,
140
"var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
142
eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );
144
new TestCase( SECTION,
145
"var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
147
eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );
149
new TestCase( SECTION,
150
"var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
152
eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );
154
new TestCase( SECTION,
155
"var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
157
eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );
159
new TestCase( SECTION,
160
"var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
162
eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );
164
new TestCase( SECTION,
165
"var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
167
eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );
169
new TestCase( SECTION,
170
"var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
172
eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );
174
new TestCase( SECTION,
175
"var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
177
eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );
179
new TestCase( SECTION,
180
"var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
182
eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );
184
new TestCase( SECTION,
185
"var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
187
eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );
189
new TestCase( SECTION,
190
"var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
192
eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );
194
new TestCase( SECTION,
195
"var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
197
eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );
199
// new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively
200
new TestCase( SECTION,
201
"var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')",
203
eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );
215
function MyObject (v) {
217
this.toString = new Function ( "return this.value +\"\"");
218
this.indexOf = String.prototype.indexOf;