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<ShuffleIterator 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
23 #include "test_macros.h"
24 #include "test_iterators.h"
27 TEST_CONSTEXPR_CXX17
bool
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
);
35 r
= std::rotate(Iter(ia
), Iter(ia
), Iter(ia
+sa
));
36 assert(base(r
) == ia
+sa
);
38 r
= std::rotate(Iter(ia
), Iter(ia
+sa
), Iter(ia
+sa
));
39 assert(base(r
) == ia
);
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
);
48 r
= std::rotate(Iter(ib
), Iter(ib
+1), Iter(ib
+sb
));
49 assert(base(r
) == ib
+1);
52 r
= std::rotate(Iter(ib
), Iter(ib
+sb
), Iter(ib
+sb
));
53 assert(base(r
) == ib
);
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
);
64 r
= std::rotate(Iter(ic
), Iter(ic
+1), Iter(ic
+sc
));
65 assert(base(r
) == ic
+2);
69 r
= std::rotate(Iter(ic
), Iter(ic
+2), Iter(ic
+sc
));
70 assert(base(r
) == ic
+1);
74 r
= std::rotate(Iter(ic
), Iter(ic
+sc
), Iter(ic
+sc
));
75 assert(base(r
) == ic
);
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
);
88 r
= std::rotate(Iter(id
), Iter(id
+1), Iter(id
+sd
));
89 assert(base(r
) == id
+3);
94 r
= std::rotate(Iter(id
), Iter(id
+2), Iter(id
+sd
));
95 assert(base(r
) == id
+2);
100 r
= std::rotate(Iter(id
), Iter(id
+3), Iter(id
+sd
));
101 assert(base(r
) == id
+1);
106 r
= std::rotate(Iter(id
), Iter(id
+sd
), Iter(id
+sd
));
107 assert(base(r
) == id
);
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
);
122 r
= std::rotate(Iter(ie
), Iter(ie
+1), Iter(ie
+se
));
123 assert(base(r
) == ie
+4);
129 r
= std::rotate(Iter(ie
), Iter(ie
+2), Iter(ie
+se
));
130 assert(base(r
) == ie
+3);
136 r
= std::rotate(Iter(ie
), Iter(ie
+3), Iter(ie
+se
));
137 assert(base(r
) == ie
+2);
143 r
= std::rotate(Iter(ie
), Iter(ie
+4), Iter(ie
+se
));
144 assert(base(r
) == ie
+1);
150 r
= std::rotate(Iter(ie
), Iter(ie
+se
), Iter(ie
+se
));
151 assert(base(r
) == ie
);
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
);
168 r
= std::rotate(Iter(ig
), Iter(ig
+1), Iter(ig
+sg
));
169 assert(base(r
) == ig
+5);
176 r
= std::rotate(Iter(ig
), Iter(ig
+2), Iter(ig
+sg
));
177 assert(base(r
) == ig
+4);
184 r
= std::rotate(Iter(ig
), Iter(ig
+3), Iter(ig
+sg
));
185 assert(base(r
) == ig
+3);
192 r
= std::rotate(Iter(ig
), Iter(ig
+4), Iter(ig
+sg
));
193 assert(base(r
) == ig
+2);
200 r
= std::rotate(Iter(ig
), Iter(ig
+5), Iter(ig
+sg
));
201 assert(base(r
) == ig
+1);
208 r
= std::rotate(Iter(ig
), Iter(ig
+sg
), Iter(ig
+sg
));
209 assert(base(r
) == ig
);
220 #if TEST_STD_VER >= 11
222 template <class Iter
>
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
);
233 r
= std::rotate(Iter(ia
), Iter(ia
), Iter(ia
+sa
));
234 assert(base(r
) == ia
+sa
);
236 r
= std::rotate(Iter(ia
), Iter(ia
+sa
), Iter(ia
+sa
));
237 assert(base(r
) == ia
);
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
);
248 r
= std::rotate(Iter(ib
), Iter(ib
+1), Iter(ib
+sb
));
249 assert(base(r
) == ib
+1);
252 r
= std::rotate(Iter(ib
), Iter(ib
+sb
), Iter(ib
+sb
));
253 assert(base(r
) == ib
);
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
);
266 r
= std::rotate(Iter(ic
), Iter(ic
+1), Iter(ic
+sc
));
267 assert(base(r
) == ic
+2);
271 r
= std::rotate(Iter(ic
), Iter(ic
+2), Iter(ic
+sc
));
272 assert(base(r
) == ic
+1);
276 r
= std::rotate(Iter(ic
), Iter(ic
+sc
), Iter(ic
+sc
));
277 assert(base(r
) == ic
);
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
);
292 r
= std::rotate(Iter(id
), Iter(id
+1), Iter(id
+sd
));
293 assert(base(r
) == id
+3);
298 r
= std::rotate(Iter(id
), Iter(id
+2), Iter(id
+sd
));
299 assert(base(r
) == id
+2);
304 r
= std::rotate(Iter(id
), Iter(id
+3), Iter(id
+sd
));
305 assert(base(r
) == id
+1);
310 r
= std::rotate(Iter(id
), Iter(id
+sd
), Iter(id
+sd
));
311 assert(base(r
) == id
);
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
);
328 r
= std::rotate(Iter(ie
), Iter(ie
+1), Iter(ie
+se
));
329 assert(base(r
) == ie
+4);
335 r
= std::rotate(Iter(ie
), Iter(ie
+2), Iter(ie
+se
));
336 assert(base(r
) == ie
+3);
342 r
= std::rotate(Iter(ie
), Iter(ie
+3), Iter(ie
+se
));
343 assert(base(r
) == ie
+2);
349 r
= std::rotate(Iter(ie
), Iter(ie
+4), Iter(ie
+se
));
350 assert(base(r
) == ie
+1);
356 r
= std::rotate(Iter(ie
), Iter(ie
+se
), Iter(ie
+se
));
357 assert(base(r
) == ie
);
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
);
376 r
= std::rotate(Iter(ig
), Iter(ig
+1), Iter(ig
+sg
));
377 assert(base(r
) == ig
+5);
384 r
= std::rotate(Iter(ig
), Iter(ig
+2), Iter(ig
+sg
));
385 assert(base(r
) == ig
+4);
392 r
= std::rotate(Iter(ig
), Iter(ig
+3), Iter(ig
+sg
));
393 assert(base(r
) == ig
+3);
400 r
= std::rotate(Iter(ig
), Iter(ig
+4), Iter(ig
+sg
));
401 assert(base(r
) == ig
+2);
408 r
= std::rotate(Iter(ig
), Iter(ig
+5), Iter(ig
+sg
));
409 assert(base(r
) == ig
+1);
416 r
= std::rotate(Iter(ig
), Iter(ig
+sg
), Iter(ig
+sg
));
417 assert(base(r
) == ig
);
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*> >();
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>*>();
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