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// size_type erase(const key_type& k);
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
28template <class KeyContainer, class Compare = std::less<>>
29constexpr void test_one() {
30 using M = std::flat_set<int, Compare, KeyContainer>;
31
32 auto make = [](std::initializer_list<int> il) {
33 M m;
34 for (int i : il) {
35 m.emplace(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 // was empty
72 n = m.erase(5);
73 assert(n == 0);
74 assert(m.empty());
75}
76
77constexpr bool test() {
78 test_one<std::vector<int>>();
79 test_one<std::vector<int>, std::greater<>>();
80#ifndef __cpp_lib_constexpr_deque
81 if (!TEST_IS_CONSTANT_EVALUATED)
82#endif
83 test_one<std::deque<int>>();
84 test_one<MinSequenceContainer<int>>();
85 test_one<std::vector<int, min_allocator<int>>>();
86
87 return true;
88}
89
90void test_exception() {
91 auto erase_function = [](auto& m, auto key_arg) {
92 using Map = std::decay_t<decltype(m)>;
93 using Key = typename Map::key_type;
94 const Key key{key_arg};
95 m.erase(key);
96 };
97 test_erase_exception_guarantee(erase_function);
98}
99
100int main(int, char**) {
101 test();
102 test_exception();
103#if TEST_STD_VER >= 26
104 static_assert(test());
105#endif
106
107 return 0;
108}
109

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