|
28 | 28 | #include <vector>
|
29 | 29 |
|
30 | 30 | #include "almost_satisfies_types.h"
|
| 31 | +#include "sized_allocator.h" |
31 | 32 | #include "test_iterators.h"
|
32 | 33 | #include "test_macros.h"
|
33 | 34 |
|
@@ -355,6 +356,133 @@ constexpr bool test() {
|
355 | 356 | assert(test_vector_bool(199));
|
356 | 357 | assert(test_vector_bool(256));
|
357 | 358 | }
|
| 359 | + |
| 360 | + // Make sure std::ranges::copy_backward behaves properly with std::vector<bool> iterators with custom size types. |
| 361 | + { |
| 362 | + //// Tests for std::ranges::copy_backward with aligned bits |
| 363 | + |
| 364 | + { // Test the first (partial) word for uint8_t |
| 365 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 366 | + std::vector<bool, Alloc> in(8, true, Alloc(1)); |
| 367 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 368 | + std::ranges::copy_backward(std::ranges::subrange(in.begin() + 2, in.end() - 2), out.end() - 2); |
| 369 | + // assert(std::ranges::equal(in.begin() + 2, in.end() - 2, out.begin() + 2, out.end() - 2)); |
| 370 | + for (std::size_t i = 2; i < static_cast<std::size_t>(in.size() - 2); ++i) |
| 371 | + assert(in[i] == out[i]); |
| 372 | + } |
| 373 | + { // Test the last word for uint8_t |
| 374 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 375 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 376 | + std::vector<bool, Alloc> out(15, false, Alloc(1)); |
| 377 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 3); |
| 378 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 3)); |
| 379 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 380 | + assert(in[i] == out[i]); |
| 381 | + } |
| 382 | + { // Test middle words for uint8_t |
| 383 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 384 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 385 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 386 | + in[i] = false; |
| 387 | + std::vector<bool, Alloc> out(29, false, Alloc(1)); |
| 388 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 5); |
| 389 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 5)); |
| 390 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 391 | + assert(in[i] == out[i]); |
| 392 | + } |
| 393 | + |
| 394 | + { // Test the first (partial) word for uint16_t |
| 395 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 396 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 397 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 398 | + std::ranges::copy_backward(std::ranges::subrange(in.begin() + 3, in.end() - 2), out.end() - 2); |
| 399 | + // assert(std::ranges::equal(in.begin() + 3, in.end() - 2, out.begin() + 3, out.end() - 2)); |
| 400 | + for (std::size_t i = 3; i < static_cast<std::size_t>(in.size() - 5); ++i) |
| 401 | + assert(in[i] == out[i]); |
| 402 | + } |
| 403 | + { // Test the last word for uint16_t |
| 404 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 405 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 406 | + std::vector<bool, Alloc> out(32, false, Alloc(1)); |
| 407 | + std::ranges::copy_backward(in.begin(), in.end(), out.end() - 8); |
| 408 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 8)); |
| 409 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 410 | + assert(in[i] == out[i]); |
| 411 | + } |
| 412 | + { // Test middle words for uint16_t |
| 413 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 414 | + std::vector<bool, Alloc> in(48, true, Alloc(1)); |
| 415 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 416 | + in[i] = false; |
| 417 | + std::vector<bool, Alloc> out(55, false, Alloc(1)); |
| 418 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 7); |
| 419 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin(), out.end() - 7)); |
| 420 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 421 | + assert(in[i] == out[i]); |
| 422 | + } |
| 423 | + |
| 424 | + // //// Tests for std::ranges::copy_backward with unaligned bits |
| 425 | + |
| 426 | + { // Test the first (partial) word for uint8_t |
| 427 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 428 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 429 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 430 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end() - 2), out.end() - 1); |
| 431 | + // assert(std::ranges::equal(in.begin(), in.end() - 2, out.begin() + 3, out.end() - 1)); |
| 432 | + for (std::size_t i = 0; i < static_cast<std::size_t>(in.size() - 2); ++i) |
| 433 | + assert(in[i] == out[i + 3]); |
| 434 | + } |
| 435 | + { // Test the last word for uint8_t |
| 436 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 437 | + std::vector<bool, Alloc> in(5, true, Alloc(1)); |
| 438 | + std::vector<bool, Alloc> out(8, false, Alloc(1)); |
| 439 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end()); |
| 440 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 3, out.end())); |
| 441 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 442 | + assert(in[i] == out[i + 3]); |
| 443 | + } |
| 444 | + { // Test middle words for uint8_t |
| 445 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 446 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 447 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 448 | + in[i] = false; |
| 449 | + std::vector<bool, Alloc> out(24, false, Alloc(1)); |
| 450 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 3); |
| 451 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 5, out.end() - 3)); |
| 452 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 453 | + assert(in[i] == out[i + 5]); |
| 454 | + } |
| 455 | + |
| 456 | + { // Test the first (partial) word for uint16_t |
| 457 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 458 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 459 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 460 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end() - 3), out.end() - 2); |
| 461 | + // assert(std::ranges::equal(in.begin(), in.end() - 3, out.begin() + 5, out.end() - 2)); |
| 462 | + for (std::size_t i = 0; i < static_cast<std::size_t>(in.size() - 3); ++i) |
| 463 | + assert(in[i] == out[i + 5]); |
| 464 | + } |
| 465 | + { // Test the last word for uint16_t |
| 466 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 467 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 468 | + std::vector<bool, Alloc> out(16, false, Alloc(1)); |
| 469 | + std::ranges::copy_backward(std::ranges::subrange(in.begin() + 3, in.end()), out.end() - 3); |
| 470 | + // assert(std::ranges::equal(in.begin() + 3, in.end(), out.begin(), out.end() - 1)); |
| 471 | + for (std::size_t i = 3; i < in.size(); ++i) |
| 472 | + assert(in[i] == out[i - 3]); |
| 473 | + } |
| 474 | + { // Test the middle words for uint16_t |
| 475 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 476 | + std::vector<bool, Alloc> in(32, true, Alloc(1)); |
| 477 | + for (std::size_t i = 0; i < in.size(); i += 2) |
| 478 | + in[i] = false; |
| 479 | + std::vector<bool, Alloc> out(64, false, Alloc(1)); |
| 480 | + std::ranges::copy_backward(std::ranges::subrange(in.begin(), in.end()), out.end() - 4); |
| 481 | + // assert(std::ranges::equal(in.begin(), in.end(), out.begin() + 28, out.end() - 4)); |
| 482 | + for (std::size_t i = 0; i < in.size(); ++i) |
| 483 | + assert(in[i] == out[i + 28]); |
| 484 | + } |
| 485 | + } |
358 | 486 | #endif
|
359 | 487 |
|
360 | 488 | return true;
|
|
0 commit comments