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 | // iterator erase(const_iterator first, const_iterator last); |
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> |
31 | void test() { |
32 | using Key = typename KeyContainer::value_type; |
33 | using Value = typename ValueContainer::value_type; |
34 | using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
35 | using P = std::pair<Key, Value>; |
36 | using I = M::iterator; |
37 | |
38 | P ar[] = { |
39 | P(1, 1.5), |
40 | P(2, 2.5), |
41 | P(2, 2.6), |
42 | P(3, 3.5), |
43 | P(3, 3.6), |
44 | P(3, 3.7), |
45 | P(4, 4.5), |
46 | P(5, 5.5), |
47 | P(6, 6.5), |
48 | P(7, 7.5), |
49 | P(8, 8.5), |
50 | }; |
51 | M m(ar, ar + sizeof(ar) / sizeof(ar[0])); |
52 | assert(m.size() == 11); |
53 | std::same_as<I> decltype(auto) i1 = m.erase(m.cbegin(), m.cbegin()); |
54 | assert(m.size() == 11); |
55 | assert(i1 == m.begin()); |
56 | assert(std::ranges::equal( |
57 | m, |
58 | std::vector<P>{ |
59 | {1, 1.5}, |
60 | {2, 2.5}, |
61 | {2, 2.6}, |
62 | {3, 3.5}, |
63 | {3, 3.6}, |
64 | {3, 3.7}, |
65 | {4, 4.5}, |
66 | {5, 5.5}, |
67 | {6, 6.5}, |
68 | {7, 7.5}, |
69 | {8, 8.5}})); |
70 | |
71 | std::same_as<I> decltype(auto) i2 = m.erase(m.cbegin(), std::next(m.cbegin(), 2)); |
72 | assert(m.size() == 9); |
73 | assert(i2 == m.begin()); |
74 | assert(std::ranges::equal( |
75 | m, std::vector<P>{{2, 2.6}, {3, 3.5}, {3, 3.6}, {3, 3.7}, {4, 4.5}, {5, 5.5}, {6, 6.5}, {7, 7.5}, {8, 8.5}})); |
76 | |
77 | std::same_as<I> decltype(auto) i3 = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6)); |
78 | assert(m.size() == 5); |
79 | assert(i3 == std::next(m.begin(), 2)); |
80 | assert(std::ranges::equal(m, std::vector<P>{{2, 2.6}, {3, 3.5}, {6, 6.5}, {7, 7.5}, {8, 8.5}})); |
81 | |
82 | std::same_as<I> decltype(auto) i4 = m.erase(m.cbegin(), m.cend()); |
83 | assert(m.size() == 0); |
84 | assert(i4 == m.begin()); |
85 | assert(i4 == m.end()); |
86 | } |
87 | |
88 | int main(int, char**) { |
89 | test<std::vector<int>, std::vector<double>>(); |
90 | test<std::deque<int>, std::vector<double>>(); |
91 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
92 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
93 | |
94 | { |
95 | auto erase_function = [](auto& m, auto) { m.erase(m.begin(), m.begin() + 2); }; |
96 | test_erase_exception_guarantee(erase_function); |
97 | } |
98 | return 0; |
99 | } |
100 | |