OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/containers/flat_tree.h" | 5 #include "base/containers/flat_tree.h" |
6 | 6 |
7 // Following tests are ported and extended tests from libcpp for std::set. | 7 // Following tests are ported and extended tests from libcpp for std::set. |
8 // They can be found here: | 8 // They can be found here: |
9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa
tive/set | 9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa
tive/set |
10 // | 10 // |
(...skipping 13 matching lines...) Expand all Loading... |
24 // * No tests with DefaultOnly. | 24 // * No tests with DefaultOnly. |
25 // Standard containers allocate each element in the separate node on the heap | 25 // Standard containers allocate each element in the separate node on the heap |
26 // and then manipulate these nodes. Flat containers store their elements in | 26 // and then manipulate these nodes. Flat containers store their elements in |
27 // contiguous memory and move them around, type is required to be movable. | 27 // contiguous memory and move them around, type is required to be movable. |
28 // * No tests for N3644. | 28 // * No tests for N3644. |
29 // This proposal suggests that all default constructed iterators compare | 29 // This proposal suggests that all default constructed iterators compare |
30 // equal. Currently we use std::vector iterators and they don't implement | 30 // equal. Currently we use std::vector iterators and they don't implement |
31 // this. | 31 // this. |
32 // * No tests with min_allocator and no tests counting allocations. | 32 // * No tests with min_allocator and no tests counting allocations. |
33 // Flat sets currently don't support allocators. | 33 // Flat sets currently don't support allocators. |
34 // * No tests for range insertion. Flat sets currently do not support this | |
35 // functionality. | |
36 | 34 |
| 35 #include <forward_list> |
| 36 #include <iterator> |
| 37 #include <list> |
37 #include <string> | 38 #include <string> |
38 #include <vector> | 39 #include <vector> |
39 | 40 |
40 #include "base/containers/container_test_utils.h" | 41 #include "base/containers/container_test_utils.h" |
41 #include "base/macros.h" | 42 #include "base/macros.h" |
42 #include "testing/gmock/include/gmock/gmock.h" | 43 #include "testing/gmock/include/gmock/gmock.h" |
43 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
44 | 45 |
45 namespace base { | 46 namespace base { |
46 namespace internal { | 47 namespace internal { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 164 |
164 using TreeWithStrangeCompare = flat_tree<int, | 165 using TreeWithStrangeCompare = flat_tree<int, |
165 int, | 166 int, |
166 GetKeyFromValueIdentity<int>, | 167 GetKeyFromValueIdentity<int>, |
167 NonDefaultConstructibleCompare>; | 168 NonDefaultConstructibleCompare>; |
168 | 169 |
169 using ::testing::ElementsAre; | 170 using ::testing::ElementsAre; |
170 | 171 |
171 } // namespace | 172 } // namespace |
172 | 173 |
| 174 TEST(FlatTree, IsMultipass) { |
| 175 static_assert(!is_multipass<std::istream_iterator<int>>(), |
| 176 "InputIterator is not multipass"); |
| 177 static_assert(!is_multipass<std::ostream_iterator<int>>(), |
| 178 "OutputIterator is not multipass"); |
| 179 |
| 180 static_assert(is_multipass<std::forward_list<int>::iterator>(), |
| 181 "ForwardIterator is multipass"); |
| 182 static_assert(is_multipass<std::list<int>::iterator>(), |
| 183 "BidirectionalIterator is multipass"); |
| 184 static_assert(is_multipass<std::vector<int>::iterator>(), |
| 185 "RandomAccessIterator is multipass"); |
| 186 } |
| 187 |
173 TEST(FlatTree, LastUnique) { | 188 TEST(FlatTree, LastUnique) { |
174 using Pair = std::pair<int, int>; | 189 using Pair = std::pair<int, int>; |
175 using Vect = std::vector<Pair>; | 190 using Vect = std::vector<Pair>; |
176 | 191 |
177 auto cmp = [](const Pair& lhs, const Pair& rhs) { | 192 auto cmp = [](const Pair& lhs, const Pair& rhs) { |
178 return lhs.first == rhs.first; | 193 return lhs.first == rhs.first; |
179 }; | 194 }; |
180 | 195 |
181 // Empty case. | 196 // Empty case. |
182 Vect empty; | 197 Vect empty; |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 EXPECT_EQ(std::prev(cont.end()), result); | 746 EXPECT_EQ(std::prev(cont.end()), result); |
732 EXPECT_EQ(3U, cont.size()); | 747 EXPECT_EQ(3U, cont.size()); |
733 EXPECT_EQ(3, result->data()); | 748 EXPECT_EQ(3, result->data()); |
734 | 749 |
735 result = cont.insert(cont.cend(), MoveOnlyInt(3)); | 750 result = cont.insert(cont.cend(), MoveOnlyInt(3)); |
736 EXPECT_EQ(std::prev(cont.end()), result); | 751 EXPECT_EQ(std::prev(cont.end()), result); |
737 EXPECT_EQ(3U, cont.size()); | 752 EXPECT_EQ(3U, cont.size()); |
738 EXPECT_EQ(3, result->data()); | 753 EXPECT_EQ(3, result->data()); |
739 } | 754 } |
740 | 755 |
| 756 // template <class InputIterator> |
| 757 // void insert(InputIterator first, InputIterator last); |
| 758 |
| 759 TEST(FlatTree, InsertIterIter) { |
| 760 struct GetKeyFromIntIntPair { |
| 761 int operator()(const std::pair<int, int>& p) const { return p.first; } |
| 762 }; |
| 763 |
| 764 using IntIntMap = |
| 765 flat_tree<int, IntPair, GetKeyFromIntIntPair, std::less<int>>; |
| 766 |
| 767 { |
| 768 IntIntMap cont; |
| 769 IntPair int_pairs[] = {{3, 1}, {1, 1}, {4, 1}, {2, 1}}; |
| 770 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 771 KEEP_FIRST_OF_DUPES); |
| 772 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 773 IntPair(4, 1))); |
| 774 } |
| 775 |
| 776 { |
| 777 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 778 std::vector<IntPair> int_pairs; |
| 779 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 780 KEEP_FIRST_OF_DUPES); |
| 781 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 782 IntPair(4, 1))); |
| 783 } |
| 784 |
| 785 { |
| 786 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 787 IntPair int_pairs[] = {{1, 1}}; |
| 788 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 789 KEEP_FIRST_OF_DUPES); |
| 790 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 791 IntPair(4, 1))); |
| 792 } |
| 793 |
| 794 { |
| 795 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 796 IntPair int_pairs[] = {{1, 2}}; |
| 797 cont.insert(std::begin(int_pairs), std::end(int_pairs), KEEP_LAST_OF_DUPES); |
| 798 EXPECT_THAT(cont, ElementsAre(IntPair(1, 2), IntPair(2, 1), IntPair(3, 1), |
| 799 IntPair(4, 1))); |
| 800 } |
| 801 |
| 802 { |
| 803 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 804 IntPair int_pairs[] = {{5, 1}}; |
| 805 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 806 KEEP_FIRST_OF_DUPES); |
| 807 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 808 IntPair(4, 1), IntPair(5, 1))); |
| 809 } |
| 810 |
| 811 { |
| 812 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 813 IntPair int_pairs[] = {{5, 1}}; |
| 814 cont.insert(std::begin(int_pairs), std::end(int_pairs), KEEP_LAST_OF_DUPES); |
| 815 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 816 IntPair(4, 1), IntPair(5, 1))); |
| 817 } |
| 818 |
| 819 { |
| 820 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 821 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}}; |
| 822 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 823 KEEP_FIRST_OF_DUPES); |
| 824 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 825 IntPair(4, 1))); |
| 826 } |
| 827 |
| 828 { |
| 829 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 830 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}}; |
| 831 cont.insert(std::begin(int_pairs), std::end(int_pairs), KEEP_LAST_OF_DUPES); |
| 832 EXPECT_THAT(cont, ElementsAre(IntPair(1, 2), IntPair(2, 2), IntPair(3, 2), |
| 833 IntPair(4, 2))); |
| 834 } |
| 835 |
| 836 { |
| 837 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 838 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}, {7, 2}, {6, 2}, |
| 839 {8, 2}, {5, 2}, {5, 3}, {6, 3}, {7, 3}, {8, 3}}; |
| 840 cont.insert(std::begin(int_pairs), std::end(int_pairs), |
| 841 KEEP_FIRST_OF_DUPES); |
| 842 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1), |
| 843 IntPair(4, 1), IntPair(5, 2), IntPair(6, 2), |
| 844 IntPair(7, 2), IntPair(8, 2))); |
| 845 } |
| 846 |
| 847 { |
| 848 IntIntMap cont({{1, 1}, {2, 1}, {3, 1}, {4, 1}}, KEEP_FIRST_OF_DUPES); |
| 849 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}, {7, 2}, {6, 2}, |
| 850 {8, 2}, {5, 2}, {5, 3}, {6, 3}, {7, 3}, {8, 3}}; |
| 851 cont.insert(std::begin(int_pairs), std::end(int_pairs), KEEP_LAST_OF_DUPES); |
| 852 EXPECT_THAT(cont, ElementsAre(IntPair(1, 2), IntPair(2, 2), IntPair(3, 2), |
| 853 IntPair(4, 2), IntPair(5, 3), IntPair(6, 3), |
| 854 IntPair(7, 3), IntPair(8, 3))); |
| 855 } |
| 856 } |
| 857 |
741 // template <class... Args> | 858 // template <class... Args> |
742 // pair<iterator, bool> emplace(Args&&... args) | 859 // pair<iterator, bool> emplace(Args&&... args) |
743 | 860 |
744 TEST(FlatTree, Emplace) { | 861 TEST(FlatTree, Emplace) { |
745 { | 862 { |
746 EmplaceableTree cont; | 863 EmplaceableTree cont; |
747 | 864 |
748 std::pair<EmplaceableTree::iterator, bool> result = cont.emplace(); | 865 std::pair<EmplaceableTree::iterator, bool> result = cont.emplace(); |
749 EXPECT_TRUE(result.second); | 866 EXPECT_TRUE(result.second); |
750 EXPECT_EQ(cont.begin(), result.first); | 867 EXPECT_EQ(cont.begin(), result.first); |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1376 EraseIf(x, [](int elem) { return !(elem & 1); }); | 1493 EraseIf(x, [](int elem) { return !(elem & 1); }); |
1377 EXPECT_THAT(x, ElementsAre(1, 3)); | 1494 EXPECT_THAT(x, ElementsAre(1, 3)); |
1378 | 1495 |
1379 x = {1, 2, 3, 4}; | 1496 x = {1, 2, 3, 4}; |
1380 EraseIf(x, [](int elem) { return elem & 1; }); | 1497 EraseIf(x, [](int elem) { return elem & 1; }); |
1381 EXPECT_THAT(x, ElementsAre(2, 4)); | 1498 EXPECT_THAT(x, ElementsAre(2, 4)); |
1382 } | 1499 } |
1383 | 1500 |
1384 } // namespace internal | 1501 } // namespace internal |
1385 } // namespace base | 1502 } // namespace base |
OLD | NEW |