[InstCombine][NFC] Precommit a test for folding a binary op of reductions. (#121568)
[llvm-project.git] / libcxx / test / std / containers / container.adaptors / flat.map / flat.map.erasure / erase_if.pass.cpp
blobfb0563eec5376b85d990a93fdc051f82d52fa059
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // <flat_map>
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);
17 #include <deque>
18 #include <flat_map>
19 #include <functional>
20 #include <initializer_list>
21 #include <vector>
23 #include "test_macros.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
27 // Verify that `flat_map` (like `map`) does NOT support std::erase.
29 template <class S>
30 concept HasStdErase = requires(S& s, typename S::value_type x) { std::erase(s, x); };
31 static_assert(HasStdErase<std::vector<int>>);
32 static_assert(!HasStdErase<std::flat_map<int, int>>);
34 template <class M>
35 M 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;
42 template <class M, class Pred>
43 void 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));
51 template <class S>
52 void 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; };
61 test0<S>({}, is1, {}, 0);
63 test0<S>({1}, is1, {}, 1);
64 test0<S>({1}, is2, {1}, 0);
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);
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);
75 test0<S>({1, 2, 3}, True, {}, 3);
76 test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
79 int 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>>();
92 return 0;