Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.7-1.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = '15.5.4.7-1.js';
7
8 /**
9 File Name: 15.5.4.7-1.js
10 ECMA Section: 15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
11 Description:
12
13 If the given searchString appears as a substring of the result of
14 converting this object to a string, at one or more positions that are
15 at or to the left of the specified position, then the index of the
16 rightmost such position is returned; otherwise -1 is returned. If position
17 is undefined or not supplied, the length of this string value is assumed,
18 so as to search all of the string.
19
20 When the lastIndexOf method is called with two arguments searchString and
21 position, the following steps are taken:
22
23 1.Call ToString, giving it the this value as its argument.
24 2.Call ToString(searchString).
25 3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
26 4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
27 5.Compute the number of characters in Result(1).
28 6.Compute min(max(Result(4), 0), Result(5)).
29 7.Compute the number of characters in the string that is Result(2).
30 8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
31 than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
32 Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
33 compute the value -1.
34
35 1.Return Result(8).
36
37 Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
38 String object. Therefore it can be transferred to other kinds of objects for use as a method.
39
40 Author: [email protected]
41 Date: 2 october 1997
42 */
43 var SECTION = "15.5.4.7-1";
44 var VERSION = "ECMA_1";
45 startTest();
46 var TITLE = "String.protoype.lastIndexOf";
47
48 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
49
50 writeHeaderToLog( SECTION + " "+ TITLE);
51
52 var j = 0;
53
54 for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) {
55 new TestCase( SECTION,
56 "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)",
57 -1,
58 TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) );
59 }
60
61 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
62 new TestCase( SECTION,
63 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")",
64 k,
65 TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) );
66 }
67
68 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
69 new TestCase( SECTION,
70 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")",
71 k,
72 TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) );
73 }
74
75 for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) {
76 new TestCase( SECTION,
77
78 "String.lastIndexOf("+(String.fromCharCode(i) +
79 String.fromCharCode(i+1)+
80 String.fromCharCode(i+2)) +", "+ 0 + ")",
81 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
82 String.fromCharCode(i+1)+String.fromCharCode(i+2), 0),
83 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
84 String.fromCharCode(i+1)+
85 String.fromCharCode(i+2)),
86 0 ) );
87 }
88
89 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
90 new TestCase( SECTION,
91 "String.lastIndexOf("+(String.fromCharCode(i) +
92 String.fromCharCode(i+1)+
93 String.fromCharCode(i+2)) +", "+ k +")",
94 k,
95 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
96 String.fromCharCode(i+1)+
97 String.fromCharCode(i+2)),
98 k ) );
99 }
100 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
101 new TestCase( SECTION,
102 "String.lastIndexOf("+(String.fromCharCode(i) +
103 String.fromCharCode(i+1)+
104 String.fromCharCode(i+2)) +", "+ k+1 +")",
105 k,
106 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
107 String.fromCharCode(i+1)+
108 String.fromCharCode(i+2)),
109 k+1 ) );
110 }
111 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
112 new TestCase( SECTION,
113 "String.lastIndexOf("+
114 (String.fromCharCode(i) +
115 String.fromCharCode(i+1)+
116 String.fromCharCode(i+2)) +", "+ (k-1) +")",
117 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
118 String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1),
119 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
120 String.fromCharCode(i+1)+
121 String.fromCharCode(i+2)),
122 k-1 ) );
123 }
124
125 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) );
126
127 // new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 ));
128
129 new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING ));
130
131 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );
132
133 test();
134
135 function LastIndexOf( string, search, position ) {
136 string = String( string );
137 search = String( search );
138
139 position = Number( position )
140
141 if ( isNaN( position ) ) {
142 position = Infinity;
143 } else {
144 position = ToInteger( position );
145 }
146
147 result5= string.length;
148 result6 = Math.min(Math.max(position, 0), result5);
149 result7 = search.length;
150
151 if (result7 == 0) {
152 return Math.min(position, result5);
153 }
154
155 result8 = -1;
156
157 for ( k = 0; k <= result6; k++ ) {
158 if ( k+ result7 > result5 ) {
159 break;
160 }
161 for ( j = 0; j < result7; j++ ) {
162 if ( string.charAt(k+j) != search.charAt(j) ){
163 break;
164 } else {
165 if ( j == result7 -1 ) {
166 result8 = k;
167 }
168 }
169 }
170 }
171
172 return result8;
173 }
174 function ToInteger( n ) {
175 n = Number( n );
176 if ( isNaN(n) ) {
177 return 0;
178 }
179 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
180 return n;
181 }
182
183 var sign = ( n < 0 ) ? -1 : 1;
184
185 return ( sign * Math.floor(Math.abs(n)) );
186 }