]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_6/Array/regress-290592.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s): Mike Shaver
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 //-----------------------------------------------------------------------------
40 var summary
= 'Array extras: forEach, indexOf, filter, map';
45 printStatus (summary
);
49 function identity(v
, index
, array
)
51 reportCompare(v
, array
[index
], 'identity: check callback argument consistency');
55 function mutate(v
, index
, array
)
57 reportCompare(v
, array
[index
], 'mutate: check callback argument consistency');
62 array
.push('not visited');
67 function mutateForEach(v
, index
, array
)
69 reportCompare(v
, array
[index
], 'mutateForEach: check callback argument consistency');
74 array
.push('not visited');
79 function makeUpperCase(v
, index
, array
)
81 reportCompare(v
, array
[index
], 'makeUpperCase: check callback argument consistency');
84 return v
.toUpperCase();
93 function concat(v
, index
, array
)
95 reportCompare(v
, array
[index
], 'concat: check callback argument consistency');
100 function isUpperCase(v
, index
, array
)
102 reportCompare(v
, array
[index
], 'isUpperCase: check callback argument consistency');
105 return v
== v
.toUpperCase();
113 function isString(v
, index
, array
)
115 reportCompare(v
, array
[index
], 'isString: check callback argument consistency');
116 return typeof v
== 'string';
121 function ArrayCallback(state
)
126 ArrayCallback
.prototype.makeUpperCase = function (v
, index
, array
)
128 reportCompare(v
, array
[index
], 'ArrayCallback.prototype.makeUpperCase: check callback argument consistency');
131 return this.state
? v
.toUpperCase() : v
.toLowerCase();
139 ArrayCallback
.prototype.concat = function(v
, index
, array
)
141 reportCompare(v
, array
[index
], 'ArrayCallback.prototype.concat: check callback argument consistency');
145 ArrayCallback
.prototype.isUpperCase = function(v
, index
, array
)
147 reportCompare(v
, array
[index
], 'ArrayCallback.prototype.isUpperCase: check callback argument consistency');
150 return this.state
? true : (v
== v
.toUpperCase());
158 ArrayCallback
.prototype.isString = function(v
, index
, array
)
160 reportCompare(v
, array
[index
], 'ArrayCallback.prototype.isString: check callback argument consistency');
161 return this.state
? true : (typeof v
== 'string');
164 function dumpError(e
)
166 var s
= e
.name
+ ': ' + e
.message
+
167 ' File: ' + e
.fileName
+
168 ', Line: ' + e
.lineNumber
+
169 ', Stack: ' + e
.stack
;
174 var strings
= ['hello', 'Array', 'WORLD'];
175 var mixed
= [0, '0', 0];
176 var sparsestrings
= new Array();
177 sparsestrings
[2] = 'sparse';
179 if ('map' in Array
.prototype)
181 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
185 // map has 1 required argument
187 actual
= Array
.prototype.map
.length
;
188 reportCompare(expect
, actual
, 'Array.prototype.map.length == 1');
190 // throw TypeError if no callback function specified
191 expect
= 'TypeError';
201 reportCompare(expect
, actual
, 'Array.map(undefined) throws TypeError');
206 expect
= 'hello,Array,WORLD';
207 actual
= strings
.map(identity
).toString();
211 actual
= dumpError(e
);
213 reportCompare(expect
, actual
, 'Array.map: identity');
218 expect
= 'hello,mutated,';
219 actual
= strings
.map(mutate
).toString();
223 actual
= dumpError(e
);
225 reportCompare(expect
, actual
, 'Array.map: mutate');
227 strings
= ['hello', 'Array', 'WORLD'];
232 expect
= 'HELLO,ARRAY,WORLD';
233 actual
= strings
.map(makeUpperCase
).toString();
237 actual
= dumpError(e
);
239 reportCompare(expect
, actual
, 'Array.map: uppercase');
243 // pass object method as map callback
244 expect
= 'HELLO,ARRAY,WORLD';
245 var obj
= new ArrayCallback(true);
246 actual
= strings
.map(obj
.makeUpperCase
, obj
).toString();
250 actual
= dumpError(e
);
252 reportCompare(expect
, actual
, 'Array.map: uppercase with object callback');
256 expect
= 'hello,array,world';
257 obj
= new ArrayCallback(false);
258 actual
= strings
.map(obj
.makeUpperCase
, obj
).toString();
262 actual
= dumpError(e
);
264 reportCompare(expect
, actual
, 'Array.map: lowercase with object callback');
268 // map on sparse arrays
270 actual
= sparsestrings
.map(makeUpperCase
).toString();
274 actual
= dumpError(e
);
276 reportCompare(expect
, actual
, 'Array.map: uppercase on sparse array');
279 if ('forEach' in Array
.prototype)
281 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
283 // test Array.forEach
285 // forEach has 1 required argument
287 actual
= Array
.prototype.forEach
.length
;
288 reportCompare(expect
, actual
, 'Array.prototype.forEach.length == 1');
290 // throw TypeError if no callback function specified
291 expect
= 'TypeError';
301 reportCompare(expect
, actual
, 'Array.forEach(undefined) throws TypeError');
306 expect
= 'hello,Array,WORLD,';
308 strings
.forEach(concat
);
312 actual
= dumpError(e
);
314 reportCompare(expect
, actual
, 'Array.forEach');
318 expect
= 'hello,mutated,';
320 strings
.forEach(mutateForEach
);
324 actual
= dumpError(e
);
326 reportCompare(expect
, actual
, 'Array.forEach: mutate');
328 strings
= ['hello', 'Array', 'WORLD'];
334 // pass object method as forEach callback
335 expect
= 'hello,Array,WORLD,';
337 obj
= new ArrayCallback(true);
338 strings
.forEach(obj
.concat
, obj
);
342 actual
= dumpError(e
);
344 reportCompare(expect
, actual
, 'Array.forEach with object callback 1');
348 expect
= 'hello,Array,WORLD,';
350 obj
= new ArrayCallback(false);
351 strings
.forEach(obj
.concat
, obj
);
355 actual
= dumpError(e
);
357 reportCompare(expect
, actual
, 'Array.forEach with object callback 2');
361 // test forEach on sparse arrays
362 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
365 sparsestrings
.forEach(concat
);
369 actual
= dumpError(e
);
371 reportCompare(expect
, actual
, 'Array.forEach on sparse array');
374 if ('filter' in Array
.prototype)
376 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
380 // filter has 1 required argument
382 actual
= Array
.prototype.filter
.length
;
383 reportCompare(expect
, actual
, 'Array.prototype.filter.length == 1');
385 // throw TypeError if no callback function specified
386 expect
= 'TypeError';
396 reportCompare(expect
, actual
, 'Array.filter(undefined) throws TypeError');
400 // test general filter
402 actual
= strings
.filter(isUpperCase
).toString();
406 actual
= dumpError(e
);
408 reportCompare(expect
, actual
, 'Array.filter');
413 obj
= new ArrayCallback(false);
414 actual
= strings
.filter(obj
.isUpperCase
, obj
).toString();
418 actual
= dumpError(e
);
420 reportCompare(expect
, actual
, 'Array.filter object callback 1');
424 expect
= 'hello,Array,WORLD';
425 obj
= new ArrayCallback(true);
426 actual
= strings
.filter(obj
.isUpperCase
, obj
).toString();
430 actual
= dumpError(e
);
432 reportCompare(expect
, actual
, 'Array.filter object callback 2');
435 if ('every' in Array
.prototype)
437 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every
441 // every has 1 required argument
444 actual
= Array
.prototype.every
.length
;
445 reportCompare(expect
, actual
, 'Array.prototype.every.length == 1');
447 // throw TypeError if no every callback function specified
448 expect
= 'TypeError';
458 reportCompare(expect
, actual
, 'Array.every(undefined) throws TypeError');
460 // test general every
465 actual
= strings
.every(isString
);
469 actual
= dumpError(e
);
471 reportCompare(expect
, actual
, 'strings: every element is a string');
476 actual
= mixed
.every(isString
);
480 actual
= dumpError(e
);
482 reportCompare(expect
, actual
, 'mixed: every element is a string');
486 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
488 actual
= sparsestrings
.every(isString
);
492 actual
= dumpError(e
);
494 reportCompare(expect
, actual
, 'sparsestrings: every element is a string');
496 // pass object method as map callback
498 obj
= new ArrayCallback(false);
503 actual
= strings
.every(obj
.isString
, obj
);
507 actual
= dumpError(e
);
509 reportCompare(expect
, actual
, 'strings: every element is a string, via object callback');
514 actual
= mixed
.every(obj
.isString
, obj
);
518 actual
= dumpError(e
) ;
520 reportCompare(expect
, actual
, 'mixed: every element is a string, via object callback');
524 // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082
526 actual
= sparsestrings
.every(obj
.isString
, obj
);
530 actual
= dumpError(e
);
532 reportCompare(expect
, actual
, 'sparsestrings: every element is a string, via object callback');
536 if ('some' in Array
.prototype)
538 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some
542 // some has 1 required argument
545 actual
= Array
.prototype.some
.length
;
546 reportCompare(expect
, actual
, 'Array.prototype.some.length == 1');
548 // throw TypeError if no some callback function specified
549 expect
= 'TypeError';
559 reportCompare(expect
, actual
, 'Array.some(undefined) throws TypeError');
566 actual
= strings
.some(isString
);
570 actual
= dumpError(e
);
572 reportCompare(expect
, actual
, 'strings: some element is a string');
577 actual
= mixed
.some(isString
);
581 actual
= dumpError(e
);
583 reportCompare(expect
, actual
, 'mixed: some element is a string');
588 actual
= sparsestrings
.some(isString
);
592 actual
= dumpError(e
);
594 reportCompare(expect
, actual
, 'sparsestrings: some element is a string');
596 // pass object method as map callback
598 obj
= new ArrayCallback(false);
603 actual
= strings
.some(obj
.isString
, obj
);
607 actual
= dumpError(e
);
609 reportCompare(expect
, actual
, 'strings: some element is a string, via object callback');
614 actual
= mixed
.some(obj
.isString
, obj
);
618 actual
= dumpError(e
);
620 reportCompare(expect
, actual
, 'mixed: some element is a string, via object callback');
625 actual
= sparsestrings
.some(obj
.isString
, obj
);
629 actual
= dumpError(e
);
631 reportCompare(expect
, actual
, 'sparsestrings: some element is a string, via object callback');
635 if ('indexOf' in Array
.prototype)
637 // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
639 // test Array.indexOf
641 // indexOf has 1 required argument
644 actual
= Array
.prototype.indexOf
.length
;
645 reportCompare(expect
, actual
, 'Array.prototype.indexOf.length == 1');
647 // test general indexOf
652 actual
= mixed
.indexOf('not found');
656 actual
= dumpError(e
);
658 reportCompare(expect
, actual
, 'indexOf returns -1 if value not found');
663 actual
= mixed
.indexOf(0);
667 actual
= dumpError(e
);
669 reportCompare(expect
, actual
, 'indexOf matches using strict equality');
674 actual
= mixed
.indexOf('0');
678 actual
= dumpError(e
);
680 reportCompare(expect
, actual
, 'indexOf matches using strict equality');
685 actual
= mixed
.indexOf(0, 1);
689 actual
= dumpError(e
);
691 reportCompare(expect
, actual
, 'indexOf begins searching at fromIndex');