| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_map> |
| 12 | |
| 13 | // class flat_multimap |
| 14 | |
| 15 | // template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate> |
| 16 | // typename flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>::size_type |
| 17 | // erase_if(flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred); |
| 18 | |
| 19 | #include <deque> |
| 20 | #include <flat_map> |
| 21 | #include <functional> |
| 22 | #include <initializer_list> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | #include "test_allocator.h" |
| 27 | #include "min_allocator.h" |
| 28 | |
| 29 | // Verify that `flat_multimap` (like `multimap`) does NOT support std::erase. |
| 30 | // |
| 31 | template <class S> |
| 32 | concept HasStdErase = requires(S& s, typename S::value_type x) { std::erase(s, x); }; |
| 33 | static_assert(HasStdErase<std::vector<int>>); |
| 34 | static_assert(!HasStdErase<std::flat_multimap<int, int>>); |
| 35 | |
| 36 | template <class M> |
| 37 | M make(std::initializer_list<int> vals) { |
| 38 | M ret; |
| 39 | for (int v : vals) { |
| 40 | ret.emplace(static_cast<typename M::key_type>(v), static_cast<typename M::mapped_type>(v + 10)); |
| 41 | } |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | template <class M, class Pred> |
| 46 | void test0( |
| 47 | std::initializer_list<int> vals, Pred p, std::initializer_list<int> expected, std::size_t expected_erased_count) { |
| 48 | M s = make<M>(vals); |
| 49 | ASSERT_SAME_TYPE(typename M::size_type, decltype(std::erase_if(s, p))); |
| 50 | assert(expected_erased_count == std::erase_if(s, p)); |
| 51 | assert(s == make<M>(expected)); |
| 52 | } |
| 53 | |
| 54 | template <class S> |
| 55 | void test() { |
| 56 | // Test all the plausible signatures for this predicate. |
| 57 | auto is1 = [](typename S::const_reference v) { return v.first == 1; }; |
| 58 | auto is2 = [](typename S::value_type v) { return v.first == 2; }; |
| 59 | auto is3 = [](const typename S::value_type& v) { return v.first == 3; }; |
| 60 | auto is4 = [](auto v) { return v.first == 4; }; |
| 61 | auto True = [](const auto&) { return true; }; |
| 62 | auto False = [](auto&&) { return false; }; |
| 63 | |
| 64 | test0<S>({}, is1, {}, 0); |
| 65 | |
| 66 | test0<S>({1}, is1, {}, 1); |
| 67 | test0<S>({1, 1}, is1, {}, 2); |
| 68 | test0<S>({1, 1}, is2, {1, 1}, 0); |
| 69 | |
| 70 | test0<S>({1, 2}, is1, {2}, 1); |
| 71 | test0<S>({1, 2}, is2, {1}, 1); |
| 72 | test0<S>({1, 2, 2, 2}, is2, {1}, 3); |
| 73 | test0<S>({1, 2, 2, 2}, is3, {1, 2, 2, 2}, 0); |
| 74 | |
| 75 | test0<S>({1, 1, 2, 2, 3, 3}, is1, {2, 2, 3, 3}, 2); |
| 76 | test0<S>({1, 1, 2, 2, 3, 3}, is2, {1, 1, 3, 3}, 2); |
| 77 | test0<S>({1, 1, 2, 2, 3, 3}, is3, {1, 1, 2, 2}, 2); |
| 78 | test0<S>({1, 1, 2, 2, 3, 3}, is4, {1, 1, 2, 2, 3, 3}, 0); |
| 79 | |
| 80 | test0<S>({1, 2, 2, 3, 3, 3}, True, {}, 6); |
| 81 | test0<S>({1, 2, 2, 3, 3, 3}, False, {1, 2, 2, 3, 3, 3}, 0); |
| 82 | } |
| 83 | |
| 84 | int main(int, char**) { |
| 85 | test<std::flat_multimap<int, char>>(); |
| 86 | test<std::flat_multimap<int, |
| 87 | char, |
| 88 | std::less<int>, |
| 89 | std::vector<int, min_allocator<int>>, |
| 90 | std::vector<char, min_allocator<char>>>>(); |
| 91 | test<std::flat_multimap<int, char, std::greater<int>, std::vector<int, test_allocator<int>>>>(); |
| 92 | test<std::flat_multimap<int, char, std::less<int>, std::deque<int, min_allocator<int>>>>(); |
| 93 | test<std::flat_multimap<int, char, std::greater<int>, std::deque<int, test_allocator<int>>>>(); |
| 94 | test<std::flat_multimap<long, int>>(); |
| 95 | test<std::flat_multimap<double, int>>(); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |