summaryrefslogtreecommitdiffstats
path: root/Source/JavaScriptCore/tests/mozilla/ecma/String/15.5.4.6-2.js
blob: 6054a51ff3e72dabf2beccd6e99418fdda8205c6 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is Mozilla Communicator client code, released March
 * 31, 1998.
 *
 * The Initial Developer of the Original Code is Netscape Communications
 * Corporation. Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation. All
 * Rights Reserved.
 *
 * Contributor(s): 
 * 
 */
/**
    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";
    startTest();
    var TITLE   = "String.protoype.indexOf";
    var BUGNUMBER="105721";

    writeHeaderToLog( SECTION + " "+ TITLE);

    var testcases = getTestCases();
    test();


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

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;
}

function getTestCases() {
    var array = new Array();
    var item = 0;

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

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


    array[item++] = new TestCase( SECTION, "String.prototype.indexOf.length",                                               1,     String.prototype.indexOf.length );
    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") );
    array[item++] = new TestCase( SECTION, "delete String.prototype.indexOf.length",                                        false,  delete String.prototype.indexOf.length );
    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") );

    array[item++] = 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++ ) {
        array[item++] = 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++ ) {
        array[item++] = 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');

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

    var boo = new MyObject(true);

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


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

    array[item++] = 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')") );

    array[item++] = 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,')") );

    array[item++] = 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]')") );

    array[item++] = 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')") );

    array[item++] = new TestCase( SECTION,
                                  "var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')",
                                  0,
                                  eval("var f = new Object( String.prototype.indexOf ); f('"+GLOBAL+"')") );

    array[item++] = 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]')") );

    array[item++] = 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')") );

    array[item++] = 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)") );

    array[item++] = 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)") );

    array[item++] = 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')") );

    array[item++] = 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('-')") );

    array[item++] = 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')") );

    array[item++] = 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
    array[item++] = 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')") );


    return array;
}

function test() {
        for ( tc = 0; tc < testcases.length; tc++ ) {

            testcases[tc].passed = writeTestCaseResult(
                    testcases[tc].expect,
                    testcases[tc].actual,
                    testcases[tc].description +" = "+
                    testcases[tc].actual );

            testcases[tc].reason += ( testcases[tc].passed )
                                    ? ""
                                    : "wrong value "
        }
        stopTest();

    //  all tests must return a boolean value
        return ( testcases );
}