|
25 | 25 | #include <vector> |
26 | 26 |
|
27 | 27 | #include "almost_satisfies_types.h" |
| 28 | +#include "sized_allocator.h" |
28 | 29 | #include "test_iterators.h" |
29 | 30 | #include "test_macros.h" |
30 | 31 | #include "type_algorithms.h" |
@@ -237,6 +238,133 @@ constexpr bool test() { |
237 | 238 | assert(test_vector_bool(199)); |
238 | 239 | assert(test_vector_bool(256)); |
239 | 240 | } |
| 241 | + |
| 242 | + // Make sure std::ranges::copy behaves properly with std::vector<bool> iterators with custom size types. |
| 243 | + { |
| 244 | + //// Tests for std::ranges::copy with aligned bits |
| 245 | + |
| 246 | + { // Test the first (partial) word for uint8_t |
| 247 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 248 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 249 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 250 | + std::ranges::copy(std::ranges::subrange(in.begin() + 4, in.end()), out.begin() + 4); |
| 251 | + // assert(std::ranges::equal(in.begin() + 4, in.end(), out.begin() + 4, out.begin() + 6)); |
| 252 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 253 | + assert(in[i] == out[i]); |
| 254 | + } |
| 255 | + { // Test the last word for uint8_t |
| 256 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 257 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 258 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 259 | + std::ranges::copy(in, out.begin()); |
| 260 | + // assert(std::ranges::equal(in, std::ranges::subrange(out.begin(), out.begin() + in.size()))); |
| 261 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 262 | + assert(in[i] == out[i]); |
| 263 | + } |
| 264 | + { // Test middle words for uint8_t |
| 265 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 266 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 267 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 268 | + in[i] = false; |
| 269 | + std::vector<bool, Alloc> out(29, false, Alloc(1)); |
| 270 | + std::ranges::copy(in, out.begin()); |
| 271 | + // assert(std::ranges::equal(in, std::ranges::subrange(out.begin(), out.begin() + in.size()))); |
| 272 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 273 | + assert(in[i] == out[i]); |
| 274 | + } |
| 275 | + |
| 276 | + { // Test the first (partial) word for uint16_t |
| 277 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 278 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 279 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 280 | + std::ranges::copy(std::ranges::subrange(in.begin() + 4, in.end()), out.begin() + 4); |
| 281 | + // assert(std::ranges::equal(in.begin() + 4, in.end(), out.begin() + 4, out.begin() + 12)); |
| 282 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 283 | + assert(in[i] == out[i]); |
| 284 | + } |
| 285 | + { // Test the last word for uint16_t |
| 286 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 287 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 288 | + std::vector<bool, Alloc> out(32, false, Alloc(1)); |
| 289 | + std::ranges::copy(in, out.begin()); |
| 290 | + // assert(std::ranges::equal(in, std::ranges::subrange(out.begin(), out.begin() + in.size()))); |
| 291 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 292 | + assert(in[i] == out[i]); |
| 293 | + } |
| 294 | + { // Test middle words for uint16_t |
| 295 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 296 | + std::vector<bool, Alloc> in(48, true, Alloc(1)); |
| 297 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 298 | + in[i] = false; |
| 299 | + std::vector<bool, Alloc> out(55, false, Alloc(1)); |
| 300 | + std::ranges::copy(in, out.begin()); |
| 301 | + // assert(std::ranges::equal(in, std::ranges::subrange(out.begin(), out.begin() + in.size()))); |
| 302 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 303 | + assert(in[i] == out[i]); |
| 304 | + } |
| 305 | + |
| 306 | + //// Tests for std::ranges::copy with unaligned bits |
| 307 | + |
| 308 | + { // Test the first (partial) word for uint8_t |
| 309 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 310 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 311 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 312 | + std::ranges::copy(std::ranges::subrange(in.begin() + 4, in.end()), out.begin()); |
| 313 | + // assert(std::ranges::equal(in.begin() + 4, in.end(), out.begin(), out.begin() + 2)); |
| 314 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 315 | + assert(in[i] == out[i - 4]); |
| 316 | + } |
| 317 | + { // Test the last word for uint8_t |
| 318 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 319 | + std::vector<bool, Alloc> in(4, true, Alloc(1)); |
| 320 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 321 | + std::ranges::copy(in, out.begin() + 3); |
| 322 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 3, out.begin() + 3 + in.size())); |
| 323 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 324 | + assert(in[i] == out[i + 3]); |
| 325 | + } |
| 326 | + { // Test middle words for uint8_t |
| 327 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 328 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 329 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 330 | + in[i] = false; |
| 331 | + std::vector<bool, Alloc> out(24, false, Alloc(1)); |
| 332 | + std::ranges::copy(in, out.begin() + 4); |
| 333 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 4, out.begin() + 4 + in.size())); |
| 334 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 335 | + assert(in[i] == out[i + 4]); |
| 336 | + } |
| 337 | + |
| 338 | + { // Test the first (partial) word for uint16_t |
| 339 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 340 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 341 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 342 | + std::ranges::copy(std::ranges::subrange(in.begin() + 4, in.end()), out.begin()); |
| 343 | + // assert(std::ranges::equal(in.begin() + 4, in.end(), out.begin(), out.begin() + 8)); |
| 344 | + for (std::size_t i = 4; i < in.size(); ++i) |
| 345 | + assert(in[i] == out[i - 4]); |
| 346 | + } |
| 347 | + { // Test the last word for uint16_t |
| 348 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 349 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 350 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 351 | + std::ranges::copy(in, out.begin() + 3); |
| 352 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 3, out.begin() + 3 + in.size())); |
| 353 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 354 | + assert(in[i] == out[i + 3]); |
| 355 | + } |
| 356 | + { // Test the middle words for uint16_t |
| 357 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 358 | + std::vector<bool, Alloc> in(32, true, Alloc(1)); |
| 359 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 360 | + in[i] = false; |
| 361 | + std::vector<bool, Alloc> out(64, false, Alloc(1)); |
| 362 | + std::ranges::copy(in, out.begin() + 4); |
| 363 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 4, out.begin() + 4 + in.size())); |
| 364 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 365 | + assert(in[i] == out[i + 4]); |
| 366 | + } |
| 367 | + } |
240 | 368 | #endif |
241 | 369 |
|
242 | 370 | return true; |
|
0 commit comments