Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.6-2.js

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