OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
649 ownPtr2 = set.take(ptr2); | 649 ownPtr2 = set.take(ptr2); |
650 EXPECT_TRUE(set.isEmpty()); | 650 EXPECT_TRUE(set.isEmpty()); |
651 } | 651 } |
652 EXPECT_FALSE(deleted1); | 652 EXPECT_FALSE(deleted1); |
653 EXPECT_FALSE(deleted2); | 653 EXPECT_FALSE(deleted2); |
654 | 654 |
655 EXPECT_EQ(ptr1, ownPtr1); | 655 EXPECT_EQ(ptr1, ownPtr1); |
656 EXPECT_EQ(ptr2, ownPtr2); | 656 EXPECT_EQ(ptr2, ownPtr2); |
657 } | 657 } |
658 | 658 |
659 TEST(ListHashSetTest, Swap) | |
Erik Corry
2014/08/14 11:06:12
I think it makes sense to do this with templates,
zerny-chromium
2014/08/14 11:14:55
Done.
| |
660 { | |
661 int num = 10; | |
662 ListHashSet<int> set0; | |
663 ListHashSet<int> set1; | |
664 ListHashSet<int> set2; | |
665 for (int i = 0; i < num; ++i) { | |
666 set1.add(i + 1); | |
667 set2.add(num - i); | |
668 } | |
669 | |
670 ListHashSet<int>::iterator it1 = set1.begin(); | |
671 ListHashSet<int>::iterator it2 = set2.begin(); | |
672 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
673 EXPECT_EQ(*it1, i + 1); | |
674 EXPECT_EQ(*it2, num - i); | |
675 } | |
676 EXPECT_EQ(set0.begin(), set0.end()); | |
677 EXPECT_EQ(it1, set1.end()); | |
678 EXPECT_EQ(it2, set2.end()); | |
679 | |
680 // Shift sets: 2->1, 1->0, 0->2 | |
681 set1.swap(set2); // Swap with non-empty sets. | |
682 set0.swap(set2); // Swap with an empty set. | |
683 | |
684 it1 = set0.begin(); | |
685 it2 = set1.begin(); | |
686 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
687 EXPECT_EQ(*it1, i + 1); | |
688 EXPECT_EQ(*it2, num - i); | |
689 } | |
690 EXPECT_EQ(it1, set0.end()); | |
691 EXPECT_EQ(it2, set1.end()); | |
692 EXPECT_EQ(set2.begin(), set2.end()); | |
693 | |
694 int removedIndex = num >> 1; | |
695 set0.remove(removedIndex + 1); | |
696 set1.remove(num - removedIndex); | |
697 | |
698 it1 = set0.begin(); | |
699 it2 = set1.begin(); | |
700 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
701 if (i == removedIndex) | |
702 ++i; | |
703 EXPECT_EQ(*it1, i + 1); | |
704 EXPECT_EQ(*it2, num - i); | |
705 } | |
706 EXPECT_EQ(it1, set0.end()); | |
707 EXPECT_EQ(it2, set1.end()); | |
708 } | |
709 | |
710 TEST(LinkedHashSetTest, Swap) | |
711 { | |
712 int num = 10; | |
713 LinkedHashSet<int> set0; | |
714 LinkedHashSet<int> set1; | |
715 LinkedHashSet<int> set2; | |
716 for (int i = 0; i < num; ++i) { | |
717 set1.add(i + 1); | |
718 set2.add(num - i); | |
719 } | |
720 | |
721 LinkedHashSet<int>::iterator it1 = set1.begin(); | |
722 LinkedHashSet<int>::iterator it2 = set2.begin(); | |
723 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
724 EXPECT_EQ(*it1, i + 1); | |
725 EXPECT_EQ(*it2, num - i); | |
726 } | |
727 EXPECT_EQ(set0.begin(), set0.end()); | |
728 EXPECT_EQ(it1, set1.end()); | |
729 EXPECT_EQ(it2, set2.end()); | |
730 | |
731 // Shift sets: 2->1, 1->0, 0->2 | |
732 set1.swap(set2); // Swap with non-empty sets. | |
733 set0.swap(set2); // Swap with an empty set. | |
734 | |
735 it1 = set0.begin(); | |
736 it2 = set1.begin(); | |
737 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
738 EXPECT_EQ(*it1, i + 1); | |
739 EXPECT_EQ(*it2, num - i); | |
740 } | |
741 EXPECT_EQ(it1, set0.end()); | |
742 EXPECT_EQ(it2, set1.end()); | |
743 EXPECT_EQ(set2.begin(), set2.end()); | |
744 | |
745 int removedIndex = num >> 1; | |
746 set0.remove(removedIndex + 1); | |
747 set1.remove(num - removedIndex); | |
748 | |
749 it1 = set0.begin(); | |
750 it2 = set1.begin(); | |
751 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
752 if (i == removedIndex) | |
753 ++i; | |
754 EXPECT_EQ(*it1, i + 1); | |
755 EXPECT_EQ(*it2, num - i); | |
756 } | |
757 EXPECT_EQ(it1, set0.end()); | |
758 EXPECT_EQ(it2, set1.end()); | |
759 } | |
760 | |
659 } // namespace | 761 } // namespace |
OLD | NEW |