js/src/tests/ecma/String/15.5.4.7-2.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/String/15.5.4.7-2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     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-2.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, pschwartau@netscape.com
    1.43 +   Date:               02 October 1997
    1.44 +   Modified:           14 July 2002
    1.45 +   Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
    1.46 +   ECMA-262 Ed.3  Section 15.5.4.8
    1.47 +   The length property of the lastIndexOf method is 1
    1.48 +   *
    1.49 +   */
    1.50 +var SECTION = "15.5.4.7-2";
    1.51 +var VERSION = "ECMA_1";
    1.52 +startTest();
    1.53 +var TITLE   = "String.protoype.lastIndexOf";
    1.54 +
    1.55 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.56 +
    1.57 +
    1.58 +new TestCase( SECTION, "String.prototype.lastIndexOf.length",           1,          String.prototype.lastIndexOf.length );
    1.59 +new TestCase( SECTION, "delete String.prototype.lastIndexOf.length",    false,      delete String.prototype.lastIndexOf.length );
    1.60 +new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length",   1,  eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) );
    1.61 +
    1.62 +new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)",          LastIndexOf("","",0),  eval("var s = new String(''); s.lastIndexOf('', 0)") );
    1.63 +new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')",             LastIndexOf("",""),  eval("var s = new String(''); s.lastIndexOf('')") );
    1.64 +new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)",     LastIndexOf("hello","",0),  eval("var s = new String('hello'); s.lastIndexOf('',0)") );
    1.65 +new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')",        LastIndexOf("hello",""),  eval("var s = new String('hello'); s.lastIndexOf('')") );
    1.66 +
    1.67 +new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')",     LastIndexOf("hello","ll"),  eval("var s = new String('hello'); s.lastIndexOf('ll')") );
    1.68 +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)") );
    1.69 +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)") );
    1.70 +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)") );
    1.71 +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)") );
    1.72 +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)") );
    1.73 +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)") );
    1.74 +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)") );
    1.75 +
    1.76 +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)") );
    1.77 +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)") );
    1.78 +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)") );
    1.79 +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)") );
    1.80 +
    1.81 +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)") );
    1.82 +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)") );
    1.83 +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)") );
    1.84 +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)") );
    1.85 +for ( var i = 0; i < ( "[object Object]" ).length; i++ ) {
    1.86 +  new TestCase(   SECTION,
    1.87 +		  "var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")",
    1.88 +		  ( i < 2 ? -1 : ( i < 9  ? 2 : 9 )) ,
    1.89 +		  eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") );
    1.90 +}
    1.91 +for ( var i = 0; i < 5; i ++ ) {
    1.92 +  new TestCase(   SECTION,
    1.93 +		  "var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")",
    1.94 +		  ( i < 2 ? -1 : 2 ),
    1.95 +		  eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") );
    1.96 +}
    1.97 +for ( var i = 0; i < 5; i ++ ) {
    1.98 +  new TestCase(   SECTION,
    1.99 +		  "var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")",
   1.100 +		  ( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ),
   1.101 +		  eval("var b = new Boolean();  b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") );
   1.102 +}
   1.103 +for ( var i = 0; i < 9; i++ ) {
   1.104 +  new TestCase(   SECTION,
   1.105 +		  "var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )",
   1.106 +		  ( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ),
   1.107 +		  eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") );
   1.108 +}
   1.109 +var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" );
   1.110 +
   1.111 +for ( var i = 0; i < (a.toString()).length; i++ ) {
   1.112 +  new TestCase( SECTION,
   1.113 +		"var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )",
   1.114 +		( i < 15 ? -1 : 15 ),
   1.115 +		eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") );
   1.116 +}
   1.117 +
   1.118 +for ( var i = 0; i < 15; i ++ ) {
   1.119 +  new TestCase(   SECTION,
   1.120 +		  "var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")",
   1.121 +		  ( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ),
   1.122 +		  eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") );
   1.123 +}
   1.124 +/*
   1.125 +  for ( var i = 0; i < 15; i++ ) {
   1.126 +  new TestCase(   SECTION,
   1.127 +  "var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )",
   1.128 +  )
   1.129 +  }
   1.130 +
   1.131 +*/
   1.132 +
   1.133 +test();
   1.134 +
   1.135 +function LastIndexOf( string, search, position ) {
   1.136 +  string = String( string );
   1.137 +  search = String( search );
   1.138 +
   1.139 +  position = Number( position )
   1.140 +
   1.141 +    if ( isNaN( position ) ) {
   1.142 +      position = Infinity;
   1.143 +    } else {
   1.144 +      position = ToInteger( position );
   1.145 +    }
   1.146 +
   1.147 +  result5= string.length;
   1.148 +  result6 = Math.min(Math.max(position, 0), result5);
   1.149 +  result7 = search.length;
   1.150 +
   1.151 +  if (result7 == 0) {
   1.152 +    return Math.min(position, result5);
   1.153 +  }
   1.154 +
   1.155 +  result8 = -1;
   1.156 +
   1.157 +  for ( k = 0; k <= result6; k++ ) {
   1.158 +    if ( k+ result7 > result5 ) {
   1.159 +      break;
   1.160 +    }
   1.161 +    for ( j = 0; j < result7; j++ ) {
   1.162 +      if ( string.charAt(k+j) != search.charAt(j) ){
   1.163 +	break;
   1.164 +      }   else  {
   1.165 +	if ( j == result7 -1 ) {
   1.166 +	  result8 = k;
   1.167 +	}
   1.168 +      }
   1.169 +    }
   1.170 +  }
   1.171 +
   1.172 +  return result8;
   1.173 +}
   1.174 +function ToInteger( n ) {
   1.175 +  n = Number( n );
   1.176 +  if ( isNaN(n) ) {
   1.177 +    return 0;
   1.178 +  }
   1.179 +  if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
   1.180 +    return n;
   1.181 +  }
   1.182 +
   1.183 +  var sign = ( n < 0 ) ? -1 : 1;
   1.184 +
   1.185 +  return ( sign * Math.floor(Math.abs(n)) );
   1.186 +}

mercurial