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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-typed-arrays | |
29 | |
30 // ArrayBuffer | 28 // ArrayBuffer |
31 | 29 |
32 function TestByteLength(param, expectedByteLength) { | 30 function TestByteLength(param, expectedByteLength) { |
33 var ab = new ArrayBuffer(param); | 31 var ab = new ArrayBuffer(param); |
34 assertSame(expectedByteLength, ab.byteLength); | 32 assertSame(expectedByteLength, ab.byteLength); |
35 } | 33 } |
36 | 34 |
37 function TestArrayBufferCreation() { | 35 function TestArrayBufferCreation() { |
38 TestByteLength(1, 1); | 36 TestByteLength(1, 1); |
39 TestByteLength(256, 256); | 37 TestByteLength(256, 256); |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 // Invalid source | 448 // Invalid source |
451 var a = new Uint16Array(50); | 449 var a = new Uint16Array(50); |
452 assertThrows(function() { a.set(0) }, TypeError); | 450 assertThrows(function() { a.set(0) }, TypeError); |
453 assertThrows(function() { a.set({}) }, TypeError); | 451 assertThrows(function() { a.set({}) }, TypeError); |
454 assertThrows(function() { a.set.call({}) }, TypeError); | 452 assertThrows(function() { a.set.call({}) }, TypeError); |
455 assertThrows(function() { a.set.call([]) }, TypeError); | 453 assertThrows(function() { a.set.call([]) }, TypeError); |
456 } | 454 } |
457 | 455 |
458 TestTypedArraySet(); | 456 TestTypedArraySet(); |
459 | 457 |
| 458 // DataView |
| 459 function TestDataViewConstructor() { |
| 460 var ab = new ArrayBuffer(256); |
| 461 |
| 462 var d1 = new DataView(ab, 1, 255); |
| 463 assertSame(ab, d1.buffer); |
| 464 assertSame(1, d1.byteOffset); |
| 465 assertSame(255, d1.byteLength); |
| 466 |
| 467 var d2 = new DataView(ab, 2); |
| 468 assertSame(ab, d2.buffer); |
| 469 assertSame(2, d2.byteOffset); |
| 470 assertSame(254, d2.byteLength); |
| 471 |
| 472 var d3 = new DataView(ab); |
| 473 assertSame(ab, d3.buffer); |
| 474 assertSame(0, d3.byteOffset); |
| 475 assertSame(256, d3.byteLength); |
| 476 |
| 477 var d3a = new DataView(ab, 1, 0); |
| 478 assertSame(ab, d3a.buffer); |
| 479 assertSame(1, d3a.byteOffset); |
| 480 assertSame(0, d3a.byteLength); |
| 481 |
| 482 var d3b = new DataView(ab, 256, 0); |
| 483 assertSame(ab, d3b.buffer); |
| 484 assertSame(256, d3b.byteOffset); |
| 485 assertSame(0, d3b.byteLength); |
| 486 |
| 487 var d3c = new DataView(ab, 256); |
| 488 assertSame(ab, d3c.buffer); |
| 489 assertSame(256, d3c.byteOffset); |
| 490 assertSame(0, d3c.byteLength); |
| 491 |
| 492 // weird args |
| 493 var d4 = new DataView(ab, -1); |
| 494 assertSame(ab, d4.buffer); |
| 495 assertSame(0, d4.byteOffset); |
| 496 assertSame(256, d4.byteLength); |
| 497 |
| 498 var d5 = new DataView(ab, 1, -1); |
| 499 assertSame(ab, d5.buffer); |
| 500 assertSame(1, d5.byteOffset); |
| 501 assertSame(0, d5.byteLength); |
| 502 |
| 503 var d6 = new DataView(ab, 1, 3.1415926); |
| 504 assertSame(ab, d6.buffer); |
| 505 assertSame(1, d6.byteOffset); |
| 506 assertSame(3, d6.byteLength); |
| 507 |
| 508 |
| 509 // error cases |
| 510 assertThrows(function() { new DataView(); }, TypeError); |
| 511 assertThrows(function() { new DataView([]); }, TypeError); |
| 512 assertThrows(function() { new DataView(ab, 257); }, RangeError); |
| 513 assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError); |
| 514 } |
| 515 |
| 516 TestDataViewConstructor(); |
| 517 |
| 518 function TestDataViewPropertyTypeChecks() { |
| 519 var a = new DataView(new ArrayBuffer(10)); |
| 520 function CheckProperty(name) { |
| 521 var d = Object.getOwnPropertyDescriptor(DataView.prototype, name); |
| 522 var o = {} |
| 523 assertThrows(function() {d.get.call(o);}, TypeError); |
| 524 d.get.call(a); // shouldn't throw |
| 525 } |
| 526 |
| 527 CheckProperty("buffer"); |
| 528 CheckProperty("byteOffset"); |
| 529 CheckProperty("byteLength"); |
| 530 } |
| 531 |
| 532 |
| 533 TestDataViewPropertyTypeChecks(); |
| 534 |
460 // General tests for properties | 535 // General tests for properties |
461 | 536 |
462 // Test property attribute [[Enumerable]] | 537 // Test property attribute [[Enumerable]] |
463 function TestEnumerable(func, obj) { | 538 function TestEnumerable(func, obj) { |
464 function props(x) { | 539 function props(x) { |
465 var array = []; | 540 var array = []; |
466 for (var p in x) array.push(p); | 541 for (var p in x) array.push(p); |
467 return array.sort(); | 542 return array.sort(); |
468 } | 543 } |
469 assertArrayEquals([], props(func)); | 544 assertArrayEquals([], props(func)); |
470 assertArrayEquals([], props(func.prototype)); | 545 assertArrayEquals([], props(func.prototype)); |
471 if (obj) | 546 if (obj) |
472 assertArrayEquals([], props(obj)); | 547 assertArrayEquals([], props(obj)); |
473 } | 548 } |
474 TestEnumerable(ArrayBuffer, new ArrayBuffer()); | 549 TestEnumerable(ArrayBuffer, new ArrayBuffer()); |
475 for(i = 0; i < typedArrayConstructors.lenght; i++) { | 550 for(i = 0; i < typedArrayConstructors.lenght; i++) { |
476 TestEnumerable(typedArrayConstructors[i]); | 551 TestEnumerable(typedArrayConstructors[i]); |
477 } | 552 } |
| 553 TestEnumerable(DataView, new DataView(new ArrayBuffer())); |
478 | 554 |
479 // Test arbitrary properties on ArrayBuffer | 555 // Test arbitrary properties on ArrayBuffer |
480 function TestArbitrary(m) { | 556 function TestArbitrary(m) { |
481 function TestProperty(map, property, value) { | 557 function TestProperty(map, property, value) { |
482 map[property] = value; | 558 map[property] = value; |
483 assertEquals(value, map[property]); | 559 assertEquals(value, map[property]); |
484 } | 560 } |
485 for (var i = 0; i < 20; i++) { | 561 for (var i = 0; i < 20; i++) { |
486 TestProperty(m, i, 'val' + i); | 562 TestProperty(m, i, 'val' + i); |
487 TestProperty(m, 'foo' + i, 'bar' + i); | 563 TestProperty(m, 'foo' + i, 'bar' + i); |
488 } | 564 } |
489 } | 565 } |
490 TestArbitrary(new ArrayBuffer(256)); | 566 TestArbitrary(new ArrayBuffer(256)); |
491 for(i = 0; i < typedArrayConstructors.lenght; i++) { | 567 for(i = 0; i < typedArrayConstructors.lenght; i++) { |
492 TestArbitary(new typedArrayConstructors[i](10)); | 568 TestArbitary(new typedArrayConstructors[i](10)); |
493 } | 569 } |
494 | 570 TestArbitrary(new DataView(new ArrayBuffer(256))); |
495 | 571 |
496 | 572 |
497 // Test direct constructor call | 573 // Test direct constructor call |
498 assertTrue(ArrayBuffer() instanceof ArrayBuffer); | 574 assertTrue(ArrayBuffer() instanceof ArrayBuffer); |
| 575 assertTrue(DataView(new ArrayBuffer()) instanceof DataView); |
OLD | NEW |