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);
21 #include "test_macros.h"
22 #include "test_iterators.h"
26 bool operator()(const int& i
) const {return i
& 1;}
31 bool operator()(const std::pair
<int,int>& p
) const
40 typedef std::pair
<int,int> P
;
54 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
55 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
56 assert(base(r
) == array
+ 4);
57 assert(array
[0] == P(1, 1));
58 assert(array
[1] == P(1, 2));
59 assert(array
[2] == P(3, 1));
60 assert(array
[3] == P(3, 2));
61 assert(array
[4] == P(0, 1));
62 assert(array
[5] == P(0, 2));
63 assert(array
[6] == P(2, 1));
64 assert(array
[7] == P(2, 2));
65 assert(array
[8] == P(4, 1));
66 assert(array
[9] == P(4, 2));
69 typedef std::pair
<int,int> P
;
83 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
84 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
85 assert(base(r
) == array
+ 4);
86 assert(array
[0] == P(1, 1));
87 assert(array
[1] == P(1, 2));
88 assert(array
[2] == P(3, 1));
89 assert(array
[3] == P(3, 2));
90 assert(array
[4] == P(0, 1));
91 assert(array
[5] == P(0, 2));
92 assert(array
[6] == P(2, 1));
93 assert(array
[7] == P(2, 2));
94 assert(array
[8] == P(4, 1));
95 assert(array
[9] == P(4, 2));
97 r
= std::stable_partition(Iter(array
), Iter(array
), odd_first());
98 assert(base(r
) == array
);
100 r
= std::stable_partition(Iter(array
), Iter(array
+1), odd_first());
101 assert(base(r
) == array
+1);
102 assert(array
[0] == P(1, 1));
104 r
= std::stable_partition(Iter(array
+4), Iter(array
+5), odd_first());
105 assert(base(r
) == array
+4);
106 assert(array
[4] == P(0, 1));
109 typedef std::pair
<int,int> P
;
123 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
124 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
125 assert(base(r
) == array
);
126 assert(array
[0] == P(0, 1));
127 assert(array
[1] == P(0, 2));
128 assert(array
[2] == P(2, 1));
129 assert(array
[3] == P(2, 2));
130 assert(array
[4] == P(4, 1));
131 assert(array
[5] == P(4, 2));
132 assert(array
[6] == P(6, 1));
133 assert(array
[7] == P(6, 2));
134 assert(array
[8] == P(8, 1));
135 assert(array
[9] == P(8, 2));
138 typedef std::pair
<int,int> P
;
152 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
153 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
154 assert(base(r
) == array
+ size
);
155 assert(array
[0] == P(1, 1));
156 assert(array
[1] == P(1, 2));
157 assert(array
[2] == P(3, 1));
158 assert(array
[3] == P(3, 2));
159 assert(array
[4] == P(5, 1));
160 assert(array
[5] == P(5, 2));
161 assert(array
[6] == P(7, 1));
162 assert(array
[7] == P(7, 2));
163 assert(array
[8] == P(9, 1));
164 assert(array
[9] == P(9, 2));
166 { // check all false but first true
167 typedef std::pair
<int,int> P
;
181 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
182 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
183 assert(base(r
) == array
+ 1);
184 assert(array
[0] == P(1, 1));
185 assert(array
[1] == P(0, 2));
186 assert(array
[2] == P(2, 1));
187 assert(array
[3] == P(2, 2));
188 assert(array
[4] == P(4, 1));
189 assert(array
[5] == P(4, 2));
190 assert(array
[6] == P(6, 1));
191 assert(array
[7] == P(6, 2));
192 assert(array
[8] == P(8, 1));
193 assert(array
[9] == P(8, 2));
195 { // check all false but last true
196 typedef std::pair
<int,int> P
;
210 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
211 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
212 assert(base(r
) == array
+ 1);
213 assert(array
[0] == P(1, 2));
214 assert(array
[1] == P(0, 1));
215 assert(array
[2] == P(0, 2));
216 assert(array
[3] == P(2, 1));
217 assert(array
[4] == P(2, 2));
218 assert(array
[5] == P(4, 1));
219 assert(array
[6] == P(4, 2));
220 assert(array
[7] == P(6, 1));
221 assert(array
[8] == P(6, 2));
222 assert(array
[9] == P(8, 1));
224 { // check all true but first false
225 typedef std::pair
<int,int> P
;
239 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
240 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
241 assert(base(r
) == array
+ size
-1);
242 assert(array
[0] == P(1, 2));
243 assert(array
[1] == P(3, 1));
244 assert(array
[2] == P(3, 2));
245 assert(array
[3] == P(5, 1));
246 assert(array
[4] == P(5, 2));
247 assert(array
[5] == P(7, 1));
248 assert(array
[6] == P(7, 2));
249 assert(array
[7] == P(9, 1));
250 assert(array
[8] == P(9, 2));
251 assert(array
[9] == P(0, 1));
253 { // check all true but last false
254 typedef std::pair
<int,int> P
;
268 const unsigned size
= sizeof(array
)/sizeof(array
[0]);
269 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), odd_first());
270 assert(base(r
) == array
+ size
-1);
271 assert(array
[0] == P(1, 1));
272 assert(array
[1] == P(1, 2));
273 assert(array
[2] == P(3, 1));
274 assert(array
[3] == P(3, 2));
275 assert(array
[4] == P(5, 1));
276 assert(array
[5] == P(5, 2));
277 assert(array
[6] == P(7, 1));
278 assert(array
[7] == P(7, 2));
279 assert(array
[8] == P(9, 1));
280 assert(array
[9] == P(0, 2));
284 #if TEST_STD_VER >= 11
289 bool operator()(const P
& p
) {return p
== 0;}
292 template <class Iter
>
296 const unsigned size
= 5;
297 std::unique_ptr
<int> array
[size
];
298 Iter r
= std::stable_partition(Iter(array
), Iter(array
+size
), is_null());
299 assert(r
== Iter(array
+size
));
302 #endif // TEST_STD_VER >= 11
304 int main(int, char**)
306 test
<bidirectional_iterator
<std::pair
<int,int>*> >();
307 test
<random_access_iterator
<std::pair
<int,int>*> >();
308 test
<std::pair
<int,int>*>();
310 #if TEST_STD_VER >= 11
311 test1
<bidirectional_iterator
<std::unique_ptr
<int>*> >();