Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 1 | //===---------- llvm/unittest/Support/Casting.cpp - Casting tests --------===// |
| 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 | |
| 10 | #include "llvm/Support/raw_ostream.h" |
| 11 | #include "llvm/Support/Debug.h" |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 12 | //#define DEBUG_CAST_OPERATORS |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Casting.h" |
| 14 | |
| 15 | #include "gtest/gtest.h" |
| 16 | #include <cstdlib> |
| 17 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | // set up two example classes |
| 22 | // with conversion facility |
| 23 | // |
| 24 | struct bar { |
| 25 | bar() {} |
| 26 | //struct foo *baz(); |
| 27 | private: |
| 28 | bar(const bar &); |
| 29 | }; |
| 30 | struct foo { |
| 31 | void ext() const; |
| 32 | /* static bool classof(const bar *X) { |
| 33 | cerr << "Classof: " << X << "\n"; |
| 34 | return true; |
| 35 | }*/ |
| 36 | }; |
| 37 | |
| 38 | template <> struct isa_impl<foo,bar> { |
| 39 | static inline bool doit(const bar &Val) { |
| 40 | dbgs() << "Classof: " << &Val << "\n"; |
| 41 | return true; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /*foo *bar::baz() { |
| 46 | return cast<foo>(this); |
| 47 | }*/ |
| 48 | |
| 49 | |
| 50 | bar *fub(); |
| 51 | } // End llvm namespace |
| 52 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 53 | using namespace llvm; |
| 54 | |
| 55 | namespace { |
| 56 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 57 | const foo *null_foo = NULL; |
| 58 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 59 | extern bar &B1; |
| 60 | extern const bar *B2; |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 61 | // test various configurations of const |
| 62 | const bar &B3 = B1; |
| 63 | const bar *const B4 = B2; |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 64 | |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 65 | TEST(CastingTest, isa) { |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 66 | EXPECT_TRUE(isa<foo>(B1)); |
Gabor Greif | af8e2ef | 2010-07-20 16:38:12 +0000 | [diff] [blame] | 67 | EXPECT_TRUE(isa<foo>(B2)); |
| 68 | EXPECT_TRUE(isa<foo>(B3)); |
| 69 | EXPECT_TRUE(isa<foo>(B4)); |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 72 | TEST(CastingTest, cast) { |
| 73 | foo &F1 = cast<foo>(B1); |
| 74 | EXPECT_NE(&F1, null_foo); |
| 75 | const foo *F3 = cast<foo>(B2); |
| 76 | EXPECT_NE(F3, null_foo); |
| 77 | const foo *F4 = cast<foo>(B2); |
| 78 | EXPECT_NE(F4, null_foo); |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 79 | const foo &F5 = cast<foo>(B3); |
| 80 | EXPECT_NE(&F5, null_foo); |
| 81 | const foo *F6 = cast<foo>(B4); |
| 82 | EXPECT_NE(F6, null_foo); |
| 83 | foo *F7 = cast<foo>(fub()); |
| 84 | EXPECT_EQ(F7, null_foo); |
| 85 | |
| 86 | /* foo *F8 = B1.baz(); |
| 87 | EXPECT_NE(F8, null_foo);*/ |
Gabor Greif | 08993c0 | 2010-07-20 16:51:18 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(CastingTest, cast_or_null) { |
| 91 | const foo *F11 = cast_or_null<foo>(B2); |
| 92 | EXPECT_NE(F11, null_foo); |
| 93 | const foo *F12 = cast_or_null<foo>(B2); |
| 94 | EXPECT_NE(F12, null_foo); |
| 95 | const foo *F13 = cast_or_null<foo>(B4); |
| 96 | EXPECT_NE(F13, null_foo); |
| 97 | const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. |
| 98 | EXPECT_EQ(F14, null_foo); |
| 99 | } |
| 100 | |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 101 | // These lines are errors... |
| 102 | //foo *F20 = cast<foo>(B2); // Yields const foo* |
| 103 | //foo &F21 = cast<foo>(B3); // Yields const foo& |
| 104 | //foo *F22 = cast<foo>(B4); // Yields const foo* |
| 105 | //foo &F23 = cast_or_null<foo>(B1); |
| 106 | //const foo &F24 = cast_or_null<foo>(B3); |
| 107 | |
| 108 | |
Gabor Greif | ee57dae | 2010-07-20 16:32:20 +0000 | [diff] [blame] | 109 | bar B; |
| 110 | bar &B1 = B; |
| 111 | const bar *B2 = &B; |
| 112 | } // anonymous namespace |
Gabor Greif | e895097 | 2010-07-20 17:06:28 +0000 | [diff] [blame] | 113 | |
| 114 | bar *llvm::fub() { return 0; } |