| 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 |
// <vector> |
| 11 |
|
| 12 |
// template <class T, class Allocator, class Predicate> |
| 13 |
// typename vector<T, Allocator>::size_type |
| 14 |
// erase_if(vector<T, Allocator>& c, Predicate pred); |
| 15 |
|
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "test_allocator.h" |
| 20 |
#include "min_allocator.h" |
| 21 |
|
| 22 |
template <class S, class Pred> |
| 23 |
TEST_CONSTEXPR_CXX20 void test0(S s, Pred p, S expected, std::size_t expected_erased_count) { |
| 24 |
ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase_if(s, p))); |
| 25 |
assert(expected_erased_count == std::erase_if(s, p)); |
| 26 |
assert(s == expected); |
| 27 |
} |
| 28 |
|
| 29 |
template <typename S> |
| 30 |
TEST_CONSTEXPR_CXX20 void test() |
| 31 |
{ |
| 32 |
auto is1 = [](auto v) { return v == 1;}; |
| 33 |
auto is2 = [](auto v) { return v == 2;}; |
| 34 |
auto is3 = [](auto v) { return v == 3;}; |
| 35 |
auto is4 = [](auto v) { return v == 4;}; |
| 36 |
auto True = [](auto) { return true; }; |
| 37 |
auto False = [](auto) { return false; }; |
| 38 |
|
| 39 |
test0(S(), is1, S(), 0); |
| 40 |
|
| 41 |
test0(S({1}), is1, S(), 1); |
| 42 |
test0(S({1}), is2, S({1}), 0); |
| 43 |
|
| 44 |
test0(S({1, 2}), is1, S({2}), 1); |
| 45 |
test0(S({1, 2}), is2, S({1}), 1); |
| 46 |
test0(S({1, 2}), is3, S({1, 2}), 0); |
| 47 |
test0(S({1, 1}), is1, S(), 2); |
| 48 |
test0(S({1, 1}), is3, S({1, 1}), 0); |
| 49 |
|
| 50 |
test0(S({1, 2, 3}), is1, S({2, 3}), 1); |
| 51 |
test0(S({1, 2, 3}), is2, S({1, 3}), 1); |
| 52 |
test0(S({1, 2, 3}), is3, S({1, 2}), 1); |
| 53 |
test0(S({1, 2, 3}), is4, S({1, 2, 3}), 0); |
| 54 |
|
| 55 |
test0(S({1, 1, 1}), is1, S(), 3); |
| 56 |
test0(S({1, 1, 1}), is2, S({1, 1, 1}), 0); |
| 57 |
test0(S({1, 1, 2}), is1, S({2}), 2); |
| 58 |
test0(S({1, 1, 2}), is2, S({1, 1}), 1); |
| 59 |
test0(S({1, 1, 2}), is3, S({1, 1, 2}), 0); |
| 60 |
test0(S({1, 2, 2}), is1, S({2, 2}), 1); |
| 61 |
test0(S({1, 2, 2}), is2, S({1}), 2); |
| 62 |
test0(S({1, 2, 2}), is3, S({1, 2, 2}), 0); |
| 63 |
|
| 64 |
test0(S({1, 2, 3}), True, S(), 3); |
| 65 |
test0(S({1, 2, 3}), False, S({1, 2, 3}), 0); |
| 66 |
} |
| 67 |
|
| 68 |
TEST_CONSTEXPR_CXX20 bool tests() |
| 69 |
{ |
| 70 |
test<std::vector<int>>(); |
| 71 |
test<std::vector<int, min_allocator<int>>> (); |
| 72 |
test<std::vector<int, test_allocator<int>>> (); |
| 73 |
test<std::vector<int, safe_allocator<int>>>(); |
| 74 |
|
| 75 |
test<std::vector<long>>(); |
| 76 |
test<std::vector<double>>(); |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
int main(int, char**) |
| 82 |
{ |
| 83 |
tests(); |
| 84 |
#if TEST_STD_VER > 17 |
| 85 |
static_assert(tests()); |
| 86 |
#endif |
| 87 |
return 0; |
| 88 |
} |