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
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/
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
15 * The Original Code is JavaScript Engine testing utilities.
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.
23 * pschwartau@netscape.com
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.
37 * ***** END LICENSE BLOCK ***** */
42 * SUMMARY: Testing re.exec(str) when re.lastIndex is < 0 or > str.length
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
47 * See http://bugzilla.mozilla.org/show_bug.cgi?id=76717
50 * From the ECMA-262 Final spec:
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.
57 * The string ToString(string) is searched for an occurrence of the
58 * regular expression pattern as follows:
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.
71 * 10. Let e be r's endIndex value.
72 * 11. If the global property is true, set lastIndex to e.
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.
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:
86 * e = (index of last input character matched so far by the pattern) + 1
88 * The match is then attempted from this position in the string (Step 7).
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.
94 * Note the |lastIndex| property is writeable, and may be set arbitrarily
95 * by the programmer - and we will do that below.
98 //-----------------------------------------------------------------------------
100 var BUGNUMBER = 76717;
101 var summary = 'Testing re.exec(str) when re.lastIndex is < 0 or > str.length';
103 var statusmessages = new Array();
105 var patterns = new Array();
107 var strings = new Array();
108 var actualmatch = '';
109 var actualmatches = new Array();
110 var expectedmatch = '';
111 var expectedmatches = new Array();
114 /******************************************************************************
116 * Case 1 : when the global flag is set -
118 *****************************************************************************/
120 string = 'AbcaBcabC';
122 status = inSection(1);
123 actualmatch = pattern.exec(string);
124 expectedmatch = Array('Abc');
127 status = inSection(2);
128 actualmatch = pattern.exec(string);
129 expectedmatch = Array('aBc');
132 status = inSection(3);
133 actualmatch = pattern.exec(string);
134 expectedmatch = Array('abC');
138 * At this point |lastIndex| is > string.length, so the match should be null -
140 status = inSection(4);
141 actualmatch = pattern.exec(string);
142 expectedmatch = null;
146 * Now let's set |lastIndex| to -1, so the match should again be null -
148 status = inSection(5);
149 pattern.lastIndex = -1;
150 actualmatch = pattern.exec(string);
151 expectedmatch = null;
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).
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.
163 status = inSection(6);
164 pattern.lastIndex = Math.pow(2,32);
165 actualmatch = pattern.exec(string);
166 expectedmatch = null;
169 status = inSection(7);
170 pattern.lastIndex = -Math.pow(2,32);
171 actualmatch = pattern.exec(string);
172 expectedmatch = null;
175 status = inSection(8);
176 pattern.lastIndex = Math.pow(2,32) + 1;
177 actualmatch = pattern.exec(string);
178 expectedmatch = null;
181 status = inSection(9);
182 pattern.lastIndex = -(Math.pow(2,32) + 1);
183 actualmatch = pattern.exec(string);
184 expectedmatch = null;
187 status = inSection(10);
188 pattern.lastIndex = Math.pow(2,32) * 2;
189 actualmatch = pattern.exec(string);
190 expectedmatch = null;
193 status = inSection(11);
194 pattern.lastIndex = -Math.pow(2,32) * 2;
195 actualmatch = pattern.exec(string);
196 expectedmatch = null;
199 status = inSection(12);
200 pattern.lastIndex = Math.pow(2,40);
201 actualmatch = pattern.exec(string);
202 expectedmatch = null;
205 status = inSection(13);
206 pattern.lastIndex = -Math.pow(2,40);
207 actualmatch = pattern.exec(string);
208 expectedmatch = null;
211 status = inSection(14);
212 pattern.lastIndex = Number.MAX_VALUE;
213 actualmatch = pattern.exec(string);
214 expectedmatch = null;
217 status = inSection(15);
218 pattern.lastIndex = -Number.MAX_VALUE;
219 actualmatch = pattern.exec(string);
220 expectedmatch = null;
225 /******************************************************************************
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.
230 * Therefore re.exec(str) should be unaffected; thus our expected values
231 * below are now DIFFERENT when |lastIndex| is < 0 or > str.length
233 *****************************************************************************/
236 string = 'AbcaBcabC';
238 status = inSection(16);
239 actualmatch = pattern.exec(string);
240 expectedmatch = Array('Abc');
243 status = inSection(17);
244 actualmatch = pattern.exec(string);
245 expectedmatch = Array('Abc'); // NOT Array('aBc') as before -
248 status = inSection(18);
249 actualmatch = pattern.exec(string);
250 expectedmatch = Array('Abc'); // NOT Array('abC') as before -
254 * At this point above, |lastIndex| WAS > string.length, but not here -
256 status = inSection(19);
257 actualmatch = pattern.exec(string);
258 expectedmatch = Array('Abc') // NOT null as before -
262 * Now let's set |lastIndex| to -1
264 status = inSection(20);
265 pattern.lastIndex = -1;
266 actualmatch = pattern.exec(string);
267 expectedmatch = Array('Abc') // NOT null as before -
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).
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.
279 status = inSection(21);
280 pattern.lastIndex = Math.pow(2,32);
281 actualmatch = pattern.exec(string);
282 expectedmatch = Array('Abc') // NOT null as before -
285 status = inSection(22);
286 pattern.lastIndex = -Math.pow(2,32);
287 actualmatch = pattern.exec(string);
288 expectedmatch = Array('Abc') // NOT null as before -
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 -
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 -
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 -
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 -
315 status = inSection(27);
316 pattern.lastIndex = Math.pow(2,40);
317 actualmatch = pattern.exec(string);
318 expectedmatch = Array('Abc') // NOT null as before -;
321 status = inSection(28);
322 pattern.lastIndex = -Math.pow(2,40);
323 actualmatch = pattern.exec(string);
324 expectedmatch = Array('Abc') // NOT null as before -
327 status = inSection(29);
328 pattern.lastIndex = Number.MAX_VALUE;
329 actualmatch = pattern.exec(string);
330 expectedmatch = Array('Abc') // NOT null as before -
333 status = inSection(30);
334 pattern.lastIndex = -Number.MAX_VALUE;
335 actualmatch = pattern.exec(string);
336 expectedmatch = Array('Abc') // NOT null as before -
342 //-------------------------------------------------------------------------------------------------
344 //-------------------------------------------------------------------------------------------------
350 statusmessages[i] = status;
351 patterns[i] = pattern;
353 actualmatches[i] = actualmatch;
354 expectedmatches[i] = expectedmatch;
362 printBugNumber(BUGNUMBER);
363 printStatus (summary);
364 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);