QML_RUNTIME_TESTING should be disabled by default.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / parserstress / tests / ecma / String / 15.5.4.7-1.js
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 gTestfile = '15.5.4.7-1.js';
40
41 /**
42    File Name:          15.5.4.7-1.js
43    ECMA Section:       15.5.4.7 String.prototype.lastIndexOf( searchString, pos)
44    Description:
45
46    If the given searchString appears as a substring of the result of
47    converting this object to a string, at one or more positions that are
48    at or to the left of the specified position, then the index of the
49    rightmost such position is returned; otherwise -1 is returned. If position
50    is undefined or not supplied, the length of this string value is assumed,
51    so as to search all of the string.
52
53    When the lastIndexOf method is called with two arguments searchString and
54    position, the following steps are taken:
55
56    1.Call ToString, giving it the this value as its argument.
57    2.Call ToString(searchString).
58    3.Call ToNumber(position). (If position is undefined or not supplied, this step produces the value NaN).
59    4.If Result(3) is NaN, use +; otherwise, call ToInteger(Result(3)).
60    5.Compute the number of characters in Result(1).
61    6.Compute min(max(Result(4), 0), Result(5)).
62    7.Compute the number of characters in the string that is Result(2).
63    8.Compute the largest possible integer k not larger than Result(6) such that k+Result(7) is not greater
64    than Result(5), and for all nonnegative integers j less than Result(7), the character at position k+j of
65    Result(1) is the same as the character at position j of Result(2); but if there is no such integer k, then
66    compute the value -1.
67
68    1.Return Result(8).
69
70    Note that the lastIndexOf function is intentionally generic; it does not require that its this value be a
71    String object. Therefore it can be transferred to other kinds of objects for use as a method.
72
73    Author:             christine@netscape.com
74    Date:               2 october 1997
75 */
76 var SECTION = "15.5.4.7-1";
77 var VERSION = "ECMA_1";
78 startTest();
79 var TITLE   = "String.protoype.lastIndexOf";
80
81 var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
82
83 writeHeaderToLog( SECTION + " "+ TITLE);
84
85 var j = 0;
86
87 for ( k = 0, i = 0x0021; i < 0x007e; i++, j++, k++ ) {
88   new TestCase( SECTION,
89                 "String.lastIndexOf(" +String.fromCharCode(i)+ ", 0)",
90                 -1,
91                 TEST_STRING.lastIndexOf( String.fromCharCode(i), 0 ) );
92 }
93
94 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
95   new TestCase( SECTION,
96                 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+ k +")",
97                 k,
98                 TEST_STRING.lastIndexOf( String.fromCharCode(i), k ) );
99 }
100
101 for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
102   new TestCase( SECTION,
103                 "String.lastIndexOf("+String.fromCharCode(i)+ ", "+k+1+")",
104                 k,
105                 TEST_STRING.lastIndexOf( String.fromCharCode(i), k+1 ) );
106 }
107
108 for ( k = 9, i = 0x0021; i < 0x007d; i++, j++, k++ ) {
109   new TestCase( SECTION,
110
111                 "String.lastIndexOf("+(String.fromCharCode(i) +
112                                        String.fromCharCode(i+1)+
113                                        String.fromCharCode(i+2)) +", "+ 0 + ")",
114                 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
115                              String.fromCharCode(i+1)+String.fromCharCode(i+2), 0),
116                 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
117                                           String.fromCharCode(i+1)+
118                                           String.fromCharCode(i+2)),
119                                          0 ) );
120 }
121
122 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
123   new TestCase( SECTION,
124                 "String.lastIndexOf("+(String.fromCharCode(i) +
125                                        String.fromCharCode(i+1)+
126                                        String.fromCharCode(i+2)) +", "+ k +")",
127                 k,
128                 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
129                                           String.fromCharCode(i+1)+
130                                           String.fromCharCode(i+2)),
131                                          k ) );
132 }
133 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
134   new TestCase( SECTION,
135                 "String.lastIndexOf("+(String.fromCharCode(i) +
136                                        String.fromCharCode(i+1)+
137                                        String.fromCharCode(i+2)) +", "+ k+1 +")",
138                 k,
139                 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
140                                           String.fromCharCode(i+1)+
141                                           String.fromCharCode(i+2)),
142                                          k+1 ) );
143 }
144 for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
145   new TestCase( SECTION,
146                 "String.lastIndexOf("+
147                 (String.fromCharCode(i) +
148                  String.fromCharCode(i+1)+
149                  String.fromCharCode(i+2)) +", "+ (k-1) +")",
150                 LastIndexOf( TEST_STRING, String.fromCharCode(i) +
151                              String.fromCharCode(i+1)+String.fromCharCode(i+2), k-1),
152                 TEST_STRING.lastIndexOf( (String.fromCharCode(i)+
153                                           String.fromCharCode(i+1)+
154                                           String.fromCharCode(i+2)),
155                                          k-1 ) );
156 }
157
158 new TestCase( SECTION,  "String.lastIndexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 0 ) );
159
160 // new TestCase( SECTION,  "String.lastIndexOf(" +TEST_STRING + ", 1 )", 0, TEST_STRING.lastIndexOf( TEST_STRING, 1 ));
161  
162 new TestCase( SECTION,  "String.lastIndexOf(" +TEST_STRING + ")", 0, TEST_STRING.lastIndexOf( TEST_STRING ));
163
164 print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );
165
166 test();
167
168 function LastIndexOf( string, search, position ) {
169   string = String( string );
170   search = String( search );
171
172   position = Number( position )
173
174     if ( isNaN( position ) ) {
175       position = Infinity;
176     } else {
177       position = ToInteger( position );
178     }
179
180   result5= string.length;
181   result6 = Math.min(Math.max(position, 0), result5);
182   result7 = search.length;
183
184   if (result7 == 0) {
185     return Math.min(position, result5);
186   }
187
188   result8 = -1;
189
190   for ( k = 0; k <= result6; k++ ) {
191     if ( k+ result7 > result5 ) {
192       break;
193     }
194     for ( j = 0; j < result7; j++ ) {
195       if ( string.charAt(k+j) != search.charAt(j) ){
196         break;
197       }   else  {
198         if ( j == result7 -1 ) {
199           result8 = k;
200         }
201       }
202     }
203   }
204
205   return result8;
206 }
207 function ToInteger( n ) {
208   n = Number( n );
209   if ( isNaN(n) ) {
210     return 0;
211   }
212   if ( Math.abs(n) == 0 || Math.abs(n) == Infinity ) {
213     return n;
214   }
215
216   var sign = ( n < 0 ) ? -1 : 1;
217
218   return ( sign * Math.floor(Math.abs(n)) );
219 }