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