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

source code of libcxx/test/std/containers/associative/map/map.erasure/erase_if.pass.cpp