OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 194 |
195 a = [1, 2, 3]; | 195 a = [1, 2, 3]; |
196 found = a.find(function(val, key) { a[key] = ++val; return false; }); | 196 found = a.find(function(val, key) { a[key] = ++val; return false; }); |
197 assertArrayEquals([2, 3, 4], a); | 197 assertArrayEquals([2, 3, 4], a); |
198 assertEquals(3, a.length); | 198 assertEquals(3, a.length); |
199 assertEquals(undefined, found); | 199 assertEquals(undefined, found); |
200 })(); | 200 })(); |
201 | 201 |
202 | 202 |
203 // | 203 // |
204 // Test predicate is only called for existing elements | 204 // Test predicate is called for holes |
205 // | 205 // |
206 (function() { | 206 (function() { |
207 var a = new Array(30); | 207 var a = new Array(30); |
208 a[11] = 21; | 208 a[11] = 21; |
209 a[7] = 10; | 209 a[7] = 10; |
210 a[29] = 31; | 210 a[29] = 31; |
211 | 211 |
212 var count = 0; | 212 var count = 0; |
213 a.find(function() { count++; return false; }); | 213 a.find(function() { count++; return false; }); |
214 assertEquals(3, count); | 214 assertEquals(30, count); |
215 })(); | 215 })(); |
216 | 216 |
217 | 217 |
| 218 (function() { |
| 219 var a = [0, 1, , 3]; |
| 220 var count = 0; |
| 221 var found = a.find(function(val) { return val === undefined; }); |
| 222 assertEquals(undefined, found); |
| 223 })(); |
| 224 |
| 225 |
| 226 (function() { |
| 227 var a = [0, 1, , 3]; |
| 228 a.__proto__ = { |
| 229 __proto__: Array.prototype, |
| 230 2: 42, |
| 231 }; |
| 232 var count = 0; |
| 233 var found = a.find(function(val) { return val === 42; }); |
| 234 assertEquals(42, found); |
| 235 })(); |
| 236 |
| 237 |
218 // | 238 // |
219 // Test thisArg | 239 // Test thisArg |
220 // | 240 // |
221 (function() { | 241 (function() { |
222 // Test String as a thisArg | 242 // Test String as a thisArg |
223 var found = [1, 2, 3].find(function(val, key) { | 243 var found = [1, 2, 3].find(function(val, key) { |
224 return this.charAt(Number(key)) === String(val); | 244 return this.charAt(Number(key)) === String(val); |
225 }, "321"); | 245 }, "321"); |
226 assertEquals(2, found); | 246 assertEquals(2, found); |
227 | 247 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 309 |
290 assertThrows('Array.prototype.find.apply({}, null, [])', TypeError); | 310 assertThrows('Array.prototype.find.apply({}, null, [])', TypeError); |
291 assertThrows('Array.prototype.find.apply({}, undefined, [])', TypeError); | 311 assertThrows('Array.prototype.find.apply({}, undefined, [])', TypeError); |
292 assertThrows('Array.prototype.find.apply({}, 0, [])', TypeError); | 312 assertThrows('Array.prototype.find.apply({}, 0, [])', TypeError); |
293 assertThrows('Array.prototype.find.apply({}, true, [])', TypeError); | 313 assertThrows('Array.prototype.find.apply({}, true, [])', TypeError); |
294 assertThrows('Array.prototype.find.apply({}, false, [])', TypeError); | 314 assertThrows('Array.prototype.find.apply({}, false, [])', TypeError); |
295 assertThrows('Array.prototype.find.apply({}, "", [])', TypeError); | 315 assertThrows('Array.prototype.find.apply({}, "", [])', TypeError); |
296 assertThrows('Array.prototype.find.apply({}, {}, [])', TypeError); | 316 assertThrows('Array.prototype.find.apply({}, {}, [])', TypeError); |
297 assertThrows('Array.prototype.find.apply({}, [], [])', TypeError); | 317 assertThrows('Array.prototype.find.apply({}, [], [])', TypeError); |
298 assertThrows('Array.prototype.find.apply({}, /\d+/, [])', TypeError); | 318 assertThrows('Array.prototype.find.apply({}, /\d+/, [])', TypeError); |
OLD | NEW |