| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // | 38 // |
| 39 (function() { | 39 (function() { |
| 40 // Simple use. | 40 // Simple use. |
| 41 var a = [0,1]; | 41 var a = [0,1]; |
| 42 assertArrayEquals([0], a.filter(function(n) { return n == 0; })); | 42 assertArrayEquals([0], a.filter(function(n) { return n == 0; })); |
| 43 assertArrayEquals([0,1], a); | 43 assertArrayEquals([0,1], a); |
| 44 | 44 |
| 45 // Use specified object as this object when calling the function. | 45 // Use specified object as this object when calling the function. |
| 46 var o = { value: 42 } | 46 var o = { value: 42 } |
| 47 a = [1,42,3,42,4]; | 47 a = [1,42,3,42,4]; |
| 48 assertArrayEquals([42,42], | 48 assertArrayEquals([42,42], a.filter(function(n) { return this.value == n }, o)
) |
| 49 a.filter(function(n) { return this.value == n }, o)) | |
| 50 | 49 |
| 51 // Modify original array. | 50 // Modify original array. |
| 52 a = [1,42,3,42,4]; | 51 a = [1,42,3,42,4]; |
| 53 assertArrayEquals([42,42], | 52 assertArrayEquals([42,42], a.filter(function(n, index, array) { array[index] =
43; return 42 == n; })); |
| 54 a.filter(function(n, index, array) { | |
| 55 array[index] = 43; return 42 == n; | |
| 56 })); | |
| 57 assertArrayEquals([43,43,43,43,43], a); | 53 assertArrayEquals([43,43,43,43,43], a); |
| 58 | 54 |
| 59 // Only loop through initial part of array eventhough elements are | 55 // Only loop through initial part of array eventhough elements are |
| 60 // added. | 56 // added. |
| 61 a = [1,1]; | 57 a = [1,1]; |
| 62 assertArrayEquals([], | 58 assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); re
turn n == 2; })); |
| 63 a.filter(function(n, index, array) { array.push(n+1); return n == 2; })); | |
| 64 assertArrayEquals([1,1,2,2], a); | 59 assertArrayEquals([1,1,2,2], a); |
| 65 | 60 |
| 66 // Respect holes. | 61 // Respect holes. |
| 67 a = new Array(20); | 62 a = new Array(20); |
| 68 var count = 0; | 63 var count = 0; |
| 69 a[2] = 2; | 64 a[2] = 2; |
| 70 a[15] = 2; | 65 a[15] = 2; |
| 71 a[17] = 4; | 66 a[17] = 4; |
| 72 var a = a.filter(function(n) { count++; return n == 2; }); | 67 var a = a.filter(function(n) { count++; return n == 2; }); |
| 73 assertEquals(3, count); | 68 assertEquals(3, count); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 159 |
| 165 // Use specified object as this object when calling the function. | 160 // Use specified object as this object when calling the function. |
| 166 var o = { value: 42 } | 161 var o = { value: 42 } |
| 167 a = [0]; | 162 a = [0]; |
| 168 assertFalse(a.every(function(n) { return this.value == n; }, o)); | 163 assertFalse(a.every(function(n) { return this.value == n; }, o)); |
| 169 a = [42]; | 164 a = [42]; |
| 170 assertTrue(a.every(function(n) { return this.value == n; }, o)); | 165 assertTrue(a.every(function(n) { return this.value == n; }, o)); |
| 171 | 166 |
| 172 // Modify original array. | 167 // Modify original array. |
| 173 a = [0,1]; | 168 a = [0,1]; |
| 174 assertFalse( | 169 assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n
== 1;})); |
| 175 a.every(function(n, index, array) { | |
| 176 array[index] = n + 1; return n == 1; | |
| 177 })); | |
| 178 assertArrayEquals([1,1], a); | 170 assertArrayEquals([1,1], a); |
| 179 | 171 |
| 180 // Only loop through initial part of array eventhough elements are | 172 // Only loop through initial part of array eventhough elements are |
| 181 // added. | 173 // added. |
| 182 a = [1,1]; | 174 a = [1,1]; |
| 183 assertTrue( | 175 assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n ==
1;})); |
| 184 a.every(function(n, index, array) { | |
| 185 array.push(n + 1); return n == 1; | |
| 186 })); | |
| 187 assertArrayEquals([1,1,2,2], a); | 176 assertArrayEquals([1,1,2,2], a); |
| 188 | 177 |
| 189 // Respect holes. | 178 // Respect holes. |
| 190 a = new Array(20); | 179 a = new Array(20); |
| 191 var count = 0; | 180 var count = 0; |
| 192 a[2] = 2; | 181 a[2] = 2; |
| 193 a[15] = 2; | 182 a[15] = 2; |
| 194 assertTrue(a.every(function(n) { count++; return n == 2; })); | 183 assertTrue(a.every(function(n) { count++; return n == 2; })); |
| 195 assertEquals(2, count); | 184 assertEquals(2, count); |
| 196 | 185 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 225 assertEquals(a, a); | 214 assertEquals(a, a); |
| 226 | 215 |
| 227 // Use specified object as this object when calling the function. | 216 // Use specified object as this object when calling the function. |
| 228 var o = { delta: 42 } | 217 var o = { delta: 42 } |
| 229 result = [42,43,44,45,46]; | 218 result = [42,43,44,45,46]; |
| 230 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); | 219 assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); |
| 231 | 220 |
| 232 // Modify original array. | 221 // Modify original array. |
| 233 a = [0,1,2,3,4]; | 222 a = [0,1,2,3,4]; |
| 234 result = [1,2,3,4,5]; | 223 result = [1,2,3,4,5]; |
| 235 assertArrayEquals(result, | 224 assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n +
1; return n + 1;})); |
| 236 a.map(function(n, index, array) { | |
| 237 array[index] = n + 1; return n + 1; | |
| 238 })); | |
| 239 assertArrayEquals(result, a); | 225 assertArrayEquals(result, a); |
| 240 | 226 |
| 241 // Only loop through initial part of array eventhough elements are | 227 // Only loop through initial part of array eventhough elements are |
| 242 // added. | 228 // added. |
| 243 a = [0,1,2,3,4]; | 229 a = [0,1,2,3,4]; |
| 244 result = [1,2,3,4,5]; | 230 result = [1,2,3,4,5]; |
| 245 assertArrayEquals(result, | 231 assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); ret
urn n + 1;})); |
| 246 a.map(function(n, index, array) { array.push(n); return n + 1; })); | |
| 247 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); | 232 assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); |
| 248 | 233 |
| 249 // Respect holes. | 234 // Respect holes. |
| 250 a = new Array(20); | 235 a = new Array(20); |
| 251 a[15] = 2; | 236 a[15] = 2; |
| 252 a = a.map(function(n) { return 2*n; }); | 237 a = a.map(function(n) { return 2*n; }); |
| 253 for (var i in a) assertEquals(4, a[i]); | 238 for (var i in a) assertEquals(4, a[i]); |
| 254 | 239 |
| 255 // Create a new object in each function call when receiver is a | 240 // Create a new object in each function call when receiver is a |
| 256 // primitive value. See ECMA-262, Annex C. | 241 // primitive value. See ECMA-262, Annex C. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 283 | 268 |
| 284 // Use specified object as this object when calling the function. | 269 // Use specified object as this object when calling the function. |
| 285 var o = { element: 42 }; | 270 var o = { element: 42 }; |
| 286 a = [1,42,3]; | 271 a = [1,42,3]; |
| 287 assertTrue(a.some(function(n) { return this.element == n; }, o)); | 272 assertTrue(a.some(function(n) { return this.element == n; }, o)); |
| 288 a = [1]; | 273 a = [1]; |
| 289 assertFalse(a.some(function(n) { return this.element == n; }, o)); | 274 assertFalse(a.some(function(n) { return this.element == n; }, o)); |
| 290 | 275 |
| 291 // Modify original array. | 276 // Modify original array. |
| 292 a = [0,1,2,3]; | 277 a = [0,1,2,3]; |
| 293 assertTrue( | 278 assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n =
= 2; })); |
| 294 a.some(function(n, index, array) { | |
| 295 array[index] = n + 1; return n == 2; })); | |
| 296 assertArrayEquals([1,2,3,3], a); | 279 assertArrayEquals([1,2,3,3], a); |
| 297 | 280 |
| 298 // Only loop through initial part when elements are added. | 281 // Only loop through initial part when elements are added. |
| 299 a = [0,1,2]; | 282 a = [0,1,2]; |
| 300 assertFalse( | 283 assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42;
})); |
| 301 a.some(function(n, index, array) { array.push(42); return n == 42; })); | |
| 302 assertArrayEquals([0,1,2,42,42,42], a); | 284 assertArrayEquals([0,1,2,42,42,42], a); |
| 303 | 285 |
| 304 // Respect holes. | 286 // Respect holes. |
| 305 a = new Array(20); | 287 a = new Array(20); |
| 306 var count = 0; | 288 var count = 0; |
| 307 a[2] = 42; | 289 a[2] = 42; |
| 308 a[10] = 2; | 290 a[10] = 2; |
| 309 a[15] = 42; | 291 a[15] = 42; |
| 310 assertTrue(a.some(function(n) { count++; return n == 2; })); | 292 assertTrue(a.some(function(n) { count++; return n == 2; })); |
| 311 assertEquals(2, count); | 293 assertEquals(2, count); |
| 312 | 294 |
| 313 // Create a new object in each function call when receiver is a | 295 // Create a new object in each function call when receiver is a |
| 314 // primitive value. See ECMA-262, Annex C. | 296 // primitive value. See ECMA-262, Annex C. |
| 315 a = []; | 297 a = []; |
| 316 [1, 2].some(function() { a.push(this) }, ""); | 298 [1, 2].some(function() { a.push(this) }, ""); |
| 317 assertTrue(a[0] !== a[1]); | 299 assertTrue(a[0] !== a[1]); |
| 318 | 300 |
| 319 // Do not create a new object otherwise. | 301 // Do not create a new object otherwise. |
| 320 a = []; | 302 a = []; |
| 321 [1, 2].some(function() { a.push(this) }, {}); | 303 [1, 2].some(function() { a.push(this) }, {}); |
| 322 assertEquals(a[0], a[1]); | 304 assertEquals(a[0], a[1]); |
| 323 | 305 |
| 324 // In strict mode primitive values should not be coerced to an object. | 306 // In strict mode primitive values should not be coerced to an object. |
| 325 a = []; | 307 a = []; |
| 326 [1, 2].some(function() { 'use strict'; a.push(this); }, ""); | 308 [1, 2].some(function() { 'use strict'; a.push(this); }, ""); |
| 327 assertEquals("", a[0]); | 309 assertEquals("", a[0]); |
| 328 assertEquals(a[0], a[1]); | 310 assertEquals(a[0], a[1]); |
| 329 | 311 |
| 330 })(); | 312 })(); |
| OLD | NEW |