[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.rotate / rotate.pass.cpp
blob4424308ea04c664a9b8c808de0859ae503d61df4
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
11 // template<ShuffleIterator Iter>
12 // Iter
13 // rotate(Iter first, Iter middle, Iter last);
15 // Older compilers don't support std::is_constant_evaluated
16 // UNSUPPORTED: clang-4, clang-5, clang-6, clang-7, clang-8
17 // UNSUPPORTED: apple-clang-9, apple-clang-10
19 #include <algorithm>
20 #include <cassert>
21 #include <memory>
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 template <class Iter>
27 TEST_CONSTEXPR_CXX17 bool
28 test()
30 int ia[] = {0};
31 const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
32 Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
33 assert(base(r) == ia);
34 assert(ia[0] == 0);
35 r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
36 assert(base(r) == ia+sa);
37 assert(ia[0] == 0);
38 r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
39 assert(base(r) == ia);
40 assert(ia[0] == 0);
42 int ib[] = {0, 1};
43 const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
44 r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
45 assert(base(r) == ib+sb);
46 assert(ib[0] == 0);
47 assert(ib[1] == 1);
48 r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
49 assert(base(r) == ib+1);
50 assert(ib[0] == 1);
51 assert(ib[1] == 0);
52 r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
53 assert(base(r) == ib);
54 assert(ib[0] == 1);
55 assert(ib[1] == 0);
57 int ic[] = {0, 1, 2};
58 const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
59 r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
60 assert(base(r) == ic+sc);
61 assert(ic[0] == 0);
62 assert(ic[1] == 1);
63 assert(ic[2] == 2);
64 r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
65 assert(base(r) == ic+2);
66 assert(ic[0] == 1);
67 assert(ic[1] == 2);
68 assert(ic[2] == 0);
69 r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
70 assert(base(r) == ic+1);
71 assert(ic[0] == 0);
72 assert(ic[1] == 1);
73 assert(ic[2] == 2);
74 r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
75 assert(base(r) == ic);
76 assert(ic[0] == 0);
77 assert(ic[1] == 1);
78 assert(ic[2] == 2);
80 int id[] = {0, 1, 2, 3};
81 const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
82 r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
83 assert(base(r) == id+sd);
84 assert(id[0] == 0);
85 assert(id[1] == 1);
86 assert(id[2] == 2);
87 assert(id[3] == 3);
88 r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
89 assert(base(r) == id+3);
90 assert(id[0] == 1);
91 assert(id[1] == 2);
92 assert(id[2] == 3);
93 assert(id[3] == 0);
94 r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
95 assert(base(r) == id+2);
96 assert(id[0] == 3);
97 assert(id[1] == 0);
98 assert(id[2] == 1);
99 assert(id[3] == 2);
100 r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
101 assert(base(r) == id+1);
102 assert(id[0] == 2);
103 assert(id[1] == 3);
104 assert(id[2] == 0);
105 assert(id[3] == 1);
106 r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
107 assert(base(r) == id);
108 assert(id[0] == 2);
109 assert(id[1] == 3);
110 assert(id[2] == 0);
111 assert(id[3] == 1);
113 int ie[] = {0, 1, 2, 3, 4};
114 const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
115 r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
116 assert(base(r) == ie+se);
117 assert(ie[0] == 0);
118 assert(ie[1] == 1);
119 assert(ie[2] == 2);
120 assert(ie[3] == 3);
121 assert(ie[4] == 4);
122 r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
123 assert(base(r) == ie+4);
124 assert(ie[0] == 1);
125 assert(ie[1] == 2);
126 assert(ie[2] == 3);
127 assert(ie[3] == 4);
128 assert(ie[4] == 0);
129 r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
130 assert(base(r) == ie+3);
131 assert(ie[0] == 3);
132 assert(ie[1] == 4);
133 assert(ie[2] == 0);
134 assert(ie[3] == 1);
135 assert(ie[4] == 2);
136 r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
137 assert(base(r) == ie+2);
138 assert(ie[0] == 1);
139 assert(ie[1] == 2);
140 assert(ie[2] == 3);
141 assert(ie[3] == 4);
142 assert(ie[4] == 0);
143 r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
144 assert(base(r) == ie+1);
145 assert(ie[0] == 0);
146 assert(ie[1] == 1);
147 assert(ie[2] == 2);
148 assert(ie[3] == 3);
149 assert(ie[4] == 4);
150 r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
151 assert(base(r) == ie);
152 assert(ie[0] == 0);
153 assert(ie[1] == 1);
154 assert(ie[2] == 2);
155 assert(ie[3] == 3);
156 assert(ie[4] == 4);
158 int ig[] = {0, 1, 2, 3, 4, 5};
159 const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
160 r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
161 assert(base(r) == ig+sg);
162 assert(ig[0] == 0);
163 assert(ig[1] == 1);
164 assert(ig[2] == 2);
165 assert(ig[3] == 3);
166 assert(ig[4] == 4);
167 assert(ig[5] == 5);
168 r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
169 assert(base(r) == ig+5);
170 assert(ig[0] == 1);
171 assert(ig[1] == 2);
172 assert(ig[2] == 3);
173 assert(ig[3] == 4);
174 assert(ig[4] == 5);
175 assert(ig[5] == 0);
176 r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
177 assert(base(r) == ig+4);
178 assert(ig[0] == 3);
179 assert(ig[1] == 4);
180 assert(ig[2] == 5);
181 assert(ig[3] == 0);
182 assert(ig[4] == 1);
183 assert(ig[5] == 2);
184 r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
185 assert(base(r) == ig+3);
186 assert(ig[0] == 0);
187 assert(ig[1] == 1);
188 assert(ig[2] == 2);
189 assert(ig[3] == 3);
190 assert(ig[4] == 4);
191 assert(ig[5] == 5);
192 r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
193 assert(base(r) == ig+2);
194 assert(ig[0] == 4);
195 assert(ig[1] == 5);
196 assert(ig[2] == 0);
197 assert(ig[3] == 1);
198 assert(ig[4] == 2);
199 assert(ig[5] == 3);
200 r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
201 assert(base(r) == ig+1);
202 assert(ig[0] == 3);
203 assert(ig[1] == 4);
204 assert(ig[2] == 5);
205 assert(ig[3] == 0);
206 assert(ig[4] == 1);
207 assert(ig[5] == 2);
208 r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
209 assert(base(r) == ig);
210 assert(ig[0] == 3);
211 assert(ig[1] == 4);
212 assert(ig[2] == 5);
213 assert(ig[3] == 0);
214 assert(ig[4] == 1);
215 assert(ig[5] == 2);
217 return true;
220 #if TEST_STD_VER >= 11
222 template <class Iter>
223 void
224 test1()
226 std::unique_ptr<int> ia[1];
227 const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
228 for (int i = 0; i < sa; ++i)
229 ia[i].reset(new int(i));
230 Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
231 assert(base(r) == ia);
232 assert(*ia[0] == 0);
233 r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
234 assert(base(r) == ia+sa);
235 assert(*ia[0] == 0);
236 r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
237 assert(base(r) == ia);
238 assert(*ia[0] == 0);
240 std::unique_ptr<int> ib[2];
241 const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
242 for (int i = 0; i < sb; ++i)
243 ib[i].reset(new int(i));
244 r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
245 assert(base(r) == ib+sb);
246 assert(*ib[0] == 0);
247 assert(*ib[1] == 1);
248 r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
249 assert(base(r) == ib+1);
250 assert(*ib[0] == 1);
251 assert(*ib[1] == 0);
252 r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
253 assert(base(r) == ib);
254 assert(*ib[0] == 1);
255 assert(*ib[1] == 0);
257 std::unique_ptr<int> ic[3];
258 const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
259 for (int i = 0; i < sc; ++i)
260 ic[i].reset(new int(i));
261 r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
262 assert(base(r) == ic+sc);
263 assert(*ic[0] == 0);
264 assert(*ic[1] == 1);
265 assert(*ic[2] == 2);
266 r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
267 assert(base(r) == ic+2);
268 assert(*ic[0] == 1);
269 assert(*ic[1] == 2);
270 assert(*ic[2] == 0);
271 r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
272 assert(base(r) == ic+1);
273 assert(*ic[0] == 0);
274 assert(*ic[1] == 1);
275 assert(*ic[2] == 2);
276 r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
277 assert(base(r) == ic);
278 assert(*ic[0] == 0);
279 assert(*ic[1] == 1);
280 assert(*ic[2] == 2);
282 std::unique_ptr<int> id[4];
283 const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
284 for (int i = 0; i < sd; ++i)
285 id[i].reset(new int(i));
286 r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
287 assert(base(r) == id+sd);
288 assert(*id[0] == 0);
289 assert(*id[1] == 1);
290 assert(*id[2] == 2);
291 assert(*id[3] == 3);
292 r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
293 assert(base(r) == id+3);
294 assert(*id[0] == 1);
295 assert(*id[1] == 2);
296 assert(*id[2] == 3);
297 assert(*id[3] == 0);
298 r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
299 assert(base(r) == id+2);
300 assert(*id[0] == 3);
301 assert(*id[1] == 0);
302 assert(*id[2] == 1);
303 assert(*id[3] == 2);
304 r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
305 assert(base(r) == id+1);
306 assert(*id[0] == 2);
307 assert(*id[1] == 3);
308 assert(*id[2] == 0);
309 assert(*id[3] == 1);
310 r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
311 assert(base(r) == id);
312 assert(*id[0] == 2);
313 assert(*id[1] == 3);
314 assert(*id[2] == 0);
315 assert(*id[3] == 1);
317 std::unique_ptr<int> ie[5];
318 const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
319 for (int i = 0; i < se; ++i)
320 ie[i].reset(new int(i));
321 r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
322 assert(base(r) == ie+se);
323 assert(*ie[0] == 0);
324 assert(*ie[1] == 1);
325 assert(*ie[2] == 2);
326 assert(*ie[3] == 3);
327 assert(*ie[4] == 4);
328 r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
329 assert(base(r) == ie+4);
330 assert(*ie[0] == 1);
331 assert(*ie[1] == 2);
332 assert(*ie[2] == 3);
333 assert(*ie[3] == 4);
334 assert(*ie[4] == 0);
335 r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
336 assert(base(r) == ie+3);
337 assert(*ie[0] == 3);
338 assert(*ie[1] == 4);
339 assert(*ie[2] == 0);
340 assert(*ie[3] == 1);
341 assert(*ie[4] == 2);
342 r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
343 assert(base(r) == ie+2);
344 assert(*ie[0] == 1);
345 assert(*ie[1] == 2);
346 assert(*ie[2] == 3);
347 assert(*ie[3] == 4);
348 assert(*ie[4] == 0);
349 r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
350 assert(base(r) == ie+1);
351 assert(*ie[0] == 0);
352 assert(*ie[1] == 1);
353 assert(*ie[2] == 2);
354 assert(*ie[3] == 3);
355 assert(*ie[4] == 4);
356 r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
357 assert(base(r) == ie);
358 assert(*ie[0] == 0);
359 assert(*ie[1] == 1);
360 assert(*ie[2] == 2);
361 assert(*ie[3] == 3);
362 assert(*ie[4] == 4);
364 std::unique_ptr<int> ig[6];
365 const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
366 for (int i = 0; i < sg; ++i)
367 ig[i].reset(new int(i));
368 r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
369 assert(base(r) == ig+sg);
370 assert(*ig[0] == 0);
371 assert(*ig[1] == 1);
372 assert(*ig[2] == 2);
373 assert(*ig[3] == 3);
374 assert(*ig[4] == 4);
375 assert(*ig[5] == 5);
376 r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
377 assert(base(r) == ig+5);
378 assert(*ig[0] == 1);
379 assert(*ig[1] == 2);
380 assert(*ig[2] == 3);
381 assert(*ig[3] == 4);
382 assert(*ig[4] == 5);
383 assert(*ig[5] == 0);
384 r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
385 assert(base(r) == ig+4);
386 assert(*ig[0] == 3);
387 assert(*ig[1] == 4);
388 assert(*ig[2] == 5);
389 assert(*ig[3] == 0);
390 assert(*ig[4] == 1);
391 assert(*ig[5] == 2);
392 r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
393 assert(base(r) == ig+3);
394 assert(*ig[0] == 0);
395 assert(*ig[1] == 1);
396 assert(*ig[2] == 2);
397 assert(*ig[3] == 3);
398 assert(*ig[4] == 4);
399 assert(*ig[5] == 5);
400 r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
401 assert(base(r) == ig+2);
402 assert(*ig[0] == 4);
403 assert(*ig[1] == 5);
404 assert(*ig[2] == 0);
405 assert(*ig[3] == 1);
406 assert(*ig[4] == 2);
407 assert(*ig[5] == 3);
408 r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
409 assert(base(r) == ig+1);
410 assert(*ig[0] == 3);
411 assert(*ig[1] == 4);
412 assert(*ig[2] == 5);
413 assert(*ig[3] == 0);
414 assert(*ig[4] == 1);
415 assert(*ig[5] == 2);
416 r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
417 assert(base(r) == ig);
418 assert(*ig[0] == 3);
419 assert(*ig[1] == 4);
420 assert(*ig[2] == 5);
421 assert(*ig[3] == 0);
422 assert(*ig[4] == 1);
423 assert(*ig[5] == 2);
426 #endif // TEST_STD_VER >= 11
428 int main(int, char**)
430 test<forward_iterator<int*> >();
431 test<bidirectional_iterator<int*> >();
432 test<random_access_iterator<int*> >();
433 test<int*>();
435 #if TEST_STD_VER >= 11
437 test1<forward_iterator<std::unique_ptr<int>*> >();
438 test1<bidirectional_iterator<std::unique_ptr<int>*> >();
439 test1<random_access_iterator<std::unique_ptr<int>*> >();
440 test1<std::unique_ptr<int>*>();
442 #endif
444 #if TEST_STD_VER > 17
445 static_assert(test<forward_iterator<int*> >());
446 static_assert(test<bidirectional_iterator<int*> >());
447 static_assert(test<random_access_iterator<int*> >());
448 static_assert(test<int*>());
449 #endif // TEST_STD_VER > 17
451 return 0;