113 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_map>12 13// class flat_multimap14 15// template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate>16// typename flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>::size_type17// erase_if(flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred);18 19#include <deque>20#include <flat_map>21#include <functional>22#include <initializer_list>23#include <vector>24 25#include "test_macros.h"26#include "test_allocator.h"27#include "min_allocator.h"28 29// Verify that `flat_multimap` (like `multimap`) does NOT support std::erase.30//31template <class S>32concept HasStdErase = requires(S& s, typename S::value_type x) { std::erase(s, x); };33static_assert(HasStdErase<std::vector<int>>);34static_assert(!HasStdErase<std::flat_multimap<int, int>>);35 36template <class M>37constexpr M make(std::initializer_list<int> vals) {38 M ret;39 for (int v : vals) {40 ret.emplace(static_cast<typename M::key_type>(v), static_cast<typename M::mapped_type>(v + 10));41 }42 return ret;43}44 45template <class M, class Pred>46constexpr void47test0(std::initializer_list<int> vals, Pred p, std::initializer_list<int> expected, std::size_t expected_erased_count) {48 M s = make<M>(vals);49 ASSERT_SAME_TYPE(typename M::size_type, decltype(std::erase_if(s, p)));50 assert(expected_erased_count == std::erase_if(s, p));51 assert(s == make<M>(expected));52}53 54template <class S>55constexpr void test() {56 // Test all the plausible signatures for this predicate.57 auto is1 = [](typename S::const_reference v) { return v.first == 1; };58 auto is2 = [](typename S::value_type v) { return v.first == 2; };59 auto is3 = [](const typename S::value_type& v) { return v.first == 3; };60 auto is4 = [](auto v) { return v.first == 4; };61 auto True = [](const auto&) { return true; };62 auto False = [](auto&&) { return false; };63 64 test0<S>({}, is1, {}, 0);65 66 test0<S>({1}, is1, {}, 1);67 test0<S>({1, 1}, is1, {}, 2);68 test0<S>({1, 1}, is2, {1, 1}, 0);69 70 test0<S>({1, 2}, is1, {2}, 1);71 test0<S>({1, 2}, is2, {1}, 1);72 test0<S>({1, 2, 2, 2}, is2, {1}, 3);73 test0<S>({1, 2, 2, 2}, is3, {1, 2, 2, 2}, 0);74 75 test0<S>({1, 1, 2, 2, 3, 3}, is1, {2, 2, 3, 3}, 2);76 test0<S>({1, 1, 2, 2, 3, 3}, is2, {1, 1, 3, 3}, 2);77 test0<S>({1, 1, 2, 2, 3, 3}, is3, {1, 1, 2, 2}, 2);78 test0<S>({1, 1, 2, 2, 3, 3}, is4, {1, 1, 2, 2, 3, 3}, 0);79 80 test0<S>({1, 2, 2, 3, 3, 3}, True, {}, 6);81 test0<S>({1, 2, 2, 3, 3, 3}, False, {1, 2, 2, 3, 3, 3}, 0);82}83 84constexpr bool test() {85 test<std::flat_multimap<int, char>>();86 test<std::flat_multimap<int,87 char,88 std::less<int>,89 std::vector<int, min_allocator<int>>,90 std::vector<char, min_allocator<char>>>>();91 test<std::flat_multimap<int, char, std::greater<int>, std::vector<int, test_allocator<int>>>>();92#ifndef __cpp_lib_constexpr_deque93 if (!TEST_IS_CONSTANT_EVALUATED)94#endif95 {96 test<std::flat_multimap<int, char, std::less<int>, std::deque<int, min_allocator<int>>>>();97 test<std::flat_multimap<int, char, std::greater<int>, std::deque<int, test_allocator<int>>>>();98 }99 test<std::flat_multimap<long, int>>();100 test<std::flat_multimap<double, int>>();101 102 return true;103}104 105int main(int, char**) {106 test();107#if TEST_STD_VER >= 26108 static_assert(test());109#endif110 111 return 0;112}113