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/ 5 * 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. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 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 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 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. 32 33 When the indexOf method is called with two arguments, 34 searchString and pos, the following steps are taken: 35 36 1. Call ToString, giving it the this value as its 37 argument. 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 44 is Result(2). 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. 52 8. Return Result(7). 53 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 57 for use as a method. 58 59 Author: christine (at) netscape.com, pschwartau (at) netscape.com 60 Date: 02 October 1997 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 65 * 66 */ 67 var SECTION = "15.5.4.6-2"; 68 var VERSION = "ECMA_1"; 69 startTest(); 70 var TITLE = "String.protoype.indexOf"; 71 var BUGNUMBER="105721"; 72 73 writeHeaderToLog( SECTION + " "+ TITLE); 74 75 var testcases = getTestCases(); 76 test(); 77 78 79 // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721 80 81 function f() { 82 return this; 83 } 84 function g() { 85 var h = f; 86 return h(); 87 } 88 89 function MyObject (v) { 90 this.value = v; 91 this.toString = new Function ( "return this.value +\"\""); 92 this.indexOf = String.prototype.indexOf; 93 } 94 95 function getTestCases() { 96 var array = new Array(); 97 var item = 0; 98 99 // regress http://scopus/bugsplat/show_bug.cgi?id=105721 100 101 array[item++] = new TestCase( SECTION, "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()", GLOBAL, g().toString() ); 102 103 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") ); 108 109 array[item++] = new TestCase( SECTION, "var s = new String(); s.indexOf()", -1, eval("var s = new String(); s.indexOf()") ); 110 111 // some Unicode tests. 112 113 // generate a test string. 114 115 var TEST_STRING = ""; 116 117 for ( var u = 0x00A1; u <= 0x00FF; u++ ) { 118 TEST_STRING += String.fromCharCode( u ); 119 } 120 121 for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) { 122 array[item++] = new TestCase( SECTION, 123 "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )", 124 i, 125 TEST_STRING.indexOf( String.fromCharCode(u) ) ); 126 } 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 )", 130 i, 131 TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) ); 132 } 133 134 135 136 var foo = new MyObject('hello'); 137 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) ); 145 146 var boo = new MyObject(true); 147 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") ); 156 157 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') ); 166 167 array[item++] = new TestCase( SECTION, 168 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')", 169 0, 170 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") ); 171 172 array[item++] = new TestCase( SECTION, 173 "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')", 174 3, 175 eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") ); 176 177 array[item++] = new TestCase( SECTION, 178 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')", 179 0, 180 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") ); 181 182 array[item++] = new TestCase( SECTION, 183 "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')", 184 2, 185 eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") ); 186 187 array[item++] = new TestCase( SECTION, 188 "var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')", 189 0, 190 eval("var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')") ); 191 192 array[item++] = new TestCase( SECTION, 193 "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')", 194 0, 195 eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") ); 196 197 array[item++] = new TestCase( SECTION, 198 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')", 199 -1, 200 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") ); 201 202 array[item++] = new TestCase( SECTION, 203 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)", 204 -1, 205 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") ); 206 207 array[item++] = new TestCase( SECTION, 208 "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)", 209 0, 210 eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") ); 211 212 array[item++] = new TestCase( SECTION, 213 "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')", 214 1, 215 eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") ); 216 217 array[item++] = new TestCase( SECTION, 218 "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')", 219 0, 220 eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") ); 221 222 array[item++] = new TestCase( SECTION, 223 "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')", 224 1, 225 eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") ); 226 227 array[item++] = new TestCase( SECTION, 228 "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )", 229 8, 230 eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") ); 231 232 // new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively 233 array[item++] = new TestCase( SECTION, 234 "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')", 235 8, 236 eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") ); 237 238 239 return array; 240 } 241 242 function test() { 243 for ( tc = 0; tc < testcases.length; tc++ ) { 244 245 testcases[tc].passed = writeTestCaseResult( 246 testcases[tc].expect, 247 testcases[tc].actual, 248 testcases[tc].description +" = "+ 249 testcases[tc].actual ); 250 251 testcases[tc].reason += ( testcases[tc].passed ) 252 ? "" 253 : "wrong value " 254 } 255 stopTest(); 256 257 // all tests must return a boolean value 258 return ( testcases ); 259 } 260