js/src/tests/ecma/String/15.5.4.6-2.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial