|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "llvm/Support/Casting.h" |
| 10 | +#include "llvm/ADT/STLExtras.h" |
10 | 11 | #include "llvm/IR/User.h" |
11 | 12 | #include "llvm/Support/Debug.h" |
12 | 13 | #include "llvm/Support/raw_ostream.h" |
@@ -561,6 +562,53 @@ TEST(CastingTest, assertion_check_unique_ptr) { |
561 | 562 | << "Invalid cast of const ref did not cause an abort()"; |
562 | 563 | } |
563 | 564 |
|
| 565 | +TEST(Casting, StaticCastPredicate) { |
| 566 | + std::vector<uint32_t> Values{1, 2}; |
| 567 | + |
| 568 | + EXPECT_TRUE( |
| 569 | + all_of(map_range(Values, StaticCastTo<uint64_t>), |
| 570 | + [](const auto &V) { return sizeof(V) == sizeof(uint64_t); })); |
| 571 | +} |
| 572 | + |
| 573 | +TEST(Casting, LLVMRTTIPredicates) { |
| 574 | + struct Base { |
| 575 | + enum Kind { BK_Base, BK_Derived }; |
| 576 | + const Kind K; |
| 577 | + Base(Kind K = BK_Base) : K(K) {} |
| 578 | + Kind getKind() const { return K; } |
| 579 | + virtual ~Base() = default; |
| 580 | + }; |
| 581 | + |
| 582 | + struct Derived : public Base { |
| 583 | + Derived() : Base(BK_Derived) {} |
| 584 | + static bool classof(const Base *B) { return B->getKind() == BK_Derived; } |
| 585 | + }; |
| 586 | + |
| 587 | + Base B; |
| 588 | + Derived D; |
| 589 | + |
| 590 | + std::vector<Base *> RTTI{&B, &D, nullptr}; |
| 591 | + auto NonNull = drop_end(RTTI); |
| 592 | + auto OnlyDerived = drop_begin(NonNull); |
| 593 | + auto Casted = map_range(OnlyDerived, CastTo<Derived>); |
| 594 | + auto DynCasted = map_range(NonNull, DynCastTo<Derived>); |
| 595 | + auto DynCastedIfPresent = map_range(RTTI, DynCastIfPresentTo<Derived>); |
| 596 | + auto CastedIfPresent = map_range(drop_begin(RTTI), CastIfPresentTo<Derived>); |
| 597 | + |
| 598 | + auto Same = [](const auto &T) { |
| 599 | + const auto &[A, B] = T; |
| 600 | + return A == B; |
| 601 | + }; |
| 602 | + EXPECT_TRUE(all_of(zip_longest(Casted, std::vector<Derived *>{&D}), Same)); |
| 603 | + EXPECT_TRUE(all_of( |
| 604 | + zip_longest(DynCasted, std::vector<Derived *>{nullptr, &D}), Same)); |
| 605 | + EXPECT_TRUE(all_of(zip_longest(DynCastedIfPresent, |
| 606 | + std::vector<Derived *>{nullptr, &D, nullptr}), |
| 607 | + Same)); |
| 608 | + EXPECT_TRUE(all_of( |
| 609 | + zip_longest(CastedIfPresent, std::vector<Derived *>{&D, nullptr}), Same)); |
| 610 | +} |
| 611 | + |
564 | 612 | } // end namespace assertion_checks |
565 | 613 | #endif |
566 | 614 | } // end namespace |
0 commit comments