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 TestMapForEachInvalidTypes() { | |
669 assertThrows(function() { | |
670 Map.prototype.map.forEach.call({}); | |
671 }, TypeError); | |
672 | |
673 var map = new Map(); | |
674 assertThrows(function() { | |
675 map.forEach({}); | |
676 }, TypeError); | |
677 })(); | |
678 | |
679 | |
680 (function TestMapForEach() { | |
681 var map = new Map(); | |
682 map.set(0, 'a'); | |
683 map.set(1, 'b'); | |
684 map.set(2, 'c'); | |
685 | |
686 var buffer = []; | |
687 var receiver = {}; | |
688 map.forEach(function(v, k, m) { | |
689 assertEquals(map, m); | |
690 assertEquals(this, receiver); | |
691 buffer.push(k, v); | |
692 if (k === 0) { | |
693 map.delete(1); | |
694 map.set(3, 'd'); | |
695 map.set(4, 'e'); | |
696 map.set(5, 'f'); | |
697 } else if (k === 2) { | |
698 map.set(1, 'B'); | |
699 map.delete(4); | |
700 } | |
701 }, receiver); | |
702 | |
703 assertArrayEquals([0, 'a', 2, 'c', 3, 'd', 5, 'f', 1, 'B'], buffer); | |
704 })(); | |
705 | |
706 | |
707 (function TestMapForEachAddAtEnd() { | |
708 var map = new Map(); | |
709 map.set(0, 'a'); | |
710 map.set(1, 'b'); | |
711 | |
712 var buffer = []; | |
713 map.forEach(function(v, k) { | |
714 buffer.push(k, v); | |
715 if (k === 1) { | |
716 map.set(2, 'c'); | |
717 } | |
718 }); | |
719 | |
720 assertArrayEquals([0, 'a', 1, 'b', 2, 'c'], buffer); | |
721 })(); | |
722 | |
723 | |
724 (function TestMapForEachDeleteNext() { | |
725 var map = new Map(); | |
726 map.set(0, 'a'); | |
727 map.set(1, 'b'); | |
728 map.set(2, 'c'); | |
729 | |
730 var buffer = []; | |
731 map.forEach(function(v, k) { | |
732 buffer.push(k, v); | |
733 if (k === 1) { | |
734 map.delete(2); | |
735 } | |
736 }); | |
737 | |
738 assertArrayEquals([0, 'a', 1, 'b'], buffer); | |
739 })(); | |
740 | |
741 | |
742 (function TestSetForEachDeleteVisitedAndAddAgain() { | |
743 var map = new Map(); | |
744 map.set(0, 'a'); | |
745 map.set(1, 'b'); | |
746 map.set(2, 'c'); | |
747 | |
748 var buffer = []; | |
749 map.forEach(function(v, k) { | |
750 buffer.push(k, v); | |
751 if (k === 1) { | |
752 map.delete(0); | |
753 } else if (k === 2) { | |
754 map.set(0, 'a'); | |
755 } | |
756 }); | |
757 | |
758 assertArrayEquals([0, 'a', 1, 'b', 2, 'c', 0, 'a'], buffer); | |
759 })(); | |
760 | |
761 | |
762 (function TestMapForEachClear() { | |
763 var map = new Map(); | |
764 map.set(0, 'a'); | |
765 map.set(1, 'b'); | |
766 map.set(2, 'c'); | |
767 | |
768 var buffer = []; | |
769 map.forEach(function(v, k) { | |
770 buffer.push(k, v); | |
771 if (k === 0) { | |
772 map.clear(); | |
773 map.set(3, 'd'); | |
774 map.set(4, 'e'); | |
775 } | |
776 }); | |
777 | |
778 assertArrayEquals([0, 'a', 3, 'd', 4, 'e'], buffer); | |
779 })(); | |
780 | |
781 | |
782 (function TestMapForEachNested() { | |
783 var map = new Map(); | |
784 map.set(0, 'a'); | |
785 map.set(1, 'b'); | |
786 map.set(2, 'c'); | |
787 | |
788 var buffer = []; | |
789 map.forEach(function(v, k) { | |
790 buffer.push(k, v); | |
791 map.forEach(function(v, k) { | |
792 buffer.push(k, v); | |
793 if (k === 0) { | |
794 map.delete(1); | |
795 } | |
796 }); | |
797 }); | |
798 | |
799 assertArrayEquals([0, 'a', 0, 'a', 2, 'c', 2, 'c', 0, 'a', 2, 'c'], buffer); | |
800 })(); | |
801 | |
802 | |
803 (function TestMapForEachEarlyExit() { | |
804 var map = new Map(); | |
805 map.set(0, 'a'); | |
806 map.set(1, 'b'); | |
807 map.set(2, 'c'); | |
808 | |
809 var buffer = []; | |
810 var ex = {}; | |
811 try { | |
812 map.forEach(function(v, k) { | |
813 buffer.push(k, v); | |
814 throw ex; | |
815 }); | |
816 } catch (e) { | |
817 assertEquals(ex, e); | |
818 } | |
819 assertArrayEquals([0, 'a'], buffer); | |
820 })(); | |
Michael Starzinger
2014/04/15 16:18:14
Also can we have a test-case that triggers a GC (i
| |
OLD | NEW |