js/src/tests/ecma/extensions/15.5.4.7-3.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/extensions/15.5.4.7-3.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          15.5.4.7-3.js
    1.12 +   ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
    1.13 +   Description:
    1.14 +
    1.15 +   If the given searchString appears as a substring of the result of
    1.16 +   converting this object to a string, at one or more positions that are
    1.17 +   at or to the left of the specified position, then the index of the
    1.18 +   rightmost such position is returned; otherwise -1 is returned. If position
    1.19 +   is undefined or not supplied, the length of this string value is assumed,
    1.20 +   so as to search all of the string.
    1.21 +
    1.22 +   When the lastIndexOf method is called with two arguments searchString and
    1.23 +   position, the following steps are taken:
    1.24 +
    1.25 +   1.Call ToString, giving it the this value as its argument.
    1.26 +   2.Call ToString(searchString).
    1.27 +   3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
    1.28 +   4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
    1.29 +   5.Compute the number of characters in Result(1).
    1.30 +   6.Compute min(max(Result(4), 0), Result(5)).
    1.31 +   7.Compute the number of characters in the string that is Result(2).
    1.32 +   8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
    1.33 +   than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
    1.34 +   Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
    1.35 +   compute the value -1.
    1.36 +
    1.37 +   1.Return Result(8).
    1.38 +
    1.39 +   Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
    1.40 +   String object. Therefore it can be transferred to other kinds of objects for use as a method.
    1.41 +
    1.42 +   Author:             christine@netscape.com
    1.43 +   Date:               2 october 1997
    1.44 +*/
    1.45 +var SECTION = "15.5.4.7-3";
    1.46 +var VERSION = "ECMA_2";
    1.47 +startTest();
    1.48 +var TITLE   = "String.protoype.lastIndexOf";
    1.49 +
    1.50 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.51 +
    1.52 +new TestCase(   SECTION,
    1.53 +		"var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )",
    1.54 +		-1,
    1.55 +		eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 0 )") );
    1.56 +
    1.57 +new TestCase(   SECTION,
    1.58 +		"var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )",
    1.59 +		1,
    1.60 +		eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 1 )") );
    1.61 +
    1.62 +new TestCase(   SECTION,
    1.63 +		"var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )",
    1.64 +		1,
    1.65 +		eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 2 )") );
    1.66 +
    1.67 +new TestCase(   SECTION,
    1.68 +		"var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )",
    1.69 +		1,
    1.70 +		eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r', 10 )") );
    1.71 +
    1.72 +new TestCase(   SECTION,
    1.73 +		"var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )",
    1.74 +		1,
    1.75 +		eval("var b = true; b.__proto__.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('r' )") );
    1.76 +
    1.77 +test();
    1.78 +
    1.79 +function LastIndexOf( string, search, position ) {
    1.80 +  string = String( string );
    1.81 +  search = String( search );
    1.82 +
    1.83 +  position = Number( position )
    1.84 +
    1.85 +    if ( isNaN( position ) ) {
    1.86 +      position = Infinity;
    1.87 +    } else {
    1.88 +      position = ToInteger( position );
    1.89 +    }
    1.90 +
    1.91 +  result5= string.length;
    1.92 +  result6 = Math.min(Math.max(position, 0), result5);
    1.93 +  result7 = search.length;
    1.94 +
    1.95 +  if (result7 == 0) {
    1.96 +    return Math.min(position, result5);
    1.97 +  }
    1.98 +
    1.99 +  result8 = -1;
   1.100 +
   1.101 +  for ( k = 0; k <= result6; k++ ) {
   1.102 +    if ( k+ result7 > result5 ) {
   1.103 +      break;
   1.104 +    }
   1.105 +    for ( j = 0; j < result7; j++ ) {
   1.106 +      if ( string.charAt(k+j) != search.charAt(j) ){
   1.107 +	break;
   1.108 +      }   else  {
   1.109 +	if ( j == result7 -1 ) {
   1.110 +	  result8 = k;
   1.111 +	}
   1.112 +      }
   1.113 +    }
   1.114 +  }
   1.115 +
   1.116 +  return result8;
   1.117 +}
   1.118 +function ToInteger( n ) {
   1.119 +  n = Number( n );
   1.120 +  if ( isNaN(n) ) {
   1.121 +    return 0;
   1.122 +  }
   1.123 +  if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
   1.124 +    return n;
   1.125 +  }
   1.126 +
   1.127 +  var sign = ( n < 0 ) ? -1 : 1;
   1.128 +
   1.129 +  return ( sign * Math.floor(Math.abs(n)) );
   1.130 +}

mercurial