1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template<BidirectionalIterator Iter, Predicate<auto, Iter::value_type> Pred>
12 // requires ShuffleIterator<Iter>
13 // && CopyConstructible<Pred>
15 // stable_partition(Iter first, Iter last, Pred pred);
22 #include "count_new.h"
23 #include "test_iterators.h"
24 #include "test_macros.h"
28 bool operator()(const int& i
) const {return i
& 1;}
33 bool operator()(const std::pair
<int,int>& p
) const
42 typedef std::pair
<int,int> P
;
56 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
57 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
58 assert(base(r
) == array
+ 4);
59 assert(array
[0] == P(1, 1));
60 assert(array
[1] == P(1, 2));
61 assert(array
[2] == P(3, 1));
62 assert(array
[3] == P(3, 2));
63 assert(array
[4] == P(0, 1));
64 assert(array
[5] == P(0, 2));
65 assert(array
[6] == P(2, 1));
66 assert(array
[7] == P(2, 2));
67 assert(array
[8] == P(4, 1));
68 assert(array
[9] == P(4, 2));
71 typedef std::pair
<int,int> P
;
85 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
86 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
87 assert(base(r
) == array
+ 4);
88 assert(array
[0] == P(1, 1));
89 assert(array
[1] == P(1, 2));
90 assert(array
[2] == P(3, 1));
91 assert(array
[3] == P(3, 2));
92 assert(array
[4] == P(0, 1));
93 assert(array
[5] == P(0, 2));
94 assert(array
[6] == P(2, 1));
95 assert(array
[7] == P(2, 2));
96 assert(array
[8] == P(4, 1));
97 assert(array
[9] == P(4, 2));
99 r
= std::stable_partition(Iter(array
), Iter(array
), odd_first());
100 assert(base(r
) == array
);
102 r
= std::stable_partition(Iter(array
), Iter(array
+1), odd_first());
103 assert(base(r
) == array
+1);
104 assert(array
[0] == P(1, 1));
106 r
= std::stable_partition(Iter(array
+4), Iter(array
+5), odd_first());
107 assert(base(r
) == array
+4);
108 assert(array
[4] == P(0, 1));
111 typedef std::pair
<int,int> P
;
125 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
126 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
127 assert(base(r
) == array
);
128 assert(array
[0] == P(0, 1));
129 assert(array
[1] == P(0, 2));
130 assert(array
[2] == P(2, 1));
131 assert(array
[3] == P(2, 2));
132 assert(array
[4] == P(4, 1));
133 assert(array
[5] == P(4, 2));
134 assert(array
[6] == P(6, 1));
135 assert(array
[7] == P(6, 2));
136 assert(array
[8] == P(8, 1));
137 assert(array
[9] == P(8, 2));
140 typedef std::pair
<int,int> P
;
154 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
155 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
156 assert(base(r
) == array
+ size
);
157 assert(array
[0] == P(1, 1));
158 assert(array
[1] == P(1, 2));
159 assert(array
[2] == P(3, 1));
160 assert(array
[3] == P(3, 2));
161 assert(array
[4] == P(5, 1));
162 assert(array
[5] == P(5, 2));
163 assert(array
[6] == P(7, 1));
164 assert(array
[7] == P(7, 2));
165 assert(array
[8] == P(9, 1));
166 assert(array
[9] == P(9, 2));
168 { // check all false but first true
169 typedef std::pair
<int,int> P
;
183 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
184 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
185 assert(base(r
) == array
+ 1);
186 assert(array
[0] == P(1, 1));
187 assert(array
[1] == P(0, 2));
188 assert(array
[2] == P(2, 1));
189 assert(array
[3] == P(2, 2));
190 assert(array
[4] == P(4, 1));
191 assert(array
[5] == P(4, 2));
192 assert(array
[6] == P(6, 1));
193 assert(array
[7] == P(6, 2));
194 assert(array
[8] == P(8, 1));
195 assert(array
[9] == P(8, 2));
197 { // check all false but last true
198 typedef std::pair
<int,int> P
;
212 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
213 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
214 assert(base(r
) == array
+ 1);
215 assert(array
[0] == P(1, 2));
216 assert(array
[1] == P(0, 1));
217 assert(array
[2] == P(0, 2));
218 assert(array
[3] == P(2, 1));
219 assert(array
[4] == P(2, 2));
220 assert(array
[5] == P(4, 1));
221 assert(array
[6] == P(4, 2));
222 assert(array
[7] == P(6, 1));
223 assert(array
[8] == P(6, 2));
224 assert(array
[9] == P(8, 1));
226 { // check all true but first false
227 typedef std::pair
<int,int> P
;
241 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
242 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
243 assert(base(r
) == array
+ size
-1);
244 assert(array
[0] == P(1, 2));
245 assert(array
[1] == P(3, 1));
246 assert(array
[2] == P(3, 2));
247 assert(array
[3] == P(5, 1));
248 assert(array
[4] == P(5, 2));
249 assert(array
[5] == P(7, 1));
250 assert(array
[6] == P(7, 2));
251 assert(array
[7] == P(9, 1));
252 assert(array
[8] == P(9, 2));
253 assert(array
[9] == P(0, 1));
255 { // check all true but last false
256 typedef std::pair
<int,int> P
;
270 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
271 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
272 assert(base(r
) == array
+ size
-1);
273 assert(array
[0] == P(1, 1));
274 assert(array
[1] == P(1, 2));
275 assert(array
[2] == P(3, 1));
276 assert(array
[3] == P(3, 2));
277 assert(array
[4] == P(5, 1));
278 assert(array
[5] == P(5, 2));
279 assert(array
[6] == P(7, 1));
280 assert(array
[7] == P(7, 2));
281 assert(array
[8] == P(9, 1));
282 assert(array
[9] == P(0, 2));
284 #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
285 // TODO: Re-enable this test once we're no longer using get_temporary_buffer().
286 // For now it trips up GCC due to the use of always_inline.
288 { // check that the algorithm still works when no memory is available
289 std::vector
<int> vec(150, 3);
291 getGlobalMemCounter()->throw_after
= 0;
292 std::stable_partition(vec
.begin(), vec
.end(), [](int i
) { return i
< 5; });
293 assert(std::is_partitioned(vec
.begin(), vec
.end(), [](int i
) { return i
< 5; }));
295 getGlobalMemCounter()->throw_after
= 0;
296 std::stable_partition(
297 forward_iterator
<int*>(vec
.data()), forward_iterator
<int*>(vec
.data() + vec
.size()), [](int i
) {
300 assert(std::is_partitioned(vec
.begin(), vec
.end(), [](int i
) { return i
< 5; }));
301 getGlobalMemCounter()->reset();
304 #endif // TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
307 #if TEST_STD_VER >= 11
312 bool operator()(const P
& p
) {return p
== 0;}
315 template <class Iter
>
319 const unsigned size
= 5;
320 std::unique_ptr
<int> array
[size
];
321 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), is_null());
322 assert(r
== Iter(array
+size
));
325 #endif // TEST_STD_VER >= 11
327 int main(int, char**)
329 test
<bidirectional_iterator
<std::pair
<int,int>*> >();
330 test
<random_access_iterator
<std::pair
<int,int>*> >();
331 test
<std::pair
<int,int>*>();
333 #if TEST_STD_VER >= 11
334 test1
<bidirectional_iterator
<std::unique_ptr
<int>*> >();