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