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// template<class Key, class Compare, class KeyContainer, class Predicate>
14// typename flat_set<Key, Compare, KeyContainer>::size_type
15// erase_if(flat_set<Key, Compare, KeyContainer>& c, Predicate pred);
16
17#include <deque>
18#include <flat_set>
19#include <functional>
20#include <initializer_list>
21#include <vector>
22
23#include "test_macros.h"
24#include "test_allocator.h"
25#include "min_allocator.h"
26
27// Verify that `flat_set` (like `set`) does NOT support std::erase.
28//
29template <class S>
30concept HasStdErase = requires(S& s, typename S::value_type x) { std::erase(s, x); };
31static_assert(HasStdErase<std::vector<int>>);
32static_assert(!HasStdErase<std::flat_set<int>>);
33
34template <class M>
35M make(std::initializer_list<int> vals) {
36 M ret;
37 for (int v : vals)
38 ret.emplace(v);
39 return ret;
40}
41
42template <class M, class Pred>
43void test0(
44 std::initializer_list<int> vals, Pred p, std::initializer_list<int> expected, std::size_t expected_erased_count) {
45 M s = make<M>(vals);
46 ASSERT_SAME_TYPE(typename M::size_type, decltype(std::erase_if(s, p)));
47 assert(expected_erased_count == std::erase_if(s, p));
48 assert(s == make<M>(expected));
49}
50
51struct NotBool {
52 bool b;
53 explicit operator bool() const { return b; }
54};
55
56template <class S>
57void test_one() {
58 // Test all the plausible signatures for this predicate.
59 auto is1 = [](typename S::const_reference v) { return v == 1; };
60 auto is2 = [](typename S::value_type v) { return v == 2; };
61 auto is3 = [](const typename S::value_type& v) { return v == 3; };
62 auto is4 = [](auto v) { return v == 4; };
63 auto True = [](const auto&) { return true; };
64 auto False = [](auto&&) { return false; };
65 auto nonBoolIs1 = [](const auto& v) { return NotBool{v == 1}; };
66
67 test0<S>({}, is1, {}, 0);
68
69 test0<S>({1}, is1, {}, 1);
70 test0<S>({1}, is2, {1}, 0);
71
72 test0<S>({1, 2}, is1, {2}, 1);
73 test0<S>({1, 2}, is2, {1}, 1);
74 test0<S>({1, 2}, is3, {1, 2}, 0);
75
76 test0<S>({1, 2, 3}, is1, {2, 3}, 1);
77 test0<S>({1, 2, 3}, is2, {1, 3}, 1);
78 test0<S>({1, 2, 3}, is3, {1, 2}, 1);
79 test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);
80
81 test0<S>({1, 2, 3}, True, {}, 3);
82 test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
83
84 test0<S>({1, 2, 3}, nonBoolIs1, {2, 3}, 1);
85}
86
87void test() {
88 test_one<std::flat_set<int>>();
89 test_one<std::flat_set<int, std::less<int>, std::vector<int, min_allocator<int>>>>();
90 test_one<std::flat_set<int, std::greater<int>, std::vector<int, test_allocator<int>>>>();
91 test_one<std::flat_set<int, std::less<int>, std::deque<int, min_allocator<int>>>>();
92 test_one<std::flat_set<int, std::greater<int>, std::deque<int, test_allocator<int>>>>();
93 test_one<std::flat_set<long>>();
94 test_one<std::flat_set<double>>();
95}
96
97int main(int, char**) {
98 test();
99
100 return 0;
101}
102

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.erasure/erase_if.pass.cpp