Debian Codegraph @

rhino/testsrc/tests/js1_6/Array/regress-290592.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 var gTestfile = 'regress-290592.js';
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 290592;
9 var summary = 'Array extras: forEach, indexOf, filter, map';
10 var actual = '';
11 var expect = '';
12
13 printBugNumber(BUGNUMBER);
14 printStatus (summary);
15
16 // Utility functions
17
18 function identity(v, index, array)
19 {
20 reportCompare(v, array[index], 'identity: check callback argument consistency');
21 return v;
22 }
23
24 function mutate(v, index, array)
25 {
26 reportCompare(v, array[index], 'mutate: check callback argument consistency');
27 if (index == 0)
28 {
29 array[1] = 'mutated';
30 delete array[2];
31 array.push('not visited');
32 }
33 return v;
34 }
35
36 function mutateForEach(v, index, array)
37 {
38 reportCompare(v, array[index], 'mutateForEach: check callback argument consistency');
39 if (index == 0)
40 {
41 array[1] = 'mutated';
42 delete array[2];
43 array.push('not visited');
44 }
45 actual += v + ',';
46 }
47
48 function makeUpperCase(v, index, array)
49 {
50 reportCompare(v, array[index], 'makeUpperCase: check callback argument consistency');
51 try
52 {
53 return v.toUpperCase();
54 }
55 catch(e)
56 {
57 }
58 return v;
59 }
60
61
62 function concat(v, index, array)
63 {
64 reportCompare(v, array[index], 'concat: check callback argument consistency');
65 actual += v + ',';
66 }
67
68
69 function isUpperCase(v, index, array)
70 {
71 reportCompare(v, array[index], 'isUpperCase: check callback argument consistency');
72 try
73 {
74 return v == v.toUpperCase();
75 }
76 catch(e)
77 {
78 }
79 return false;
80 }
81
82 function isString(v, index, array)
83 {
84 reportCompare(v, array[index], 'isString: check callback argument consistency');
85 return typeof v == 'string';
86 }
87
88
89 // callback object.
90 function ArrayCallback(state)
91 {
92 this.state = state;
93 }
94
95 ArrayCallback.prototype.makeUpperCase = function (v, index, array)
96 {
97 reportCompare(v, array[index], 'ArrayCallback.prototype.makeUpperCase: check callback argument consistency');
98 try
99 {
100 return this.state ? v.toUpperCase() : v.toLowerCase();
101 }
102 catch(e)
103 {
104 }
105 return v;
106 };
107
108 ArrayCallback.prototype.concat = function(v, index, array)
109 {
110 reportCompare(v, array[index], 'ArrayCallback.prototype.concat: check callback argument consistency');
111 actual += v + ',';
112 };
113
114 ArrayCallback.prototype.isUpperCase = function(v, index, array)
115 {
116 reportCompare(v, array[index], 'ArrayCallback.prototype.isUpperCase: check callback argument consistency');
117 try
118 {
119 return this.state ? true : (v == v.toUpperCase());
120 }
121 catch(e)
122 {
123 }
124 return false;
125 };
126
127 ArrayCallback.prototype.isString = function(v, index, array)
128 {
129 reportCompare(v, array[index], 'ArrayCallback.prototype.isString: check callback argument consistency');
130 return this.state ? true : (typeof v == 'string');
131 };
132
133 function dumpError(e)
134 {
135 var s = e.name + ': ' + e.message +
136 ' File: ' + e.fileName +
137 ', Line: ' + e.lineNumber +
138 ', Stack: ' + e.stack;
139 return s;
140 }
141
142 var obj;
143 var strings = ['hello', 'Array', 'WORLD'];
144 var mixed = [0, '0', 0];
145 var sparsestrings = new Array();
146 sparsestrings[2] = 'sparse';
147
148 if ('map' in Array.prototype)
149 {
150 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
151
152 // test Array.map
153
154 // map has 1 required argument
155 expect = 1;
156 actual = Array.prototype.map.length;
157 reportCompare(expect, actual, 'Array.prototype.map.length == 1');
158
159 // throw TypeError if no callback function specified
160 expect = 'TypeError';
161 try
162 {
163 strings.map();
164 actual = 'no error';
165 }
166 catch(e)
167 {
168 actual = e.name;
169 }
170 reportCompare(expect, actual, 'Array.map(undefined) throws TypeError');
171
172 try
173 {
174 // identity map
175 expect = 'hello,Array,WORLD';
176 actual = strings.map(identity).toString();
177 }
178 catch(e)
179 {
180 actual = dumpError(e);
181 }
182 reportCompare(expect, actual, 'Array.map: identity');
183
184
185 try
186 {
187 expect = 'hello,mutated,';
188 actual = strings.map(mutate).toString();
189 }
190 catch(e)
191 {
192 actual = dumpError(e);
193 }
194 reportCompare(expect, actual, 'Array.map: mutate');
195
196 strings = ['hello', 'Array', 'WORLD'];
197
198 try
199 {
200 // general map
201 expect = 'HELLO,ARRAY,WORLD';
202 actual = strings.map(makeUpperCase).toString();
203 }
204 catch(e)
205 {
206 actual = dumpError(e);
207 }
208 reportCompare(expect, actual, 'Array.map: uppercase');
209
210 try
211 {
212 // pass object method as map callback
213 expect = 'HELLO,ARRAY,WORLD';
214 var obj = new ArrayCallback(true);
215 actual = strings.map(obj.makeUpperCase, obj).toString();
216 }
217 catch(e)
218 {
219 actual = dumpError(e);
220 }
221 reportCompare(expect, actual, 'Array.map: uppercase with object callback');
222
223 try
224 {
225 expect = 'hello,array,world';
226 obj = new ArrayCallback(false);
227 actual = strings.map(obj.makeUpperCase, obj).toString();
228 }
229 catch(e)
230 {
231 actual = dumpError(e);
232 }
233 reportCompare(expect, actual, 'Array.map: lowercase with object callback');
234
235 try
236 {
237 // map on sparse arrays
238 expect = ',,SPARSE';
239 actual = sparsestrings.map(makeUpperCase).toString();
240 }
241 catch(e)
242 {
243 actual = dumpError(e);
244 }
245 reportCompare(expect, actual, 'Array.map: uppercase on sparse array');
246 }
247
248 if ('forEach' in Array.prototype)
249 {
250 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
251
252 // test Array.forEach
253
254 // forEach has 1 required argument
255 expect = 1;
256 actual = Array.prototype.forEach.length;
257 reportCompare(expect, actual, 'Array.prototype.forEach.length == 1');
258
259 // throw TypeError if no callback function specified
260 expect = 'TypeError';
261 try
262 {
263 strings.forEach();
264 actual = 'no error';
265 }
266 catch(e)
267 {
268 actual = e.name;
269 }
270 reportCompare(expect, actual, 'Array.forEach(undefined) throws TypeError');
271
272 try
273 {
274 // general forEach
275 expect = 'hello,Array,WORLD,';
276 actual = '';
277 strings.forEach(concat);
278 }
279 catch(e)
280 {
281 actual = dumpError(e);
282 }
283 reportCompare(expect, actual, 'Array.forEach');
284
285 try
286 {
287 expect = 'hello,mutated,';
288 actual = '';
289 strings.forEach(mutateForEach);
290 }
291 catch(e)
292 {
293 actual = dumpError(e);
294 }
295 reportCompare(expect, actual, 'Array.forEach: mutate');
296
297 strings = ['hello', 'Array', 'WORLD'];
298
299
300
301 try
302 {
303 // pass object method as forEach callback
304 expect = 'hello,Array,WORLD,';
305 actual = '';
306 obj = new ArrayCallback(true);
307 strings.forEach(obj.concat, obj);
308 }
309 catch(e)
310 {
311 actual = dumpError(e);
312 }
313 reportCompare(expect, actual, 'Array.forEach with object callback 1');
314
315 try
316 {
317 expect = 'hello,Array,WORLD,';
318 actual = '';
319 obj = new ArrayCallback(false);
320 strings.forEach(obj.concat, obj);
321 }
322 catch(e)
323 {
324 actual = dumpError(e);
325 }
326 reportCompare(expect, actual, 'Array.forEach with object callback 2');
327
328 try
329 {
330 // test forEach on sparse arrays
331 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
332 expect = 'sparse,';
333 actual = '';
334 sparsestrings.forEach(concat);
335 }
336 catch(e)
337 {
338 actual = dumpError(e);
339 }
340 reportCompare(expect, actual, 'Array.forEach on sparse array');
341 }
342
343 if ('filter' in Array.prototype)
344 {
345 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
346
347 // test Array.filter
348
349 // filter has 1 required argument
350 expect = 1;
351 actual = Array.prototype.filter.length;
352 reportCompare(expect, actual, 'Array.prototype.filter.length == 1');
353
354 // throw TypeError if no callback function specified
355 expect = 'TypeError';
356 try
357 {
358 strings.filter();
359 actual = 'no error';
360 }
361 catch(e)
362 {
363 actual = e.name;
364 }
365 reportCompare(expect, actual, 'Array.filter(undefined) throws TypeError');
366
367 try
368 {
369 // test general filter
370 expect = 'WORLD';
371 actual = strings.filter(isUpperCase).toString();
372 }
373 catch(e)
374 {
375 actual = dumpError(e);
376 }
377 reportCompare(expect, actual, 'Array.filter');
378
379 try
380 {
381 expect = 'WORLD';
382 obj = new ArrayCallback(false);
383 actual = strings.filter(obj.isUpperCase, obj).toString();
384 }
385 catch(e)
386 {
387 actual = dumpError(e);
388 }
389 reportCompare(expect, actual, 'Array.filter object callback 1');
390
391 try
392 {
393 expect = 'hello,Array,WORLD';
394 obj = new ArrayCallback(true);
395 actual = strings.filter(obj.isUpperCase, obj).toString();
396 }
397 catch(e)
398 {
399 actual = dumpError(e);
400 }
401 reportCompare(expect, actual, 'Array.filter object callback 2');
402 }
403
404 if ('every' in Array.prototype)
405 {
406 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every
407
408 // test Array.every
409
410 // every has 1 required argument
411
412 expect = 1;
413 actual = Array.prototype.every.length;
414 reportCompare(expect, actual, 'Array.prototype.every.length == 1');
415
416 // throw TypeError if no every callback function specified
417 expect = 'TypeError';
418 try
419 {
420 strings.every();
421 actual = 'no error';
422 }
423 catch(e)
424 {
425 actual = e.name;
426 }
427 reportCompare(expect, actual, 'Array.every(undefined) throws TypeError');
428
429 // test general every
430
431 try
432 {
433 expect = true;
434 actual = strings.every(isString);
435 }
436 catch(e)
437 {
438 actual = dumpError(e);
439 }
440 reportCompare(expect, actual, 'strings: every element is a string');
441
442 try
443 {
444 expect = false;
445 actual = mixed.every(isString);
446 }
447 catch(e)
448 {
449 actual = dumpError(e);
450 }
451 reportCompare(expect, actual, 'mixed: every element is a string');
452
453 try
454 {
455 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
456 expect = true;
457 actual = sparsestrings.every(isString);
458 }
459 catch(e)
460 {
461 actual = dumpError(e);
462 }
463 reportCompare(expect, actual, 'sparsestrings: every element is a string');
464
465 // pass object method as map callback
466
467 obj = new ArrayCallback(false);
468
469 try
470 {
471 expect = true;
472 actual = strings.every(obj.isString, obj);
473 }
474 catch(e)
475 {
476 actual = dumpError(e);
477 }
478 reportCompare(expect, actual, 'strings: every element is a string, via object callback');
479
480 try
481 {
482 expect = false;
483 actual = mixed.every(obj.isString, obj);
484 }
485 catch(e)
486 {
487 actual = dumpError(e) ;
488 }
489 reportCompare(expect, actual, 'mixed: every element is a string, via object callback');
490
491 try
492 {
493 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
494 expect = true;
495 actual = sparsestrings.every(obj.isString, obj);
496 }
497 catch(e)
498 {
499 actual = dumpError(e);
500 }
501 reportCompare(expect, actual, 'sparsestrings: every element is a string, via object callback');
502
503 }
504
505 if ('some' in Array.prototype)
506 {
507 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some
508
509 // test Array.some
510
511 // some has 1 required argument
512
513 expect = 1;
514 actual = Array.prototype.some.length;
515 reportCompare(expect, actual, 'Array.prototype.some.length == 1');
516
517 // throw TypeError if no some callback function specified
518 expect = 'TypeError';
519 try
520 {
521 strings.some();
522 actual = 'no error';
523 }
524 catch(e)
525 {
526 actual = e.name;
527 }
528 reportCompare(expect, actual, 'Array.some(undefined) throws TypeError');
529
530 // test general some
531
532 try
533 {
534 expect = true;
535 actual = strings.some(isString);
536 }
537 catch(e)
538 {
539 actual = dumpError(e);
540 }
541 reportCompare(expect, actual, 'strings: some element is a string');
542
543 try
544 {
545 expect = true;
546 actual = mixed.some(isString);
547 }
548 catch(e)
549 {
550 actual = dumpError(e);
551 }
552 reportCompare(expect, actual, 'mixed: some element is a string');
553
554 try
555 {
556 expect = true;
557 actual = sparsestrings.some(isString);
558 }
559 catch(e)
560 {
561 actual = dumpError(e);
562 }
563 reportCompare(expect, actual, 'sparsestrings: some element is a string');
564
565 // pass object method as map callback
566
567 obj = new ArrayCallback(false);
568
569 try
570 {
571 expect = true;
572 actual = strings.some(obj.isString, obj);
573 }
574 catch(e)
575 {
576 actual = dumpError(e);
577 }
578 reportCompare(expect, actual, 'strings: some element is a string, via object callback');
579
580 try
581 {
582 expect = true;
583 actual = mixed.some(obj.isString, obj);
584 }
585 catch(e)
586 {
587 actual = dumpError(e);
588 }
589 reportCompare(expect, actual, 'mixed: some element is a string, via object callback');
590
591 try
592 {
593 expect = true;
594 actual = sparsestrings.some(obj.isString, obj);
595 }
596 catch(e)
597 {
598 actual = dumpError(e);
599 }
600 reportCompare(expect, actual, 'sparsestrings: some element is a string, via object callback');
601
602 }
603
604 if ('indexOf' in Array.prototype)
605 {
606 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
607
608 // test Array.indexOf
609
610 // indexOf has 1 required argument
611
612 expect = 1;
613 actual = Array.prototype.indexOf.length;
614 reportCompare(expect, actual, 'Array.prototype.indexOf.length == 1');
615
616 // test general indexOf
617
618 try
619 {
620 expect = -1;
621 actual = mixed.indexOf('not found');
622 }
623 catch(e)
624 {
625 actual = dumpError(e);
626 }
627 reportCompare(expect, actual, 'indexOf returns -1 if value not found');
628
629 try
630 {
631 expect = 0;
632 actual = mixed.indexOf(0);
633 }
634 catch(e)
635 {
636 actual = dumpError(e);
637 }
638 reportCompare(expect, actual, 'indexOf matches using strict equality');
639
640 try
641 {
642 expect = 1;
643 actual = mixed.indexOf('0');
644 }
645 catch(e)
646 {
647 actual = dumpError(e);
648 }
649 reportCompare(expect, actual, 'indexOf matches using strict equality');
650
651 try
652 {
653 expect = 2;
654 actual = mixed.indexOf(0, 1);
655 }
656 catch(e)
657 {
658 actual = dumpError(e);
659 }
660 reportCompare(expect, actual, 'indexOf begins searching at fromIndex');
661 }
662