| 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 | // size_type erase(const key_type& k); |
| 16 | |
| 17 | #include <compare> |
| 18 | #include <concepts> |
| 19 | #include <deque> |
| 20 | #include <flat_map> |
| 21 | #include <functional> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "MinSequenceContainer.h" |
| 26 | #include "../helpers.h" |
| 27 | #include "test_macros.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | template <class KeyContainer, class ValueContainer, class Compare = std::less<>> |
| 31 | void test() { |
| 32 | using M = std::flat_multimap<int, char, Compare, KeyContainer, ValueContainer>; |
| 33 | |
| 34 | auto make = [](std::initializer_list<int> il) { |
| 35 | M m; |
| 36 | for (int i : il) { |
| 37 | m.emplace(i, i); |
| 38 | } |
| 39 | return m; |
| 40 | }; |
| 41 | M m = make({1, 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 8, 8, 9}); |
| 42 | ASSERT_SAME_TYPE(decltype(m.erase(9)), typename M::size_type); |
| 43 | auto n = m.erase(10); |
| 44 | assert(n == 0); |
| 45 | assert(m == make({1, 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 8, 8, 9})); |
| 46 | n = m.erase(4); |
| 47 | assert(n == 1); |
| 48 | assert(m == make({1, 1, 2, 2, 2, 3, 5, 5, 6, 7, 8, 8, 8, 8, 9})); |
| 49 | n = m.erase(1); |
| 50 | assert(n == 2); |
| 51 | assert(m == make({2, 2, 2, 3, 5, 5, 6, 7, 8, 8, 8, 8, 9})); |
| 52 | n = m.erase(8); |
| 53 | assert(n == 4); |
| 54 | assert(m == make({2, 2, 2, 3, 5, 5, 6, 7, 9})); |
| 55 | n = m.erase(3); |
| 56 | assert(n == 1); |
| 57 | assert(m == make({2, 2, 2, 5, 5, 6, 7, 9})); |
| 58 | n = m.erase(4); |
| 59 | assert(n == 0); |
| 60 | assert(m == make({2, 2, 2, 5, 5, 6, 7, 9})); |
| 61 | n = m.erase(6); |
| 62 | assert(n == 1); |
| 63 | assert(m == make({2, 2, 2, 5, 5, 7, 9})); |
| 64 | n = m.erase(7); |
| 65 | assert(n == 1); |
| 66 | assert(m == make({2, 2, 2, 5, 5, 9})); |
| 67 | n = m.erase(2); |
| 68 | assert(n == 3); |
| 69 | assert(m == make({5, 5, 9})); |
| 70 | n = m.erase(5); |
| 71 | assert(n == 2); |
| 72 | assert(m == make({9})); |
| 73 | n = m.erase(9); |
| 74 | assert(n == 1); |
| 75 | assert(m.empty()); |
| 76 | n = m.erase(1); |
| 77 | assert(n == 0); |
| 78 | assert(m.empty()); |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | test<std::vector<int>, std::vector<char>>(); |
| 83 | test<std::vector<int>, std::vector<char>, std::greater<>>(); |
| 84 | test<std::deque<int>, std::vector<char>>(); |
| 85 | test<MinSequenceContainer<int>, MinSequenceContainer<char>>(); |
| 86 | test<std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>(); |
| 87 | |
| 88 | { |
| 89 | auto erase_function = [](auto& m, auto key_arg) { |
| 90 | using Map = std::decay_t<decltype(m)>; |
| 91 | using Key = typename Map::key_type; |
| 92 | const Key key{key_arg}; |
| 93 | m.erase(key); |
| 94 | }; |
| 95 | test_erase_exception_guarantee(erase_function); |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |