| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
gTestfile = '15.5.4.7-1.js'; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
var SECTION = "15.5.4.7-1"; |
| 44 |
var VERSION = "ECMA_1"; |
| 45 |
startTest(); |
| 46 |
var TITLE = "String.protoype.lastIndexOf"; |
| 47 |
|
| 48 |
var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ); |
| 49 |
|
| 50 |
writeHeaderToLog( SECTION + " "+ TITLE); |
| 51 |
|
| 52 |
var j = 0; |
| 53 |
|
| 54 |
for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) { |
| 55 |
new TestCase( SECTION, |
| 56 |
"String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)", |
| 57 |
-1, |
| 58 |
TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) ); |
| 59 |
} |
| 60 |
|
| 61 |
for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
| 62 |
new TestCase( SECTION, |
| 63 |
"String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")", |
| 64 |
k, |
| 65 |
TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) ); |
| 66 |
} |
| 67 |
|
| 68 |
for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) { |
| 69 |
new TestCase( SECTION, |
| 70 |
"String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")", |
| 71 |
k, |
| 72 |
TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) ); |
| 73 |
} |
| 74 |
|
| 75 |
for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) { |
| 76 |
new TestCase( SECTION, |
| 77 |
|
| 78 |
"String.lastIndexOf("+(String.fromCharCode(i) + |
| 79 |
String.fromCharCode(i+1)+ |
| 80 |
String.fromCharCode(i+2)) +", "+ 0 + ")", |
| 81 |
LastIndexOf( TEST_STRING, String.fromCharCode(i) + |
| 82 |
String.fromCharCode(i+1)+String.fromCharCode(i+2), 0), |
| 83 |
TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
| 84 |
String.fromCharCode(i+1)+ |
| 85 |
String.fromCharCode(i+2)), |
| 86 |
0 ) ); |
| 87 |
} |
| 88 |
|
| 89 |
for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
| 90 |
new TestCase( SECTION, |
| 91 |
"String.lastIndexOf("+(String.fromCharCode(i) + |
| 92 |
String.fromCharCode(i+1)+ |
| 93 |
String.fromCharCode(i+2)) +", "+ k +")", |
| 94 |
k, |
| 95 |
TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
| 96 |
String.fromCharCode(i+1)+ |
| 97 |
String.fromCharCode(i+2)), |
| 98 |
k ) ); |
| 99 |
} |
| 100 |
for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
| 101 |
new TestCase( SECTION, |
| 102 |
"String.lastIndexOf("+(String.fromCharCode(i) + |
| 103 |
String.fromCharCode(i+1)+ |
| 104 |
String.fromCharCode(i+2)) +", "+ k+1 +")", |
| 105 |
k, |
| 106 |
TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
| 107 |
String.fromCharCode(i+1)+ |
| 108 |
String.fromCharCode(i+2)), |
| 109 |
k+1 ) ); |
| 110 |
} |
| 111 |
for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) { |
| 112 |
new TestCase( SECTION, |
| 113 |
"String.lastIndexOf("+ |
| 114 |
(String.fromCharCode(i) + |
| 115 |
String.fromCharCode(i+1)+ |
| 116 |
String.fromCharCode(i+2)) +", "+ (k-1) +")", |
| 117 |
LastIndexOf( TEST_STRING, String.fromCharCode(i) + |
| 118 |
String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1), |
| 119 |
TEST_STRING.lastIndexOf( (String.fromCharCode(i)+ |
| 120 |
String.fromCharCode(i+1)+ |
| 121 |
String.fromCharCode(i+2)), |
| 122 |
k-1 ) ); |
| 123 |
} |
| 124 |
|
| 125 |
new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) ); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
new TestCase( SECTION, "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING )); |
| 130 |
|
| 131 |
print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" ); |
| 132 |
|
| 133 |
test(); |
| 134 |
|
| 135 |
function LastIndexOf( string, search, position ) { |
| 136 |
string = String( string ); |
| 137 |
search = String( search ); |
| 138 |
|
| 139 |
position = Number( position ) |
| 140 |
|
| 141 |
if ( isNaN( position ) ) { |
| 142 |
position = Infinity; |
| 143 |
} else { |
| 144 |
position = ToInteger( position ); |
| 145 |
} |
| 146 |
|
| 147 |
result5= string.length; |
| 148 |
result6 = Math.min(Math.max(position, 0), result5); |
| 149 |
result7 = search.length; |
| 150 |
|
| 151 |
if (result7 == 0) { |
| 152 |
return Math.min(position, result5); |
| 153 |
} |
| 154 |
|
| 155 |
result8 = -1; |
| 156 |
|
| 157 |
for ( k = 0; k <= result6; k++ ) { |
| 158 |
if ( k+ result7 > result5 ) { |
| 159 |
break; |
| 160 |
} |
| 161 |
for ( j = 0; j < result7; j++ ) { |
| 162 |
if ( string.charAt(k+j) != search.charAt(j) ){ |
| 163 |
break; |
| 164 |
} else { |
| 165 |
if ( j == result7 -1 ) { |
| 166 |
result8 = k; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return result8; |
| 173 |
} |
| 174 |
function ToInteger( n ) { |
| 175 |
n = Number( n ); |
| 176 |
if ( isNaN(n) ) { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) { |
| 180 |
return n; |
| 181 |
} |
| 182 |
|
| 183 |
var sign = ( n < 0 ) ? -1 : 1; |
| 184 |
|
| 185 |
return ( sign * Math.floor(Math.abs(n)) ); |
| 186 |
} |