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// size_type erase(const key_type& k);
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, class Compare = std::less<>>
29void test() {
30 using M = std::flat_map<int, char, Compare, KeyContainer, ValueContainer>;
31
32 auto make = [](std::initializer_list<int> il) {
33 M m;
34 for (int i : il) {
35 m.emplace(i, i);
36 }
37 return m;
38 };
39 M m = make({1, 2, 3, 4, 5, 6, 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, 2, 3, 4, 5, 6, 7, 8}));
44 n = m.erase(4);
45 assert(n == 1);
46 assert(m == make({1, 2, 3, 5, 6, 7, 8}));
47 n = m.erase(1);
48 assert(n == 1);
49 assert(m == make({2, 3, 5, 6, 7, 8}));
50 n = m.erase(8);
51 assert(n == 1);
52 assert(m == make({2, 3, 5, 6, 7}));
53 n = m.erase(3);
54 assert(n == 1);
55 assert(m == make({2, 5, 6, 7}));
56 n = m.erase(4);
57 assert(n == 0);
58 assert(m == make({2, 5, 6, 7}));
59 n = m.erase(6);
60 assert(n == 1);
61 assert(m == make({2, 5, 7}));
62 n = m.erase(7);
63 assert(n == 1);
64 assert(m == make({2, 5}));
65 n = m.erase(2);
66 assert(n == 1);
67 assert(m == make({5}));
68 n = m.erase(5);
69 assert(n == 1);
70 assert(m.empty());
71}
72
73int main(int, char**) {
74 test<std::vector<int>, std::vector<char>>();
75 test<std::vector<int>, std::vector<char>, std::greater<>>();
76 test<std::deque<int>, std::vector<char>>();
77 test<MinSequenceContainer<int>, MinSequenceContainer<char>>();
78 test<std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>();
79
80 {
81 auto erase_function = [](auto& m, auto key_arg) {
82 using Map = std::decay_t<decltype(m)>;
83 using Key = typename Map::key_type;
84 const Key key{key_arg};
85 m.erase(key);
86 };
87 test_erase_exception_guarantee(erase_function);
88 }
89
90 return 0;
91}
92

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