| 36 | class Baz : public Foo {}; |
| 37 | |
| 38 | TEST(CheckedCast, TestInvalidSubclassCast) { |
| 39 | static_assert(std::is_polymorphic<Foo>::value, "Foo is not polymorphic"); |
| 40 | |
| 41 | Foo foo; |
| 42 | FooSub foosub; |
| 43 | const Foo& foosubref = foosub; |
| 44 | Baz<double> baz; |
| 45 | const Foo& bazref = baz; |
| 46 | |
| 47 | #ifndef NDEBUG // debug mode |
| 48 | // illegal pointer cast |
| 49 | ASSERT_EQ(nullptr, checked_cast<Bar*>(&foo)); |
| 50 | |
| 51 | // illegal reference cast |
| 52 | ASSERT_THROW(checked_cast<const Bar&>(foosubref), std::bad_cast); |
| 53 | |
| 54 | // legal reference casts |
| 55 | ASSERT_NO_THROW(checked_cast<const FooSub&>(foosubref)); |
| 56 | ASSERT_NO_THROW(checked_cast<const Baz<double>&>(bazref)); |
| 57 | #else // release mode |
| 58 | // failure modes for the invalid casts occur at compile time |
| 59 | |
| 60 | // legal pointer cast |
| 61 | ASSERT_NE(nullptr, checked_cast<const FooSub*>(&foosubref)); |
| 62 | |
| 63 | // legal reference casts: this is static_cast in a release build, so ASSERT_NO_THROW |
| 64 | // doesn't make a whole lot of sense here. |
| 65 | auto& x = checked_cast<const FooSub&>(foosubref); |
| 66 | ASSERT_EQ(&foosubref, &x); |
| 67 | |
| 68 | auto& y = checked_cast<const Baz<double>&>(bazref); |
| 69 | ASSERT_EQ(&bazref, &y); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | } // namespace internal |
| 74 | } // namespace arrow |
nothing calls this directly
no outgoing calls
no test coverage detected