Debian Codegraph @

rhino/testsrc/tests/ecma_3/RegExp/15.10.6.2-2.js

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