1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is JavaScript Engine testing utilities.
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2002
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s): pschwartau@netscape.com
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the NPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the NPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK *****
38 * SUMMARY: Testing re.exec(str) when re.lastIndex is < 0 or > str.length
40 * Case 1: If re has the global flag set, then re(str) should be null
41 * Case 2: If re doesn't have this set, then re(str) should be unaffected
43 * See http://bugzilla.mozilla.org/show_bug.cgi?id=76717
46 * From the ECMA-262 Final spec:
48 * 15.10.6.2 RegExp.prototype.exec(string)
49 * Performs a regular expression match of string against the regular
50 * expression and returns an Array object containing the results of
51 * the match, or null if the string did not match.
53 * The string ToString(string) is searched for an occurrence of the
54 * regular expression pattern as follows:
56 * 1. Let S be the value of ToString(string).
57 * 2. Let length be the length of S.
58 * 3. Let lastIndex be the value of the lastIndex property.
59 * 4. Let i be the value of ToInteger(lastIndex).
60 * 5. If the global property is false, let i = 0.
61 * 6. If i < 0 or i > length then set lastIndex to 0 and return null.
62 * 7. Call [[Match]], giving it the arguments S and i.
63 * If [[Match]] returned failure, go to step 8;
64 * otherwise let r be its State result and go to step 10.
67 * 10. Let e be r's endIndex value.
68 * 11. If the global property is true, set lastIndex to e.
75 * A. If the global flag is not set, |lastIndex| is set to 0
76 * before the match is attempted; thus the match is unaffected.
78 * B. If the global flag IS set and re.lastIndex is >= 0 and <= str.length,
79 * |lastIndex| is incremented every time there is a match; not from
80 * i to i+1, but from i to "endIndex" e:
82 * e = (index of last input character matched so far by the pattern) + 1
84 * The match is then attempted from this position in the string (Step 7).
86 * C. When the global flag IS set and re.lastIndex is < 0 or > str.length,
87 * |lastIndex| is set to 0 and the match returns null.
90 * Note the |lastIndex| property is writeable, and may be set arbitrarily
91 * by the programmer - and we will do that below.
94 //-----------------------------------------------------------------------------
97 var summary = 'Testing re.exec(str) when re.lastIndex is < 0 or > str.length';
99 var statusmessages = new Array();
101 var patterns = new Array();
103 var strings = new Array();
104 var actualmatch = '';
105 var actualmatches = new Array();
106 var expectedmatch = '';
107 var expectedmatches = new Array();
110 /******************************************************************************
112 * Case 1 : when the global flag is set -
114 *****************************************************************************/
116 string = 'AbcaBcabC';
118 status = inSection(1);
119 actualmatch = pattern.exec(string);
120 expectedmatch = Array('Abc');
123 status = inSection(2);
124 actualmatch = pattern.exec(string);
125 expectedmatch = Array('aBc');
128 status = inSection(3);
129 actualmatch = pattern.exec(string);
130 expectedmatch = Array('abC');
134 * At this point |lastIndex| is > string.length, so the match should be null -
136 status = inSection(4);
137 actualmatch = pattern.exec(string);
138 expectedmatch = null;
142 * Now let's set |lastIndex| to -1, so the match should again be null -
144 status = inSection(5);
145 pattern.lastIndex = -1;
146 actualmatch = pattern.exec(string);
147 expectedmatch = null;
151 * Now try some edge-case values. Thanks to the work done in
152 * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
153 * is now stored as a double instead of a uint32 (unsigned integer).
155 * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
156 * all the way up to Number.MAX_VALUE. So that's why we need cases
157 * between those two numbers.
159 status = inSection(6);
160 pattern.lastIndex = Math.pow(2,32);
161 actualmatch = pattern.exec(string);
162 expectedmatch = null;
165 status = inSection(7);
166 pattern.lastIndex = -Math.pow(2,32);
167 actualmatch = pattern.exec(string);
168 expectedmatch = null;
171 status = inSection(8);
172 pattern.lastIndex = Math.pow(2,32) + 1;
173 actualmatch = pattern.exec(string);
174 expectedmatch = null;
177 status = inSection(9);
178 pattern.lastIndex = -(Math.pow(2,32) + 1);
179 actualmatch = pattern.exec(string);
180 expectedmatch = null;
183 status = inSection(10);
184 pattern.lastIndex = Math.pow(2,32) * 2;
185 actualmatch = pattern.exec(string);
186 expectedmatch = null;
189 status = inSection(11);
190 pattern.lastIndex = -Math.pow(2,32) * 2;
191 actualmatch = pattern.exec(string);
192 expectedmatch = null;
195 status = inSection(12);
196 pattern.lastIndex = Math.pow(2,40);
197 actualmatch = pattern.exec(string);
198 expectedmatch = null;
201 status = inSection(13);
202 pattern.lastIndex = -Math.pow(2,40);
203 actualmatch = pattern.exec(string);
204 expectedmatch = null;
207 status = inSection(14);
208 pattern.lastIndex = Number.MAX_VALUE;
209 actualmatch = pattern.exec(string);
210 expectedmatch = null;
213 status = inSection(15);
214 pattern.lastIndex = -Number.MAX_VALUE;
215 actualmatch = pattern.exec(string);
216 expectedmatch = null;
221 /******************************************************************************
223 * Case 2: repeat all the above cases WITHOUT the global flag set.
224 * According to EMCA. |lastIndex| should get set to 0 before the match.
226 * Therefore re.exec(str) should be unaffected; thus our expected values
227 * below are now DIFFERENT when |lastIndex| is < 0 or > str.length
229 *****************************************************************************/
232 string = 'AbcaBcabC';
234 status = inSection(16);
235 actualmatch = pattern.exec(string);
236 expectedmatch = Array('Abc');
239 status = inSection(17);
240 actualmatch = pattern.exec(string);
241 expectedmatch = Array('Abc'); // NOT Array('aBc') as before -
244 status = inSection(18);
245 actualmatch = pattern.exec(string);
246 expectedmatch = Array('Abc'); // NOT Array('abC') as before -
250 * At this point above, |lastIndex| WAS > string.length, but not here -
252 status = inSection(19);
253 actualmatch = pattern.exec(string);
254 expectedmatch = Array('Abc') // NOT null as before -
258 * Now let's set |lastIndex| to -1
260 status = inSection(20);
261 pattern.lastIndex = -1;
262 actualmatch = pattern.exec(string);
263 expectedmatch = Array('Abc') // NOT null as before -
267 * Now try some edge-case values. Thanks to the work done in
268 * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
269 * is now stored as a double instead of a uint32 (unsigned integer).
271 * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
272 * all the way up to Number.MAX_VALUE. So that's why we need cases
273 * between those two numbers.
275 status = inSection(21);
276 pattern.lastIndex = Math.pow(2,32);
277 actualmatch = pattern.exec(string);
278 expectedmatch = Array('Abc') // NOT null as before -
281 status = inSection(22);
282 pattern.lastIndex = -Math.pow(2,32);
283 actualmatch = pattern.exec(string);
284 expectedmatch = Array('Abc') // NOT null as before -
287 status = inSection(23);
288 pattern.lastIndex = Math.pow(2,32) + 1;
289 actualmatch = pattern.exec(string);
290 expectedmatch = Array('Abc') // NOT null as before -
293 status = inSection(24);
294 pattern.lastIndex = -(Math.pow(2,32) + 1);
295 actualmatch = pattern.exec(string);
296 expectedmatch = Array('Abc') // NOT null as before -
299 status = inSection(25);
300 pattern.lastIndex = Math.pow(2,32) * 2;
301 actualmatch = pattern.exec(string);
302 expectedmatch = Array('Abc') // NOT null as before -
305 status = inSection(26);
306 pattern.lastIndex = -Math.pow(2,32) * 2;
307 actualmatch = pattern.exec(string);
308 expectedmatch = Array('Abc') // NOT null as before -
311 status = inSection(27);
312 pattern.lastIndex = Math.pow(2,40);
313 actualmatch = pattern.exec(string);
314 expectedmatch = Array('Abc') // NOT null as before -;
317 status = inSection(28);
318 pattern.lastIndex = -Math.pow(2,40);
319 actualmatch = pattern.exec(string);
320 expectedmatch = Array('Abc') // NOT null as before -
323 status = inSection(29);
324 pattern.lastIndex = Number.MAX_VALUE;
325 actualmatch = pattern.exec(string);
326 expectedmatch = Array('Abc') // NOT null as before -
329 status = inSection(30);
330 pattern.lastIndex = -Number.MAX_VALUE;
331 actualmatch = pattern.exec(string);
332 expectedmatch = Array('Abc') // NOT null as before -
338 //-------------------------------------------------------------------------------------------------
340 //-------------------------------------------------------------------------------------------------
346 statusmessages[i] = status;
347 patterns[i] = pattern;
349 actualmatches[i] = actualmatch;
350 expectedmatches[i] = expectedmatch;
358 printBugNumber (bug);
359 printStatus (summary);
360 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);