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