| 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 |
|
| 9 |
// <list> |
| 10 |
|
| 11 |
// template <class Pred> void remove_if(Pred pred); // before C++20 |
| 12 |
// template <class Pred> size_type remove_if(Pred pred); // c++20 and later |
| 13 |
|
| 14 |
#include <list> |
| 15 |
#include <cassert> |
| 16 |
#include <functional> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "min_allocator.h" |
| 20 |
#include "counting_predicates.h" |
| 21 |
|
| 22 |
bool even(int i) |
| 23 |
{ |
| 24 |
return i % 2 == 0; |
| 25 |
} |
| 26 |
|
| 27 |
bool g(int i) |
| 28 |
{ |
| 29 |
return i < 3; |
| 30 |
} |
| 31 |
|
| 32 |
struct PredLWG526 { |
| 33 |
PredLWG526(int i) : i_(i) {} |
| 34 |
~PredLWG526() { i_ = -32767; } |
| 35 |
bool operator()(const PredLWG526& p) const { return p.i_ == i_; } |
| 36 |
|
| 37 |
bool operator==(int i) const { return i == i_; } |
| 38 |
int i_; |
| 39 |
}; |
| 40 |
|
| 41 |
typedef unary_counting_predicate<bool(*)(int), int> Predicate; |
| 42 |
|
| 43 |
int main(int, char**) |
| 44 |
{ |
| 45 |
{ |
| 46 |
int a1[] = {1, 2, 3, 4}; |
| 47 |
int a2[] = {3, 4}; |
| 48 |
typedef std::list<int> L; |
| 49 |
L c(a1, a1+4); |
| 50 |
Predicate cp(g); |
| 51 |
#if TEST_STD_VER > 17 |
| 52 |
ASSERT_SAME_TYPE(L::size_type, decltype(c.remove_if(std::ref(cp)))); |
| 53 |
assert(c.remove_if(std::ref(cp)) == 2); |
| 54 |
#else |
| 55 |
ASSERT_SAME_TYPE(void, decltype(c.remove_if(std::ref(cp)))); |
| 56 |
c.remove_if(std::ref(cp)); |
| 57 |
#endif |
| 58 |
assert(c == std::list<int>(a2, a2+2)); |
| 59 |
assert(cp.count() == 4); |
| 60 |
} |
| 61 |
{ |
| 62 |
int a1[] = {1, 2, 3, 4}; |
| 63 |
int a2[] = {1, 3}; |
| 64 |
std::list<int> c(a1, a1+4); |
| 65 |
Predicate cp(even); |
| 66 |
#if TEST_STD_VER > 17 |
| 67 |
assert(c.remove_if(std::ref(cp)) == 2); |
| 68 |
#else |
| 69 |
c.remove_if(std::ref(cp)); |
| 70 |
#endif |
| 71 |
assert(c == std::list<int>(a2, a2+2)); |
| 72 |
assert(cp.count() == 4); |
| 73 |
} |
| 74 |
{ // LWG issue #526 |
| 75 |
int a1[] = {1, 2, 1, 3, 5, 8, 11}; |
| 76 |
int a2[] = {2, 3, 5, 8, 11}; |
| 77 |
std::list<PredLWG526> c(a1, a1 + 7); |
| 78 |
c.remove_if(std::ref(c.front())); |
| 79 |
assert(c.size() == 5); |
| 80 |
for (std::size_t i = 0; i < c.size(); ++i) |
| 81 |
{ |
| 82 |
assert(c.front() == a2[i]); |
| 83 |
c.pop_front(); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#if TEST_STD_VER >= 11 |
| 88 |
{ |
| 89 |
int a1[] = {1, 2, 3, 4}; |
| 90 |
int a2[] = {3, 4}; |
| 91 |
std::list<int, min_allocator<int>> c(a1, a1+4); |
| 92 |
Predicate cp(g); |
| 93 |
#if TEST_STD_VER > 17 |
| 94 |
assert(c.remove_if(std::ref(cp)) == 2); |
| 95 |
#else |
| 96 |
c.remove_if(std::ref(cp)); |
| 97 |
#endif |
| 98 |
assert((c == std::list<int, min_allocator<int>>(a2, a2+2))); |
| 99 |
assert(cp.count() == 4); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |