1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Tests for non-standard array iteration functions.
32 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array>
34 // for an explanation of each of the functions.
37 // Array.prototype.filter
42 assertArrayEquals([0], a.filter(function(n) { return n == 0; }));
43 assertArrayEquals([0,1], a);
45 // Use specified object as this object when calling the function.
48 assertArrayEquals([42,42], a.filter(function(n) { return this.value == n }, o))
50 // Modify original array.
52 assertArrayEquals([42,42], a.filter(function(n, index, array) { array[index] = 43; return 42 == n; }));
53 assertArrayEquals([43,43,43,43,43], a);
55 // Only loop through initial part of array eventhough elements are
58 assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); return n == 2; }));
59 assertArrayEquals([1,1,2,2], a);
67 var a = a.filter(function(n) { count++; return n == 2; });
68 assertEquals(3, count);
69 for (var i in a) assertEquals(2, a[i]);
71 // Create a new object in each function call when receiver is a
72 // primitive value. See ECMA-262, Annex C.
74 [1, 2].filter(function() { a.push(this) }, "");
75 assertTrue(a[0] !== a[1]);
77 // Do not create a new object otherwise.
79 [1, 2].filter(function() { a.push(this) }, {});
80 assertEquals(a[0], a[1]);
82 // In strict mode primitive values should not be coerced to an object.
84 [1, 2].filter(function() { 'use strict'; a.push(this); }, "");
85 assertEquals("", a[0]);
86 assertEquals(a[0], a[1]);
92 // Array.prototype.forEach
98 a.forEach(function(n) { count++; });
99 assertEquals(2, count);
101 // Use specified object as this object when calling the function.
102 var o = { value: 42 }
104 a.forEach(function(n) { result.push(this.value); }, o);
105 assertArrayEquals([42,42], result);
107 // Modify original array.
110 a.forEach(function(n, index, array) { array[index] = n + 1; count++; });
111 assertEquals(2, count);
112 assertArrayEquals([1,2], a);
114 // Only loop through initial part of array eventhough elements are
118 a.forEach(function(n, index, array) { array.push(n+1); count++; });
119 assertEquals(2, count);
120 assertArrayEquals([1,1,2,2], a);
126 a.forEach(function(n) { count++; });
127 assertEquals(1, count);
129 // Create a new object in each function call when receiver is a
130 // primitive value. See ECMA-262, Annex C.
132 [1, 2].forEach(function() { a.push(this) }, "");
133 assertTrue(a[0] !== a[1]);
135 // Do not create a new object otherwise.
137 [1, 2].forEach(function() { a.push(this) }, {});
138 assertEquals(a[0], a[1]);
140 // In strict mode primitive values should not be coerced to an object.
142 [1, 2].forEach(function() { 'use strict'; a.push(this); }, "");
143 assertEquals("", a[0]);
144 assertEquals(a[0], a[1]);
150 // Array.prototype.every
155 assertFalse(a.every(function(n) { return n == 0 }));
157 assertTrue(a.every(function(n) { return n == 0 }));
158 assertTrue([].every(function(n) { return n == 0}));
160 // Use specified object as this object when calling the function.
161 var o = { value: 42 }
163 assertFalse(a.every(function(n) { return this.value == n; }, o));
165 assertTrue(a.every(function(n) { return this.value == n; }, o));
167 // Modify original array.
169 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;}));
170 assertArrayEquals([1,1], a);
172 // Only loop through initial part of array eventhough elements are
175 assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n == 1;}));
176 assertArrayEquals([1,1,2,2], a);
183 assertTrue(a.every(function(n) { count++; return n == 2; }));
184 assertEquals(2, count);
186 // Create a new object in each function call when receiver is a
187 // primitive value. See ECMA-262, Annex C.
189 [1, 2].every(function() { a.push(this); return true; }, "");
190 assertTrue(a[0] !== a[1]);
192 // Do not create a new object otherwise.
194 [1, 2].every(function() { a.push(this); return true; }, {});
195 assertEquals(a[0], a[1]);
197 // In strict mode primitive values should not be coerced to an object.
199 [1, 2].every(function() { 'use strict'; a.push(this); return true; }, "");
200 assertEquals("", a[0]);
201 assertEquals(a[0], a[1]);
206 // Array.prototype.map
212 var result = [1,2,3,4,5];
213 assertArrayEquals(result, a.map(function(n) { return n + 1; }));
216 // Use specified object as this object when calling the function.
217 var o = { delta: 42 }
218 result = [42,43,44,45,46];
219 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o));
221 // Modify original array.
223 result = [1,2,3,4,5];
224 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n + 1; return n + 1;}));
225 assertArrayEquals(result, a);
227 // Only loop through initial part of array eventhough elements are
230 result = [1,2,3,4,5];
231 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); return n + 1;}));
232 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);
237 a = a.map(function(n) { return 2*n; });
238 for (var i in a) assertEquals(4, a[i]);
240 // Create a new object in each function call when receiver is a
241 // primitive value. See ECMA-262, Annex C.
243 [1, 2].map(function() { a.push(this) }, "");
244 assertTrue(a[0] !== a[1]);
246 // Do not create a new object otherwise.
248 [1, 2].map(function() { a.push(this) }, {});
249 assertEquals(a[0], a[1]);
251 // In strict mode primitive values should not be coerced to an object.
253 [1, 2].map(function() { 'use strict'; a.push(this); }, "");
254 assertEquals("", a[0]);
255 assertEquals(a[0], a[1]);
260 // Array.prototype.some
266 assertTrue(a.some(function(n) { return n == 3}));
267 assertFalse(a.some(function(n) { return n == 5}));
269 // Use specified object as this object when calling the function.
270 var o = { element: 42 };
272 assertTrue(a.some(function(n) { return this.element == n; }, o));
274 assertFalse(a.some(function(n) { return this.element == n; }, o));
276 // Modify original array.
278 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n == 2; }));
279 assertArrayEquals([1,2,3,3], a);
281 // Only loop through initial part when elements are added.
283 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; }));
284 assertArrayEquals([0,1,2,42,42,42], a);
292 assertTrue(a.some(function(n) { count++; return n == 2; }));
293 assertEquals(2, count);
295 // Create a new object in each function call when receiver is a
296 // primitive value. See ECMA-262, Annex C.
298 [1, 2].some(function() { a.push(this) }, "");
299 assertTrue(a[0] !== a[1]);
301 // Do not create a new object otherwise.
303 [1, 2].some(function() { a.push(this) }, {});
304 assertEquals(a[0], a[1]);
306 // In strict mode primitive values should not be coerced to an object.
308 [1, 2].some(function() { 'use strict'; a.push(this); }, "");
309 assertEquals("", a[0]);
310 assertEquals(a[0], a[1]);