[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / ExtensibleRTTITest.cpp
blob9715d26a9de297ca6ee7d0695984160c9b2fe1fb
1 //===------ unittests/ExtensibleRTTITest.cpp - Extensible RTTI Tests ------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/ExtensibleRTTI.h"
10 #include "llvm/Support/Casting.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
16 namespace {
18 class MyBaseType : public RTTIExtends<MyBaseType, RTTIRoot> {
19 public:
20 static char ID;
23 class MyDerivedType : public RTTIExtends<MyDerivedType, MyBaseType> {
24 public:
25 static char ID;
28 class MyOtherDerivedType : public RTTIExtends<MyOtherDerivedType, MyBaseType> {
29 public:
30 static char ID;
33 class MyDeeperDerivedType
34 : public RTTIExtends<MyDeeperDerivedType, MyDerivedType> {
35 public:
36 static char ID;
39 char MyBaseType::ID = 0;
40 char MyDerivedType::ID = 0;
41 char MyOtherDerivedType::ID = 0;
42 char MyDeeperDerivedType::ID = 0;
44 TEST(ExtensibleRTTI, isa) {
45 MyBaseType B;
46 MyDerivedType D;
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) {
66 MyDerivedType D;
67 MyBaseType &BD = D;
69 (void)cast<MyBaseType>(D);
70 (void)cast<MyBaseType>(BD);
71 (void)cast<MyDerivedType>(BD);
74 TEST(ExtensibleRTTI, dyn_cast) {
75 MyBaseType B;
76 MyDerivedType D;
77 MyBaseType &BD = D;
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);
85 } // namespace