summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma/String/15.5.4.6-2.js
blob: ee78b2b39f739efc7a820f75168dcee2b5108ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


/**
   File Name:          15.5.4.6-1.js
   ECMA Section:       15.5.4.6 String.prototype.indexOf( searchString, pos)
   Description:        If the given searchString appears as a substring of the
   result of converting this object to a string, at one or
   more positions that are at or to the right of the
   specified position, then the index of the leftmost such
   position is returned; otherwise -1 is returned.  If
   positionis undefined or not supplied, 0 is assumed, so
   as to search all of the string.

   When the indexOf method is called with two arguments,
   searchString and pos, the following steps are taken:

   1. Call ToString, giving it the this value as its
   argument.
   2. Call ToString(searchString).
   3. Call ToInteger(position). (If position is undefined
   or not supplied, this step produces the value 0).
   4. Compute the number of characters in Result(1).
   5. Compute min(max(Result(3), 0), Result(4)).
   6. Compute the number of characters in the string that
   is Result(2).
   7. Compute the smallest possible integer k not smaller
   than Result(5) such that k+Result(6) is not greater
   than Result(4), and for all nonnegative integers j
   less than Result(6), the character at position k+j
   of Result(1) is the same as the character at position
   j of Result(2); but if there is no such integer k,
   then compute the value -1.
   8. Return Result(7).

   Note that the indexOf function is intentionally generic;
   it does not require that its this value be a String object.
   Therefore it can be transferred to other kinds of objects
   for use as a method.

   Author:             christine@netscape.com, pschwartau@netscape.com
   Date:               02 October 1997
   Modified:           14 July 2002
   Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
   ECMA-262 Ed.3  Section 15.5.4.7
   The length property of the indexOf method is 1
   *
   */
var SECTION = "15.5.4.6-2";
var VERSION = "ECMA_1";
var TITLE   = "String.protoype.indexOf";
var BUGNUMBER="105721";

startTest();

writeHeaderToLog( SECTION + " "+ TITLE);


// the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721

// regress http://scopus/bugsplat/show_bug.cgi?id=105721

new TestCase( SECTION,
              "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()",   
              GLOBAL, 
              g().toString()
  );


new TestCase( SECTION, "String.prototype.indexOf.length",                                               1,     String.prototype.indexOf.length );
new TestCase( SECTION, "String.prototype.indexOf.length = null; String.prototype.indexOf.length",       1,     eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") );

new TestCase( SECTION,
              "var s = new String(); s.indexOf()",    
              -1,    
              eval("var s = new String(); s.indexOf()") );

// some Unicode tests.

// generate a test string.

var TEST_STRING = "";

for ( var u = 0x00A1; u <= 0x00FF; u++ ) {
  TEST_STRING += String.fromCharCode( u );
}

for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
  new TestCase(   SECTION,
		  "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )",
		  i,
		  TEST_STRING.indexOf( String.fromCharCode(u) ) );
}
for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
  new TestCase(   SECTION,
		  "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )",
		  i,
		  TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) );
}



var foo = new MyObject('hello');

new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo.indexOf("h")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo.indexOf("e")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo.indexOf("o")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('X')", -1,  foo.indexOf("X")  );
new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf(5) ", -1,  foo.indexOf(5)  );

var boo = new MyObject(true);

new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo.indexOf("t")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo.indexOf("r")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo.indexOf("u")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo.indexOf("e")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo.indexOf("true")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo.indexOf("rue")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo.indexOf("ue")  );
new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo.indexOf("oy")  );


var noo = new MyObject( Math.PI );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo.indexOf('3')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo.indexOf('.')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo.indexOf('4')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo.indexOf('5')  );
new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo.indexOf('9')  );

new TestCase( SECTION,
	      "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
	      0,
	      eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );

new TestCase( SECTION,
	      "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
	      3,
	      eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );

new TestCase( SECTION,
	      "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
	      0,
	      eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );

new TestCase( SECTION,
	      "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
	      2,
	      eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );

new TestCase( SECTION,
	      "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
	      0,
	      eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );

new TestCase( SECTION,
	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
	      -1,
	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );

new TestCase( SECTION,
	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
	      -1,
	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );

new TestCase( SECTION,
	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
	      0,
	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );

new TestCase( SECTION,
	      "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
	      1,
	      eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );

new TestCase( SECTION,
	      "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
	      0,
	      eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );

new TestCase( SECTION,
	      "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
	      1,
	      eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );

new TestCase( SECTION,
	      "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
	      8,
	      eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );

// new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively
new TestCase( SECTION,
	      "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')",
	      8,
	      eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );

test();

function f() {
  return this;
}
function g() {
  var h = f;
  return h();
}

function MyObject (v) {
  this.value      = v;
  this.toString   = new Function ( "return this.value +\"\"");
  this.indexOf     = String.prototype.indexOf;
}