Add PR check to suggest alternatives to using undef (#118506)
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.rotate / rotate_copy.pass.cpp
blobc19f44a6d7eebd81b64b92d19e6fb23705b29f58
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<ForwardIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
12 // constexpr OutIter // constexpr after C++17
13 // rotate_copy(InIter first, InIter middle, InIter last, OutIter result);
15 #include <algorithm>
16 #include <cassert>
18 #include "test_macros.h"
19 #include "test_iterators.h"
22 template <class InIter, class OutIter>
23 TEST_CONSTEXPR_CXX20 void test() {
24 int ia[] = {0, 1, 2, 3};
25 const unsigned sa = sizeof(ia) / sizeof(ia[0]);
26 int ib[sa] = {0};
28 OutIter r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia), OutIter(ib));
29 assert(base(r) == ib);
31 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia + 1), OutIter(ib));
32 assert(base(r) == ib + 1);
33 assert(ib[0] == 0);
35 r = std::rotate_copy(InIter(ia), InIter(ia + 1), InIter(ia + 1), OutIter(ib));
36 assert(base(r) == ib + 1);
37 assert(ib[0] == 0);
39 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia + 2), OutIter(ib));
40 assert(base(r) == ib + 2);
41 assert(ib[0] == 0);
42 assert(ib[1] == 1);
44 r = std::rotate_copy(InIter(ia), InIter(ia + 1), InIter(ia + 2), OutIter(ib));
45 assert(base(r) == ib + 2);
46 assert(ib[0] == 1);
47 assert(ib[1] == 0);
49 r = std::rotate_copy(InIter(ia), InIter(ia + 2), InIter(ia + 2), OutIter(ib));
50 assert(base(r) == ib + 2);
51 assert(ib[0] == 0);
52 assert(ib[1] == 1);
54 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia + 3), OutIter(ib));
55 assert(base(r) == ib + 3);
56 assert(ib[0] == 0);
57 assert(ib[1] == 1);
58 assert(ib[2] == 2);
60 r = std::rotate_copy(InIter(ia), InIter(ia + 1), InIter(ia + 3), OutIter(ib));
61 assert(base(r) == ib + 3);
62 assert(ib[0] == 1);
63 assert(ib[1] == 2);
64 assert(ib[2] == 0);
66 r = std::rotate_copy(InIter(ia), InIter(ia + 2), InIter(ia + 3), OutIter(ib));
67 assert(base(r) == ib + 3);
68 assert(ib[0] == 2);
69 assert(ib[1] == 0);
70 assert(ib[2] == 1);
72 r = std::rotate_copy(InIter(ia), InIter(ia + 3), InIter(ia + 3), OutIter(ib));
73 assert(base(r) == ib + 3);
74 assert(ib[0] == 0);
75 assert(ib[1] == 1);
76 assert(ib[2] == 2);
78 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia + 4), OutIter(ib));
79 assert(base(r) == ib + 4);
80 assert(ib[0] == 0);
81 assert(ib[1] == 1);
82 assert(ib[2] == 2);
83 assert(ib[3] == 3);
85 r = std::rotate_copy(InIter(ia), InIter(ia + 1), InIter(ia + 4), OutIter(ib));
86 assert(base(r) == ib + 4);
87 assert(ib[0] == 1);
88 assert(ib[1] == 2);
89 assert(ib[2] == 3);
90 assert(ib[3] == 0);
92 r = std::rotate_copy(InIter(ia), InIter(ia + 2), InIter(ia + 4), OutIter(ib));
93 assert(base(r) == ib + 4);
94 assert(ib[0] == 2);
95 assert(ib[1] == 3);
96 assert(ib[2] == 0);
97 assert(ib[3] == 1);
99 r = std::rotate_copy(InIter(ia), InIter(ia + 3), InIter(ia + 4), OutIter(ib));
100 assert(base(r) == ib + 4);
101 assert(ib[0] == 3);
102 assert(ib[1] == 0);
103 assert(ib[2] == 1);
104 assert(ib[3] == 2);
106 r = std::rotate_copy(InIter(ia), InIter(ia + 4), InIter(ia + 4), OutIter(ib));
107 assert(base(r) == ib + 4);
108 assert(ib[0] == 0);
109 assert(ib[1] == 1);
110 assert(ib[2] == 2);
111 assert(ib[3] == 3);
114 int ints[] = {1, 3, 5, 2, 5, 6};
115 int const n_ints = sizeof(ints)/sizeof(int);
116 int zeros[n_ints] = {0};
118 const std::size_t N = 2;
119 const auto middle = std::begin(ints) + N;
120 auto it = std::rotate_copy(std::begin(ints), middle, std::end(ints), std::begin(zeros));
121 assert(std::distance(std::begin(zeros), it) == n_ints);
122 assert(std::equal(std::begin(ints), middle, std::begin(zeros) + n_ints - N));
123 assert(std::equal(middle, std::end(ints), std::begin(zeros)));
127 TEST_CONSTEXPR_CXX20 bool all_tests() {
128 test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
129 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
130 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
131 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
132 test<bidirectional_iterator<const int*>, int*>();
134 test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
135 test<random_access_iterator<const int*>, forward_iterator<int*> >();
136 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
137 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
138 test<random_access_iterator<const int*>, int*>();
140 test<const int*, cpp17_output_iterator<int*> >();
141 test<const int*, forward_iterator<int*> >();
142 test<const int*, bidirectional_iterator<int*> >();
143 test<const int*, random_access_iterator<int*> >();
144 test<const int*, int*>();
146 return true;
149 int main(int, char**) {
150 all_tests();
152 #if TEST_STD_VER > 17
153 static_assert(all_tests());
154 #endif
155 return 0;