strmbase: Add support for rendering algorithms to quality control.
[wine] / dlls / jscript / tests / regexp.js
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19
20 var m, re, b, i, obj;
21
22 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
23 RegExp.leftContext = "abc";
24 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
25
26 re = /a+/;
27 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
28
29 m = re.exec(" aabaaa");
30 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
31 ok(m.index === 1, "m.index = " + m.index);
32 ok(m.input === " aabaaa", "m.input = " + m.input);
33 ok(m.length === 1, "m.length = " + m.length);
34 ok(m[0] === "aa", "m[0] = " + m[0]);
35 ok(RegExp.leftContext === " ", "RegExp.leftContext = " + RegExp.leftContext);
36 ok(RegExp.rightContext === "baaa", "RegExp.rightContext = " + RegExp.rightContext);
37
38 m = re.exec(" aabaaa");
39 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
40 ok(m.index === 1, "m.index = " + m.index);
41 ok(m.input === " aabaaa", "m.input = " + m.input);
42 ok(m.length === 1, "m.length = " + m.length);
43 ok(m[0] === "aa", "m[0] = " + m[0]);
44 ok(RegExp.leftContext === " ", "RegExp.leftContext = " + RegExp.leftContext);
45 ok(RegExp.rightContext === "baaa", "RegExp.rightContext = " + RegExp.rightContext);
46
47 re = /a+/g;
48 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
49
50 m = re.exec(" aabaaa");
51 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
52 ok(m.index === 1, "m.index = " + m.index);
53 ok(m.lastIndex == 3, "m.lastIndex = " + m.lastIndex);
54 ok(m.input === " aabaaa", "m.input = " + m.input);
55 ok(m.length === 1, "m.length = " + m.length);
56 ok(m[0] === "aa", "m[0] = " + m[0]);
57
58 m = re.exec(" aabaaa");
59 ok(re.lastIndex === 7, "re.lastIndex = " + re.lastIndex);
60 ok(m.index === 4, "m.index = " + m.index);
61 ok(m.input === " aabaaa", "m.input = " + m.input);
62 ok(m.length === 1, "m.length = " + m.length);
63 ok(m[0] === "aaa", "m[0] = " + m[0]);
64
65 m = re.exec(" aabaaa");
66 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
67 ok(m === null, "m is not null");
68
69 re.exec("               a");
70 ok(re.lastIndex === 16, "re.lastIndex = " + re.lastIndex);
71 ok(RegExp.leftContext === "               ",
72    "RegExp.leftContext = " + RegExp.leftContext);
73 ok(RegExp.rightContext === "", "RegExp.rightContext = " + RegExp.rightContext);
74
75 m = re.exec(" a");
76 ok(m === null, "m is not null");
77 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
78
79 m = re.exec(" a");
80 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex);
81
82 m = re.exec();
83 ok(m === null, "m is not null");
84 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
85
86 m = /(a|b)+|(c)/.exec("aa");
87 ok(m[0] === "aa", "m[0] = " + m[0]);
88 ok(m[1] === "a", "m[1] = " + m[1]);
89 ok(m[2] === "", "m[2] = " + m[2]);
90
91 b = re.test("  a ");
92 ok(b === true, "re.test('  a ') returned " + b);
93 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
94 ok(RegExp.leftContext === "  ", "RegExp.leftContext = " + RegExp.leftContext);
95 ok(RegExp.rightContext === " ", "RegExp.rightContext = " + RegExp.rightContext);
96
97 b = re.test(" a ");
98 ok(b === false, "re.test(' a ') returned " + b);
99 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
100 ok(RegExp.leftContext === "  ", "RegExp.leftContext = " + RegExp.leftContext);
101 ok(RegExp.rightContext === " ", "RegExp.rightContext = " + RegExp.rightContext);
102
103 re = /\[([^\[]+)\]/g;
104 m = re.exec(" [test]  ");
105 ok(re.lastIndex === 7, "re.lastIndex = " + re.lastIndex);
106 ok(m.index === 1, "m.index = " + m.index);
107 ok(m.input === " [test]  ", "m.input = " + m.input);
108 ok(m.length === 2, "m.length = " + m.length);
109 ok(m[0] === "[test]", "m[0] = " + m[0]);
110 ok(m[1] === "test", "m[1] = " + m[1]);
111
112 b = /a*/.test();
113 ok(b === true, "/a*/.test() returned " + b);
114 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
115 ok(RegExp.rightContext === "undefined", "RegExp.rightContext = " + RegExp.rightContext);
116
117 b = /f/.test();
118 ok(b === true, "/f/.test() returned " + b);
119 ok(RegExp.leftContext === "unde", "RegExp.leftContext = " + RegExp.leftContext);
120 ok(RegExp.rightContext === "ined", "RegExp.rightContext = " + RegExp.rightContext);
121
122 b = /abc/.test();
123 ok(b === false, "/abc/.test() returned " + b);
124 ok(RegExp.leftContext === "unde", "RegExp.leftContext = " + RegExp.leftContext);
125 ok(RegExp.rightContext === "ined", "RegExp.rightContext = " + RegExp.rightContext);
126
127 m = "abcabc".match(re = /ca/);
128 ok(typeof(m) === "object", "typeof m is not object");
129 ok(m.length === 1, "m.length is not 1");
130 ok(m["0"] === "ca", "m[0] is not \"ca\"");
131 ok(m.constructor === Array, "unexpected m.constructor");
132 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex);
133 ok(RegExp.leftContext === "ab", "RegExp.leftContext = " + RegExp.leftContext);
134 ok(RegExp.rightContext === "bc", "RegExp.rightContext = " + RegExp.rightContext);
135
136 m = "abcabc".match(/ab/);
137 ok(typeof(m) === "object", "typeof m is not object");
138 ok(m.length === 1, "m.length is not 1");
139 ok(m["0"] === "ab", "m[0] is not \"ab\"");
140 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
141 ok(RegExp.rightContext === "cabc", "RegExp.rightContext = " + RegExp.rightContext);
142
143 m = "abcabc".match(/ab/g);
144 ok(typeof(m) === "object", "typeof m is not object");
145 ok(m.length === 2, "m.length is not 2");
146 ok(m["0"] === "ab", "m[0] is not \"ab\"");
147 ok(m["1"] === "ab", "m[1] is not \"ab\"");
148 /* ok(m.input === "abcabc", "m.input = " + m.input); */
149
150 m = "abcabc".match(/Ab/g);
151 ok(typeof(m) === "object", "typeof m is not object");
152 ok(m === null, "m is not null");
153
154 m = "abcabc".match(/Ab/gi);
155 ok(typeof(m) === "object", "typeof m is not object");
156 ok(m.length === 2, "m.length is not 2");
157 ok(m["0"] === "ab", "m[0] is not \"ab\"");
158 ok(m["1"] === "ab", "m[1] is not \"ab\"");
159 ok(RegExp.leftContext === "abc", "RegExp.leftContext = " + RegExp.leftContext);
160 ok(RegExp.rightContext === "c", "RegExp.rightContext = " + RegExp.rightContext);
161
162 m = "aaabcabc".match(/a+b/g);
163 ok(typeof(m) === "object", "typeof m is not object");
164 ok(m.length === 2, "m.length is not 2");
165 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
166 ok(m["1"] === "ab", "m[1] is not \"ab\"");
167
168 m = "aaa\\\\cabc".match(/\\/g);
169 ok(typeof(m) === "object", "typeof m is not object");
170 ok(m.length === 2, "m.length is not 2");
171 ok(m["0"] === "\\", "m[0] is not \"\\\"");
172 ok(m["1"] === "\\", "m[1] is not \"\\\"");
173
174 m = "abcabc".match(new RegExp("ab"));
175 ok(typeof(m) === "object", "typeof m is not object");
176 ok(m.length === 1, "m.length is not 1");
177 ok(m["0"] === "ab", "m[0] is not \"ab\"");
178
179 m = "abcabc".match(new RegExp("ab","g"));
180 ok(typeof(m) === "object", "typeof m is not object");
181 ok(m.length === 2, "m.length is not 2");
182 ok(m["0"] === "ab", "m[0] is not \"ab\"");
183 ok(m["1"] === "ab", "m[1] is not \"ab\"");
184 ok(RegExp.leftContext === "abc", "RegExp.leftContext = " + RegExp.leftContext);
185 ok(RegExp.rightContext === "c", "RegExp.rightContext = " + RegExp.rightContext);
186
187 m = "abcabc".match(new RegExp(/ab/g));
188 ok(typeof(m) === "object", "typeof m is not object");
189 ok(m.length === 2, "m.length is not 2");
190 ok(m["0"] === "ab", "m[0] is not \"ab\"");
191 ok(m["1"] === "ab", "m[1] is not \"ab\"");
192
193 m = "abcabc".match(new RegExp("ab","g", "test"));
194 ok(typeof(m) === "object", "typeof m is not object");
195 ok(m.length === 2, "m.length is not 2");
196 ok(m["0"] === "ab", "m[0] is not \"ab\"");
197 ok(m["1"] === "ab", "m[1] is not \"ab\"");
198 ok(m.index === 3, "m.index = " + m.index);
199 ok(m.input === "abcabc", "m.input = " + m.input);
200 ok(m.lastIndex === 5, "m.lastIndex = " + m.lastIndex);
201
202 m = "abcabcg".match("ab", "g");
203 ok(typeof(m) === "object", "typeof m is not object");
204 ok(m.length === 1, "m.length is not 1");
205 ok(m["0"] === "ab", "m[0] is not \"ab\"");
206 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
207 ok(RegExp.rightContext === "cabcg", "RegExp.rightContext = " + RegExp.rightContext);
208
209 m = "abcabc".match();
210 ok(m === null, "m is not null");
211 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
212 ok(RegExp.rightContext === "cabcg", "RegExp.rightContext = " + RegExp.rightContext);
213
214 m = "abcabc".match(/(a)(b)cabc/);
215 ok(typeof(m) === "object", "typeof m is not object");
216 ok(m.length === 3, "m.length is not 3");
217 ok(m[0] === "abcabc", "m[0] is not \"abc\"");
218 ok(m[1] === "a", "m[1] is not \"a\"");
219 ok(m[2] === "b", "m[2] is not \"b\"");
220
221 re = /(a)bcabc/;
222 re.lastIndex = -3;
223 m = "abcabc".match(re);
224 ok(typeof(m) === "object", "typeof m is not object");
225 ok(m.length === 2, "m.length = " + m.length + "expected 3");
226 ok(m[0] === "abcabc", "m[0] is not \"abc\"");
227 ok(m[1] === "a", "m[1] is not \"a\"");
228 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
229
230 re = /(a)bcabc/;
231 re.lastIndex = 2;
232 m = "abcabcxxx".match(re);
233 ok(typeof(m) === "object", "typeof m is not object");
234 ok(m.length === 2, "m.length = " + m.length + "expected 3");
235 ok(m[0] === "abcabc", "m[0] is not \"abc\"");
236 ok(m[1] === "a", "m[1] is not \"a\"");
237 ok(m.input === "abcabcxxx", "m.input = " + m.input);
238 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
239
240 r = "- [test] -".replace(re = /\[([^\[]+)\]/g, "success");
241 ok(r === "- success -", "r = " + r + " expected '- success -'");
242 ok(re.lastIndex === 8, "re.lastIndex = " + re.lastIndex);
243 ok(RegExp.leftContext === "- ", "RegExp.leftContext = " + RegExp.leftContext);
244 ok(RegExp.rightContext === " -", "RegExp.rightContext = " + RegExp.rightContext);
245
246 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
247 ok(r === "aa aa", "r = " + r + "aa aa");
248 ok(RegExp.leftContext === "[test] ",
249    "RegExp.leftContext = " + RegExp.leftContext);
250 ok(RegExp.rightContext === "",
251    "RegExp.rightContext = " + RegExp.rightContext);
252
253 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
254 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
255
256 r = "- [test] -".replace(/\[([^\[]+)\]/g);
257 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
258
259 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
260 ok(r === "- true -", "r = " + r + " expected '- true -'");
261
262 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
263 ok(r === "- true -", "r = " + r + " expected '- true -'");
264 ok(RegExp.leftContext === "- ", "RegExp.leftContext = " + RegExp.leftContext);
265 ok(RegExp.rightContext === " -", "RegExp.rightContext = " + RegExp.rightContext);
266
267 var tmp = 0;
268
269 function replaceFunc1(m, off, str) {
270     ok(arguments.length === 3, "arguments.length = " + arguments.length);
271
272     switch(tmp) {
273     case 0:
274         ok(m === "[test1]", "m = " + m + " expected [test1]");
275         ok(off === 0, "off = " + off + " expected 0");
276         ok(RegExp.leftContext === "- ",
277            "RegExp.leftContext = " + RegExp.leftContext);
278         ok(RegExp.rightContext === " -",
279            "RegExp.rightContext = " + RegExp.rightContext);
280         break;
281     case 1:
282         ok(m === "[test2]", "m = " + m + " expected [test2]");
283         ok(off === 8, "off = " + off + " expected 8");
284         ok(RegExp.leftContext === "- ",
285            "RegExp.leftContext = " + RegExp.leftContext);
286         ok(RegExp.rightContext === " -",
287            "RegExp.rightContext = " + RegExp.rightContext);
288         break;
289     default:
290         ok(false, "unexpected call");
291     }
292
293     ok(str === "[test1] [test2]", "str = " + arguments[3]);
294     return "r" + tmp++;
295 }
296
297 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
298 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
299 ok(RegExp.leftContext === "[test1] ", "RegExp.leftContext = " + RegExp.leftContext);
300 ok(RegExp.rightContext === "", "RegExp.rightContext = " + RegExp.rightContext);
301
302 tmp = 0;
303
304 function replaceFunc2(m, subm, off, str) {
305     ok(arguments.length === 4, "arguments.length = " + arguments.length);
306
307     switch(tmp) {
308     case 0:
309         ok(subm === "test1", "subm = " + subm);
310         ok(m === "[test1]", "m = " + m + " expected [test1]");
311         ok(off === 0, "off = " + off + " expected 0");
312         break;
313     case 1:
314         ok(subm === "test2", "subm = " + subm);
315         ok(m === "[test2]", "m = " + m + " expected [test2]");
316         ok(off === 8, "off = " + off + " expected 8");
317         break;
318     default:
319         ok(false, "unexpected call");
320     }
321
322     ok(str === "[test1] [test2]", "str = " + arguments[3]);
323     return "r" + tmp++;
324 }
325
326 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
327 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
328
329 r = "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
330 ok(r === "$1-$11,$1-$22", "r = '" + r + "' expected '$1-$11,$1-$22'");
331 ok(RegExp.leftContext === "$1,", "RegExp.leftContext = " + RegExp.leftContext);
332 ok(RegExp.rightContext === "", "RegExp.rightContext = " + RegExp.rightContext);
333
334 r = "abc &1 123".replace(/(\&(\d))/g, "$&");
335 ok(r === "abc &1 123", "r = '" + r + "' expected 'abc &1 123'");
336 ok(RegExp.leftContext === "abc ", "RegExp.leftContext = " + RegExp.leftContext);
337 ok(RegExp.rightContext === " 123", "RegExp.rightContext = " + RegExp.rightContext);
338
339 r = "abc &1 123".replace(/(\&(\d))/g, "$'");
340 ok(r === "abc  123 123", "r = '" + r + "' expected 'abc  123 123'");
341
342 r = "abc &1 123".replace(/(\&(\d))/g, "$`");
343 ok(r === "abc abc  123", "r = '" + r + "' expected 'abc abc  123'");
344
345 r = "abc &1 123".replace(/(\&(\d))/g, "$3");
346 ok(r === "abc $3 123", "r = '" + r + "' expected 'abc $3 123'");
347
348 r = "abc &1 123".replace(/(\&(\d))/g, "$");
349 ok(r === "abc $ 123", "r = '" + r + "' expected 'abc $ 123'");
350
351 r = "abc &1 123".replace(/(\&(\d))/g, "$a");
352 ok(r === "abc $a 123", "r = '" + r + "' expected 'abc $a 123'");
353
354 r = "abc &1 123".replace(/(\&(\d))/g, "$11");
355 ok(r === "abc &11 123", "r = '" + r + "' expected 'abc &11 123'");
356
357 r = "abc &1 123".replace(/(\&(\d))/g, "$0");
358 ok(r === "abc $0 123", "r = '" + r + "' expected 'abc $0 123'");
359
360 /a/.test("a");
361 r = "1 2 3".replace("2", "$&");
362 ok(r === "1 $& 3", "r = '" + r + "' expected '1 $& 3'");
363 ok(RegExp.leftContext === "", "RegExp.leftContext = " + RegExp.leftContext);
364 ok(RegExp.rightContext === "", "RegExp.rightContext = " + RegExp.rightContext);
365
366 r = "1 2 3".replace("2", "$'");
367 ok(r === "1 $' 3", "r = '" + r + "' expected '1 $' 3'");
368
369 r = "1,,2,3".split(/,+/g);
370 ok(r.length === 3, "r.length = " + r.length);
371 ok(r[0] === "1", "r[0] = " + r[0]);
372 ok(r[1] === "2", "r[1] = " + r[1]);
373 ok(r[2] === "3", "r[2] = " + r[2]);
374 ok(RegExp.leftContext === "1,,2", "RegExp.leftContext = " + RegExp.leftContext);
375 ok(RegExp.rightContext === "3", "RegExp.rightContext = " + RegExp.rightContext);
376
377 r = "1,,2,3".split(/,+/);
378 ok(r.length === 3, "r.length = " + r.length);
379 ok(r[0] === "1", "r[0] = " + r[0]);
380 ok(r[1] === "2", "r[1] = " + r[1]);
381 ok(r[2] === "3", "r[2] = " + r[2]);
382
383 r = "1,,2,".split(/,+/);
384 ok(r.length === 2, "r.length = " + r.length);
385 ok(r[0] === "1", "r[0] = " + r[0]);
386 ok(r[1] === "2", "r[1] = " + r[1]);
387
388 re = /,+/;
389 r = "1,,2,".split(re);
390 ok(r.length === 2, "r.length = " + r.length);
391 ok(r[0] === "1", "r[0] = " + r[0]);
392 ok(r[1] === "2", "r[1] = " + r[1]);
393 ok(re.lastIndex === 5, "re.lastIndex = " + re.lastIndex);
394
395 re = /,+/g;
396 r = "1,,2,".split(re);
397 ok(r.length === 2, "r.length = " + r.length);
398 ok(r[0] === "1", "r[0] = " + r[0]);
399 ok(r[1] === "2", "r[1] = " + r[1]);
400 ok(re.lastIndex === 5, "re.lastIndex = " + re.lastIndex);
401
402 r = "1 12 \t3".split(re = /\s+/).join(";");
403 ok(r === "1;12;3", "r = " + r);
404 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
405
406 r = "123".split(re = /\s+/).join(";");
407 ok(r === "123", "r = " + r);
408 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
409
410 /* another standard violation */
411 r = "1 12 \t3".split(re = /(\s)+/g).join(";");
412 ok(r === "1;12;3", "r = " + r);
413 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
414 ok(RegExp.leftContext === "1 12", "RegExp.leftContext = " + RegExp.leftContext);
415 ok(RegExp.rightContext === "3", "RegExp.rightContext = " + RegExp.rightContext);
416
417 re = /,+/;
418 re.lastIndex = 4;
419 r = "1,,2,".split(re);
420 ok(r.length === 2, "r.length = " + r.length);
421 ok(r[0] === "1", "r[0] = " + r[0]);
422 ok(r[1] === "2", "r[1] = " + r[1]);
423 ok(re.lastIndex === 5, "re.lastIndex = " + re.lastIndex);
424
425 re = /abc[^d]/g;
426 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
427
428 re = /a\bc[^d]/g;
429 ok(re.source === "a\\bc[^d]", "re.source = '" + re.source + "', expected 'a\\bc[^d]'");
430
431 re = /abc/;
432 ok(re === RegExp(re), "re !== RegExp(re)");
433
434 re = RegExp("abc[^d]", "g");
435 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
436
437 re = /abc/;
438 ok(re === RegExp(re, undefined), "re !== RegExp(re, undefined)");
439
440 re = /abc/;
441 ok(re === RegExp(re, undefined, 1), "re !== RegExp(re, undefined, 1)");
442
443 re = /a/g;
444 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
445
446 m = re.exec(" a   ");
447 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
448 ok(m.index === 1, "m.index = " + m.index + " expected 1");
449 ok(RegExp.leftContext === " ", "RegExp.leftContext = " + RegExp.leftContext);
450 ok(RegExp.rightContext === "   ", "RegExp.rightContext = " + RegExp.rightContext);
451
452 m = re.exec(" a   ");
453 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
454 ok(m === null, "m = " + m + " expected null");
455
456 re.lastIndex = 2;
457 m = re.exec(" a a ");
458 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
459 ok(m.index === 3, "m.index = " + m.index + " expected 3");
460
461 re.lastIndex = "2";
462 ok(re.lastIndex === "2", "re.lastIndex = " + re.lastIndex + " expected '2'");
463 m = re.exec(" a a ");
464 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
465 ok(m.index === 3, "m.index = " + m.index + " expected 3");
466
467 var li = 0;
468 var obj = new Object();
469 obj.valueOf = function() { return li; };
470
471 re.lastIndex = obj;
472 ok(re.lastIndex === obj, "re.lastIndex = " + re.lastIndex + " expected obj");
473 li = 2;
474 m = re.exec(" a a ");
475 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
476 ok(m.index === 1, "m.index = " + m.index + " expected 1");
477
478 re.lastIndex = 3;
479 re.lastIndex = "test";
480 ok(re.lastIndex === "test", "re.lastIndex = " + re.lastIndex + " expected 'test'");
481 m = re.exec(" a a ");
482 ok(re.lastIndex === 2 || re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 2 or 0");
483 if(re.lastIndex != 0)
484     ok(m.index === 1, "m.index = " + m.index + " expected 1");
485 else
486     ok(m === null, "m = " + m + " expected null");
487
488 re.lastIndex = 0;
489 re.lastIndex = 3.9;
490 ok(re.lastIndex === 3.9, "re.lastIndex = " + re.lastIndex + " expected 3.9");
491 m = re.exec(" a a ");
492 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
493 ok(m.index === 3, "m.index = " + m.index + " expected 3");
494
495 obj.valueOf = function() { throw 0; }
496 re.lastIndex = obj;
497 ok(re.lastIndex === obj, "unexpected re.lastIndex");
498 m = re.exec(" a a ");
499 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
500 ok(m.index === 1, "m.index = " + m.index + " expected 1");
501
502 re.lastIndex = -3;
503 ok(re.lastIndex === -3, "re.lastIndex = " + re.lastIndex + " expected -3");
504 m = re.exec(" a a ");
505 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
506 ok(m === null, "m = " + m + " expected null");
507
508 re.lastIndex = -1;
509 ok(re.lastIndex === -1, "re.lastIndex = " + re.lastIndex + " expected -1");
510 m = re.exec("  ");
511 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
512 ok(m === null, "m = " + m + " expected null");
513
514 re = /a/;
515 re.lastIndex = -3;
516 ok(re.lastIndex === -3, "re.lastIndex = " + re.lastIndex + " expected -3");
517 m = re.exec(" a a ");
518 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 0");
519 ok(m.index === 1, "m = " + m + " expected 1");
520 ok(RegExp.leftContext === " ", "RegExp.leftContext = " + RegExp.leftContext);
521 ok(RegExp.rightContext === " a ", "RegExp.rightContext = " + RegExp.rightContext);
522
523 re.lastIndex = -1;
524 ok(re.lastIndex === -1, "re.lastIndex = " + re.lastIndex + " expected -1");
525 m = re.exec("  ");
526 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
527 ok(m === null, "m = " + m + " expected null");
528
529 re = /aa/g;
530 i = 'baacd'.search(re);
531 ok(i === 1, "'baacd'.search(re) = " + i);
532 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
533 ok(RegExp.leftContext === "b", "RegExp.leftContext = " + RegExp.leftContext);
534 ok(RegExp.rightContext === "cd", "RegExp.rightContext = " + RegExp.rightContext);
535
536 re.lastIndex = 2;
537 i = 'baacdaa'.search(re);
538 ok(i === 1, "'baacd'.search(re) = " + i);
539 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
540
541 re = /aa/;
542 i = 'baacd'.search(re);
543 ok(i === 1, "'baacd'.search(re) = " + i);
544 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
545
546 re.lastIndex = 2;
547 i = 'baacdaa'.search(re);
548 ok(i === 1, "'baacd'.search(re) = " + i);
549 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
550 ok(RegExp.leftContext === "b", "RegExp.leftContext = " + RegExp.leftContext);
551 ok(RegExp.rightContext === "cdaa", "RegExp.rightContext = " + RegExp.rightContext);
552
553 re = /d/g;
554 re.lastIndex = 1;
555 i = 'abc'.search(re);
556 ok(i === -1, "'abc'.search(/d/g) = " + i);
557 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
558 ok(RegExp.leftContext === "b", "RegExp.leftContext = " + RegExp.leftContext);
559 ok(RegExp.rightContext === "cdaa", "RegExp.rightContext = " + RegExp.rightContext);
560
561 i = 'abcdde'.search(/[df]/);
562 ok(i === 3, "'abc'.search(/[df]/) = " + i);
563
564 i = 'abcdde'.search(/[df]/, "a");
565 ok(i === 3, "'abc'.search(/[df]/) = " + i);
566
567 i = 'abcdde'.search("[df]");
568 ok(i === 3, "'abc'.search(/d*/) = " + i);
569
570 obj = {
571     toString: function() { return "abc"; }
572 };
573 i = String.prototype.search.call(obj, "b");
574 ok(i === 1, "String.prototype.seatch.apply(obj, 'b') = " + i);
575
576 i = " undefined ".search();
577 ok(i === null, "' undefined '.search() = " + i);
578
579 reportSuccess();