Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / String / 15.5.4.11.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
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
12  * License.
13  *
14  * The Original Code is JavaScript Engine testing utilities.
15  *
16  * The Initial Developer of the Original Code is
17  * Mozilla Foundation.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   <x00000000@freenet.de>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 var BUGNUMBER = 392378;
39 var summary = '15.5.4.11 - String.prototype.replace';
40 var rex, f, a, i;
41
42 reportCompare(
43   2,
44   String.prototype.replace.length,
45   "Section 1"
46 );
47
48 reportCompare(
49   "321",
50   String.prototype.replace.call(123, "123", "321"),
51   "Section 2"
52 );
53
54 reportCompare(
55   "ok",
56   "ok".replace(),
57   "Section 3"
58 );
59
60 reportCompare(
61   "undefined**",
62   "***".replace("*"),
63   "Section 4"
64 );
65
66 reportCompare(
67   "xnullz",
68   "xyz".replace("y", null),
69   "Section 5"
70 );
71
72 reportCompare(
73   "x123",
74   "xyz".replace("yz", 123),
75   "Section 6"
76 );
77
78 reportCompare(
79   "/x/g/x/g/x/g",
80   "xxx".replace(/x/g, /x/g),
81   "Section 7"
82 );
83
84 reportCompare(
85   "ok",
86   "undefined".replace(undefined, "ok"),
87   "Section 8"
88 );
89
90 reportCompare(
91   "ok",
92   "null".replace(null, "ok"),
93   "Section 9"
94 );
95
96 reportCompare(
97   "ok",
98   "123".replace(123, "ok"),
99   "Section 10"
100 );
101
102 reportCompare(
103   "xzyxyz",
104   "xyzxyz".replace("yz", "zy"),
105   "Section 11"
106 );
107
108 reportCompare(
109   "ok",
110   "(xyz)".replace("(xyz)", "ok"),
111   "Section 12"
112 );
113
114 reportCompare(
115   "*$&yzxyz",
116   "xyzxyz".replace("x", "*$$&"),
117   "Section 13"
118 );
119
120 reportCompare(
121   "xy*z*",
122   "xyz".replace("z", "*$&*"),
123   "Section 14"
124 );
125
126 reportCompare(
127   "xyxyzxyz",
128   "xyzxyzxyz".replace("zxy", "$`"),
129   "Section 15"
130 );
131
132 reportCompare(
133   "zxyzxyzzxyz",
134   "xyzxyz".replace("xy", "$'xyz"),
135   "Section 16"
136 );
137
138 reportCompare(
139   "$",
140   "xyzxyz".replace("xyzxyz", "$"),
141   "Section 17"
142 );
143
144 reportCompare(
145   "x$0$00xyz",
146   "xyzxyz".replace("yz", "$0$00"),
147   "Section 18"
148 );
149
150 // Result for $1/$01 .. $99 is implementation-defined if searchValue is no
151 // regular expression. $+ is a non-standard Mozilla extension.
152
153 reportCompare(
154   "$!$\"$-1$*$#$.$xyz$$",
155   "xyzxyz$$".replace("xyz", "$!$\"$-1$*$#$.$"),
156   "Section 19"
157 );
158
159 reportCompare(
160   "$$$&$$$&$&",
161   "$$$&".replace("$$", "$$$$$$&$&$$&"),
162   "Section 20"
163 );
164
165 reportCompare(
166   "yxx",
167   "xxx".replace(/x/, "y"),
168   "Section 21"
169 );
170
171 reportCompare(
172   "yyy",
173   "xxx".replace(/x/g, "y"),
174   "Section 22"
175 );
176
177 rex = /x/, rex.lastIndex = 1;
178 reportCompare(
179   "yxx1",
180   "xxx".replace(rex, "y") + rex.lastIndex,
181   "Section 23"
182 );
183
184 rex = /x/g, rex.lastIndex = 1;
185 reportCompare(
186   "yyy0",
187   "xxx".replace(rex, "y") + rex.lastIndex,
188   "Section 24"
189 );
190
191 rex = /y/, rex.lastIndex = 1;
192 reportCompare(
193   "xxx1",
194   "xxx".replace(rex, "y") + rex.lastIndex,
195   "Section 25"
196 );
197
198 rex = /y/g, rex.lastIndex = 1;
199 reportCompare(
200   "xxx0",
201   "xxx".replace(rex, "y") + rex.lastIndex,
202   "Section 26"
203 );
204
205 rex = /x?/, rex.lastIndex = 1;
206 reportCompare(
207   "(x)xx1",
208   "xxx".replace(rex, "($&)") + rex.lastIndex,
209   "Section 27"
210 );
211
212 rex = /x?/g, rex.lastIndex = 1;
213 reportCompare(
214   "(x)(x)(x)()0",
215   "xxx".replace(rex, "($&)") + rex.lastIndex,
216   "Section 28"
217 );
218
219 rex = /y?/, rex.lastIndex = 1;
220 reportCompare(
221   "()xxx1",
222   "xxx".replace(rex, "($&)") + rex.lastIndex,
223   "Section 29"
224 );
225
226 rex = /y?/g, rex.lastIndex = 1;
227 reportCompare(
228   "()x()x()x()0",
229   "xxx".replace(rex, "($&)") + rex.lastIndex,
230   "Section 30"
231 );
232
233 reportCompare(
234   "xy$0xy$zxy$zxyz$zxyz",
235   "xyzxyzxyz".replace(/zxy/, "$0$`$$$&$$$'$"),
236   "Section 31"
237 );
238
239 reportCompare(
240   "xy$0xy$zxy$zxyz$$0xyzxy$zxy$z$z",
241   "xyzxyzxyz".replace(/zxy/g, "$0$`$$$&$$$'$"),
242   "Section 32"
243 );
244
245 reportCompare(
246   "xyxyxyzxyxyxyz",
247   "xyzxyz".replace(/(((x)(y)()()))()()()(z)/g, "$01$2$3$04$5$6$7$8$09$10"),
248   "Section 33"
249 );
250
251 rex = RegExp(
252   "()()()()()()()()()()" +
253   "()()()()()()()()()()" +
254   "()()()()()()()()()()" +
255   "()()()()()()()()()()" +
256   "()()()()()()()()()()" +
257   "()()()()()()()()()()" +
258   "()()()()()()()()()()" +
259   "()()()()()()()()()()" +
260   "()()()()()()()()()()" +
261   "()()()()()()()()(y)");
262 reportCompare(
263   "x(y)z",
264   "xyz".replace(rex, "($99)"),
265   "Section 34"
266 );
267
268 rex = RegExp(
269   "()()()()()()()()()(x)" +
270   "()()()()()()()()()()" +
271   "()()()()()()()()()()" +
272   "()()()()()()()()()()" +
273   "()()()()()()()()()()" +
274   "()()()()()()()()()()" +
275   "()()()()()()()()()()" +
276   "()()()()()()()()()()" +
277   "()()()()()()()()()()" +
278   "()()()()()()()()()(y)");
279 reportCompare(
280   "(x0)z",
281   "xyz".replace(rex, "($100)"),
282   "Section 35"
283 );
284
285 reportCompare(
286   "xyz(XYZ)",
287   "xyzXYZ".replace(/XYZ/g, "($&)"),
288   "Section 36"
289 );
290
291 reportCompare(
292   "(xyz)(XYZ)",
293   "xyzXYZ".replace(/xYz/gi, "($&)"),
294   "Section 37"
295 );
296
297 reportCompare(
298   "xyz\rxyz\n",
299   "xyz\rxyz\n".replace(/xyz$/g, "($&)"),
300   "Section 38"
301 );
302
303 reportCompare(
304   "(xyz)\r(xyz)\n",
305   "xyz\rxyz\n".replace(/xyz$/gm, "($&)"),
306   "Section 39"
307 );
308
309 f = function () { return "failure" };
310
311 reportCompare(
312   "ok",
313   "ok".replace("x", f),
314   "Section 40"
315 );
316
317 reportCompare(
318   "ok",
319   "ok".replace(/(?=k)ok/, f),
320   "Section 41"
321 );
322
323 reportCompare(
324   "ok",
325   "ok".replace(/(?!)ok/, f),
326   "Section 42"
327 );
328
329 reportCompare(
330   "ok",
331   "ok".replace(/ok(?!$)/, f),
332   "Section 43"
333 );
334
335 f = function (sub, offs, str) {
336   return ["", sub, typeof sub, offs, typeof offs, str, typeof str, ""]
337     .join("|");
338 };
339
340 reportCompare(
341   "x|y|string|1|number|xyz|string|z",
342   "xyz".replace("y", f),
343   "Section 44"
344 );
345
346 reportCompare(
347   "x|(y)|string|1|number|x(y)z|string|z",
348   "x(y)z".replace("(y)", f),
349   "Section 45"
350 );
351
352 reportCompare(
353   "x|y*|string|1|number|xy*z|string|z",
354   "xy*z".replace("y*", f),
355   "Section 46"
356 );
357
358 reportCompare(
359   "12|3|string|2|number|12345|string|45",
360   String.prototype.replace.call(1.2345e4, 3, f),
361   "Section 47"
362 );
363
364 reportCompare(
365   "|x|string|0|number|xxx|string|xx",
366   "xxx".replace(/^x/g, f),
367   "Section 48"
368 );
369
370 reportCompare(
371   "xx|x|string|2|number|xxx|string|",
372   "xxx".replace(/x$/g, f),
373   "Section 49"
374 );
375
376 f = function (sub, paren, offs, str) {
377   return ["", sub, typeof sub, paren, typeof paren, offs, typeof offs,
378     str, typeof str, ""].join("|");
379 };
380
381 reportCompare(
382   "xy|z|string|z|string|2|number|xyz|string|",
383   "xyz".replace(/(z)/g, f),
384   "Section 50"
385 );
386
387 reportCompare(
388   "xyz||string||string|3|number|xyz|string|",
389   "xyz".replace(/($)/g, f),
390   "Section 51"
391 );
392
393 reportCompare(
394   "|xy|string|y|string|0|number|xyz|string|z",
395   "xyz".replace(/(?:x)(y)/g, f),
396   "Section 52"
397 );
398
399 reportCompare(
400   "|x|string|x|string|0|number|xyz|string|yz",
401   "xyz".replace(/((?=xy)x)/g, f),
402   "Section 53"
403 );
404
405 reportCompare(
406   "|x|string|x|string|0|number|xyz|string|yz",
407   "xyz".replace(/(x(?=y))/g, f),
408   "Section 54"
409 );
410
411 reportCompare(
412   "x|y|string|y|string|1|number|xyz|string|z",
413   "xyz".replace(/((?!x)y)/g, f),
414   "Section 55"
415 );
416
417 reportCompare(
418   "|x|string|x|string|0|number|xyz|string|" +
419     "|y|string||undefined|1|number|xyz|string|z",
420   "xyz".replace(/y|(x)/g, f),
421   "Section 56"
422 );
423
424 reportCompare(
425   "xy|z|string||string|2|number|xyz|string|",
426   "xyz".replace(/(z?)z/, f),
427   "Section 57"
428 );
429
430 reportCompare(
431   "xy|z|string||undefined|2|number|xyz|string|",
432   "xyz".replace(/(z)?z/, f),
433   "Section 58"
434 );
435
436 reportCompare(
437   "xy|z|string||undefined|2|number|xyz|string|",
438   "xyz".replace(/(z)?\1z/, f),
439   "Section 59"
440 );
441
442 reportCompare(
443   "xy|z|string||undefined|2|number|xyz|string|",
444   "xyz".replace(/\1(z)?z/, f),
445   "Section 60"
446 );
447
448 reportCompare(
449   "xy|z|string||string|2|number|xyz|string|",
450   "xyz".replace(/(z?\1)z/, f),
451   "Section 61"
452 );
453
454 f = function (sub, paren1, paren2, offs, str) {
455   return ["", sub, typeof sub, paren1, typeof paren1, paren2, typeof paren2,
456     offs, typeof offs, str, typeof str, ""].join("|");
457 };
458
459 reportCompare(
460   "x|y|string|y|string||undefined|1|number|xyz|string|z",
461   "xyz".replace(/(y)(\1)?/, f),
462   "Section 62"
463 );
464
465 reportCompare(
466   "x|yy|string|y|string|y|string|1|number|xyyz|string|z",
467   "xyyz".replace(/(y)(\1)?/g, f),
468   "Section 63"
469 );
470
471 reportCompare(
472   "x|y|string|y|string||undefined|1|number|xyyz|string|" +
473     "|y|string|y|string||undefined|2|number|xyyz|string|z",
474   "xyyz".replace(/(y)(\1)??/g, f),
475   "Section 64"
476 );
477
478 reportCompare(
479   "x|y|string|y|string|y|string|1|number|xyz|string|z",
480   "xyz".replace(/(?=(y))(\1)?/, f),
481   "Section 65"
482 );
483
484 reportCompare(
485   "xyy|z|string||undefined||string|3|number|xyyz|string|",
486   "xyyz".replace(/(?!(y)y)(\1)z/, f),
487   "Section 66"
488 );
489
490 rex = RegExp(
491   "()()()()()()()()()()" +
492   "()()()()()()()()()()" +
493   "()()()()()()()()()()" +
494   "()()()()()()()()()()" +
495   "()()()()()()()()()()" +
496   "()()()()()()()()()()" +
497   "()()()()()()()()()()" +
498   "()()()()()()()()()()" +
499   "()()()()()()()()()()" +
500   "()()()()()()()()()()(z)?(y)");
501 a = ["sub"];
502 for (i = 1; i <= 102; ++i)
503   a[i] = "p" + i;
504 a[103] = "offs";
505 a[104] = "str";
506 a[105] = "return ['', sub, typeof sub, offs, typeof offs, str, typeof str, " +
507   "p100, typeof p100, p101, typeof p101, p102, typeof p102, ''].join('|');";
508 f = Function.apply(null, a);
509 reportCompare(
510   "x|y|string|1|number|xyz|string||string||undefined|y|string|z",
511   "xyz".replace(rex, f),
512   "Section 67"
513 );
514
515 reportCompare(
516   "undefined",
517   "".replace(/.*/g, function () {}),
518   "Section 68"
519 );
520
521 reportCompare(
522   "nullxnullynullznull",
523   "xyz".replace(/.??/g, function () { return null; }),
524   "Section 69"
525 );
526
527 reportCompare(
528   "111",
529   "xyz".replace(/./g, function () { return 1; }),
530   "Section 70"
531 );