1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/String/15.5.4.7-1.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,185 @@ 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-1.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-1"; 1.46 +var VERSION = "ECMA_1"; 1.47 +startTest(); 1.48 +var TITLE = "String.protoype.lastIndexOf"; 1.49 + 1.50 +var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); 1.51 + 1.52 +writeHeaderToLog( SECTION + " "+ TITLE); 1.53 + 1.54 +var j = 0; 1.55 + 1.56 +for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) { 1.57 + new TestCase( SECTION, 1.58 + "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)", 1.59 + -1, 1.60 + TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) ); 1.61 +} 1.62 + 1.63 +for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { 1.64 + new TestCase( SECTION, 1.65 + "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")", 1.66 + k, 1.67 + TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) ); 1.68 +} 1.69 + 1.70 +for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { 1.71 + new TestCase( SECTION, 1.72 + "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")", 1.73 + k, 1.74 + TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) ); 1.75 +} 1.76 + 1.77 +for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) { 1.78 + new TestCase( SECTION, 1.79 + 1.80 + "String.lastIndexOf("+(String.fromCharCode(i) + 1.81 + String.fromCharCode(i+1)+ 1.82 + String.fromCharCode(i+2)) +", "+ 0 + ")", 1.83 + LastIndexOf( TEST_STRING, String.fromCharCode(i) + 1.84 + String.fromCharCode(i+1)+String.fromCharCode(i+2), 0), 1.85 + TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ 1.86 + String.fromCharCode(i+1)+ 1.87 + String.fromCharCode(i+2)), 1.88 + 0 ) ); 1.89 +} 1.90 + 1.91 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { 1.92 + new TestCase( SECTION, 1.93 + "String.lastIndexOf("+(String.fromCharCode(i) + 1.94 + String.fromCharCode(i+1)+ 1.95 + String.fromCharCode(i+2)) +", "+ k +")", 1.96 + k, 1.97 + TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ 1.98 + String.fromCharCode(i+1)+ 1.99 + String.fromCharCode(i+2)), 1.100 + k ) ); 1.101 +} 1.102 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { 1.103 + new TestCase( SECTION, 1.104 + "String.lastIndexOf("+(String.fromCharCode(i) + 1.105 + String.fromCharCode(i+1)+ 1.106 + String.fromCharCode(i+2)) +", "+ k+1 +")", 1.107 + k, 1.108 + TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ 1.109 + String.fromCharCode(i+1)+ 1.110 + String.fromCharCode(i+2)), 1.111 + k+1 ) ); 1.112 +} 1.113 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { 1.114 + new TestCase( SECTION, 1.115 + "String.lastIndexOf("+ 1.116 + (String.fromCharCode(i) + 1.117 + String.fromCharCode(i+1)+ 1.118 + String.fromCharCode(i+2)) +", "+ (k-1) +")", 1.119 + LastIndexOf( TEST_STRING, String.fromCharCode(i) + 1.120 + String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1), 1.121 + TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ 1.122 + String.fromCharCode(i+1)+ 1.123 + String.fromCharCode(i+2)), 1.124 + k-1 ) ); 1.125 +} 1.126 + 1.127 +new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) ); 1.128 + 1.129 +// new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 )); 1.130 + 1.131 +new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING )); 1.132 + 1.133 +print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); 1.134 + 1.135 +test(); 1.136 + 1.137 +function LastIndexOf( string, search, position ) { 1.138 + string = String( string ); 1.139 + search = String( search ); 1.140 + 1.141 + position = Number( position ) 1.142 + 1.143 + if ( isNaN( position ) ) { 1.144 + position = Infinity; 1.145 + } else { 1.146 + position = ToInteger( position ); 1.147 + } 1.148 + 1.149 + result5= string.length; 1.150 + result6 = Math.min(Math.max(position, 0), result5); 1.151 + result7 = search.length; 1.152 + 1.153 + if (result7 == 0) { 1.154 + return Math.min(position, result5); 1.155 + } 1.156 + 1.157 + result8 = -1; 1.158 + 1.159 + for ( k = 0; k <= result6; k++ ) { 1.160 + if ( k+ result7 > result5 ) { 1.161 + break; 1.162 + } 1.163 + for ( j = 0; j < result7; j++ ) { 1.164 + if ( string.charAt(k+j) != search.charAt(j) ){ 1.165 + break; 1.166 + } else { 1.167 + if ( j == result7 -1 ) { 1.168 + result8 = k; 1.169 + } 1.170 + } 1.171 + } 1.172 + } 1.173 + 1.174 + return result8; 1.175 +} 1.176 +function ToInteger( n ) { 1.177 + n = Number( n ); 1.178 + if ( isNaN(n) ) { 1.179 + return 0; 1.180 + } 1.181 + if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { 1.182 + return n; 1.183 + } 1.184 + 1.185 + var sign = ( n < 0 ) ? -1 : 1; 1.186 + 1.187 + return ( sign * Math.floor(Math.abs(n)) ); 1.188 +}