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

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp