Chromium Code Reviews| 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_set.h" | 5 #include "base/containers/flat_set.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/containers/container_test_utils.h" | |
| 11 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/test/move_only_int.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 // A flat_set is basically a interface to flat_tree. So several basic | 16 // A flat_set is basically a interface to flat_tree. So several basic |
| 16 // operations are tested to make sure things are set up properly, but the bulk | 17 // operations are tested to make sure things are set up properly, but the bulk |
| 17 // of the tests are in flat_tree_unittests.cc. | 18 // of the tests are in flat_tree_unittests.cc. |
| 18 | 19 |
| 19 using ::testing::ElementsAre; | 20 using ::testing::ElementsAre; |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 | 23 |
| 24 namespace { | |
| 25 | |
| 26 struct UniquePtrComparator { | |
| 27 // Mark your comparison as transparent. | |
| 28 using is_transparent = int; | |
| 29 | |
| 30 template <typename T> | |
| 31 bool operator()(const std::unique_ptr<T>& lhs, | |
| 32 const std::unique_ptr<T>& rhs) const { | |
| 33 return lhs < rhs; | |
| 34 } | |
| 35 | |
| 36 template <typename T> | |
| 37 bool operator()(const T* lhs, const std::unique_ptr<T>& rhs) const { | |
| 38 return lhs < rhs.get(); | |
| 39 } | |
| 40 | |
| 41 template <typename T> | |
| 42 bool operator()(const std::unique_ptr<T>& lhs, const T* rhs) const { | |
| 43 return lhs.get() < rhs; | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 // Declare a typedef. | |
| 48 template <typename T> | |
| 49 using UniquePtrSet = base::flat_set<std::unique_ptr<T>, UniquePtrComparator>; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 23 TEST(FlatSet, IncompleteType) { | 53 TEST(FlatSet, IncompleteType) { |
| 24 struct A { | 54 struct A { |
| 25 using Set = flat_set<A>; | 55 using Set = flat_set<A>; |
| 26 int data; | 56 int data; |
| 27 Set set_with_incomplete_type; | 57 Set set_with_incomplete_type; |
| 28 Set::iterator it; | 58 Set::iterator it; |
| 29 Set::const_iterator cit; | 59 Set::const_iterator cit; |
| 30 | 60 |
| 31 // We do not declare operator< because clang complains that it's unused. | 61 // We do not declare operator< because clang complains that it's unused. |
| 32 }; | 62 }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 | 113 |
| 84 copy.erase(copy.begin()); | 114 copy.erase(copy.begin()); |
| 85 copy.insert(10); | 115 copy.insert(10); |
| 86 EXPECT_THAT(copy, ElementsAre(2, 10)); | 116 EXPECT_THAT(copy, ElementsAre(2, 10)); |
| 87 | 117 |
| 88 original.swap(copy); | 118 original.swap(copy); |
| 89 EXPECT_THAT(original, ElementsAre(2, 10)); | 119 EXPECT_THAT(original, ElementsAre(2, 10)); |
| 90 EXPECT_THAT(copy, ElementsAre(1, 2)); | 120 EXPECT_THAT(copy, ElementsAre(1, 2)); |
| 91 } | 121 } |
| 92 | 122 |
| 123 TEST(FlatSet, UsingTransparentCompare) { | |
| 124 using ExplicitInt = base::MoveOnlyInt; | |
| 125 base::flat_set<ExplicitInt> s; | |
| 126 const auto& s1 = s; | |
| 127 int x = 0; | |
| 128 | |
| 129 // Check if we can use lookup functions without converting to key_type. | |
| 130 // Correctness is checked in flat_tree tests. | |
| 131 s.count(x); | |
| 132 s1.count(x); | |
| 133 s.find(x); | |
| 134 s1.find(x); | |
| 135 s.equal_range(x); | |
| 136 s1.equal_range(x); | |
| 137 s.lower_bound(x); | |
| 138 s1.lower_bound(x); | |
| 139 s.upper_bound(x); | |
| 140 s1.upper_bound(x); | |
| 141 s.erase(x); | |
| 142 | |
| 143 // Check if we broke overload resolution. | |
| 144 s.emplace(0); | |
| 145 s.emplace(1); | |
| 146 s.erase(s.begin()); | |
| 147 s.erase(s.cbegin()); | |
| 148 } | |
| 149 | |
| 150 TEST(FlatSet, Examples) { | |
|
danakj
2017/06/27 23:48:25
same
dyaroshev
2017/06/28 09:13:59
Done
| |
| 151 // Collect data. | |
| 152 std::vector<std::unique_ptr<int>> ptr_vec; | |
| 153 ptr_vec.reserve(5); | |
| 154 std::generate_n(std::back_inserter(ptr_vec), 5, | |
| 155 [] { return base::MakeUnique<int>(0); }); | |
| 156 | |
| 157 // Construct set. | |
| 158 UniquePtrSet<int> ptr_set(std::move(ptr_vec), base::KEEP_FIRST_OF_DUPES); | |
| 159 | |
| 160 // Use it with plain pointers. | |
| 161 int* ptr = ptr_set.begin()->get(); | |
| 162 EXPECT_TRUE(ptr_set.find(ptr) == ptr_set.begin()); | |
| 163 } | |
| 164 | |
| 93 } // namespace base | 165 } // namespace base |
| OLD | NEW |