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