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_map>
12
13// template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate>
14// typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
15// erase_if(flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred);
16
17#include <deque>
18#include <flat_map>
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_map` (like `map`) 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_map<int, int>>);
33
34template <class M>
35M make(std::initializer_list<int> vals) {
36 M ret;
37 for (int v : vals)
38 ret[static_cast<typename M::key_type>(v)] = static_cast<typename M::mapped_type>(v + 10);
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
51template <class S>
52void test() {
53 // Test all the plausible signatures for this predicate.
54 auto is1 = [](typename S::const_reference v) { return v.first == 1; };
55 auto is2 = [](typename S::value_type v) { return v.first == 2; };
56 auto is3 = [](const typename S::value_type& v) { return v.first == 3; };
57 auto is4 = [](auto v) { return v.first == 4; };
58 auto True = [](const auto&) { return true; };
59 auto False = [](auto&&) { return false; };
60
61 test0<S>({}, is1, {}, 0);
62
63 test0<S>({1}, is1, {}, 1);
64 test0<S>({1}, is2, {1}, 0);
65
66 test0<S>({1, 2}, is1, {2}, 1);
67 test0<S>({1, 2}, is2, {1}, 1);
68 test0<S>({1, 2}, is3, {1, 2}, 0);
69
70 test0<S>({1, 2, 3}, is1, {2, 3}, 1);
71 test0<S>({1, 2, 3}, is2, {1, 3}, 1);
72 test0<S>({1, 2, 3}, is3, {1, 2}, 1);
73 test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);
74
75 test0<S>({1, 2, 3}, True, {}, 3);
76 test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
77}
78
79int main(int, char**) {
80 test<std::flat_map<int, char>>();
81 test<std::flat_map<int,
82 char,
83 std::less<int>,
84 std::vector<int, min_allocator<int>>,
85 std::vector<char, min_allocator<char>>>>();
86 test<std::flat_map<int, char, std::greater<int>, std::vector<int, test_allocator<int>>>>();
87 test<std::flat_map<int, char, std::less<int>, std::deque<int, min_allocator<int>>>>();
88 test<std::flat_map<int, char, std::greater<int>, std::deque<int, test_allocator<int>>>>();
89 test<std::flat_map<long, int>>();
90 test<std::flat_map<double, int>>();
91
92 return 0;
93}
94

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