|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 |
| -#include "llvm/ADT/ilist.h" |
10 | 9 | #include "llvm/ADT/iterator.h"
|
11 | 10 | #include "llvm/ADT/ArrayRef.h"
|
12 | 11 | #include "llvm/ADT/STLExtras.h"
|
13 | 12 | #include "llvm/ADT/SmallVector.h"
|
| 13 | +#include "llvm/ADT/ilist.h" |
| 14 | +#include "gmock/gmock.h" |
14 | 15 | #include "gtest/gtest.h"
|
15 | 16 | #include <optional>
|
| 17 | +#include <type_traits> |
| 18 | +#include <vector> |
16 | 19 |
|
17 | 20 | using namespace llvm;
|
| 21 | +using testing::ElementsAre; |
18 | 22 |
|
19 | 23 | namespace {
|
20 | 24 |
|
@@ -430,6 +434,108 @@ TEST(ZipIteratorTest, ZipEqualBasic) {
|
430 | 434 | EXPECT_EQ(iters, 6u);
|
431 | 435 | }
|
432 | 436 |
|
| 437 | +template <typename T> |
| 438 | +constexpr bool IsConstRef = |
| 439 | + std::is_reference_v<T> && std::is_const_v<std::remove_reference_t<T>>; |
| 440 | + |
| 441 | +template <typename T> |
| 442 | +constexpr bool IsBoolConstRef = |
| 443 | + std::is_same_v<llvm::remove_cvref_t<T>, std::vector<bool>::const_reference>; |
| 444 | + |
| 445 | +/// Returns a `const` copy of the passed value. The `const` on the returned |
| 446 | +/// value is intentional here so that `MakeConst` can be used in range-for |
| 447 | +/// loops. |
| 448 | +template <typename T> const T MakeConst(T &&value) { |
| 449 | + return std::forward<T>(value); |
| 450 | +} |
| 451 | + |
| 452 | +TEST(ZipIteratorTest, ZipEqualConstCorrectness) { |
| 453 | + const std::vector<unsigned> c_first = {3, 1, 4}; |
| 454 | + std::vector<unsigned> first = c_first; |
| 455 | + const SmallVector<bool> c_second = {1, 1, 0}; |
| 456 | + SmallVector<bool> second = c_second; |
| 457 | + |
| 458 | + for (auto [a, b, c, d] : zip_equal(c_first, first, c_second, second)) { |
| 459 | + b = 0; |
| 460 | + d = true; |
| 461 | + static_assert(IsConstRef<decltype(a)>); |
| 462 | + static_assert(!IsConstRef<decltype(b)>); |
| 463 | + static_assert(IsConstRef<decltype(c)>); |
| 464 | + static_assert(!IsConstRef<decltype(d)>); |
| 465 | + } |
| 466 | + |
| 467 | + EXPECT_THAT(first, ElementsAre(0, 0, 0)); |
| 468 | + EXPECT_THAT(second, ElementsAre(true, true, true)); |
| 469 | + |
| 470 | + std::vector<bool> nemesis = {true, false, true}; |
| 471 | + const std::vector<bool> c_nemesis = nemesis; |
| 472 | + |
| 473 | + for (auto &&[a, b, c, d] : zip_equal(first, c_first, nemesis, c_nemesis)) { |
| 474 | + a = 2; |
| 475 | + c = true; |
| 476 | + static_assert(!IsConstRef<decltype(a)>); |
| 477 | + static_assert(IsConstRef<decltype(b)>); |
| 478 | + static_assert(!IsBoolConstRef<decltype(c)>); |
| 479 | + static_assert(IsBoolConstRef<decltype(d)>); |
| 480 | + } |
| 481 | + |
| 482 | + EXPECT_THAT(first, ElementsAre(2, 2, 2)); |
| 483 | + EXPECT_THAT(nemesis, ElementsAre(true, true, true)); |
| 484 | + |
| 485 | + unsigned iters = 0; |
| 486 | + for (const auto &[a, b, c, d] : |
| 487 | + zip_equal(first, c_first, nemesis, c_nemesis)) { |
| 488 | + static_assert(!IsConstRef<decltype(a)>); |
| 489 | + static_assert(IsConstRef<decltype(b)>); |
| 490 | + static_assert(!IsBoolConstRef<decltype(c)>); |
| 491 | + static_assert(IsBoolConstRef<decltype(d)>); |
| 492 | + ++iters; |
| 493 | + } |
| 494 | + EXPECT_EQ(iters, 3u); |
| 495 | + iters = 0; |
| 496 | + |
| 497 | + for (const auto &[a, b, c, d] : |
| 498 | + MakeConst(zip_equal(first, c_first, nemesis, c_nemesis))) { |
| 499 | + static_assert(!IsConstRef<decltype(a)>); |
| 500 | + static_assert(IsConstRef<decltype(b)>); |
| 501 | + static_assert(!IsBoolConstRef<decltype(c)>); |
| 502 | + static_assert(IsBoolConstRef<decltype(d)>); |
| 503 | + ++iters; |
| 504 | + } |
| 505 | + EXPECT_EQ(iters, 3u); |
| 506 | +} |
| 507 | + |
| 508 | +TEST(ZipIteratorTest, ZipEqualTemporaries) { |
| 509 | + unsigned iters = 0; |
| 510 | + |
| 511 | + // These temporary ranges get moved into the `tuple<...> storage;` inside |
| 512 | + // `zippy`. From then on, we can use references obtained from this storage to |
| 513 | + // access them. This does not rely on any lifetime extensions on the |
| 514 | + // temporaries passed to `zip_equal`. |
| 515 | + for (auto [a, b, c] : zip_equal(SmallVector<int>{1, 2, 3}, std::string("abc"), |
| 516 | + std::vector<bool>{true, false, true})) { |
| 517 | + a = 3; |
| 518 | + b = 'c'; |
| 519 | + c = false; |
| 520 | + static_assert(!IsConstRef<decltype(a)>); |
| 521 | + static_assert(!IsConstRef<decltype(b)>); |
| 522 | + static_assert(!IsBoolConstRef<decltype(c)>); |
| 523 | + ++iters; |
| 524 | + } |
| 525 | + EXPECT_EQ(iters, 3u); |
| 526 | + iters = 0; |
| 527 | + |
| 528 | + for (auto [a, b, c] : |
| 529 | + MakeConst(zip_equal(SmallVector<int>{1, 2, 3}, std::string("abc"), |
| 530 | + std::vector<bool>{true, false, true}))) { |
| 531 | + static_assert(IsConstRef<decltype(a)>); |
| 532 | + static_assert(IsConstRef<decltype(b)>); |
| 533 | + static_assert(IsBoolConstRef<decltype(c)>); |
| 534 | + ++iters; |
| 535 | + } |
| 536 | + EXPECT_EQ(iters, 3u); |
| 537 | +} |
| 538 | + |
433 | 539 | #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
434 | 540 | // Check that an assertion is triggered when ranges passed to `zip_equal` differ
|
435 | 541 | // in length.
|
|
0 commit comments