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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 assertFalse(2 in x,"longlength third"); | 207 assertFalse(2 in x,"longlength third"); |
208 } | 208 } |
209 | 209 |
210 TestNonArrayLongerLength(4); | 210 TestNonArrayLongerLength(4); |
211 TestNonArrayLongerLength(10); | 211 TestNonArrayLongerLength(10); |
212 TestNonArrayLongerLength(1000); | 212 TestNonArrayLongerLength(1000); |
213 TestNonArrayLongerLength(500000); | 213 TestNonArrayLongerLength(500000); |
214 TestNonArrayLongerLength(Math.pow(2,32) - 1); | 214 TestNonArrayLongerLength(Math.pow(2,32) - 1); |
215 | 215 |
216 | 216 |
| 217 function TestNonArrayWithAccessors() { |
| 218 // Regression test for issue 346, more info at URL |
| 219 // http://code.google.com/p/v8/issues/detail?id=346 |
| 220 // Reported by nth10sd, test based on this report. |
| 221 var x = {}; |
| 222 x[0] = 42; |
| 223 x.__defineGetter__("1", function(){return this.foo;}); |
| 224 x.__defineSetter__("1", function(val){this.foo = val;}); |
| 225 x[1] = 49 |
| 226 x[3] = 37; |
| 227 x.length = 4; |
| 228 Array.prototype.sort.call(x); |
| 229 // Behavior of sort with accessors is undefined. This accessor is |
| 230 // well-behaved (acts like a normal property), so it should work. |
| 231 assertEquals(4, x.length, "sortaccessors length"); |
| 232 assertEquals(37, x[0], "sortaccessors first"); |
| 233 assertEquals(42, x[1], "sortaccessors second"); |
| 234 assertEquals(49, x[2], "sortaccessors third") |
| 235 assertFalse(3 in x, "sortaccessors fourth"); |
| 236 } |
| 237 |
| 238 TestNonArrayWithAccessors(); |
| 239 |
| 240 |
217 function TestInheritedElementSort(depth) { | 241 function TestInheritedElementSort(depth) { |
218 var length = depth * 2 + 3; | 242 var length = depth * 2 + 3; |
219 var obj = {length: length}; | 243 var obj = {length: length}; |
220 obj[depth * 2 + 1] = 0; | 244 obj[depth * 2 + 1] = 0; |
221 for (var i = 0; i < depth; i++) { | 245 for (var i = 0; i < depth; i++) { |
222 var newObj = {}; | 246 var newObj = {}; |
223 newObj.__proto__ = obj; | 247 newObj.__proto__ = obj; |
224 obj[i] = undefined; | 248 obj[i] = undefined; |
225 obj[i + depth + 1] = depth - i; | 249 obj[i + depth + 1] = depth - i; |
226 obj = newObj; | 250 obj = newObj; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 285 |
262 Array.prototype.sort.call(y); | 286 Array.prototype.sort.call(y); |
263 | 287 |
264 assertEquals(length, y.length, name +"length"); | 288 assertEquals(length, y.length, name +"length"); |
265 | 289 |
266 for (var i = 0; i < 10; i++) { | 290 for (var i = 0; i < 10; i++) { |
267 assertTrue(y.hasOwnProperty(i), name + "hasvalue" + i); | 291 assertTrue(y.hasOwnProperty(i), name + "hasvalue" + i); |
268 assertEquals(i, y[i], name + "value" + i); | 292 assertEquals(i, y[i], name + "value" + i); |
269 } | 293 } |
270 for (var i = 10; i < length; i++) { | 294 for (var i = 10; i < length; i++) { |
271 assertEquals(x.hasOwnProperty(i), y.hasOwnProperty(i), | 295 assertEquals(x.hasOwnProperty(i), y.hasOwnProperty(i), |
272 name + "hasundef" + i); | 296 name + "hasundef" + i); |
273 assertEquals(undefined, y[i], name+"undefined"+i); | 297 assertEquals(undefined, y[i], name+"undefined"+i); |
274 if (x.hasOwnProperty(i)) { | 298 if (x.hasOwnProperty(i)) { |
275 assertTrue(0 == i % (2 * scale), name + "new_x" + i); | 299 assertTrue(0 == i % (2 * scale), name + "new_x" + i); |
276 } | 300 } |
277 } | 301 } |
278 } | 302 } |
279 | 303 |
280 TestSparseInheritedElementSort(10); | 304 TestSparseInheritedElementSort(10); |
281 TestSparseInheritedElementSort(100); | 305 TestSparseInheritedElementSort(100); |
282 TestSparseInheritedElementSort(1000); | 306 TestSparseInheritedElementSort(1000); |
283 | 307 |
284 function TestSpecialCasesInheritedElementSort() { | 308 function TestSpecialCasesInheritedElementSort() { |
285 | 309 |
286 var x = { | 310 var x = { |
287 1:"d1", | 311 1:"d1", |
288 2:"c1", | 312 2:"c1", |
289 3:"b1", | 313 3:"b1", |
290 4: undefined, | 314 4: undefined, |
291 __proto__: { | 315 __proto__: { |
292 length: 10000, | 316 length: 10000, |
293 1: "e2", | 317 1: "e2", |
294 10: "a2", | 318 10: "a2", |
295 100: "b2", | 319 100: "b2", |
296 1000: "c2", | 320 1000: "c2", |
297 2000: undefined, | 321 2000: undefined, |
298 8000: "d2", | 322 8000: "d2", |
299 12000: "XX", | 323 12000: "XX", |
300 __proto__: { | 324 __proto__: { |
301 0: "e3", | 325 0: "e3", |
302 1: "d3", | 326 1: "d3", |
303 2: "c3", | 327 2: "c3", |
304 3: "b3", | 328 3: "b3", |
305 4: "f3", | 329 4: "f3", |
306 5: "a3", | 330 5: "a3", |
307 6: undefined, | 331 6: undefined, |
308 } | 332 } |
309 } | 333 } |
310 }; | 334 }; |
311 Array.prototype.sort.call(x); | 335 Array.prototype.sort.call(x); |
312 | 336 |
313 var name = "SpecialInherit-"; | 337 var name = "SpecialInherit-"; |
314 | 338 |
315 assertEquals(10000, x.length, name + "length"); | 339 assertEquals(10000, x.length, name + "length"); |
316 var sorted = ["a2", "a3", "b1", "b2", "c1", "c2", "d1", "d2", "e3", | 340 var sorted = ["a2", "a3", "b1", "b2", "c1", "c2", "d1", "d2", "e3", |
317 undefined, undefined, undefined]; | 341 undefined, undefined, undefined]; |
318 for (var i = 0; i < sorted.length; i++) { | 342 for (var i = 0; i < sorted.length; i++) { |
319 assertTrue(x.hasOwnProperty(i), name + "has" + i) | 343 assertTrue(x.hasOwnProperty(i), name + "has" + i) |
320 assertEquals(sorted[i], x[i], name + i); | 344 assertEquals(sorted[i], x[i], name + i); |
321 } | 345 } |
322 assertFalse(x.hasOwnProperty(sorted.length), name + "haspost"); | 346 assertFalse(x.hasOwnProperty(sorted.length), name + "haspost"); |
323 assertFalse(sorted.length in x, name + "haspost2"); | 347 assertFalse(sorted.length in x, name + "haspost2"); |
324 | |
325 assertTrue(x.hasOwnProperty(10), name + "hasundefined10"); | 348 assertTrue(x.hasOwnProperty(10), name + "hasundefined10"); |
326 assertEquals(undefined, x[10], name + "undefined10"); | 349 assertEquals(undefined, x[10], name + "undefined10"); |
327 assertTrue(x.hasOwnProperty(100), name + "hasundefined100"); | 350 assertTrue(x.hasOwnProperty(100), name + "hasundefined100"); |
328 assertEquals(undefined, x[100], name + "undefined100"); | 351 assertEquals(undefined, x[100], name + "undefined100"); |
329 assertTrue(x.hasOwnProperty(1000), name + "hasundefined1000"); | 352 assertTrue(x.hasOwnProperty(1000), name + "hasundefined1000"); |
330 assertEquals(undefined, x[1000], name + "undefined1000"); | 353 assertEquals(undefined, x[1000], name + "undefined1000"); |
331 assertTrue(x.hasOwnProperty(2000), name + "hasundefined2000"); | 354 assertTrue(x.hasOwnProperty(2000), name + "hasundefined2000"); |
332 assertEquals(undefined, x[2000], name + "undefined2000"); | 355 assertEquals(undefined, x[2000], name + "undefined2000"); |
333 assertTrue(x.hasOwnProperty(8000), name + "hasundefined8000"); | 356 assertTrue(x.hasOwnProperty(8000), name + "hasundefined8000"); |
334 assertEquals(undefined, x[8000], name + "undefined8000"); | 357 assertEquals(undefined, x[8000], name + "undefined8000"); |
335 | |
336 assertFalse(x.hasOwnProperty(12000), name + "has12000"); | 358 assertFalse(x.hasOwnProperty(12000), name + "has12000"); |
337 assertEquals("XX", x[12000], name + "XX12000"); | 359 assertEquals("XX", x[12000], name + "XX12000"); |
338 | |
339 } | 360 } |
340 | 361 |
341 TestSpecialCasesInheritedElementSort(); | 362 TestSpecialCasesInheritedElementSort(); |
342 | |
OLD | NEW |