Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / String / 15.5.4.7-2.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
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.
22  *
23  * Contributor(s):
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          15.5.4.7-2.js
42    ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
43    Description:
44
45    If the given searchString appears as a substring of the result of
46    converting this object to a string, at one or more positions that are
47    at or to the left of the specified position, then the index of the
48    rightmost such position is returned; otherwise -1 is returned. If position
49    is undefined or not supplied, the length of this string value is assumed,
50    so as to search all of the string.
51
52    When the lastIndexOf method is called with two arguments searchString and
53    position, the following steps are taken:
54
55    1.Call ToString, giving it the this value as its argument.
56    2.Call ToString(searchString).
57    3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
58    4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
59    5.Compute the number of characters in Result(1).
60    6.Compute min(max(Result(4), 0), Result(5)).
61    7.Compute the number of characters in the string that is Result(2).
62    8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
63    than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
64    Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
65    compute the value -1.
66
67    1.Return Result(8).
68
69    Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
70    String object. Therefore it can be transferred to other kinds of objects for use as a method.
71
72    Author:             christine@netscape.com, pschwartau@netscape.com
73    Date:               02 October 1997
74    Modified:           14 July 2002
75    Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
76    ECMA-262 Ed.3  Section 15.5.4.8
77    The length property of the lastIndexOf method is 1
78    *
79    */
80 var SECTION = "15.5.4.7-2";
81 var VERSION = "ECMA_1";
82 startTest();
83 var TITLE   = "String.protoype.lastIndexOf";
84
85 writeHeaderToLog( SECTION + " "+ TITLE);
86
87
88 new TestCase( SECTION, "String.prototype.lastIndexOf.length",           1,          String.prototype.lastIndexOf.length );
89 new TestCase( SECTION, "delete String.prototype.lastIndexOf.length",    false,      delete String.prototype.lastIndexOf.length );
90 new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length",   1,  eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) );
91
92 new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)",          LastIndexOf("","",0),  eval("var s = new String(''); s.lastIndexOf('', 0)") );
93 new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')",             LastIndexOf("",""),  eval("var s = new String(''); s.lastIndexOf('')") );
94 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)",     LastIndexOf("hello","",0),  eval("var s = new String('hello'); s.lastIndexOf('',0)") );
95 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')",        LastIndexOf("hello",""),  eval("var s = new String('hello'); s.lastIndexOf('')") );
96
97 new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')",     LastIndexOf("hello","ll"),  eval("var s = new String('hello'); s.lastIndexOf('ll')") );
98 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)") );
99 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)") );
100 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)") );
101 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)") );
102 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)") );
103 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)") );
104 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)") );
105
106 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)") );
107 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)") );
108 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)") );
109 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)") );
110
111 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)") );
112 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)") );
113 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)") );
114 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)") );
115 for ( var i = 0; i < ( "[object Object]" ).length; i++ ) {
116   new TestCase(   SECTION,
117                   "var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")",
118                   ( i < 2 ? -1 : ( i < 9  ? 2 : 9 )) ,
119                   eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") );
120 }
121 for ( var i = 0; i < 5; i ++ ) {
122   new TestCase(   SECTION,
123                   "var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")",
124                   ( i < 2 ? -1 : 2 ),
125                   eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") );
126 }
127 for ( var i = 0; i < 5; i ++ ) {
128   new TestCase(   SECTION,
129                   "var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")",
130                   ( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ),
131                   eval("var b = new Boolean();  b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") );
132 }
133 for ( var i = 0; i < 9; i++ ) {
134   new TestCase(   SECTION,
135                   "var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )",
136                   ( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ),
137                   eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") );
138 }
139 var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" );
140
141 for ( var i = 0; i < (a.toString()).length; i++ ) {
142   new TestCase( SECTION,
143                 "var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )",
144                 ( i < 15 ? -1 : 15 ),
145                 eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") );
146 }
147
148 for ( var i = 0; i < 15; i ++ ) {
149   new TestCase(   SECTION,
150                   "var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")",
151                   ( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ),
152                   eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") );
153 }
154 /*
155   for ( var i = 0; i < 15; i++ ) {
156   new TestCase(   SECTION,
157   "var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )",
158   )
159   }
160
161 */
162
163 test();
164
165 function LastIndexOf( string, search, position ) {
166   string = String( string );
167   search = String( search );
168
169   position = Number( position )
170
171     if ( isNaN( position ) ) {
172       position = Infinity;
173     } else {
174       position = ToInteger( position );
175     }
176
177   result5= string.length;
178   result6 = Math.min(Math.max(position, 0), result5);
179   result7 = search.length;
180
181   if (result7 == 0) {
182     return Math.min(position, result5);
183   }
184
185   result8 = -1;
186
187   for ( k = 0; k <= result6; k++ ) {
188     if ( k+ result7 > result5 ) {
189       break;
190     }
191     for ( j = 0; j < result7; j++ ) {
192       if ( string.charAt(k+j) != search.charAt(j) ){
193         break;
194       }   else  {
195         if ( j == result7 -1 ) {
196           result8 = k;
197         }
198       }
199     }
200   }
201
202   return result8;
203 }
204 function ToInteger( n ) {
205   n = Number( n );
206   if ( isNaN(n) ) {
207     return 0;
208   }
209   if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
210     return n;
211   }
212
213   var sign = ( n < 0 ) ? -1 : 1;
214
215   return ( sign * Math.floor(Math.abs(n)) );
216 }