|
12 | 12 | // constexpr OutIter // constexpr after C++17
|
13 | 13 | // copy(InIter first, InIter last, OutIter result);
|
14 | 14 |
|
| 15 | +// XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 16 | + |
15 | 17 | #include <algorithm>
|
16 | 18 | #include <cassert>
|
17 | 19 | #include <vector>
|
18 | 20 |
|
| 21 | +#include "sized_allocator.h" |
19 | 22 | #include "test_macros.h"
|
20 | 23 | #include "test_iterators.h"
|
21 | 24 | #include "type_algorithms.h"
|
@@ -109,6 +112,153 @@ TEST_CONSTEXPR_CXX20 bool test() {
|
109 | 112 | assert(test_vector_bool(256));
|
110 | 113 | }
|
111 | 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 | + |
112 | 262 | return true;
|
113 | 263 | }
|
114 | 264 |
|
|
0 commit comments