michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: 15.5.4.6-1.js michael@0: ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos) michael@0: Description: If the given searchString appears as a substring of the michael@0: result of converting this object to a string, at one or michael@0: more positions that are at or to the right of the michael@0: specified position, then the index of the leftmost such michael@0: position is returned; otherwise -1 is returned. If michael@0: positionis undefined or not supplied, 0 is assumed, so michael@0: as to search all of the string. michael@0: michael@0: When the indexOf method is called with two arguments, michael@0: searchString and pos, the following steps are taken: michael@0: michael@0: 1. Call ToString, giving it the this value as its michael@0: argument. michael@0: 2. Call ToString(searchString). michael@0: 3. Call ToInteger(position). (If position is undefined michael@0: or not supplied, this step produces the value 0). michael@0: 4. Compute the number of characters in Result(1). michael@0: 5. Compute min(max(Result(3), 0), Result(4)). michael@0: 6. Compute the number of characters in the string that michael@0: is Result(2). michael@0: 7. Compute the smallest possible integer k not smaller michael@0: than Result(5) such that k+Result(6) is not greater michael@0: than Result(4), and for all nonnegative integers j michael@0: less than Result(6), the character at position k+j michael@0: of Result(1) is the same as the character at position michael@0: j of Result(2); but if there is no such integer k, michael@0: then compute the value -1. michael@0: 8. Return Result(7). michael@0: michael@0: Note that the indexOf function is intentionally generic; michael@0: it does not require that its this value be a String object. michael@0: Therefore it can be transferred to other kinds of objects michael@0: for use as a method. michael@0: michael@0: Author: christine@netscape.com, pschwartau@netscape.com michael@0: Date: 02 October 1997 michael@0: Modified: 14 July 2002 michael@0: Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289 michael@0: ECMA-262 Ed.3 Section 15.5.4.7 michael@0: The length property of the indexOf method is 1 michael@0: * michael@0: */ michael@0: var SECTION = "15.5.4.6-2"; michael@0: var VERSION = "ECMA_1"; michael@0: var TITLE = "String.protoype.indexOf"; michael@0: var BUGNUMBER="105721"; michael@0: michael@0: startTest(); michael@0: michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: michael@0: // the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721 michael@0: michael@0: // regress http://scopus/bugsplat/show_bug.cgi?id=105721 michael@0: michael@0: new TestCase( SECTION, michael@0: "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()", michael@0: GLOBAL, michael@0: g().toString() michael@0: ); michael@0: michael@0: michael@0: new TestCase( SECTION, "String.prototype.indexOf.length", 1, String.prototype.indexOf.length ); michael@0: new TestCase( SECTION, "String.prototype.indexOf.length = null; String.prototype.indexOf.length", 1, eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") ); michael@0: new TestCase( SECTION, "delete String.prototype.indexOf.length", false, delete String.prototype.indexOf.length ); michael@0: new TestCase( SECTION, "delete String.prototype.indexOf.length; String.prototype.indexOf.length", 1, eval("delete String.prototype.indexOf.length; String.prototype.indexOf.length") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var s = new String(); s.indexOf()", michael@0: -1, michael@0: eval("var s = new String(); s.indexOf()") ); michael@0: michael@0: // some Unicode tests. michael@0: michael@0: // generate a test string. michael@0: michael@0: var TEST_STRING = ""; michael@0: michael@0: for ( var u = 0x00A1; u <= 0x00FF; u++ ) { michael@0: TEST_STRING += String.fromCharCode( u ); michael@0: } michael@0: michael@0: for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) { michael@0: new TestCase( SECTION, michael@0: "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )", michael@0: i, michael@0: TEST_STRING.indexOf( String.fromCharCode(u) ) ); michael@0: } michael@0: for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) { michael@0: new TestCase( SECTION, michael@0: "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )", michael@0: i, michael@0: TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) ); michael@0: } michael@0: michael@0: michael@0: michael@0: var foo = new MyObject('hello'); michael@0: michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo.indexOf("h") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo.indexOf("e") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo.indexOf("o") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('X')", -1, foo.indexOf("X") ); michael@0: new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf(5) ", -1, foo.indexOf(5) ); michael@0: michael@0: var boo = new MyObject(true); michael@0: michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo.indexOf("t") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo.indexOf("r") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo.indexOf("u") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo.indexOf("e") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo.indexOf("true") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo.indexOf("rue") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo.indexOf("ue") ); michael@0: new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo.indexOf("oy") ); michael@0: michael@0: michael@0: var noo = new MyObject( Math.PI ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo.indexOf('3') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo.indexOf('.') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo.indexOf('4') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo.indexOf('5') ); michael@0: new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo.indexOf('9') ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')", michael@0: 0, michael@0: eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')", michael@0: 3, michael@0: eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')", michael@0: 0, michael@0: eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')", michael@0: 2, michael@0: eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')", michael@0: 0, michael@0: eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')", michael@0: -1, michael@0: eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)", michael@0: -1, michael@0: eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)", michael@0: 0, michael@0: eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')", michael@0: 1, michael@0: eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')", michael@0: 0, michael@0: eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')", michael@0: 1, michael@0: eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") ); michael@0: michael@0: new TestCase( SECTION, michael@0: "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )", michael@0: 8, michael@0: eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") ); michael@0: michael@0: // new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively michael@0: new TestCase( SECTION, michael@0: "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')", michael@0: 8, michael@0: eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") ); michael@0: michael@0: test(); michael@0: michael@0: function f() { michael@0: return this; michael@0: } michael@0: function g() { michael@0: var h = f; michael@0: return h(); michael@0: } michael@0: michael@0: function MyObject (v) { michael@0: this.value = v; michael@0: this.toString = new Function ( "return this.value +\"\""); michael@0: this.indexOf = String.prototype.indexOf; michael@0: } michael@0: