1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 gTestfile = '15.5.4.6-1.js';
42 File Name: 15.5.4.6-1.js
43 ECMA Section: 15.5.4.6 String.prototype.indexOf( searchString, pos)
44 Description: If the given searchString appears as a substring of the
45 result of converting this object to a string, at one or
46 more positions that are at or to the right of the
47 specified position, then the index of the leftmost such
48 position is returned; otherwise -1 is returned. If
49 positionis undefined or not supplied, 0 is assumed, so
50 as to search all of the string.
52 When the indexOf method is called with two arguments,
53 searchString and pos, the following steps are taken:
55 1. Call ToString, giving it the this value as its
57 2. Call ToString(searchString).
58 3. Call ToInteger(position). (If position is undefined
59 or not supplied, this step produces the value 0).
60 4. Compute the number of characters in Result(1).
61 5. Compute min(max(Result(3), 0), Result(4)).
62 6. Compute the number of characters in the string that
64 7. Compute the smallest possible integer k not smaller
65 than Result(5) such that k+Result(6) is not greater
66 than Result(4), and for all nonnegative integers j
67 less than Result(6), the character at position k+j
68 of Result(1) is the same as the character at position
69 j of Result(2); but if there is no such integer k,
70 then compute the value -1.
73 Note that the indexOf function is intentionally generic;
74 it does not require that its this value be a String object.
75 Therefore it can be transferred to other kinds of objects
78 Author: christine@netscape.com
81 var SECTION = "15.5.4.6-1";
82 var VERSION = "ECMA_1";
84 var TITLE = "String.protoype.indexOf";
86 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
88 writeHeaderToLog( SECTION + " "+ TITLE);
92 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
93 new TestCase( SECTION,
94 "String.indexOf(" +String.fromCharCode(i)+ ", 0)",
96 TEST_STRING.indexOf( String.fromCharCode(i), 0 ) );
99 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
100 new TestCase( SECTION,
101 "String.indexOf("+String.fromCharCode(i)+ ", "+ k +")",
103 TEST_STRING.indexOf( String.fromCharCode(i), k ) );
106 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
107 new TestCase( SECTION,
108 "String.indexOf("+String.fromCharCode(i)+ ", "+k+1+")",
110 TEST_STRING.indexOf( String.fromCharCode(i), k+1 ) );
113 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
114 new TestCase( SECTION,
115 "String.indexOf("+(String.fromCharCode(i) +
116 String.fromCharCode(i+1)+
117 String.fromCharCode(i+2)) +", "+0+")",
119 TEST_STRING.indexOf( (String.fromCharCode(i)+
120 String.fromCharCode(i+1)+
121 String.fromCharCode(i+2)),
125 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
126 new TestCase( SECTION,
127 "String.indexOf("+(String.fromCharCode(i) +
128 String.fromCharCode(i+1)+
129 String.fromCharCode(i+2)) +", "+ k +")",
131 TEST_STRING.indexOf( (String.fromCharCode(i)+
132 String.fromCharCode(i+1)+
133 String.fromCharCode(i+2)),
136 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
137 new TestCase( SECTION,
138 "String.indexOf("+(String.fromCharCode(i) +
139 String.fromCharCode(i+1)+
140 String.fromCharCode(i+2)) +", "+ k+1 +")",
142 TEST_STRING.indexOf( (String.fromCharCode(i)+
143 String.fromCharCode(i+1)+
144 String.fromCharCode(i+2)),
148 new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.indexOf( TEST_STRING, 0 ) );
150 new TestCase( SECTION, "String.indexOf(" +TEST_STRING + ", 1 )", -1, TEST_STRING.indexOf( TEST_STRING, 1 ));
152 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );