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

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter_iter.pass.cpp