Gabor Greif | dd8ba3c | 2010-07-20 19:35:55 +0000 | [diff] [blame] | 1 | //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===// |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Casting.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 11 | #include "llvm/IR/User.h" |
Gabor Greif | dd8ba3c | 2010-07-20 19:35:55 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "llvm/Support/raw_ostream.h" |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | #include <cstdlib> |
| 16 | |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 17 | namespace llvm { |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 18 | // Used to test illegal cast. If a cast doesn't match any of the "real" ones, |
| 19 | // it will match this one. |
| 20 | struct IllegalCast; |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 21 | template <typename T> IllegalCast *cast(...) { return nullptr; } |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 22 | |
| 23 | // set up two example classes |
| 24 | // with conversion facility |
| 25 | // |
| 26 | struct bar { |
| 27 | bar() {} |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 28 | struct foo *baz(); |
| 29 | struct foo *caz(); |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 30 | struct foo *daz(); |
| 31 | struct foo *naz(); |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 32 | private: |
| 33 | bar(const bar &); |
| 34 | }; |
| 35 | struct foo { |
| 36 | void ext() const; |
| 37 | /* static bool classof(const bar *X) { |
| 38 | cerr << "Classof: " << X << "\n"; |
| 39 | return true; |
| 40 | }*/ |
| 41 | }; |
| 42 | |
Zachary Turner | 2bb94cd | 2017-04-12 19:59:37 +0000 | [diff] [blame] | 43 | struct base { |
| 44 | virtual ~base() {} |
| 45 | }; |
| 46 | |
| 47 | struct derived : public base { |
| 48 | static bool classof(const base *B) { return true; } |
| 49 | }; |
| 50 | |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 51 | template <> struct isa_impl<foo, bar> { |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 52 | static inline bool doit(const bar &Val) { |
| 53 | dbgs() << "Classof: " << &Val << "\n"; |
| 54 | return true; |
| 55 | } |
| 56 | }; |
| 57 | |
Zachary Turner | 2bb94cd | 2017-04-12 19:59:37 +0000 | [diff] [blame] | 58 | template <typename T> struct isa_impl<foo, T> { |
| 59 | static inline bool doit(const T &Val) { return false; } |
| 60 | }; |
| 61 | |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 62 | foo *bar::baz() { |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 63 | return cast<foo>(this); |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | foo *bar::caz() { |
| 67 | return cast_or_null<foo>(this); |
| 68 | } |
| 69 | |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 70 | foo *bar::daz() { |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 71 | return dyn_cast<foo>(this); |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | foo *bar::naz() { |
| 75 | return dyn_cast_or_null<foo>(this); |
| 76 | } |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | bar *fub(); |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 80 | |
| 81 | template <> struct simplify_type<foo> { |
| 82 | typedef int SimpleType; |
| 83 | static SimpleType getSimplifiedValue(foo &Val) { return 0; } |
| 84 | }; |
| 85 | |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 86 | } // End llvm namespace |
| 87 | |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 88 | using namespace llvm; |
| 89 | |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 90 | |
| 91 | // Test the peculiar behavior of Use in simplify_type. |
Benjamin Kramer | f04ddd0 | 2014-03-07 14:42:25 +0000 | [diff] [blame] | 92 | static_assert(std::is_same<simplify_type<Use>::SimpleType, Value *>::value, |
| 93 | "Use doesn't simplify correctly!"); |
| 94 | static_assert(std::is_same<simplify_type<Use *>::SimpleType, Value *>::value, |
| 95 | "Use doesn't simplify correctly!"); |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 96 | |
| 97 | // Test that a regular class behaves as expected. |
Benjamin Kramer | f04ddd0 | 2014-03-07 14:42:25 +0000 | [diff] [blame] | 98 | static_assert(std::is_same<simplify_type<foo>::SimpleType, int>::value, |
| 99 | "Unexpected simplify_type result!"); |
| 100 | static_assert(std::is_same<simplify_type<foo *>::SimpleType, foo *>::value, |
| 101 | "Unexpected simplify_type result!"); |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 102 | |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 103 | namespace { |
| 104 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 105 | const foo *null_foo = nullptr; |
Gabor Greif | 1804d30 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 106 | |
NAKAMURA Takumi | 3119440 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 107 | bar B; |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 108 | extern bar &B1; |
NAKAMURA Takumi | 3119440 | 2012-01-22 12:14:35 +0000 | [diff] [blame] | 109 | bar &B1 = B; |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 110 | extern const bar *B2; |
Gabor Greif | 1804d30 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 111 | // test various configurations of const |
| 112 | const bar &B3 = B1; |
| 113 | const bar *const B4 = B2; |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 114 | |
Gabor Greif | 3eae1b3 | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 115 | TEST(CastingTest, isa) { |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 116 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | 3eae1b3 | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 117 | EXPECT_TRUE(isa<foo>(B2)); |
| 118 | EXPECT_TRUE(isa<foo>(B3)); |
| 119 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Gabor Greif | 1804d30 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 122 | TEST(CastingTest, cast) { |
| 123 | foo &F1 = cast<foo>(B1); |
| 124 | EXPECT_NE(&F1, null_foo); |
| 125 | const foo *F3 = cast<foo>(B2); |
| 126 | EXPECT_NE(F3, null_foo); |
| 127 | const foo *F4 = cast<foo>(B2); |
| 128 | EXPECT_NE(F4, null_foo); |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 129 | const foo &F5 = cast<foo>(B3); |
| 130 | EXPECT_NE(&F5, null_foo); |
| 131 | const foo *F6 = cast<foo>(B4); |
| 132 | EXPECT_NE(F6, null_foo); |
Richard Smith | c4379c3 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 133 | // Can't pass null pointer to cast<>. |
| 134 | // foo *F7 = cast<foo>(fub()); |
| 135 | // EXPECT_EQ(F7, null_foo); |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 136 | foo *F8 = B1.baz(); |
| 137 | EXPECT_NE(F8, null_foo); |
Zachary Turner | 2bb94cd | 2017-04-12 19:59:37 +0000 | [diff] [blame] | 138 | |
| 139 | std::unique_ptr<const bar> BP(B2); |
| 140 | auto FP = cast<foo>(std::move(BP)); |
| 141 | static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value, |
| 142 | "Incorrect deduced return type!"); |
| 143 | EXPECT_NE(FP.get(), null_foo); |
| 144 | FP.release(); |
Gabor Greif | 1804d30 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST(CastingTest, cast_or_null) { |
| 148 | const foo *F11 = cast_or_null<foo>(B2); |
| 149 | EXPECT_NE(F11, null_foo); |
| 150 | const foo *F12 = cast_or_null<foo>(B2); |
| 151 | EXPECT_NE(F12, null_foo); |
| 152 | const foo *F13 = cast_or_null<foo>(B4); |
| 153 | EXPECT_NE(F13, null_foo); |
| 154 | const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. |
| 155 | EXPECT_EQ(F14, null_foo); |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 156 | foo *F15 = B1.caz(); |
| 157 | EXPECT_NE(F15, null_foo); |
Zachary Turner | 2bb94cd | 2017-04-12 19:59:37 +0000 | [diff] [blame] | 158 | |
| 159 | std::unique_ptr<const bar> BP(fub()); |
| 160 | auto FP = cast_or_null<foo>(std::move(BP)); |
| 161 | EXPECT_EQ(FP.get(), null_foo); |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | TEST(CastingTest, dyn_cast) { |
Gabor Greif | 7859581 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 165 | const foo *F1 = dyn_cast<foo>(B2); |
| 166 | EXPECT_NE(F1, null_foo); |
| 167 | const foo *F2 = dyn_cast<foo>(B2); |
| 168 | EXPECT_NE(F2, null_foo); |
| 169 | const foo *F3 = dyn_cast<foo>(B4); |
Gabor Greif | b9e6977 | 2010-07-22 15:24:48 +0000 | [diff] [blame] | 170 | EXPECT_NE(F3, null_foo); |
Richard Smith | c4379c3 | 2012-08-21 20:39:25 +0000 | [diff] [blame] | 171 | // Can't pass null pointer to dyn_cast<>. |
| 172 | // foo *F4 = dyn_cast<foo>(fub()); |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 173 | // EXPECT_EQ(F4, null_foo); |
| 174 | foo *F5 = B1.daz(); |
| 175 | EXPECT_NE(F5, null_foo); |
| 176 | } |
| 177 | |
| 178 | TEST(CastingTest, dyn_cast_or_null) { |
| 179 | const foo *F1 = dyn_cast_or_null<foo>(B2); |
| 180 | EXPECT_NE(F1, null_foo); |
| 181 | const foo *F2 = dyn_cast_or_null<foo>(B2); |
| 182 | EXPECT_NE(F2, null_foo); |
| 183 | const foo *F3 = dyn_cast_or_null<foo>(B4); |
| 184 | EXPECT_NE(F3, null_foo); |
| 185 | foo *F4 = dyn_cast_or_null<foo>(fub()); |
Gabor Greif | 7859581 | 2010-07-22 15:28:30 +0000 | [diff] [blame] | 186 | EXPECT_EQ(F4, null_foo); |
Gabor Greif | fd23a97 | 2010-07-22 15:37:20 +0000 | [diff] [blame] | 187 | foo *F5 = B1.naz(); |
| 188 | EXPECT_NE(F5, null_foo); |
Gabor Greif | 1804d30 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Zachary Turner | 2bb94cd | 2017-04-12 19:59:37 +0000 | [diff] [blame] | 191 | std::unique_ptr<derived> newd() { return llvm::make_unique<derived>(); } |
| 192 | std::unique_ptr<base> newb() { return llvm::make_unique<derived>(); } |
| 193 | |
| 194 | TEST(CastingTest, unique_dyn_cast) { |
| 195 | derived *OrigD = nullptr; |
| 196 | auto D = llvm::make_unique<derived>(); |
| 197 | OrigD = D.get(); |
| 198 | |
| 199 | // Converting from D to itself is valid, it should return a new unique_ptr |
| 200 | // and the old one should become nullptr. |
| 201 | auto NewD = unique_dyn_cast<derived>(D); |
| 202 | ASSERT_EQ(OrigD, NewD.get()); |
| 203 | ASSERT_EQ(nullptr, D); |
| 204 | |
| 205 | // Converting from D to B is valid, B should have a value and D should be |
| 206 | // nullptr. |
| 207 | auto B = unique_dyn_cast<base>(NewD); |
| 208 | ASSERT_EQ(OrigD, B.get()); |
| 209 | ASSERT_EQ(nullptr, NewD); |
| 210 | |
| 211 | // Converting from B to itself is valid, it should return a new unique_ptr |
| 212 | // and the old one should become nullptr. |
| 213 | auto NewB = unique_dyn_cast<base>(B); |
| 214 | ASSERT_EQ(OrigD, NewB.get()); |
| 215 | ASSERT_EQ(nullptr, B); |
| 216 | |
| 217 | // Converting from B to D is valid, D should have a value and B should be |
| 218 | // nullptr; |
| 219 | D = unique_dyn_cast<derived>(NewB); |
| 220 | ASSERT_EQ(OrigD, D.get()); |
| 221 | ASSERT_EQ(nullptr, NewB); |
| 222 | |
| 223 | // Converting between unrelated types should fail. The original value should |
| 224 | // remain unchanged and it should return nullptr. |
| 225 | auto F = unique_dyn_cast<foo>(D); |
| 226 | ASSERT_EQ(nullptr, F); |
| 227 | ASSERT_EQ(OrigD, D.get()); |
| 228 | |
| 229 | // All of the above should also hold for temporaries. |
| 230 | auto D2 = unique_dyn_cast<derived>(newd()); |
| 231 | EXPECT_NE(nullptr, D2); |
| 232 | |
| 233 | auto B2 = unique_dyn_cast<derived>(newb()); |
| 234 | EXPECT_NE(nullptr, B2); |
| 235 | |
| 236 | auto B3 = unique_dyn_cast<base>(newb()); |
| 237 | EXPECT_NE(nullptr, B3); |
| 238 | |
| 239 | auto F2 = unique_dyn_cast<foo>(newb()); |
| 240 | EXPECT_EQ(nullptr, F2); |
| 241 | } |
| 242 | |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 243 | // These lines are errors... |
| 244 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 245 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 246 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 247 | //foo &F23 = cast_or_null<foo>(B1); |
| 248 | //const foo &F24 = cast_or_null<foo>(B3); |
| 249 | |
Gabor Greif | 704524a | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 250 | const bar *B2 = &B; |
| 251 | } // anonymous namespace |
Gabor Greif | c4dc393 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 252 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 253 | bar *llvm::fub() { return nullptr; } |
Sean Silva | 35dd877 | 2012-10-11 23:30:40 +0000 | [diff] [blame] | 254 | |
| 255 | namespace { |
| 256 | namespace inferred_upcasting { |
| 257 | // This test case verifies correct behavior of inferred upcasts when the |
| 258 | // types are statically known to be OK to upcast. This is the case when, |
| 259 | // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`. |
| 260 | |
| 261 | // Note: This test will actually fail to compile without inferred |
| 262 | // upcasting. |
| 263 | |
| 264 | class Base { |
| 265 | public: |
| 266 | // No classof. We are testing that the upcast is inferred. |
| 267 | Base() {} |
| 268 | }; |
| 269 | |
| 270 | class Derived : public Base { |
| 271 | public: |
| 272 | Derived() {} |
| 273 | }; |
| 274 | |
| 275 | // Even with no explicit classof() in Base, we should still be able to cast |
| 276 | // Derived to its base class. |
| 277 | TEST(CastingTest, UpcastIsInferred) { |
| 278 | Derived D; |
| 279 | EXPECT_TRUE(isa<Base>(D)); |
| 280 | Base *BP = dyn_cast<Base>(&D); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 281 | EXPECT_TRUE(BP != nullptr); |
Sean Silva | 35dd877 | 2012-10-11 23:30:40 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | // This test verifies that the inferred upcast takes precedence over an |
| 286 | // explicitly written one. This is important because it verifies that the |
| 287 | // dynamic check gets optimized away. |
| 288 | class UseInferredUpcast { |
| 289 | public: |
| 290 | int Dummy; |
| 291 | static bool classof(const UseInferredUpcast *) { |
| 292 | return false; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | TEST(CastingTest, InferredUpcastTakesPrecedence) { |
| 297 | UseInferredUpcast UIU; |
| 298 | // Since the explicit classof() returns false, this will fail if the |
| 299 | // explicit one is used. |
| 300 | EXPECT_TRUE(isa<UseInferredUpcast>(&UIU)); |
| 301 | } |
| 302 | |
| 303 | } // end namespace inferred_upcasting |
| 304 | } // end anonymous namespace |
Rafael Espindola | 9ed1761 | 2013-07-18 02:42:40 +0000 | [diff] [blame] | 305 | // Test that we reject casts of temporaries (and so the illegal cast gets used). |
| 306 | namespace TemporaryCast { |
| 307 | struct pod {}; |
| 308 | IllegalCast *testIllegalCast() { return cast<foo>(pod()); } |
| 309 | } |
Duncan P. N. Exon Smith | 4030118 | 2014-11-24 03:13:02 +0000 | [diff] [blame] | 310 | |
| 311 | namespace { |
| 312 | namespace pointer_wrappers { |
| 313 | |
| 314 | struct Base { |
| 315 | bool IsDerived; |
| 316 | Base(bool IsDerived = false) : IsDerived(IsDerived) {} |
| 317 | }; |
| 318 | |
| 319 | struct Derived : Base { |
| 320 | Derived() : Base(true) {} |
| 321 | static bool classof(const Base *B) { return B->IsDerived; } |
| 322 | }; |
| 323 | |
| 324 | class PTy { |
| 325 | Base *B; |
| 326 | public: |
| 327 | PTy(Base *B) : B(B) {} |
Aaron Ballman | b46962f | 2015-02-15 22:00:20 +0000 | [diff] [blame] | 328 | explicit operator bool() const { return get(); } |
Duncan P. N. Exon Smith | 4030118 | 2014-11-24 03:13:02 +0000 | [diff] [blame] | 329 | Base *get() const { return B; } |
| 330 | }; |
| 331 | |
| 332 | } // end namespace pointer_wrappers |
| 333 | } // end namespace |
| 334 | |
| 335 | namespace llvm { |
| 336 | |
| 337 | template <> struct simplify_type<pointer_wrappers::PTy> { |
| 338 | typedef pointer_wrappers::Base *SimpleType; |
| 339 | static SimpleType getSimplifiedValue(pointer_wrappers::PTy &P) { |
| 340 | return P.get(); |
| 341 | } |
| 342 | }; |
| 343 | template <> struct simplify_type<const pointer_wrappers::PTy> { |
| 344 | typedef pointer_wrappers::Base *SimpleType; |
| 345 | static SimpleType getSimplifiedValue(const pointer_wrappers::PTy &P) { |
| 346 | return P.get(); |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | } // end namespace llvm |
| 351 | |
| 352 | namespace { |
| 353 | namespace pointer_wrappers { |
| 354 | |
| 355 | // Some objects. |
| 356 | pointer_wrappers::Base B; |
| 357 | pointer_wrappers::Derived D; |
| 358 | |
| 359 | // Mutable "smart" pointers. |
| 360 | pointer_wrappers::PTy MN(nullptr); |
| 361 | pointer_wrappers::PTy MB(&B); |
| 362 | pointer_wrappers::PTy MD(&D); |
| 363 | |
| 364 | // Const "smart" pointers. |
| 365 | const pointer_wrappers::PTy CN(nullptr); |
| 366 | const pointer_wrappers::PTy CB(&B); |
| 367 | const pointer_wrappers::PTy CD(&D); |
| 368 | |
| 369 | TEST(CastingTest, smart_isa) { |
| 370 | EXPECT_TRUE(!isa<pointer_wrappers::Derived>(MB)); |
| 371 | EXPECT_TRUE(!isa<pointer_wrappers::Derived>(CB)); |
| 372 | EXPECT_TRUE(isa<pointer_wrappers::Derived>(MD)); |
| 373 | EXPECT_TRUE(isa<pointer_wrappers::Derived>(CD)); |
| 374 | } |
| 375 | |
| 376 | TEST(CastingTest, smart_cast) { |
| 377 | EXPECT_TRUE(cast<pointer_wrappers::Derived>(MD) == &D); |
| 378 | EXPECT_TRUE(cast<pointer_wrappers::Derived>(CD) == &D); |
| 379 | } |
| 380 | |
| 381 | TEST(CastingTest, smart_cast_or_null) { |
| 382 | EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MN) == nullptr); |
| 383 | EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CN) == nullptr); |
| 384 | EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(MD) == &D); |
| 385 | EXPECT_TRUE(cast_or_null<pointer_wrappers::Derived>(CD) == &D); |
| 386 | } |
| 387 | |
| 388 | TEST(CastingTest, smart_dyn_cast) { |
| 389 | EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MB) == nullptr); |
| 390 | EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CB) == nullptr); |
| 391 | EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(MD) == &D); |
| 392 | EXPECT_TRUE(dyn_cast<pointer_wrappers::Derived>(CD) == &D); |
| 393 | } |
| 394 | |
| 395 | TEST(CastingTest, smart_dyn_cast_or_null) { |
| 396 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MN) == nullptr); |
| 397 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CN) == nullptr); |
| 398 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MB) == nullptr); |
| 399 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CB) == nullptr); |
| 400 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(MD) == &D); |
| 401 | EXPECT_TRUE(dyn_cast_or_null<pointer_wrappers::Derived>(CD) == &D); |
| 402 | } |
| 403 | |
| 404 | } // end namespace pointer_wrappers |
| 405 | } // end namespace |