Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.7-2.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-2.js';
7
8 /**
9 File Name: 15.5.4.7-2.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], [email protected]
41 Date: 02 October 1997
42 Modified: 14 July 2002
43 Reason: See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
44 ECMA-262 Ed.3 Section 15.5.4.8
45 The length property of the lastIndexOf method is 1
46 *
47 */
48 var SECTION = "15.5.4.7-2";
49 var VERSION = "ECMA_1";
50 startTest();
51 var TITLE = "String.protoype.lastIndexOf";
52
53 writeHeaderToLog( SECTION + " "+ TITLE);
54
55
56 new TestCase( SECTION, "String.prototype.lastIndexOf.length", 1, String.prototype.lastIndexOf.length );
57 new TestCase( SECTION, "delete String.prototype.lastIndexOf.length", false, delete String.prototype.lastIndexOf.length );
58 new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length", 1, eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) );
59
60 new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)", LastIndexOf("","",0), eval("var s = new String(''); s.lastIndexOf('', 0)") );
61 new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')", LastIndexOf("",""), eval("var s = new String(''); s.lastIndexOf('')") );
62 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)", LastIndexOf("hello","",0), eval("var s = new String('hello'); s.lastIndexOf('',0)") );
63 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')", LastIndexOf("hello",""), eval("var s = new String('hello'); s.lastIndexOf('')") );
64
65 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')", LastIndexOf("hello","ll"), eval("var s = new String('hello'); s.lastIndexOf('ll')") );
66 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 0)", LastIndexOf("hello","ll",0), eval("var s = new String('hello'); s.lastIndexOf('ll', 0)") );
67 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 1)", LastIndexOf("hello","ll",1), eval("var s = new String('hello'); s.lastIndexOf('ll', 1)") );
68 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 2)", LastIndexOf("hello","ll",2), eval("var s = new String('hello'); s.lastIndexOf('ll', 2)") );
69 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 3)", LastIndexOf("hello","ll",3), eval("var s = new String('hello'); s.lastIndexOf('ll', 3)") );
70 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 4)", LastIndexOf("hello","ll",4), eval("var s = new String('hello'); s.lastIndexOf('ll', 4)") );
71 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 5)", LastIndexOf("hello","ll",5), eval("var s = new String('hello'); s.lastIndexOf('ll', 5)") );
72 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 6)", LastIndexOf("hello","ll",6), eval("var s = new String('hello'); s.lastIndexOf('ll', 6)") );
73
74 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 1.5)", LastIndexOf('hello','ll', 1.5), eval("var s = new String('hello'); s.lastIndexOf('ll', 1.5)") );
75 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', 2.5)", LastIndexOf('hello','ll', 2.5), eval("var s = new String('hello'); s.lastIndexOf('ll', 2.5)") );
76 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -1)", LastIndexOf('hello','ll', -1), eval("var s = new String('hello'); s.lastIndexOf('ll', -1)") );
77 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -1.5)",LastIndexOf('hello','ll', -1.5), eval("var s = new String('hello'); s.lastIndexOf('ll', -1.5)") );
78
79 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -Infinity)", LastIndexOf("hello","ll",-Infinity), eval("var s = new String('hello'); s.lastIndexOf('ll', -Infinity)") );
80 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', Infinity)", LastIndexOf("hello","ll",Infinity), eval("var s = new String('hello'); s.lastIndexOf('ll', Infinity)") );
81 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', NaN)", LastIndexOf("hello","ll",NaN), eval("var s = new String('hello'); s.lastIndexOf('ll', NaN)") );
82 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll', -0)", LastIndexOf("hello","ll",-0), eval("var s = new String('hello'); s.lastIndexOf('ll', -0)") );
83 for ( var i = 0; i < ( "[object Object]" ).length; i++ ) {
84 new TestCase( SECTION,
85 "var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")",
86 ( i < 2 ? -1 : ( i < 9 ? 2 : 9 )) ,
87 eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") );
88 }
89 for ( var i = 0; i < 5; i ++ ) {
90 new TestCase( SECTION,
91 "var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")",
92 ( i < 2 ? -1 : 2 ),
93 eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") );
94 }
95 for ( var i = 0; i < 5; i ++ ) {
96 new TestCase( SECTION,
97 "var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")",
98 ( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ),
99 eval("var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") );
100 }
101 for ( var i = 0; i < 9; i++ ) {
102 new TestCase( SECTION,
103 "var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )",
104 ( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ),
105 eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") );
106 }
107 var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" );
108
109 for ( var i = 0; i < (a.toString()).length; i++ ) {
110 new TestCase( SECTION,
111 "var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )",
112 ( i < 15 ? -1 : 15 ),
113 eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") );
114 }
115
116 for ( var i = 0; i < 15; i ++ ) {
117 new TestCase( SECTION,
118 "var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")",
119 ( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ),
120 eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") );
121 }
122 /*
123 for ( var i = 0; i < 15; i++ ) {
124 new TestCase( SECTION,
125 "var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )",
126 )
127 }
128
129 */
130
131 test();
132
133 function LastIndexOf( string, search, position ) {
134 string = String( string );
135 search = String( search );
136
137 position = Number( position )
138
139 if ( isNaN( position ) ) {
140 position = Infinity;
141 } else {
142 position = ToInteger( position );
143 }
144
145 result5= string.length;
146 result6 = Math.min(Math.max(position, 0), result5);
147 result7 = search.length;
148
149 if (result7 == 0) {
150 return Math.min(position, result5);
151 }
152
153 result8 = -1;
154
155 for ( k = 0; k <= result6; k++ ) {
156 if ( k+ result7 > result5 ) {
157 break;
158 }
159 for ( j = 0; j < result7; j++ ) {
160 if ( string.charAt(k+j) != search.charAt(j) ){
161 break;
162 } else {
163 if ( j == result7 -1 ) {
164 result8 = k;
165 }
166 }
167 }
168 }
169
170 return result8;
171 }
172 function ToInteger( n ) {
173 n = Number( n );
174 if ( isNaN(n) ) {
175 return 0;
176 }
177 if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
178 return n;
179 }
180
181 var sign = ( n < 0 ) ? -1 : 1;
182
183 return ( sign * Math.floor(Math.abs(n)) );
184 }