]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.7-3.js
   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/ 
   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. 
  11  * The Original Code is Mozilla Communicator client code, released March 
  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 
  23     File Name:          15.5.4.7-3.js 
  24     ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos) 
  27     If the given searchString appears as a substring of the result of 
  28     converting this object to a string, at one or more positions that are 
  29     at or to the left of the specified position, then the index of the 
  30     rightmost such position is returned; otherwise -1 is returned. If position 
  31     is undefined or not supplied, the length of this string value is assumed, 
  32     so as to search all of the string. 
  34     When the lastIndexOf method is called with two arguments searchString and 
  35     position, the following steps are taken: 
  37    1.Call ToString, giving it the this value as its argument. 
  38    2.Call ToString(searchString). 
  39    3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN). 
  40    4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)). 
  41    5.Compute the number of characters in Result(1). 
  42    6.Compute min(max(Result(4), 0), Result(5)). 
  43    7.Compute the number of characters in the string that is Result(2). 
  44    8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater 
  45       than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of 
  46       Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then 
  51     Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a 
  52     String object. Therefore it can be transferred to other kinds of objects for use as a method. 
  54     Author:             christine@netscape.com 
  57     var SECTION 
= "15.5.4.7-3"; 
  58     var VERSION 
= "ECMA_2"; 
  60     var TITLE   
= "String.protoype.lastIndexOf"; 
  62     writeHeaderToLog( SECTION 
+ " "+ TITLE
); 
  64     var testcases 
= getTestCases(); 
  68 function getTestCases() { 
  69     var array 
= new Array(); 
  72     array
[item
++] = new TestCase(   SECTION
, 
  73                                     "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )", 
  75                                     eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )") ); 
  77     array
[item
++] = new TestCase(   SECTION
, 
  78                                     "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )", 
  80                                     eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )") ); 
  82     array
[item
++] = new TestCase(   SECTION
, 
  83                                     "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )", 
  85                                     eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )") ); 
  87     array
[item
++] = new TestCase(   SECTION
, 
  88                                     "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )", 
  90                                     eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )") ); 
  92     array
[item
++] = new TestCase(   SECTION
, 
  93                                     "var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )", 
  95                                     eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )") ); 
 101     for ( tc 
= 0; tc 
< testcases
.length
; tc
++ ) { 
 103         testcases
[tc
].passed 
= writeTestCaseResult( 
 104                     testcases
[tc
].expect
, 
 105                     testcases
[tc
].actual
, 
 106                     testcases
[tc
].description 
+" = "+ testcases
[tc
].actual 
); 
 108         testcases
[tc
].reason 
+= ( testcases
[tc
].passed 
) ? "" : "wrong value " 
 112     return ( testcases 
); 
 115 function LastIndexOf( string
, search
, position 
) { 
 116     string 
= String( string 
); 
 117     search 
= String( search 
); 
 119     position 
= Number( position 
) 
 121     if ( isNaN( position 
) ) { 
 124         position 
= ToInteger( position 
); 
 127     result5
= string
.length
; 
 128     result6 
= Math
.min(Math
.max(position
, 0), result5
); 
 129     result7 
= search
.length
; 
 132         return Math
.min(position
, result5
); 
 137     for ( k 
= 0; k 
<= result6
; k
++ ) { 
 138         if ( k
+ result7 
> result5 
) { 
 141         for ( j 
= 0; j 
< result7
; j
++ ) { 
 142             if ( string
.charAt(k
+j
) != search
.charAt(j
) ){ 
 145                 if ( j 
== result7 
-1 ) { 
 154 function ToInteger( n 
) { 
 159     if ( Math
.abs(n
) == 0 || Math
.abs(n
) == Infinity 
) { 
 163     var sign 
= ( n 
< 0 ) ? -1 : 1; 
 165     return ( sign 
* Math
.floor(Math
.abs(n
)) );