]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
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/ | |
5 | * | |
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. | |
10 | * | |
11 | * The Original Code is Mozilla Communicator client code, released March | |
12 | * 31, 1998. | |
13 | * | |
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 | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | * | |
21 | */ | |
22 | /** | |
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. | |
32 | ||
33 | When the indexOf method is called with two arguments, | |
34 | searchString and pos, the following steps are taken: | |
35 | ||
36 | 1. Call ToString, giving it the this value as its | |
37 | argument. | |
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 | |
44 | is Result(2). | |
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. | |
52 | 8. Return Result(7). | |
53 | ||
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 | |
57 | for use as a method. | |
58 | ||
59 | Author: christine@netscape.com | |
60 | Date: 2 october 1997 | |
61 | */ | |
62 | var SECTION = "15.5.4.6-1"; | |
63 | var VERSION = "ECMA_1"; | |
64 | startTest(); | |
65 | var TITLE = "String.protoype.indexOf"; | |
66 | ||
67 | var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); | |
68 | ||
69 | writeHeaderToLog( SECTION + " "+ TITLE); | |
70 | ||
71 | var testcases = getTestCases(); | |
72 | test(); | |
73 | ||
74 | ||
75 | function getTestCases() { | |
76 | var array = new Array(); | |
77 | var j = 0; | |
78 | ||
79 | for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { | |
80 | array[j] = new TestCase( SECTION, | |
81 | "String.indexOf(" +String.fromCharCode(i)+ ", 0)", | |
82 | k, | |
83 | TEST_STRING.indexOf( String.fromCharCode(i), 0 ) ); | |
84 | } | |
85 | ||
86 | for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { | |
87 | array[j] = new TestCase( SECTION, | |
88 | "String.indexOf("+String.fromCharCode(i)+ ", "+ k +")", | |
89 | k, | |
90 | TEST_STRING.indexOf( String.fromCharCode(i), k ) ); | |
91 | } | |
92 | ||
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+")", | |
96 | -1, | |
97 | TEST_STRING.indexOf( String.fromCharCode(i), k+1 ) ); | |
98 | } | |
99 | ||
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+")", | |
105 | k, | |
106 | TEST_STRING.indexOf( (String.fromCharCode(i)+ | |
107 | String.fromCharCode(i+1)+ | |
108 | String.fromCharCode(i+2)), | |
109 | 0 ) ); | |
110 | } | |
111 | ||
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 +")", | |
117 | k, | |
118 | TEST_STRING.indexOf( (String.fromCharCode(i)+ | |
119 | String.fromCharCode(i+1)+ | |
120 | String.fromCharCode(i+2)), | |
121 | k ) ); | |
122 | } | |
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 +")", | |
128 | -1, | |
129 | TEST_STRING.indexOf( (String.fromCharCode(i)+ | |
130 | String.fromCharCode(i+1)+ | |
131 | String.fromCharCode(i+2)), | |
132 | k+1 ) ); | |
133 | } | |
134 | ||
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 )); | |
137 | ||
138 | return array; | |
139 | } | |
140 | ||
141 | function test() { | |
142 | writeLineToLog( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); | |
143 | ||
144 | for ( tc = 0; tc < testcases.length; tc++ ) { | |
145 | ||
146 | testcases[tc].passed = writeTestCaseResult( | |
147 | testcases[tc].expect, | |
148 | testcases[tc].actual, | |
149 | testcases[tc].description +" = "+ testcases[tc].actual ); | |
150 | ||
151 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value " | |
152 | ||
153 | } | |
154 | stopTest(); | |
155 | ||
156 | return ( testcases ); | |
157 | } | |
158 |