|
31 | 31 | #include <vector> |
32 | 32 |
|
33 | 33 | #include "almost_satisfies_types.h" |
| 34 | +#include "sized_allocator.h" |
34 | 35 | #include "test_iterators.h" |
35 | 36 | #include "test_macros.h" |
36 | 37 |
|
@@ -432,15 +433,123 @@ constexpr bool test() { |
432 | 433 | assert(projCount == 6); |
433 | 434 | } |
434 | 435 | } |
| 436 | + } |
| 437 | + |
| 438 | + { // Test vector<bool>::iterator optimization |
| 439 | + test_vector_bool<8>(); |
| 440 | + test_vector_bool<19>(); |
| 441 | + test_vector_bool<32>(); |
| 442 | + test_vector_bool<49>(); |
| 443 | + test_vector_bool<64>(); |
| 444 | + test_vector_bool<199>(); |
| 445 | + test_vector_bool<256>(); |
| 446 | + } |
| 447 | + |
| 448 | + // Make sure std::equal behaves properly with std::vector<bool> iterators with custom size types. |
| 449 | + // See issue: https://github.com/llvm/llvm-project/issues/126369. |
| 450 | + { |
| 451 | + //// Tests for std::equal with aligned bits |
| 452 | + |
| 453 | + { // Test the first (partial) word for uint8_t |
| 454 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 455 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 456 | + std::vector<bool, Alloc> expected(8, true, Alloc(1)); |
| 457 | + auto a = std::ranges::subrange(in.begin() + 4, in.end()); |
| 458 | + auto b = std::ranges::subrange(expected.begin() + 4, expected.begin() + 4 + a.size()); |
| 459 | + assert(std::ranges::equal(a, b)); |
| 460 | + } |
| 461 | + { // Test the last word for uint8_t |
| 462 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 463 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 464 | + std::vector<bool, Alloc> expected(16, true, Alloc(1)); |
| 465 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 466 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 467 | + assert(std::ranges::equal(a, b)); |
| 468 | + } |
| 469 | + { // Test middle words for uint8_t |
| 470 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 471 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 472 | + std::vector<bool, Alloc> expected(29, true, Alloc(1)); |
| 473 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 474 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 475 | + assert(std::ranges::equal(a, b)); |
| 476 | + } |
435 | 477 |
|
436 | | - { // Test vector<bool>::iterator optimization |
437 | | - test_vector_bool<8>(); |
438 | | - test_vector_bool<19>(); |
439 | | - test_vector_bool<32>(); |
440 | | - test_vector_bool<49>(); |
441 | | - test_vector_bool<64>(); |
442 | | - test_vector_bool<199>(); |
443 | | - test_vector_bool<256>(); |
| 478 | + { // Test the first (partial) word for uint16_t |
| 479 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 480 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 481 | + std::vector<bool, Alloc> expected(16, true, Alloc(1)); |
| 482 | + auto a = std::ranges::subrange(in.begin() + 4, in.end()); |
| 483 | + auto b = std::ranges::subrange(expected.begin() + 4, expected.begin() + 4 + a.size()); |
| 484 | + assert(std::ranges::equal(a, b)); |
| 485 | + } |
| 486 | + { // Test the last word for uint16_t |
| 487 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 488 | + std::vector<bool, Alloc> in(24, true, Alloc(1)); |
| 489 | + std::vector<bool, Alloc> expected(32, true, Alloc(1)); |
| 490 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 491 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 492 | + assert(std::ranges::equal(a, b)); |
| 493 | + } |
| 494 | + { // Test middle words for uint16_t |
| 495 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 496 | + std::vector<bool, Alloc> in(48, true, Alloc(1)); |
| 497 | + std::vector<bool, Alloc> expected(55, true, Alloc(1)); |
| 498 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 499 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 500 | + assert(std::ranges::equal(a, b)); |
| 501 | + } |
| 502 | + |
| 503 | + //// Tests for std::equal with unaligned bits |
| 504 | + |
| 505 | + { // Test the first (partial) word for uint8_t |
| 506 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 507 | + std::vector<bool, Alloc> in(6, true, Alloc(1)); |
| 508 | + std::vector<bool, Alloc> expected(8, true, Alloc(1)); |
| 509 | + auto a = std::ranges::subrange(in.begin() + 4, in.end()); |
| 510 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 511 | + assert(std::ranges::equal(a, b)); |
| 512 | + } |
| 513 | + { // Test the last word for uint8_t |
| 514 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 515 | + std::vector<bool, Alloc> in(4, true, Alloc(1)); |
| 516 | + std::vector<bool, Alloc> expected(8, true, Alloc(1)); |
| 517 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 518 | + auto b = std::ranges::subrange(expected.begin() + 3, expected.begin() + 3 + a.size()); |
| 519 | + assert(std::ranges::equal(a, b)); |
| 520 | + } |
| 521 | + { // Test middle words for uint8_t |
| 522 | + using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>; |
| 523 | + std::vector<bool, Alloc> in(16, true, Alloc(1)); |
| 524 | + std::vector<bool, Alloc> expected(24, true, Alloc(1)); |
| 525 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 526 | + auto b = std::ranges::subrange(expected.begin() + 4, expected.begin() + 4 + a.size()); |
| 527 | + assert(std::ranges::equal(a, b)); |
| 528 | + } |
| 529 | + |
| 530 | + { // Test the first (partial) word for uint16_t |
| 531 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 532 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 533 | + std::vector<bool, Alloc> expected(16, true, Alloc(1)); |
| 534 | + auto a = std::ranges::subrange(in.begin() + 4, in.end()); |
| 535 | + auto b = std::ranges::subrange(expected.begin(), expected.begin() + a.size()); |
| 536 | + assert(std::ranges::equal(a, b)); |
| 537 | + } |
| 538 | + { // Test the last word for uint16_t |
| 539 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 540 | + std::vector<bool, Alloc> in(12, true, Alloc(1)); |
| 541 | + std::vector<bool, Alloc> expected(16, true, Alloc(1)); |
| 542 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 543 | + auto b = std::ranges::subrange(expected.begin() + 3, expected.begin() + 3 + a.size()); |
| 544 | + assert(std::ranges::equal(a, b)); |
| 545 | + } |
| 546 | + { // Test the middle words for uint16_t |
| 547 | + using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>; |
| 548 | + std::vector<bool, Alloc> in(32, true, Alloc(1)); |
| 549 | + std::vector<bool, Alloc> expected(64, true, Alloc(1)); |
| 550 | + auto a = std::ranges::subrange(in.begin(), in.end()); |
| 551 | + auto b = std::ranges::subrange(expected.begin() + 4, expected.begin() + 4 + a.size()); |
| 552 | + assert(std::ranges::equal(a, b)); |
444 | 553 | } |
445 | 554 | } |
446 | 555 |
|
|
0 commit comments