| 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 | // <forward_list> |
| 10 | |
| 11 | // template <class Predicate> void remove_if(Predicate pred); // C++17 and before |
| 12 | // template <class Predicate> size_type remove_if(Predicate pred); // C++20 and after; constexpr since C++26 |
| 13 | |
| 14 | #include <forward_list> |
| 15 | #include <iterator> |
| 16 | #include <cassert> |
| 17 | #include <cstddef> |
| 18 | #include <functional> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "min_allocator.h" |
| 22 | #include "counting_predicates.h" |
| 23 | |
| 24 | template <class L, class Predicate> |
| 25 | TEST_CONSTEXPR_CXX26 void do_remove_if(L& l, Predicate pred, typename L::size_type expected) { |
| 26 | typename L::size_type old_size = std::distance(l.begin(), l.end()); |
| 27 | #if TEST_STD_VER > 17 |
| 28 | ASSERT_SAME_TYPE(decltype(l.remove_if(pred)), typename L::size_type); |
| 29 | assert(l.remove_if(pred) == expected); |
| 30 | #else |
| 31 | ASSERT_SAME_TYPE(decltype(l.remove_if(pred)), void); |
| 32 | l.remove_if(pred); |
| 33 | #endif |
| 34 | assert(old_size - std::distance(l.begin(), l.end()) == expected); |
| 35 | } |
| 36 | |
| 37 | TEST_CONSTEXPR bool g(int i) { return i < 3; } |
| 38 | |
| 39 | struct PredLWG526 { |
| 40 | TEST_CONSTEXPR_CXX20 PredLWG526(int i) : i_(i) {} |
| 41 | TEST_CONSTEXPR_CXX20 ~PredLWG526() { i_ = -32767; } |
| 42 | TEST_CONSTEXPR bool operator()(const PredLWG526& p) const { return p.i_ == i_; } |
| 43 | |
| 44 | TEST_CONSTEXPR bool operator==(int i) const { return i == i_; } |
| 45 | int i_; |
| 46 | }; |
| 47 | |
| 48 | TEST_CONSTEXPR_CXX26 bool test() { |
| 49 | { |
| 50 | typedef int T; |
| 51 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 52 | typedef std::forward_list<T> C; |
| 53 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
| 54 | const T t2[] = {5, 5, 5}; |
| 55 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 56 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 57 | Predicate cp(g); |
| 58 | do_remove_if(c1, std::ref(t&: cp), 4); |
| 59 | assert(c1 == c2); |
| 60 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 61 | } |
| 62 | { |
| 63 | typedef int T; |
| 64 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 65 | typedef std::forward_list<T> C; |
| 66 | const T t1[] = {0, 0, 0, 0}; |
| 67 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 68 | C c2; |
| 69 | Predicate cp(g); |
| 70 | do_remove_if(c1, std::ref(t&: cp), 4); |
| 71 | assert(c1 == c2); |
| 72 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 73 | } |
| 74 | { |
| 75 | typedef int T; |
| 76 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 77 | typedef std::forward_list<T> C; |
| 78 | const T t1[] = {5, 5, 5}; |
| 79 | const T t2[] = {5, 5, 5}; |
| 80 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 81 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 82 | Predicate cp(g); |
| 83 | do_remove_if(c1, std::ref(t&: cp), 0); |
| 84 | assert(c1 == c2); |
| 85 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 86 | } |
| 87 | { |
| 88 | typedef int T; |
| 89 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 90 | typedef std::forward_list<T> C; |
| 91 | C c1; |
| 92 | C c2; |
| 93 | Predicate cp(g); |
| 94 | do_remove_if(c1, std::ref(t&: cp), 0); |
| 95 | assert(c1 == c2); |
| 96 | assert(cp.count() == 0); |
| 97 | } |
| 98 | { |
| 99 | typedef int T; |
| 100 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 101 | typedef std::forward_list<T> C; |
| 102 | const T t1[] = {5, 5, 5, 0}; |
| 103 | const T t2[] = {5, 5, 5}; |
| 104 | C c1(std::begin(arr: t1), std::end(arr: t1)); |
| 105 | C c2(std::begin(arr: t2), std::end(arr: t2)); |
| 106 | Predicate cp(g); |
| 107 | do_remove_if(c1, std::ref(t&: cp), 1); |
| 108 | assert(c1 == c2); |
| 109 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 110 | } |
| 111 | |
| 112 | { // LWG issue #526 |
| 113 | int a1[] = {1, 2, 1, 3, 5, 8, 11}; |
| 114 | int a2[] = {2, 3, 5, 8, 11}; |
| 115 | std::forward_list<PredLWG526> c(a1, a1 + 7); |
| 116 | do_remove_if(c, std::ref(t&: c.front()), 2); |
| 117 | for (std::size_t i = 0; i < 5; ++i) { |
| 118 | assert(!c.empty()); |
| 119 | assert(c.front() == a2[i]); |
| 120 | c.pop_front(); |
| 121 | } |
| 122 | assert(c.empty()); |
| 123 | } |
| 124 | |
| 125 | #if TEST_STD_VER >= 11 |
| 126 | { |
| 127 | typedef int T; |
| 128 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 129 | typedef std::forward_list<T, min_allocator<T>> C; |
| 130 | const T t1[] = {0, 5, 5, 0, 0, 0, 5}; |
| 131 | const T t2[] = {5, 5, 5}; |
| 132 | C c1(std::begin(t1), std::end(t1)); |
| 133 | C c2(std::begin(t2), std::end(t2)); |
| 134 | Predicate cp(g); |
| 135 | do_remove_if(c1, std::ref(cp), 4); |
| 136 | assert(c1 == c2); |
| 137 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 138 | } |
| 139 | { |
| 140 | typedef int T; |
| 141 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 142 | typedef std::forward_list<T, min_allocator<T>> C; |
| 143 | const T t1[] = {0, 0, 0, 0}; |
| 144 | C c1(std::begin(t1), std::end(t1)); |
| 145 | C c2; |
| 146 | Predicate cp(g); |
| 147 | do_remove_if(c1, std::ref(cp), 4); |
| 148 | assert(c1 == c2); |
| 149 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 150 | } |
| 151 | { |
| 152 | typedef int T; |
| 153 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 154 | typedef std::forward_list<T, min_allocator<T>> C; |
| 155 | const T t1[] = {5, 5, 5}; |
| 156 | const T t2[] = {5, 5, 5}; |
| 157 | C c1(std::begin(t1), std::end(t1)); |
| 158 | C c2(std::begin(t2), std::end(t2)); |
| 159 | Predicate cp(g); |
| 160 | do_remove_if(c1, std::ref(cp), 0); |
| 161 | assert(c1 == c2); |
| 162 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 163 | } |
| 164 | { |
| 165 | typedef int T; |
| 166 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 167 | typedef std::forward_list<T, min_allocator<T>> C; |
| 168 | C c1; |
| 169 | C c2; |
| 170 | Predicate cp(g); |
| 171 | do_remove_if(c1, std::ref(cp), 0); |
| 172 | assert(c1 == c2); |
| 173 | assert(cp.count() == 0); |
| 174 | } |
| 175 | { |
| 176 | typedef int T; |
| 177 | typedef unary_counting_predicate<bool (*)(T), T> Predicate; |
| 178 | typedef std::forward_list<T, min_allocator<T>> C; |
| 179 | const T t1[] = {5, 5, 5, 0}; |
| 180 | const T t2[] = {5, 5, 5}; |
| 181 | C c1(std::begin(t1), std::end(t1)); |
| 182 | C c2(std::begin(t2), std::end(t2)); |
| 183 | Predicate cp(g); |
| 184 | do_remove_if(c1, std::ref(cp), 1); |
| 185 | assert(c1 == c2); |
| 186 | assert(cp.count() == static_cast<std::size_t>(std::distance(std::begin(t1), std::end(t1)))); |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | int main(int, char**) { |
| 194 | assert(test()); |
| 195 | #if TEST_STD_VER >= 26 |
| 196 | static_assert(test()); |
| 197 | #endif |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |