~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/lang/js/test/ecma/String/15.5.4.7-2.js

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla Communicator client code, released
 
16
 * March 31, 1998.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Netscape Communications Corporation.
 
20
 * Portions created by the Initial Developer are Copyright (C) 1998
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the MPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the MPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
/**
 
40
   File Name:          15.5.4.7-2.js
 
41
   ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
 
42
   Description:
 
43
 
 
44
   If the given searchString appears as a substring of the result of
 
45
   converting this object to a string, at one or more positions that are
 
46
   at or to the left of the specified position, then the index of the
 
47
   rightmost such position is returned; otherwise -1 is returned. If position
 
48
   is undefined or not supplied, the length of this string value is assumed,
 
49
   so as to search all of the string.
 
50
 
 
51
   When the lastIndexOf method is called with two arguments searchString and
 
52
   position, the following steps are taken:
 
53
 
 
54
   1.Call ToString, giving it the this value as its argument.
 
55
   2.Call ToString(searchString).
 
56
   3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
 
57
   4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
 
58
   5.Compute the number of characters in Result(1).
 
59
   6.Compute min(max(Result(4), 0), Result(5)).
 
60
   7.Compute the number of characters in the string that is Result(2).
 
61
   8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
 
62
   than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
 
63
   Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
 
64
   compute the value -1.
 
65
 
 
66
   1.Return Result(8).
 
67
 
 
68
   Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
 
69
   String object. Therefore it can be transferred to other kinds of objects for use as a method.
 
70
 
 
71
   Author:             christine@netscape.com, pschwartau@netscape.com
 
72
   Date:               02 October 1997
 
73
   Modified:           14 July 2002
 
74
   Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
 
75
   ECMA-262 Ed.3  Section 15.5.4.8
 
76
   The length property of the lastIndexOf method is 1
 
77
   *
 
78
   */
 
79
var SECTION = "15.5.4.7-2";
 
80
var VERSION = "ECMA_1";
 
81
startTest();
 
82
var TITLE   = "String.protoype.lastIndexOf";
 
83
 
 
84
writeHeaderToLog( SECTION + " "+ TITLE);
 
85
 
 
86
 
 
87
new TestCase( SECTION, "String.prototype.lastIndexOf.length",           1,          String.prototype.lastIndexOf.length );
 
88
new TestCase( SECTION, "delete String.prototype.lastIndexOf.length",    false,      delete String.prototype.lastIndexOf.length );
 
89
new TestCase( SECTION, "delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length",   1,  eval("delete String.prototype.lastIndexOf.length; String.prototype.lastIndexOf.length" ) );
 
90
 
 
91
new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('', 0)",          LastIndexOf("","",0),  eval("var s = new String(''); s.lastIndexOf('', 0)") );
 
92
new TestCase( SECTION, "var s = new String(''); s.lastIndexOf('')",             LastIndexOf("",""),  eval("var s = new String(''); s.lastIndexOf('')") );
 
93
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('', 0)",     LastIndexOf("hello","",0),  eval("var s = new String('hello'); s.lastIndexOf('',0)") );
 
94
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('')",        LastIndexOf("hello",""),  eval("var s = new String('hello'); s.lastIndexOf('')") );
 
95
 
 
96
new TestCase( SECTION, "var s = new String('hello'); s.lastIndexOf('ll')",     LastIndexOf("hello","ll"),  eval("var s = new String('hello'); s.lastIndexOf('ll')") );
 
97
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)") );
 
98
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)") );
 
99
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)") );
 
100
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)") );
 
101
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)") );
 
102
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)") );
 
103
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)") );
 
104
 
 
105
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)") );
 
106
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)") );
 
107
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)") );
 
108
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)") );
 
109
 
 
110
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)") );
 
111
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)") );
 
112
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)") );
 
113
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)") );
 
114
for ( var i = 0; i < ( "[object Object]" ).length; i++ ) {
 
115
  new TestCase(   SECTION,
 
116
                  "var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")",
 
117
                  ( i < 2 ? -1 : ( i < 9  ? 2 : 9 )) ,
 
118
                  eval("var o = new Object(); o.lastIndexOf = String.prototype.lastIndexOf; o.lastIndexOf('b', "+ i + ")") );
 
119
}
 
