|
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/. */ |
|
5 |
|
6 |
|
7 /** |
|
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. |
|
17 |
|
18 When the indexOf method is called with two arguments, |
|
19 searchString and pos, the following steps are taken: |
|
20 |
|
21 1. Call ToString, giving it the this value as its |
|
22 argument. |
|
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 |
|
29 is Result(2). |
|
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. |
|
37 8. Return Result(7). |
|
38 |
|
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 |
|
42 for use as a method. |
|
43 |
|
44 Author: christine@netscape.com, pschwartau@netscape.com |
|
45 Date: 02 October 1997 |
|
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 |
|
50 * |
|
51 */ |
|
52 var SECTION = "15.5.4.6-2"; |
|
53 var VERSION = "ECMA_1"; |
|
54 var TITLE = "String.protoype.indexOf"; |
|
55 var BUGNUMBER="105721"; |
|
56 |
|
57 startTest(); |
|
58 |
|
59 writeHeaderToLog( SECTION + " "+ TITLE); |
|
60 |
|
61 |
|
62 // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721 |
|
63 |
|
64 // regress http://scopus/bugsplat/show_bug.cgi?id=105721 |
|
65 |
|
66 new TestCase( SECTION, |
|
67 "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()", |
|
68 GLOBAL, |
|
69 g().toString() |
|
70 ); |
|
71 |
|
72 |
|
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") ); |
|
77 |
|
78 new TestCase( SECTION, |
|
79 "var s = new String(); s.indexOf()", |
|
80 -1, |
|
81 eval("var s = new String(); s.indexOf()") ); |
|
82 |
|
83 // some Unicode tests. |
|
84 |
|
85 // generate a test string. |
|
86 |
|
87 var TEST_STRING = ""; |
|
88 |
|
89 for ( var u = 0x00A1; u <= 0x00FF; u++ ) { |
|
90 TEST_STRING += String.fromCharCode( u ); |
|
91 } |
|
92 |
|
93 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) { |
|
94 new TestCase( SECTION, |
|
95 "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )", |
|
96 i, |
|
97 TEST_STRING.indexOf( String.fromCharCode(u) ) ); |
|
98 } |
|
99 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) { |
|
100 new TestCase( SECTION, |
|
101 "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )", |
|
102 i, |
|
103 TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) ); |
|
104 } |
|
105 |
|
106 |
|
107 |
|
108 var foo = new MyObject('hello'); |
|
109 |
|
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) ); |
|
117 |
|
118 var boo = new MyObject(true); |
|
119 |
|
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") ); |
|
128 |
|
129 |
|
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') ); |
|
138 |
|
139 new TestCase( SECTION, |
|
140 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')", |
|
141 0, |
|
142 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") ); |
|
143 |
|
144 new TestCase( SECTION, |
|
145 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')", |
|
146 3, |
|
147 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") ); |
|
148 |
|
149 new TestCase( SECTION, |
|
150 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')", |
|
151 0, |
|
152 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") ); |
|
153 |
|
154 new TestCase( SECTION, |
|
155 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')", |
|
156 2, |
|
157 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") ); |
|
158 |
|
159 new TestCase( SECTION, |
|
160 "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')", |
|
161 0, |
|
162 eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") ); |
|
163 |
|
164 new TestCase( SECTION, |
|
165 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')", |
|
166 -1, |
|
167 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") ); |
|
168 |
|
169 new TestCase( SECTION, |
|
170 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)", |
|
171 -1, |
|
172 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") ); |
|
173 |
|
174 new TestCase( SECTION, |
|
175 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)", |
|
176 0, |
|
177 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") ); |
|
178 |
|
179 new TestCase( SECTION, |
|
180 "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')", |
|
181 1, |
|
182 eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") ); |
|
183 |
|
184 new TestCase( SECTION, |
|
185 "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')", |
|
186 0, |
|
187 eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") ); |
|
188 |
|
189 new TestCase( SECTION, |
|
190 "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')", |
|
191 1, |
|
192 eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") ); |
|
193 |
|
194 new TestCase( SECTION, |
|
195 "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )", |
|
196 8, |
|
197 eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") ); |
|
198 |
|
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')", |
|
202 8, |
|
203 eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") ); |
|
204 |
|
205 test(); |
|
206 |
|
207 function f() { |
|
208 return this; |
|
209 } |
|
210 function g() { |
|
211 var h = f; |
|
212 return h(); |
|
213 } |
|
214 |
|
215 function MyObject (v) { |
|
216 this.value = v; |
|
217 this.toString = new Function ( "return this.value +\"\""); |
|
218 this.indexOf = String.prototype.indexOf; |
|
219 } |
|
220 |