]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.6-1.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
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.
33 When the indexOf method is called with two arguments,
34 searchString and pos, the following steps are taken:
36 1. Call ToString, giving it the this value as its
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
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.
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
59 Author: christine@netscape.com
62 var SECTION
= "15.5.4.6-1";
63 var VERSION
= "ECMA_1";
65 var TITLE
= "String.protoype.indexOf";
67 var TEST_STRING
= new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
69 writeHeaderToLog( SECTION
+ " "+ TITLE
);
71 var testcases
= getTestCases();
75 function getTestCases() {
76 var array
= new Array();
79 for ( k
= 0, i
= 0x0020; i
< 0x007e; i
++, j
++, k
++ ) {
80 array
[j
] = new TestCase( SECTION
,
81 "String.indexOf(" +String
.fromCharCode(i
)+ ", 0)",
83 TEST_STRING
.indexOf( String
.fromCharCode(i
), 0 ) );
86 for ( k
= 0, i
= 0x0020; i
< 0x007e; i
++, j
++, k
++ ) {
87 array
[j
] = new TestCase( SECTION
,
88 "String.indexOf("+String
.fromCharCode(i
)+ ", "+ k
+")",
90 TEST_STRING
.indexOf( String
.fromCharCode(i
), k
) );
93 for ( k
= 0, i
= 0x0020; i
< 0x007e; i
++, j
++, k
++ ) {
94 array
[j
] = new TestCase( SECTION
,
95 "String.indexOf("+String
.fromCharCode(i
)+ ", "+k
+1+")",
97 TEST_STRING
.indexOf( String
.fromCharCode(i
), k
+1 ) );
100 for ( k
= 0, i
= 0x0020; i
< 0x007d; i
++, j
++, k
++ ) {
101 array
[j
] = new TestCase( SECTION
,
102 "String.indexOf("+(String
.fromCharCode(i
) +
103 String
.fromCharCode(i
+1)+
104 String
.fromCharCode(i
+2)) +", "+0+")",
106 TEST_STRING
.indexOf( (String
.fromCharCode(i
)+
107 String
.fromCharCode(i
+1)+
108 String
.fromCharCode(i
+2)),
112 for ( k
= 0, i
= 0x0020; i
< 0x007d; i
++, j
++, k
++ ) {
113 array
[j
] = new TestCase( SECTION
,
114 "String.indexOf("+(String
.fromCharCode(i
) +
115 String
.fromCharCode(i
+1)+
116 String
.fromCharCode(i
+2)) +", "+ k
+")",
118 TEST_STRING
.indexOf( (String
.fromCharCode(i
)+
119 String
.fromCharCode(i
+1)+
120 String
.fromCharCode(i
+2)),
123 for ( k
= 0, i
= 0x0020; i
< 0x007d; i
++, j
++, k
++ ) {
124 array
[j
] = new TestCase( SECTION
,
125 "String.indexOf("+(String
.fromCharCode(i
) +
126 String
.fromCharCode(i
+1)+
127 String
.fromCharCode(i
+2)) +", "+ k
+1 +")",
129 TEST_STRING
.indexOf( (String
.fromCharCode(i
)+
130 String
.fromCharCode(i
+1)+
131 String
.fromCharCode(i
+2)),
135 array
[j
++] = new TestCase( SECTION
, "String.indexOf(" +TEST_STRING
+ ", 0 )", 0, TEST_STRING
.indexOf( TEST_STRING
, 0 ) );
136 array
[j
++] = new TestCase( SECTION
, "String.indexOf(" +TEST_STRING
+ ", 1 )", -1, TEST_STRING
.indexOf( TEST_STRING
, 1 ));
142 writeLineToLog( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );
144 for ( tc
= 0; tc
< testcases
.length
; tc
++ ) {
146 testcases
[tc
].passed
= writeTestCaseResult(
147 testcases
[tc
].expect
,
148 testcases
[tc
].actual
,
149 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
);
151 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value "
156 return ( testcases
);