brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 337ad04 Raw
124 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_set>12 13// template<class Key, class Compare, class KeyContainer, class Predicate>14//   typename flat_multiset<Key, Compare, KeyContainer>::size_type15//   erase_if(flat_multiset<Key, Compare, KeyContainer>& c, Predicate pred);16 17#include <deque>18#include <flat_set>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_multiset` (like `set`) 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_multiset<int>>);33 34template <class M>35constexpr M make(std::initializer_list<int> vals) {36  M ret;37  for (int v : vals)38    ret.emplace(v);39  return ret;40}41 42template <class M, class Pred>43constexpr void44test0(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 51struct NotBool {52  bool b;53  explicit constexpr operator bool() const { return b; }54};55 56template <class S>57constexpr void test_one() {58  // Test all the plausible signatures for this predicate.59  auto is1        = [](typename S::const_reference v) { return v == 1; };60  auto is2        = [](typename S::value_type v) { return v == 2; };61  auto is3        = [](const typename S::value_type& v) { return v == 3; };62  auto is4        = [](auto v) { return v == 4; };63  auto True       = [](const auto&) { return true; };64  auto False      = [](auto&&) { return false; };65  auto nonBoolIs1 = [](const auto& v) { return NotBool{v == 1}; };66 67  test0<S>({}, is1, {}, 0);68 69  test0<S>({1}, is1, {}, 1);70  test0<S>({1, 1, 1}, is1, {}, 3);71  test0<S>({1}, is2, {1}, 0);72  test0<S>({1, 1, 1}, is2, {1, 1, 1}, 0);73 74  test0<S>({1, 2}, is1, {2}, 1);75  test0<S>({1, 1, 1, 2, 2}, is1, {2, 2}, 3);76  test0<S>({1, 2}, is2, {1}, 1);77  test0<S>({1, 1, 1, 2, 2}, is2, {1, 1, 1}, 2);78  test0<S>({1, 2}, is3, {1, 2}, 0);79  test0<S>({1, 1, 1, 2, 2}, is3, {1, 1, 1, 2, 2}, 0);80 81  test0<S>({1, 2, 3}, is1, {2, 3}, 1);82  test0<S>({1, 1, 2, 2, 3, 3}, is1, {2, 2, 3, 3}, 2);83  test0<S>({1, 2, 3}, is2, {1, 3}, 1);84  test0<S>({1, 1, 2, 2, 3, 3}, is2, {1, 1, 3, 3}, 2);85  test0<S>({1, 2, 3}, is3, {1, 2}, 1);86  test0<S>({1, 1, 2, 2, 3, 3}, is3, {1, 1, 2, 2}, 2);87  test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);88  test0<S>({1, 1, 2, 2, 3, 3}, is4, {1, 1, 2, 2, 3, 3}, 0);89 90  test0<S>({1, 2, 3}, True, {}, 3);91  test0<S>({1, 2, 2, 3, 3, 3}, True, {}, 6);92  test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);93  test0<S>({1, 2, 2, 3, 3, 3}, False, {1, 2, 2, 3, 3, 3}, 0);94 95  test0<S>({1, 2, 3}, nonBoolIs1, {2, 3}, 1);96  test0<S>({1, 1, 2, 2, 3}, nonBoolIs1, {2, 2, 3}, 2);97}98 99constexpr bool test() {100  test_one<std::flat_multiset<int>>();101  test_one<std::flat_multiset<int, std::less<int>, std::vector<int, min_allocator<int>>>>();102  test_one<std::flat_multiset<int, std::greater<int>, std::vector<int, test_allocator<int>>>>();103#ifndef __cpp_lib_constexpr_deque104  if (!TEST_IS_CONSTANT_EVALUATED)105#endif106  {107    test_one<std::flat_multiset<int, std::less<int>, std::deque<int, min_allocator<int>>>>();108    test_one<std::flat_multiset<int, std::greater<int>, std::deque<int, test_allocator<int>>>>();109  }110  test_one<std::flat_multiset<long>>();111  test_one<std::flat_multiset<double>>();112 113  return true;114}115 116int main(int, char**) {117  test();118#if TEST_STD_VER >= 26119  static_assert(test());120#endif121 122  return 0;123}124