1 //===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ExtensibleRTTI.h"
10 #include "llvm/Support/Casting.h"
12 #include "gtest/gtest.h"
18 class MyBaseType
: public RTTIExtends
<MyBaseType
, RTTIRoot
> {
23 class MyDerivedType
: public RTTIExtends
<MyDerivedType
, MyBaseType
> {
28 class MyOtherDerivedType
: public RTTIExtends
<MyOtherDerivedType
, MyBaseType
> {
33 class MyDeeperDerivedType
34 : public RTTIExtends
<MyDeeperDerivedType
, MyDerivedType
> {
39 char MyBaseType::ID
= 0;
40 char MyDerivedType::ID
= 0;
41 char MyOtherDerivedType::ID
= 0;
42 char MyDeeperDerivedType::ID
= 0;
44 TEST(ExtensibleRTTI
, isa
) {
47 MyDeeperDerivedType DD
;
49 EXPECT_TRUE(isa
<MyBaseType
>(B
));
50 EXPECT_FALSE(isa
<MyDerivedType
>(B
));
51 EXPECT_FALSE(isa
<MyOtherDerivedType
>(B
));
52 EXPECT_FALSE(isa
<MyDeeperDerivedType
>(B
));
54 EXPECT_TRUE(isa
<MyBaseType
>(D
));
55 EXPECT_TRUE(isa
<MyDerivedType
>(D
));
56 EXPECT_FALSE(isa
<MyOtherDerivedType
>(D
));
57 EXPECT_FALSE(isa
<MyDeeperDerivedType
>(D
));
59 EXPECT_TRUE(isa
<MyBaseType
>(DD
));
60 EXPECT_TRUE(isa
<MyDerivedType
>(DD
));
61 EXPECT_FALSE(isa
<MyOtherDerivedType
>(DD
));
62 EXPECT_TRUE(isa
<MyDeeperDerivedType
>(DD
));
65 TEST(ExtensibleRTTI
, cast
) {
69 (void)cast
<MyBaseType
>(D
);
70 (void)cast
<MyBaseType
>(BD
);
71 (void)cast
<MyDerivedType
>(BD
);
74 TEST(ExtensibleRTTI
, dyn_cast
) {
79 EXPECT_EQ(dyn_cast
<MyDerivedType
>(&B
), nullptr);
80 EXPECT_EQ(dyn_cast
<MyDerivedType
>(&D
), &D
);
81 EXPECT_EQ(dyn_cast
<MyBaseType
>(&BD
), &BD
);
82 EXPECT_EQ(dyn_cast
<MyDerivedType
>(&BD
), &D
);