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