120
for ( var i = 0; i < 5; i ++ ) {
 
121
  new TestCase(   SECTION,
 
122
                  "var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")",
 
123
                  ( i < 2 ? -1 : 2 ),
 
124
                  eval("var b = new Boolean(); b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('l', "+ i + ")") );
 
125
}
 
126
for ( var i = 0; i < 5; i ++ ) {
 
127
  new TestCase(   SECTION,
 
128
                  "var b = new Boolean(); b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")",
 
129
                  ( i < 1 ? -1 : ( i < 9 ? 1 : ( i < 10 ? 9 : 10 ) ) ),
 
130
                  eval("var b = new Boolean();  b.toString = Object.prototype.toString; b.lastIndexOf = String.prototype.lastIndexOf; b.lastIndexOf('o', "+ i + ")") );
 
131
}
 
132
for ( var i = 0; i < 9; i++ ) {
 
133
  new TestCase(   SECTION,
 
134
                  "var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )",
 
135
                  ( i < 3 ? -1 : ( i < 5 ? 3 : 5 ) ),
 
136
                  eval("var n = new Number(Infinity); n.lastIndexOf = String.prototype.lastIndexOf; n.lastIndexOf( 'i', " + i + " )") );
 
137
}
 
138
var a = new Array( "abc","def","ghi","jkl","mno","pqr","stu","vwx","yz" );
 
139
 
 
140
for ( var i = 0; i < (a.toString()).length; i++ ) {
 
141
  new TestCase( SECTION,
 
142
                "var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )",
 
143
                ( i < 15 ? -1 : 15 ),
 
144
                eval("var a = new Array( 'abc','def','ghi','jkl','mno','pqr','stu','vwx','yz' ); a.lastIndexOf = String.prototype.lastIndexOf; a.lastIndexOf( ',mno,p', "+i+" )") );
 
145
}
 
146
 
 
147
for ( var i = 0; i < 15; i ++ ) {
 
148
  new TestCase(   SECTION,
 
149
                  "var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")",
 
150
                  ( i < 6 ? -1 : ( i < 10 ? 6 : 10 ) ),
 
151
                  eval("var m = Math; m.lastIndexOf = String.prototype.lastIndexOf; m.lastIndexOf('t', "+ i + ")") );
 
152
}
 
153
/*
 
154
  for ( var i = 0; i < 15; i++ ) {
 
155
  new TestCase(   SECTION,
 
156
  "var d = new Date(); d.lastIndexOf = String.prototype.lastIndexOf; d.lastIndexOf( '0' )",
 
157
  )
 
158
  }
 
159
 
 
160
*/
 
161
 
 
162
test();
 
163
 
 
164
function LastIndexOf( string, search, position ) {
 
165
  string = String( string );
 
166
  search = String( search );
 
167
 
 
168
  position = Number( position )
 
169
 
 
170
    if ( isNaN( position ) ) {
 
171
      position = Infinity;
 
172
    } else {
 
173
      position = ToInteger( position );
 
174
    }
 
175
 
 
176
  result5= string.length;
 
177
  result6 = Math.min(Math.max(position, 0), result5);
 
178
  result7 = search.length;
 
179
 
 
180
  if (result7 == 0) {
 
181
    return Math.min(position, result5);
 
182
  }
 
183
 
 
184
  result8 = -1;
 
185
 
 
186
  for ( k = 0; k <= result6; k++ ) {
 
187
    if ( k+ result7 > result5 ) {
 
188
      break;
 
189
    }
 
190
    for ( j = 0; j < result7; j++ ) {
 
191
      if ( string.charAt(k+j) != search.charAt(j) ){
 
192
        break;
 
193
      }   else  {
 
194
        if ( j == result7 -1 ) {
 
195
          result8 = k;
 
196
        }
 
197
      }
 
198
    }
 
199
  }
 
200
 
 
201
  return result8;
 
202
}
 
203
function ToInteger( n ) {
 
204
  n = Number( n );
 
205
  if ( isNaN(n) ) {
 
206
    return 0;
 
207
  }
 
208
  if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
 
209
    return n;
 
210
  }
 
211
 
 
212
  var sign = ( n < 0 ) ? -1 : 1;
 
213
 
 
214
  return ( sign * Math.floor(Math.abs(n)) );
 
215
}