]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.6-2.js
1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
23 File Name: 15.5.4.6-1.js
24 ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos)
25 Description: If the given searchString appears as a substring of the
26 result of converting this object to a string, at one or
27 more positions that are at or to the right of the
28 specified position, then the index of the leftmost such
29 position is returned; otherwise -1 is returned. If
30 positionis undefined or not supplied, 0 is assumed, so
31 as to search all of the string.
33 When the indexOf method is called with two arguments,
34 searchString and pos, the following steps are taken:
36 1. Call ToString, giving it the this value as its
38 2. Call ToString(searchString).
39 3. Call ToInteger(position). (If position is undefined
40 or not supplied, this step produces the value 0).
41 4. Compute the number of characters in Result(1).
42 5. Compute min(max(Result(3), 0), Result(4)).
43 6. Compute the number of characters in the string that
45 7. Compute the smallest possible integer k not smaller
46 than Result(5) such that k+Result(6) is not greater
47 than Result(4), and for all nonnegative integers j
48 less than Result(6), the character at position k+j
49 of Result(1) is the same as the character at position
50 j of Result(2); but if there is no such integer k,
51 then compute the value -1.
54 Note that the indexOf function is intentionally generic;
55 it does not require that its this value be a String object.
56 Therefore it can be transferred to other kinds of objects
59 Author: christine@netscape.com, pschwartau@netscape.com
61 Modified: 14 July 2002
62 Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
63 ECMA-262 Ed.3 Section 15.5.4.7
64 The length property of the indexOf method is 1
67 var SECTION
= "15.5.4.6-2";
68 var VERSION
= "ECMA_1";
70 var TITLE
= "String.protoype.indexOf";
71 var BUGNUMBER
="105721";
73 writeHeaderToLog( SECTION
+ " "+ TITLE
);
75 var testcases
= getTestCases();
79 // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721
89 function MyObject (v
) {
91 this.toString
= new Function ( "return this.value +\"\"");
92 this.indexOf
= String
.prototype.indexOf
;
95 function getTestCases() {
96 var array
= new Array();
99 // regress http://scopus/bugsplat/show_bug.cgi?id=105721
101 array
[item
++] = new TestCase( SECTION
, "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()", GLOBAL
, g().toString() );
104 array
[item
++] = new TestCase( SECTION
, "String.prototype.indexOf.length", 1, String
.prototype.indexOf
.length
);
105 array
[item
++] = new TestCase( SECTION
, "String.prototype.indexOf.length = null; String.prototype.indexOf.length", 1, eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") );
106 array
[item
++] = new TestCase( SECTION
, "delete String.prototype.indexOf.length", false, delete String
.prototype.indexOf
.length
);
107 array
[item
++] = new TestCase( SECTION
, "delete String.prototype.indexOf.length; String.prototype.indexOf.length", 1, eval("delete String.prototype.indexOf.length; String.prototype.indexOf.length") );
109 array
[item
++] = new TestCase( SECTION
, "var s = new String(); s.indexOf()", -1, eval("var s = new String(); s.indexOf()") );
111 // some Unicode tests.
113 // generate a test string.
115 var TEST_STRING
= "";
117 for ( var u
= 0x00A1; u
<= 0x00FF; u
++ ) {
118 TEST_STRING
+= String
.fromCharCode( u
);
121 for ( var u
= 0x00A1, i
= 0; u
<= 0x00FF; u
++, i
++ ) {
122 array
[item
++] = new TestCase( SECTION
,
123 "TEST_STRING.indexOf( " + String
.fromCharCode(u
) + " )",
125 TEST_STRING
.indexOf( String
.fromCharCode(u
) ) );
127 for ( var u
= 0x00A1, i
= 0; u
<= 0x00FF; u
++, i
++ ) {
128 array
[item
++] = new TestCase( SECTION
,
129 "TEST_STRING.indexOf( " + String
.fromCharCode(u
) + ", void 0 )",
131 TEST_STRING
.indexOf( String
.fromCharCode(u
), void 0 ) );
136 var foo
= new MyObject('hello');
138 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo
.indexOf("h") );
139 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo
.indexOf("e") );
140 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo
.indexOf("l") );
141 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo
.indexOf("l") );
142 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo
.indexOf("o") );
143 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf('X')", -1, foo
.indexOf("X") );
144 array
[item
++] = new TestCase( SECTION
, "var foo = new MyObject('hello');foo.indexOf(5) ", -1, foo
.indexOf(5) );
146 var boo
= new MyObject(true);
148 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo
.indexOf("t") );
149 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo
.indexOf("r") );
150 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo
.indexOf("u") );
151 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo
.indexOf("e") );
152 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo
.indexOf("true") );
153 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo
.indexOf("rue") );
154 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo
.indexOf("ue") );
155 array
[item
++] = new TestCase( SECTION
, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo
.indexOf("oy") );
158 var noo
= new MyObject( Math
.PI
);
159 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo
.indexOf('3') );
160 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo
.indexOf('.') );
161 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo
.indexOf('1') );
162 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo
.indexOf('4') );
163 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo
.indexOf('1') );
164 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo
.indexOf('5') );
165 array
[item
++] = new TestCase( SECTION
, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo
.indexOf('9') );
167 array
[item
++] = new TestCase( SECTION
,
168 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
170 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );
172 array
[item
++] = new TestCase( SECTION
,
173 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
175 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );
177 array
[item
++] = new TestCase( SECTION
,
178 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
180 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );
182 array
[item
++] = new TestCase( SECTION
,
183 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
185 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );
187 // This correctly throws, due to ES5 15.5.4.7 step 1
188 // array[item++] = new TestCase( SECTION,
189 // "var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')",
191 // eval("var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')") );
193 array
[item
++] = new TestCase( SECTION
,
194 "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
196 eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );
198 array
[item
++] = new TestCase( SECTION
,
199 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
201 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );
203 array
[item
++] = new TestCase( SECTION
,
204 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
206 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );
208 array
[item
++] = new TestCase( SECTION
,
209 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
211 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );
213 array
[item
++] = new TestCase( SECTION
,
214 "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
216 eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );
218 array
[item
++] = new TestCase( SECTION
,
219 "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
221 eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );
223 array
[item
++] = new TestCase( SECTION
,
224 "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
226 eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );
228 array
[item
++] = new TestCase( SECTION
,
229 "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
231 eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );
233 // new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively
234 array
[item
++] = new TestCase( SECTION
,
235 "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')",
237 eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );
244 for ( tc
= 0; tc
< testcases
.length
; tc
++ ) {
246 testcases
[tc
].passed
= writeTestCaseResult(
247 testcases
[tc
].expect
,
248 testcases
[tc
].actual
,
249 testcases
[tc
].description
+" = "+
250 testcases
[tc
].actual
);
252 testcases
[tc
].reason
+= ( testcases
[tc
].passed
)
258 // all tests must return a boolean value
259 return ( testcases
);