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(iterator position); |
14 | // iterator erase(const_iterator position); |
15 | |
16 | #include <compare> |
17 | #include <concepts> |
18 | #include <deque> |
19 | #include <flat_set> |
20 | #include <functional> |
21 | #include <utility> |
22 | #include <vector> |
23 | |
24 | #include "MinSequenceContainer.h" |
25 | #include "../helpers.h" |
26 | #include "test_macros.h" |
27 | #include "min_allocator.h" |
28 | |
29 | template <class KeyContainer> |
30 | void test_one() { |
31 | using Key = typename KeyContainer::value_type; |
32 | using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>; |
33 | using I = M::iterator; |
34 | |
35 | int ar[] = { |
36 | 1, |
37 | 3, |
38 | 3, |
39 | 3, |
40 | 5, |
41 | 5, |
42 | 7, |
43 | 8, |
44 | }; |
45 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
46 | |
47 | auto make = [](std::initializer_list<int> il) { |
48 | M m2; |
49 | for (int i : il) { |
50 | m2.emplace(i); |
51 | } |
52 | return m2; |
53 | }; |
54 | assert(m.size() == 8); |
55 | assert(m == make({1, 3, 3, 3, 5, 5, 7, 8})); |
56 | std::same_as<I> decltype(auto) i1 = m.erase(std::next(m.cbegin(), 3)); |
57 | assert(m.size() == 7); |
58 | assert(i1 == std::next(m.begin(), 3)); |
59 | assert(m == make({1, 3, 3, 5, 5, 7, 8})); |
60 | |
61 | std::same_as<I> decltype(auto) i2 = m.erase(std::next(m.begin(), 0)); |
62 | assert(m.size() == 6); |
63 | assert(i2 == m.begin()); |
64 | assert(m == make({3, 3, 5, 5, 7, 8})); |
65 | |
66 | std::same_as<I> decltype(auto) i3 = m.erase(std::next(m.cbegin(), 5)); |
67 | assert(m.size() == 5); |
68 | assert(i3 == m.end()); |
69 | assert(m == make({3, 3, 5, 5, 7})); |
70 | |
71 | std::same_as<I> decltype(auto) i4 = m.erase(std::next(m.begin(), 1)); |
72 | assert(m.size() == 4); |
73 | assert(i4 == std::next(m.begin())); |
74 | assert(m == make({3, 5, 5, 7})); |
75 | |
76 | std::same_as<I> decltype(auto) i5 = m.erase(std::next(m.cbegin(), 2)); |
77 | assert(m.size() == 3); |
78 | assert(i5 == std::next(m.begin(), 2)); |
79 | assert(m == make({3, 5, 7})); |
80 | |
81 | std::same_as<I> decltype(auto) i6 = m.erase(std::next(m.begin(), 2)); |
82 | assert(m.size() == 2); |
83 | assert(i6 == std::next(m.begin(), 2)); |
84 | assert(m == make({3, 5})); |
85 | |
86 | std::same_as<I> decltype(auto) i7 = m.erase(std::next(m.cbegin(), 0)); |
87 | assert(m.size() == 1); |
88 | assert(i7 == std::next(m.begin(), 0)); |
89 | assert(m == make({5})); |
90 | |
91 | std::same_as<I> decltype(auto) i8 = m.erase(m.begin()); |
92 | assert(m.size() == 0); |
93 | assert(i8 == m.begin()); |
94 | assert(i8 == m.end()); |
95 | } |
96 | |
97 | void test() { |
98 | test_one<std::vector<int>>(); |
99 | test_one<std::deque<int>>(); |
100 | test_one<MinSequenceContainer<int>>(); |
101 | test_one<std::vector<int, min_allocator<int>>>(); |
102 | } |
103 | |
104 | void test_exception() { |
105 | auto erase_function = [](auto& m, auto) { m.erase(m.begin() + 2); }; |
106 | test_erase_exception_guarantee(erase_function); |
107 | } |
108 | |
109 | int main(int, char**) { |
110 | test(); |
111 | test_exception(); |
112 | |
113 | return 0; |
114 | } |
115 | |