OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 (function TestMinusZeroMap() { | 500 (function TestMinusZeroMap() { |
501 var m = new Map(); | 501 var m = new Map(); |
502 m.set(0, 'plus'); | 502 m.set(0, 'plus'); |
503 m.set(-0, 'minus'); | 503 m.set(-0, 'minus'); |
504 assertEquals(1, m.size); | 504 assertEquals(1, m.size); |
505 assertTrue(m.has(0)); | 505 assertTrue(m.has(0)); |
506 assertTrue(m.has(-0)); | 506 assertTrue(m.has(-0)); |
507 assertEquals('minus', m.get(0)); | 507 assertEquals('minus', m.get(0)); |
508 assertEquals('minus', m.get(-0)); | 508 assertEquals('minus', m.get(-0)); |
509 })(); | 509 })(); |
| 510 |
| 511 |
| 512 (function TestSetForEachInvalidTypes() { |
| 513 assertThrows(function() { |
| 514 Set.prototype.set.forEach.call({}); |
| 515 }, TypeError); |
| 516 |
| 517 var set = new Set(); |
| 518 assertThrows(function() { |
| 519 set.forEach({}); |
| 520 }, TypeError); |
| 521 })(); |
| 522 |
| 523 |
| 524 (function TestSetForEach() { |
| 525 var set = new Set(); |
| 526 set.add('a'); |
| 527 set.add('b'); |
| 528 set.add('c'); |
| 529 |
| 530 var buffer = ''; |
| 531 var receiver = {}; |
| 532 set.forEach(function(v, k, s) { |
| 533 assertSame(v, k); |
| 534 assertSame(set, s); |
| 535 assertSame(this, receiver); |
| 536 buffer += v; |
| 537 if (v === 'a') { |
| 538 set.delete('b'); |
| 539 set.add('d'); |
| 540 set.add('e'); |
| 541 set.add('f'); |
| 542 } else if (v === 'c') { |
| 543 set.add('b'); |
| 544 set.delete('e'); |
| 545 } |
| 546 }, receiver); |
| 547 |
| 548 assertEquals('acdfb', buffer); |
| 549 })(); |
| 550 |
| 551 |
| 552 (function TestSetForEachAddAtEnd() { |
| 553 var set = new Set(); |
| 554 set.add('a'); |
| 555 set.add('b'); |
| 556 |
| 557 var buffer = ''; |
| 558 set.forEach(function(v) { |
| 559 buffer += v; |
| 560 if (v === 'b') { |
| 561 set.add('c'); |
| 562 } |
| 563 }); |
| 564 |
| 565 assertEquals('abc', buffer); |
| 566 })(); |
| 567 |
| 568 |
| 569 (function TestSetForEachDeleteNext() { |
| 570 var set = new Set(); |
| 571 set.add('a'); |
| 572 set.add('b'); |
| 573 set.add('c'); |
| 574 |
| 575 var buffer = ''; |
| 576 set.forEach(function(v) { |
| 577 buffer += v; |
| 578 if (v === 'b') { |
| 579 set.delete('c'); |
| 580 } |
| 581 }); |
| 582 |
| 583 assertEquals('ab', buffer); |
| 584 })(); |
| 585 |
| 586 |
| 587 (function TestSetForEachDeleteVisitedAndAddAgain() { |
| 588 var set = new Set(); |
| 589 set.add('a'); |
| 590 set.add('b'); |
| 591 set.add('c'); |
| 592 |
| 593 var buffer = ''; |
| 594 set.forEach(function(v) { |
| 595 buffer += v; |
| 596 if (v === 'b') { |
| 597 set.delete('a'); |
| 598 } else if (v === 'c') { |
| 599 set.add('a'); |
| 600 } |
| 601 }); |
| 602 |
| 603 assertEquals('abca', buffer); |
| 604 })(); |
| 605 |
| 606 |
| 607 (function TestSetForEachClear() { |
| 608 var set = new Set(); |
| 609 set.add('a'); |
| 610 set.add('b'); |
| 611 set.add('c'); |
| 612 |
| 613 var buffer = ''; |
| 614 set.forEach(function(v) { |
| 615 buffer += v; |
| 616 if (v === 'a') { |
| 617 set.clear(); |
| 618 set.add('d'); |
| 619 set.add('e'); |
| 620 } |
| 621 }); |
| 622 |
| 623 assertEquals('ade', buffer); |
| 624 })(); |
| 625 |
| 626 |
| 627 (function TestSetForEachNested() { |
| 628 var set = new Set(); |
| 629 set.add('a'); |
| 630 set.add('b'); |
| 631 set.add('c'); |
| 632 |
| 633 var buffer = ''; |
| 634 set.forEach(function(v) { |
| 635 buffer += v; |
| 636 set.forEach(function(v) { |
| 637 buffer += v; |
| 638 if (v === 'a') { |
| 639 set.delete('b'); |
| 640 } |
| 641 }); |
| 642 }); |
| 643 |
| 644 assertEquals('aaccac', buffer); |
| 645 })(); |
| 646 |
| 647 |
| 648 (function TestSetForEachEarlyExit() { |
| 649 var set = new Set(); |
| 650 set.add('a'); |
| 651 set.add('b'); |
| 652 set.add('c'); |
| 653 |
| 654 var buffer = ''; |
| 655 var ex = {}; |
| 656 try { |
| 657 set.forEach(function(v) { |
| 658 buffer += v; |
| 659 throw ex; |
| 660 }); |
| 661 } catch (e) { |
| 662 assertEquals(ex, e); |
| 663 } |
| 664 assertEquals('a', buffer); |
| 665 })(); |
| 666 |
| 667 |
| 668 (function TestSetForEachGC() { |
| 669 var set = new Set(); |
| 670 for (var i = 0; i < 100; i++) { |
| 671 set.add(i); |
| 672 } |
| 673 |
| 674 var accumulated = 0; |
| 675 set.forEach(function(v) { |
| 676 accumulated += v; |
| 677 if (v % 10 === 0) { |
| 678 gc(); |
| 679 } |
| 680 }); |
| 681 assertEquals(4950, accumulated); |
| 682 })(); |
| 683 |
| 684 (function TestMapForEachInvalidTypes() { |
| 685 assertThrows(function() { |
| 686 Map.prototype.map.forEach.call({}); |
| 687 }, TypeError); |
| 688 |
| 689 var map = new Map(); |
| 690 assertThrows(function() { |
| 691 map.forEach({}); |
| 692 }, TypeError); |
| 693 })(); |
| 694 |
| 695 |
| 696 (function TestMapForEach() { |
| 697 var map = new Map(); |
| 698 map.set(0, 'a'); |
| 699 map.set(1, 'b'); |
| 700 map.set(2, 'c'); |
| 701 |
| 702 var buffer = []; |
| 703 var receiver = {}; |
| 704 map.forEach(function(v, k, m) { |
| 705 assertEquals(map, m); |
| 706 assertEquals(this, receiver); |
| 707 buffer.push(k, v); |
| 708 if (k === 0) { |
| 709 map.delete(1); |
| 710 map.set(3, 'd'); |
| 711 map.set(4, 'e'); |
| 712 map.set(5, 'f'); |
| 713 } else if (k === 2) { |
| 714 map.set(1, 'B'); |
| 715 map.delete(4); |
| 716 } |
| 717 }, receiver); |
| 718 |
| 719 assertArrayEquals([0, 'a', 2, 'c', 3, 'd', 5, 'f', 1, 'B'], buffer); |
| 720 })(); |
| 721 |
| 722 |
| 723 (function TestMapForEachAddAtEnd() { |
| 724 var map = new Map(); |
| 725 map.set(0, 'a'); |
| 726 map.set(1, 'b'); |
| 727 |
| 728 var buffer = []; |
| 729 map.forEach(function(v, k) { |
| 730 buffer.push(k, v); |
| 731 if (k === 1) { |
| 732 map.set(2, 'c'); |
| 733 } |
| 734 }); |
| 735 |
| 736 assertArrayEquals([0, 'a', 1, 'b', 2, 'c'], buffer); |
| 737 })(); |
| 738 |
| 739 |
| 740 (function TestMapForEachDeleteNext() { |
| 741 var map = new Map(); |
| 742 map.set(0, 'a'); |
| 743 map.set(1, 'b'); |
| 744 map.set(2, 'c'); |
| 745 |
| 746 var buffer = []; |
| 747 map.forEach(function(v, k) { |
| 748 buffer.push(k, v); |
| 749 if (k === 1) { |
| 750 map.delete(2); |
| 751 } |
| 752 }); |
| 753 |
| 754 assertArrayEquals([0, 'a', 1, 'b'], buffer); |
| 755 })(); |
| 756 |
| 757 |
| 758 (function TestSetForEachDeleteVisitedAndAddAgain() { |
| 759 var map = new Map(); |
| 760 map.set(0, 'a'); |
| 761 map.set(1, 'b'); |
| 762 map.set(2, 'c'); |
| 763 |
| 764 var buffer = []; |
| 765 map.forEach(function(v, k) { |
| 766 buffer.push(k, v); |
| 767 if (k === 1) { |
| 768 map.delete(0); |
| 769 } else if (k === 2) { |
| 770 map.set(0, 'a'); |
| 771 } |
| 772 }); |
| 773 |
| 774 assertArrayEquals([0, 'a', 1, 'b', 2, 'c', 0, 'a'], buffer); |
| 775 })(); |
| 776 |
| 777 |
| 778 (function TestMapForEachClear() { |
| 779 var map = new Map(); |
| 780 map.set(0, 'a'); |
| 781 map.set(1, 'b'); |
| 782 map.set(2, 'c'); |
| 783 |
| 784 var buffer = []; |
| 785 map.forEach(function(v, k) { |
| 786 buffer.push(k, v); |
| 787 if (k === 0) { |
| 788 map.clear(); |
| 789 map.set(3, 'd'); |
| 790 map.set(4, 'e'); |
| 791 } |
| 792 }); |
| 793 |
| 794 assertArrayEquals([0, 'a', 3, 'd', 4, 'e'], buffer); |
| 795 })(); |
| 796 |
| 797 |
| 798 (function TestMapForEachNested() { |
| 799 var map = new Map(); |
| 800 map.set(0, 'a'); |
| 801 map.set(1, 'b'); |
| 802 map.set(2, 'c'); |
| 803 |
| 804 var buffer = []; |
| 805 map.forEach(function(v, k) { |
| 806 buffer.push(k, v); |
| 807 map.forEach(function(v, k) { |
| 808 buffer.push(k, v); |
| 809 if (k === 0) { |
| 810 map.delete(1); |
| 811 } |
| 812 }); |
| 813 }); |
| 814 |
| 815 assertArrayEquals([0, 'a', 0, 'a', 2, 'c', 2, 'c', 0, 'a', 2, 'c'], buffer); |
| 816 })(); |
| 817 |
| 818 |
| 819 (function TestMapForEachEarlyExit() { |
| 820 var map = new Map(); |
| 821 map.set(0, 'a'); |
| 822 map.set(1, 'b'); |
| 823 map.set(2, 'c'); |
| 824 |
| 825 var buffer = []; |
| 826 var ex = {}; |
| 827 try { |
| 828 map.forEach(function(v, k) { |
| 829 buffer.push(k, v); |
| 830 throw ex; |
| 831 }); |
| 832 } catch (e) { |
| 833 assertEquals(ex, e); |
| 834 } |
| 835 assertArrayEquals([0, 'a'], buffer); |
| 836 })(); |
| 837 |
| 838 |
| 839 (function TestMapForEachGC() { |
| 840 var map = new Map(); |
| 841 for (var i = 0; i < 100; i++) { |
| 842 map.set(i, i); |
| 843 } |
| 844 |
| 845 var accumulated = 0; |
| 846 map.forEach(function(v) { |
| 847 accumulated += v; |
| 848 if (v % 10 === 0) { |
| 849 gc(); |
| 850 } |
| 851 }); |
| 852 assertEquals(4950, accumulated); |
| 853 })(); |
OLD | NEW |