Chromium Code Reviews| Index: base/containers/flat_set_unittest.cc |
| diff --git a/base/containers/flat_set_unittest.cc b/base/containers/flat_set_unittest.cc |
| index dc024fcf0a9729a15d4987e3980da46306a6b143..af133638bee0b7421fd987bb8591de9d8e4c718c 100644 |
| --- a/base/containers/flat_set_unittest.cc |
| +++ b/base/containers/flat_set_unittest.cc |
| @@ -7,8 +7,9 @@ |
| #include <string> |
| #include <vector> |
| -#include "base/containers/container_test_utils.h" |
| #include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/test/move_only_int.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -20,6 +21,35 @@ using ::testing::ElementsAre; |
| namespace base { |
| +namespace { |
| + |
| +struct UniquePtrComparator { |
| + // Mark your comparison as transparent. |
| + using is_transparent = int; |
| + |
| + template <typename T> |
| + bool operator()(const std::unique_ptr<T>& lhs, |
| + const std::unique_ptr<T>& rhs) const { |
| + return lhs < rhs; |
| + } |
| + |
| + template <typename T> |
| + bool operator()(const T* lhs, const std::unique_ptr<T>& rhs) const { |
| + return lhs < rhs.get(); |
| + } |
| + |
| + template <typename T> |
| + bool operator()(const std::unique_ptr<T>& lhs, const T* rhs) const { |
| + return lhs.get() < rhs; |
| + } |
| +}; |
| + |
| +// Declare a typedef. |
| +template <typename T> |
| +using UniquePtrSet = base::flat_set<std::unique_ptr<T>, UniquePtrComparator>; |
| + |
| +} // namespace |
| + |
| TEST(FlatSet, IncompleteType) { |
| struct A { |
| using Set = flat_set<A>; |
| @@ -90,4 +120,46 @@ TEST(FlatSet, CopySwap) { |
| EXPECT_THAT(copy, ElementsAre(1, 2)); |
| } |
| +TEST(FlatSet, UsingTransparentCompare) { |
| + using ExplicitInt = base::MoveOnlyInt; |
| + base::flat_set<ExplicitInt> s; |
| + const auto& s1 = s; |
| + int x = 0; |
| + |
| + // Check if we can use lookup functions without converting to key_type. |
| + // Correctness is checked in flat_tree tests. |
| + s.count(x); |
| + s1.count(x); |
| + s.find(x); |
| + s1.find(x); |
| + s.equal_range(x); |
| + s1.equal_range(x); |
| + s.lower_bound(x); |
| + s1.lower_bound(x); |
| + s.upper_bound(x); |
| + s1.upper_bound(x); |
| + s.erase(x); |
| + |
| + // Check if we broke overload resolution. |
| + s.emplace(0); |
| + s.emplace(1); |
| + s.erase(s.begin()); |
| + s.erase(s.cbegin()); |
| +} |
| + |
| +TEST(FlatSet, Examples) { |
|
danakj
2017/06/27 23:48:25
same
dyaroshev
2017/06/28 09:13:59
Done
|
| + // Collect data. |
| + std::vector<std::unique_ptr<int>> ptr_vec; |
| + ptr_vec.reserve(5); |
| + std::generate_n(std::back_inserter(ptr_vec), 5, |
| + [] { return base::MakeUnique<int>(0); }); |
| + |
| + // Construct set. |
| + UniquePtrSet<int> ptr_set(std::move(ptr_vec), base::KEEP_FIRST_OF_DUPES); |
| + |
| + // Use it with plain pointers. |
| + int* ptr = ptr_set.begin()->get(); |
| + EXPECT_TRUE(ptr_set.find(ptr) == ptr_set.begin()); |
| +} |
| + |
| } // namespace base |