1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Casting.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
19 // set up two example classes
20 // with conversion facility
33 /* static bool classof(const bar *X) {
34 cerr << "Classof: " << X << "\n";
39 template <> struct isa_impl
<foo
, bar
> {
40 static inline bool doit(const bar
&Val
) {
41 dbgs() << "Classof: " << &Val
<< "\n";
47 return cast
<foo
>(this);
51 return cast_or_null
<foo
>(this);
55 return dyn_cast
<foo
>(this);
59 return dyn_cast_or_null
<foo
>(this);
64 } // End llvm namespace
70 const foo
*null_foo
= NULL
;
74 // test various configurations of const
76 const bar
*const B4
= B2
;
78 TEST(CastingTest
, isa
) {
79 EXPECT_TRUE(isa
<foo
>(B1
));
80 EXPECT_TRUE(isa
<foo
>(B2
));
81 EXPECT_TRUE(isa
<foo
>(B3
));
82 EXPECT_TRUE(isa
<foo
>(B4
));
85 TEST(CastingTest
, cast
) {
86 foo
&F1
= cast
<foo
>(B1
);
87 EXPECT_NE(&F1
, null_foo
);
88 const foo
*F3
= cast
<foo
>(B2
);
89 EXPECT_NE(F3
, null_foo
);
90 const foo
*F4
= cast
<foo
>(B2
);
91 EXPECT_NE(F4
, null_foo
);
92 const foo
&F5
= cast
<foo
>(B3
);
93 EXPECT_NE(&F5
, null_foo
);
94 const foo
*F6
= cast
<foo
>(B4
);
95 EXPECT_NE(F6
, null_foo
);
96 foo
*F7
= cast
<foo
>(fub());
97 EXPECT_EQ(F7
, null_foo
);
99 EXPECT_NE(F8
, null_foo
);
102 TEST(CastingTest
, cast_or_null
) {
103 const foo
*F11
= cast_or_null
<foo
>(B2
);
104 EXPECT_NE(F11
, null_foo
);
105 const foo
*F12
= cast_or_null
<foo
>(B2
);
106 EXPECT_NE(F12
, null_foo
);
107 const foo
*F13
= cast_or_null
<foo
>(B4
);
108 EXPECT_NE(F13
, null_foo
);
109 const foo
*F14
= cast_or_null
<foo
>(fub()); // Shouldn't print.
110 EXPECT_EQ(F14
, null_foo
);
112 EXPECT_NE(F15
, null_foo
);
115 TEST(CastingTest
, dyn_cast
) {
116 const foo
*F1
= dyn_cast
<foo
>(B2
);
117 EXPECT_NE(F1
, null_foo
);
118 const foo
*F2
= dyn_cast
<foo
>(B2
);
119 EXPECT_NE(F2
, null_foo
);
120 const foo
*F3
= dyn_cast
<foo
>(B4
);
121 EXPECT_NE(F3
, null_foo
);
122 // foo *F4 = dyn_cast<foo>(fub()); // not permittible
123 // EXPECT_EQ(F4, null_foo);
125 EXPECT_NE(F5
, null_foo
);
128 TEST(CastingTest
, dyn_cast_or_null
) {
129 const foo
*F1
= dyn_cast_or_null
<foo
>(B2
);
130 EXPECT_NE(F1
, null_foo
);
131 const foo
*F2
= dyn_cast_or_null
<foo
>(B2
);
132 EXPECT_NE(F2
, null_foo
);
133 const foo
*F3
= dyn_cast_or_null
<foo
>(B4
);
134 EXPECT_NE(F3
, null_foo
);
135 foo
*F4
= dyn_cast_or_null
<foo
>(fub());
136 EXPECT_EQ(F4
, null_foo
);
138 EXPECT_NE(F5
, null_foo
);
141 // These lines are errors...
142 //foo *F20 = cast<foo>(B2); // Yields const foo*
143 //foo &F21 = cast<foo>(B3); // Yields const foo&
144 //foo *F22 = cast<foo>(B4); // Yields const foo*
145 //foo &F23 = cast_or_null<foo>(B1);
146 //const foo &F24 = cast_or_null<foo>(B3);
152 } // anonymous namespace
154 bar
*llvm::fub() { return 0; }