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<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
12// constexpr OutIter // constexpr after C++17
13// copy(InIter first, InIter last, OutIter result);
14
15// XFAIL: FROZEN-CXX03-HEADERS-FIXME
16
17#include <algorithm>
18#include <cassert>
19#include <vector>
20
21#include "sized_allocator.h"
22#include "test_macros.h"
23#include "test_iterators.h"
24#include "type_algorithms.h"
25
26class PaddedBase {
27public:
28 TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
29
30 std::int16_t a_;
31 std::int8_t b_;
32};
33
34class Derived : public PaddedBase {
35public:
36 TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {}
37
38 std::int8_t c_;
39};
40
41template <class InIter>
42struct Test {
43 template <class OutIter>
44 TEST_CONSTEXPR_CXX20 void operator()() {
45 const unsigned N = 1000;
46 int ia[N] = {};
47 for (unsigned i = 0; i < N; ++i)
48 ia[i] = i;
49 int ib[N] = {0};
50
51 OutIter r = std::copy(InIter(ia), InIter(ia + N), OutIter(ib));
52 assert(base(r) == ib + N);
53 for (unsigned i = 0; i < N; ++i)
54 assert(ia[i] == ib[i]);
55 }
56};
57
58struct TestInIters {
59 template <class InIter>
60 TEST_CONSTEXPR_CXX20 void operator()() {
61 types::for_each(
62 types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(),
63 Test<InIter>());
64 }
65};
66
67TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
68 std::vector<bool> in(N, false);
69 for (std::size_t i = 0; i < N; i += 2)
70 in[i] = true;
71
72 { // Test copy with aligned bytes
73 std::vector<bool> out(N);
74 std::copy(first: in.begin(), last: in.end(), result: out.begin());
75 assert(in == out);
76 }
77 { // Test copy with unaligned bytes
78 std::vector<bool> out(N + 8);
79 std::copy(first: in.begin(), last: in.end(), result: out.begin() + 4);
80 for (std::size_t i = 0; i < N; ++i)
81 assert(out[i + 4] == in[i]);
82 }
83
84 return true;
85}
86
87TEST_CONSTEXPR_CXX20 bool test() {
88 types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
89
90 { // Make sure that padding bits aren't copied
91 Derived src(1, 2, 3);
92 Derived dst(4, 5, 6);
93 std::copy(static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst));
94 assert(dst.a_ == 1);
95 assert(dst.b_ == 2);
96 assert(dst.c_ == 6);
97 }
98 { // Make sure that overlapping ranges can be copied
99 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
100 std::copy(first: a + 3, last: a + 10, result: a);
101 int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
102 assert(std::equal(a, a + 10, expected));
103 }
104
105 { // Test vector<bool>::iterator optimization
106 assert(test_vector_bool(8));
107 assert(test_vector_bool(19));
108 assert(test_vector_bool(32));
109 assert(test_vector_bool(49));
110 assert(test_vector_bool(64));
111 assert(test_vector_bool(199));
112 assert(test_vector_bool(256));
113 }
114
115 // Validate std::copy with std::vector<bool> iterators and custom storage types.
116 // Ensure that assigned bits hold the intended values, while unassigned bits stay unchanged.
117 // Related issue: https://github.com/llvm/llvm-project/issues/131692.
118 {
119 //// Tests for std::copy with aligned bits
120
121 { // Test the first (partial) word for uint8_t
122 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
123 std::vector<bool, Alloc> in(8, false, Alloc(1));
124 std::vector<bool, Alloc> out(8, true, Alloc(1));
125 std::copy(in.begin() + 1, in.begin() + 2, out.begin() + 1); // out[1] = false
126 assert(out[1] == false);
127 for (std::size_t i = 0; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
128 if (i != 1)
129 assert(out[i] == true);
130 }
131 { // Test the last (partial) word for uint8_t
132 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
133 std::vector<bool, Alloc> in(8, false, Alloc(1));
134 std::vector<bool, Alloc> out(8, true, Alloc(1));
135 std::copy(in.begin(), in.begin() + 1, out.begin()); // out[0] = false
136 assert(out[0] == false);
137 for (std::size_t i = 1; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
138 assert(out[i] == true);
139 }
140 { // Test middle (whole) words for uint8_t
141 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
142 std::vector<bool, Alloc> in(32, true, Alloc(1));
143 for (std::size_t i = 0; i < in.size(); i += 2)
144 in[i] = false;
145 std::vector<bool, Alloc> out(32, false, Alloc(1));
146 std::copy(in.begin() + 4, in.end() - 4, out.begin() + 4);
147 for (std::size_t i = 4; i < static_cast<std::size_t>(in.size() - 4); ++i)
148 assert(in[i] == out[i]);
149 for (std::size_t i = 0; i < 4; ++i)
150 assert(out[i] == false);
151 for (std::size_t i = 28; i < out.size(); ++i)
152 assert(out[i] == false);
153 }
154
155 { // Test the first (partial) word for uint16_t
156 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
157 std::vector<bool, Alloc> in(16, false, Alloc(1));
158 std::vector<bool, Alloc> out(16, true, Alloc(1));
159 std::copy(in.begin() + 1, in.begin() + 3, out.begin() + 1); // out[1..2] = false
160 assert(out[1] == false);
161 assert(out[2] == false);
162 for (std::size_t i = 0; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
163 if (i != 1 && i != 2)
164 assert(out[i] == true);
165 }
166 { // Test the last (partial) word for uint16_t
167 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
168 std::vector<bool, Alloc> in(16, false, Alloc(1));
169 std::vector<bool, Alloc> out(16, true, Alloc(1));
170 std::copy(in.begin(), in.begin() + 2, out.begin()); // out[0..1] = false
171 assert(out[0] == false);
172 assert(out[1] == false);
173 for (std::size_t i = 2; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
174 assert(out[i] == true);
175 }
176 { // Test middle (whole) words for uint16_t
177 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
178 std::vector<bool, Alloc> in(64, true, Alloc(1));
179 for (std::size_t i = 0; i < in.size(); i += 2)
180 in[i] = false;
181 std::vector<bool, Alloc> out(64, false, Alloc(1));
182 std::copy(in.begin() + 8, in.end() - 8, out.begin() + 8);
183 for (std::size_t i = 8; i < static_cast<std::size_t>(in.size() - 8); ++i)
184 assert(in[i] == out[i]);
185 for (std::size_t i = 0; i < 8; ++i)
186 assert(out[i] == false);
187 for (std::size_t i = static_cast<std::size_t>(out.size() - 8); i < out.size(); ++i)
188 assert(out[i] == false);
189 }
190
191 //// Tests for std::copy with unaligned bits
192
193 { // Test the first (partial) word for uint8_t
194 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
195 std::vector<bool, Alloc> in(8, false, Alloc(1));
196 std::vector<bool, Alloc> out(8, true, Alloc(1));
197 std::copy(in.begin() + 7, in.end(), out.begin()); // out[0] = false
198 assert(out[0] == false);
199 for (std::size_t i = 1; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
200 assert(out[i] == true);
201 }
202 { // Test the last (partial) word for uint8_t
203 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
204 std::vector<bool, Alloc> in(8, false, Alloc(1));
205 std::vector<bool, Alloc> out(8, true, Alloc(1));
206 std::copy(in.begin(), in.begin() + 1, out.begin() + 2); // out[2] = false
207 assert(out[2] == false);
208 for (std::size_t i = 1; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
209 if (i != 2)
210 assert(out[i] == true);
211 }
212 { // Test middle (whole) words for uint8_t
213 using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
214 std::vector<bool, Alloc> in(36, true, Alloc(1));
215 for (std::size_t i = 0; i < in.size(); i += 2)
216 in[i] = false;
217 std::vector<bool, Alloc> out(40, false, Alloc(1));
218 std::copy(in.begin(), in.end(), out.begin() + 4);
219 for (std::size_t i = 0; i < in.size(); ++i)
220 assert(in[i] == out[i + 4]);
221 for (std::size_t i = 0; i < 4; ++i)
222 assert(out[i] == false);
223 }
224
225 { // Test the first (partial) word for uint16_t
226 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
227 std::vector<bool, Alloc> in(16, false, Alloc(1));
228 std::vector<bool, Alloc> out(16, true, Alloc(1));
229 std::copy(in.begin() + 14, in.end(), out.begin()); // out[0..1] = false
230 assert(out[0] == false);
231 assert(out[1] == false);
232 for (std::size_t i = 2; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
233 assert(out[i] == true);
234 }
235 { // Test the last (partial) word for uint16_t
236 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
237 std::vector<bool, Alloc> in(16, false, Alloc(1));
238 std::vector<bool, Alloc> out(16, true, Alloc(1));
239 std::copy(in.begin(), in.begin() + 2, out.begin() + 1); // out[1..2] = false
240 assert(out[1] == false);
241 assert(out[2] == false);
242 for (std::size_t i = 0; i < out.size(); ++i) // Ensure that unassigned bits remain unchanged
243 if (i != 1 && i != 2)
244 assert(out[i] == true);
245 }
246 { // Test middle (whole) words for uint16_t
247 using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
248 std::vector<bool, Alloc> in(72, true, Alloc(1));
249 for (std::size_t i = 0; i < in.size(); i += 2)
250 in[i] = false;
251 std::vector<bool, Alloc> out(80, false, Alloc(1));
252 std::copy(in.begin(), in.end(), out.begin() + 4);
253 for (std::size_t i = 0; i < in.size(); ++i)
254 assert(in[i] == out[i + 4]);
255 for (std::size_t i = 0; i < 4; ++i)
256 assert(out[i] == false);
257 for (std::size_t i = in.size() + 4; i < out.size(); ++i)
258 assert(out[i] == false);
259 }
260 }
261
262 return true;
263}
264
265int main(int, char**) {
266 test();
267
268#if TEST_STD_VER >= 20
269 static_assert(test());
270#endif
271
272 return 0;
273}
274

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