| 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 | // iterator erase(const_iterator first, const_iterator last); |
| 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> |
| 29 | void test_one() { |
| 30 | using Key = typename KeyContainer::value_type; |
| 31 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
| 32 | using I = M::iterator; |
| 33 | |
| 34 | auto make = [](std::initializer_list<int> il) { |
| 35 | M m; |
| 36 | for (int i : il) { |
| 37 | m.emplace(i); |
| 38 | } |
| 39 | return m; |
| 40 | }; |
| 41 | |
| 42 | int ar[] = { |
| 43 | 1, |
| 44 | 1, |
| 45 | 3, |
| 46 | 3, |
| 47 | 5, |
| 48 | 6, |
| 49 | 6, |
| 50 | 8, |
| 51 | }; |
| 52 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
| 53 | assert(m.size() == 8); |
| 54 | std::same_as<I> decltype(auto) i1 = m.erase(m.cbegin(), m.cbegin()); |
| 55 | assert(m.size() == 8); |
| 56 | assert(i1 == m.begin()); |
| 57 | assert(m == make({1, 1, 3, 3, 5, 6, 6, 8})); |
| 58 | |
| 59 | std::same_as<I> decltype(auto) i2 = m.erase(m.cbegin(), std::next(m.cbegin(), 2)); |
| 60 | assert(m.size() == 6); |
| 61 | assert(i2 == m.begin()); |
| 62 | assert(m == make({3, 3, 5, 6, 6, 8})); |
| 63 | |
| 64 | std::same_as<I> decltype(auto) i3 = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6)); |
| 65 | assert(m.size() == 2); |
| 66 | assert(i3 == std::next(m.begin(), 2)); |
| 67 | assert(m == make({3, 3})); |
| 68 | |
| 69 | std::same_as<I> decltype(auto) i4 = m.erase(m.cbegin(), m.cend()); |
| 70 | assert(m.size() == 0); |
| 71 | assert(i4 == m.begin()); |
| 72 | assert(i4 == m.end()); |
| 73 | |
| 74 | // was empty |
| 75 | std::same_as<I> decltype(auto) i5 = m.erase(m.cbegin(), m.cend()); |
| 76 | assert(m.size() == 0); |
| 77 | assert(i5 == m.begin()); |
| 78 | assert(i5 == m.end()); |
| 79 | } |
| 80 | |
| 81 | void test() { |
| 82 | test_one<std::vector<int>>(); |
| 83 | test_one<std::deque<int>>(); |
| 84 | test_one<MinSequenceContainer<int>>(); |
| 85 | test_one<std::vector<int, min_allocator<int>>>(); |
| 86 | } |
| 87 | |
| 88 | void test_exception() { |
| 89 | auto erase_function = [](auto& m, auto) { m.erase(m.begin(), m.begin() + 2); }; |
| 90 | test_erase_exception_guarantee(erase_function); |
| 91 | } |
| 92 | |
| 93 | int main(int, char**) { |
| 94 | test(); |
| 95 | test_exception(); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |