Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / RegExp / 15.10.6.2-2.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 JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2002
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com
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  *
41  * Date:    18 Feb 2002
42  * SUMMARY: Testing re.exec(str) when re.lastIndex is < 0 or > str.length
43  *
44  * Case 1: If re has the global flag set, then re(str) should be null
45  * Case 2: If re doesn't have this set, then re(str) should be unaffected
46  *
47  * See http://bugzilla.mozilla.org/show_bug.cgi?id=76717
48  *
49  *
50  * From the ECMA-262 Final spec:
51  *
52  * 15.10.6.2 RegExp.prototype.exec(string)
53  * Performs a regular expression match of string against the regular
54  * expression and returns an Array object containing the results of
55  * the match, or null if the string did not match.
56  *
57  * The string ToString(string) is searched for an occurrence of the
58  * regular expression pattern as follows:
59  *
60  * 1.  Let S be the value of ToString(string).
61  * 2.  Let length be the length of S.
62  * 3.  Let lastIndex be the value of the lastIndex property.
63  * 4.  Let i be the value of ToInteger(lastIndex).
64  * 5.  If the global property is false, let i = 0.
65  * 6.  If i < 0 or i > length then set lastIndex to 0 and return null.
66  * 7.  Call [[Match]], giving it the arguments S and i.
67  *     If [[Match]] returned failure, go to step 8;
68  *     otherwise let r be its State result and go to step 10.
69  * 8.  Let i = i+1.
70  * 9.  Go to step 6.
71  * 10. Let e be r's endIndex value.
72  * 11. If the global property is true, set lastIndex to e.
73  *
74  *          etc.
75  *
76  *
77  * So:
78  *
79  * A. If the global flag is not set, |lastIndex| is set to 0
80  *    before the match is attempted; thus the match is unaffected.
81  *
82  * B. If the global flag IS set and re.lastIndex is >= 0 and <= str.length,
83  *    |lastIndex| is incremented every time there is a match; not from
84  *    i to i+1, but from i to "endIndex" e:
85  *
86  *      e = (index of last input character matched so far by the pattern) + 1
87  *
88  *    The match is then attempted from this position in the string (Step 7).
89  *
90  * C. When the global flag IS set and re.lastIndex is < 0 or > str.length,
91  *    |lastIndex| is set to 0 and the match returns null.
92  *
93  *
94  * Note the |lastIndex| property is writeable, and may be set arbitrarily
95  * by the programmer - and we will do that below.
96  *
97  */
98 //-----------------------------------------------------------------------------
99 var i = 0;
100 var BUGNUMBER = 76717;
101 var summary = 'Testing re.exec(str) when re.lastIndex is < 0 or > str.length';
102 var status = '';
103 var statusmessages = new Array();
104 var pattern = '';
105 var patterns = new Array();
106 var string = '';
107 var strings = new Array();
108 var actualmatch = '';
109 var actualmatches = new Array();
110 var expectedmatch = '';
111 var expectedmatches = new Array();
112
113
114 /******************************************************************************
115  *
116  * Case 1 : when the global flag is set -
117  *
118  *****************************************************************************/
119 pattern = /abc/gi;
120 string = 'AbcaBcabC';
121
122 status = inSection(1);
123 actualmatch = pattern.exec(string);
124 expectedmatch = Array('Abc');
125 addThis();
126
127 status = inSection(2);
128 actualmatch = pattern.exec(string);
129 expectedmatch = Array('aBc');
130 addThis();
131
132 status = inSection(3);
133 actualmatch = pattern.exec(string);
134 expectedmatch = Array('abC');
135 addThis();
136
137 /*
138  * At this point |lastIndex| is > string.length, so the match should be null -
139  */
140 status = inSection(4);
141 actualmatch = pattern.exec(string);
142 expectedmatch = null;
143 addThis();
144
145 /*
146  * Now let's set |lastIndex| to -1, so the match should again be null -
147  */
148 status = inSection(5);
149 pattern.lastIndex = -1;
150 actualmatch = pattern.exec(string);
151 expectedmatch = null;
152 addThis();
153
154 /*
155  * Now try some edge-case values. Thanks to the work done in
156  * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
157  * is now stored as a double instead of a uint32 (unsigned integer).
158  *
159  * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
160  * all the way up to Number.MAX_VALUE. So that's why we need cases
161  * between those two numbers.
162  */
163 status = inSection(6);
164 pattern.lastIndex = Math.pow(2,32);
165 actualmatch = pattern.exec(string);
166 expectedmatch = null;
167 addThis();
168  
169 status = inSection(7);
170 pattern.lastIndex = -Math.pow(2,32);
171 actualmatch = pattern.exec(string);
172 expectedmatch = null;
173 addThis();
174
175 status = inSection(8);
176 pattern.lastIndex = Math.pow(2,32) + 1;
177 actualmatch = pattern.exec(string);
178 expectedmatch = null;
179 addThis();
180
181 status = inSection(9);
182 pattern.lastIndex = -(Math.pow(2,32) + 1);
183 actualmatch = pattern.exec(string);
184 expectedmatch = null;
185 addThis();
186
187 status = inSection(10);
188 pattern.lastIndex = Math.pow(2,32) * 2;
189 actualmatch = pattern.exec(string);
190 expectedmatch = null;
191 addThis();
192
193 status = inSection(11);
194 pattern.lastIndex = -Math.pow(2,32) * 2;
195 actualmatch = pattern.exec(string);
196 expectedmatch = null;
197 addThis();
198
199 status = inSection(12);
200 pattern.lastIndex = Math.pow(2,40);
201 actualmatch = pattern.exec(string);
202 expectedmatch = null;
203 addThis();
204
205 status = inSection(13);
206 pattern.lastIndex = -Math.pow(2,40);
207 actualmatch = pattern.exec(string);
208 expectedmatch = null;
209 addThis();
210
211 status = inSection(14);
212 pattern.lastIndex = Number.MAX_VALUE;
213 actualmatch = pattern.exec(string);
214 expectedmatch = null;
215 addThis();
216
217 status = inSection(15);
218 pattern.lastIndex = -Number.MAX_VALUE;
219 actualmatch = pattern.exec(string);
220 expectedmatch = null;
221 addThis();
222  
223
224
225 /******************************************************************************
226  *
227  * Case 2: repeat all the above cases WITHOUT the global flag set.
228  * According to EMCA. |lastIndex| should get set to 0 before the match.
229  *
230  * Therefore re.exec(str) should be unaffected; thus our expected values
231  * below are now DIFFERENT when |lastIndex| is < 0 or > str.length
232  *
233  *****************************************************************************/
234
235 pattern = /abc/i;
236 string = 'AbcaBcabC';
237
238 status = inSection(16);
239 actualmatch = pattern.exec(string);
240 expectedmatch = Array('Abc');
241 addThis();
242
243 status = inSection(17);
244 actualmatch = pattern.exec(string);
245 expectedmatch = Array('Abc'); // NOT Array('aBc') as before -
246 addThis();
247
248 status = inSection(18);
249 actualmatch = pattern.exec(string);
250 expectedmatch = Array('Abc'); // NOT Array('abC') as before -
251 addThis();
252
253 /*
254  * At this point above, |lastIndex| WAS > string.length, but not here -
255  */
256 status = inSection(19);
257 actualmatch = pattern.exec(string);
258 expectedmatch = Array('Abc') // NOT null as before -
259   addThis();
260
261 /*
262  * Now let's set |lastIndex| to -1
263  */
264 status = inSection(20);
265 pattern.lastIndex = -1;
266 actualmatch = pattern.exec(string);
267 expectedmatch = Array('Abc') // NOT null as before -
268   addThis();
269
270 /*
271  * Now try some edge-case values. Thanks to the work done in
272  * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
273  * is now stored as a double instead of a uint32 (unsigned integer).
274  *
275  * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
276  * all the way up to Number.MAX_VALUE. So that's why we need cases
277  * between those two numbers.
278  */
279 status = inSection(21);
280 pattern.lastIndex = Math.pow(2,32);
281 actualmatch = pattern.exec(string);
282 expectedmatch = Array('Abc') // NOT null as before -
283   addThis();
284  
285 status = inSection(22);
286 pattern.lastIndex = -Math.pow(2,32);
287 actualmatch = pattern.exec(string);
288 expectedmatch = Array('Abc') // NOT null as before -
289   addThis();
290
291 status = inSection(23);
292 pattern.lastIndex = Math.pow(2,32) + 1;
293 actualmatch = pattern.exec(string);
294 expectedmatch = Array('Abc') // NOT null as before -
295   addThis();
296
297 status = inSection(24);
298 pattern.lastIndex = -(Math.pow(2,32) + 1);
299 actualmatch = pattern.exec(string);
300 expectedmatch = Array('Abc') // NOT null as before -
301   addThis();
302
303 status = inSection(25);
304 pattern.lastIndex = Math.pow(2,32) * 2;
305 actualmatch = pattern.exec(string);
306 expectedmatch = Array('Abc') // NOT null as before -
307   addThis();
308
309 status = inSection(26);
310 pattern.lastIndex = -Math.pow(2,32) * 2;
311 actualmatch = pattern.exec(string);
312 expectedmatch = Array('Abc') // NOT null as before -
313   addThis();
314
315 status = inSection(27);
316 pattern.lastIndex = Math.pow(2,40);
317 actualmatch = pattern.exec(string);
318 expectedmatch = Array('Abc') // NOT null as before -;
319   addThis();
320
321 status = inSection(28);
322 pattern.lastIndex = -Math.pow(2,40);
323 actualmatch = pattern.exec(string);
324 expectedmatch = Array('Abc') // NOT null as before -
325   addThis();
326
327 status = inSection(29);
328 pattern.lastIndex = Number.MAX_VALUE;
329 actualmatch = pattern.exec(string);
330 expectedmatch = Array('Abc') // NOT null as before -
331   addThis();
332
333 status = inSection(30);
334 pattern.lastIndex = -Number.MAX_VALUE;
335 actualmatch = pattern.exec(string);
336 expectedmatch = Array('Abc') // NOT null as before -
337   addThis();
338
339
340
341
342 //-------------------------------------------------------------------------------------------------
343 test();
344 //-------------------------------------------------------------------------------------------------
345
346
347
348 function addThis()
349 {
350   statusmessages[i] = status;
351   patterns[i] = pattern;
352   strings[i] = string;
353   actualmatches[i] = actualmatch;
354   expectedmatches[i] = expectedmatch;
355   i++;
356 }
357
358
359 function test()
360 {
361   enterFunc ('test');
362   printBugNumber(BUGNUMBER);
363   printStatus (summary);
364   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
365   exitFunc ('test');
366 }