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// UNSUPPORTED: c++03, c++11, c++14, c++17
9
10// <unordered_set>
11
12// template <class T, class Hash, class Compare, class Allocator, class Predicate>
13// typename unordered_set<T, Hash, Compare, Allocator>::size_type
14// erase_if(unordered_set<T, Hash, Compare, Allocator>& c, Predicate pred);
15
16#include <unordered_set>
17#include <algorithm>
18
19#include "test_macros.h"
20#include "test_allocator.h"
21#include "min_allocator.h"
22
23using Init = std::initializer_list<int>;
24
25template <typename M>
26M make(Init vals) {
27 M ret;
28 for (int v : vals)
29 ret.insert(v);
30 return ret;
31}
32
33template <typename M, typename Pred>
34void test0(Init vals, Pred p, Init expected, std::size_t expected_erased_count) {
35 M s = make<M>(vals);
36 ASSERT_SAME_TYPE(typename M::size_type, decltype(std::erase_if(s, p)));
37 assert(expected_erased_count == std::erase_if(s, p));
38 M e = make<M>(expected);
39 assert((std::is_permutation(s.begin(), s.end(), e.begin(), e.end())));
40}
41
42template <typename S>
43void test() {
44 auto is1 = [](auto v) { return v == 1; };
45 auto is2 = [](auto v) { return v == 2; };
46 auto is3 = [](auto v) { return v == 3; };
47 auto is4 = [](auto v) { return v == 4; };
48 auto True = [](auto) { return true; };
49 auto False = [](auto) { return false; };
50
51 test0<S>({}, is1, {}, 0);
52
53 test0<S>({1}, is1, {}, 1);
54 test0<S>({1}, is2, {1}, 0);
55
56 test0<S>({1, 2}, is1, {2}, 1);
57 test0<S>({1, 2}, is2, {1}, 1);
58 test0<S>({1, 2}, is3, {1, 2}, 0);
59
60 test0<S>({1, 2, 3}, is1, {2, 3}, 1);
61 test0<S>({1, 2, 3}, is2, {1, 3}, 1);
62 test0<S>({1, 2, 3}, is3, {1, 2}, 1);
63 test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);
64
65 test0<S>({1, 2, 3}, True, {}, 3);
66 test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
67}
68
69int main(int, char**) {
70 test<std::unordered_set<int>>();
71 test<std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>>>();
72 test<std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>>();
73
74 test<std::unordered_set<long>>();
75 test<std::unordered_set<double>>();
76
77 return 0;
78}
79

source code of libcxx/test/std/containers/unord/unord.set/erase_if.pass.cpp