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_multiset<Key, Compare, KeyContainer>::size_type
15// erase_if(flat_multiset<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_multiset` (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_multiset<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, 1, 1}, is1, {}, 3);
71 test0<S>({1}, is2, {1}, 0);
72 test0<S>({1, 1, 1}, is2, {1, 1, 1}, 0);
73
74 test0<S>({1, 2}, is1, {2}, 1);
75 test0<S>({1, 1, 1, 2, 2}, is1, {2, 2}, 3);
76 test0<S>({1, 2}, is2, {1}, 1);
77 test0<S>({1, 1, 1, 2, 2}, is2, {1, 1, 1}, 2);
78 test0<S>({1, 2}, is3, {1, 2}, 0);
79 test0<S>({1, 1, 1, 2, 2}, is3, {1, 1, 1, 2, 2}, 0);
80
81 test0<S>({1, 2, 3}, is1, {2, 3}, 1);
82 test0<S>({1, 1, 2, 2, 3, 3}, is1, {2, 2, 3, 3}, 2);
83 test0<S>({1, 2, 3}, is2, {1, 3}, 1);
84 test0<S>({1, 1, 2, 2, 3, 3}, is2, {1, 1, 3, 3}, 2);
85 test0<S>({1, 2, 3}, is3, {1, 2}, 1);
86 test0<S>({1, 1, 2, 2, 3, 3}, is3, {1, 1, 2, 2}, 2);
87 test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);
88 test0<S>({1, 1, 2, 2, 3, 3}, is4, {1, 1, 2, 2, 3, 3}, 0);
89
90 test0<S>({1, 2, 3}, True, {}, 3);
91 test0<S>({1, 2, 2, 3, 3, 3}, True, {}, 6);
92 test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
93 test0<S>({1, 2, 2, 3, 3, 3}, False, {1, 2, 2, 3, 3, 3}, 0);
94
95 test0<S>({1, 2, 3}, nonBoolIs1, {2, 3}, 1);
96 test0<S>({1, 1, 2, 2, 3}, nonBoolIs1, {2, 2, 3}, 2);
97}
98
99void test() {
100 test_one<std::flat_multiset<int>>();
101 test_one<std::flat_multiset<int, std::less<int>, std::vector<int, min_allocator<int>>>>();
102 test_one<std::flat_multiset<int, std::greater<int>, std::vector<int, test_allocator<int>>>>();
103 test_one<std::flat_multiset<int, std::less<int>, std::deque<int, min_allocator<int>>>>();
104 test_one<std::flat_multiset<int, std::greater<int>, std::deque<int, test_allocator<int>>>>();
105 test_one<std::flat_multiset<long>>();
106 test_one<std::flat_multiset<double>>();
107}
108
109int main(int, char**) {
110 test();
111
112 return 0;
113}
114

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