ExtensibleRTTITest.cpp
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/ExtensibleRTTI.h"
#include "llvm/Support/Casting.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class MyBaseType : public RTTIExtends<MyBaseType, RTTIRoot> {
public:
static char ID;
};
class MyDerivedType : public RTTIExtends<MyDerivedType, MyBaseType> {
public:
static char ID;
};
class MyOtherDerivedType : public RTTIExtends<MyOtherDerivedType, MyBaseType> {
public:
static char ID;
};
class MyDeeperDerivedType
: public RTTIExtends<MyDeeperDerivedType, MyDerivedType> {
public:
static char ID;
};
char MyBaseType::ID = 0;
char MyDerivedType::ID = 0;
char MyOtherDerivedType::ID = 0;
char MyDeeperDerivedType::ID = 0;
TEST(ExtensibleRTTI, isa) {
MyBaseType B;
MyDerivedType D;
MyDeeperDerivedType DD;
EXPECT_TRUE(isa<MyBaseType>(B));
EXPECT_FALSE(isa<MyDerivedType>(B));
EXPECT_FALSE(isa<MyOtherDerivedType>(B));
EXPECT_FALSE(isa<MyDeeperDerivedType>(B));
EXPECT_TRUE(isa<MyBaseType>(D));
EXPECT_TRUE(isa<MyDerivedType>(D));
EXPECT_FALSE(isa<MyOtherDerivedType>(D));
EXPECT_FALSE(isa<MyDeeperDerivedType>(D));
EXPECT_TRUE(isa<MyBaseType>(DD));
EXPECT_TRUE(isa<MyDerivedType>(DD));
EXPECT_FALSE(isa<MyOtherDerivedType>(DD));
EXPECT_TRUE(isa<MyDeeperDerivedType>(DD));
}
TEST(ExtensibleRTTI, cast) {
MyDerivedType D;
MyBaseType &BD = D;
cast<MyBaseType>(D);
cast<MyBaseType>(BD);
cast<MyDerivedType>(BD);
}
TEST(ExtensibleRTTI, dyn_cast) {
MyBaseType B;
MyDerivedType D;
MyBaseType &BD = D;
EXPECT_EQ(dyn_cast<MyDerivedType>(&B), nullptr);
EXPECT_EQ(dyn_cast<MyDerivedType>(&D), &D);
EXPECT_EQ(dyn_cast<MyBaseType>(&BD), &BD);
EXPECT_EQ(dyn_cast<MyDerivedType>(&BD), &D);
}
} // namespace