1 //===- llvm/unittest/ADT/PointerSumTypeTest.cpp ---------------------------===//
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 "gtest/gtest.h"
11 #include "llvm/ADT/PointerSumType.h"
12 using namespace llvm;
13
14 namespace {
15
16 struct PointerSumTypeTest : public testing::Test {
17 enum Kinds { Float, Int1, Int2 };
18 float f;
19 int i1, i2;
20
21 typedef PointerSumType<Kinds, PointerSumTypeMember<Float, float *>,
22 PointerSumTypeMember<Int1, int *>,
23 PointerSumTypeMember<Int2, int *>>
24 SumType;
25 SumType a, b, c, n;
26
PointerSumTypeTest__anon6bff38ed0111::PointerSumTypeTest27 PointerSumTypeTest()
28 : f(3.14f), i1(42), i2(-1), a(SumType::create<Float>(&f)),
29 b(SumType::create<Int1>(&i1)), c(SumType::create<Int2>(&i2)), n() {}
30 };
31
TEST_F(PointerSumTypeTest,NullTest)32 TEST_F(PointerSumTypeTest, NullTest) {
33 EXPECT_TRUE(a);
34 EXPECT_TRUE(b);
35 EXPECT_TRUE(c);
36 EXPECT_FALSE(n);
37 }
38
TEST_F(PointerSumTypeTest,GetTag)39 TEST_F(PointerSumTypeTest, GetTag) {
40 EXPECT_EQ(Float, a.getTag());
41 EXPECT_EQ(Int1, b.getTag());
42 EXPECT_EQ(Int2, c.getTag());
43 EXPECT_EQ((Kinds)0, n.getTag());
44 }
45
TEST_F(PointerSumTypeTest,Is)46 TEST_F(PointerSumTypeTest, Is) {
47 EXPECT_TRUE(a.is<Float>());
48 EXPECT_FALSE(a.is<Int1>());
49 EXPECT_FALSE(a.is<Int2>());
50 EXPECT_FALSE(b.is<Float>());
51 EXPECT_TRUE(b.is<Int1>());
52 EXPECT_FALSE(b.is<Int2>());
53 EXPECT_FALSE(c.is<Float>());
54 EXPECT_FALSE(c.is<Int1>());
55 EXPECT_TRUE(c.is<Int2>());
56 }
57
TEST_F(PointerSumTypeTest,Get)58 TEST_F(PointerSumTypeTest, Get) {
59 EXPECT_EQ(&f, a.get<Float>());
60 EXPECT_EQ(nullptr, a.get<Int1>());
61 EXPECT_EQ(nullptr, a.get<Int2>());
62 EXPECT_EQ(nullptr, b.get<Float>());
63 EXPECT_EQ(&i1, b.get<Int1>());
64 EXPECT_EQ(nullptr, b.get<Int2>());
65 EXPECT_EQ(nullptr, c.get<Float>());
66 EXPECT_EQ(nullptr, c.get<Int1>());
67 EXPECT_EQ(&i2, c.get<Int2>());
68
69 // Note that we can use .get even on a null sum type. It just always produces
70 // a null pointer, even if one of the discriminants is null.
71 EXPECT_EQ(nullptr, n.get<Float>());
72 EXPECT_EQ(nullptr, n.get<Int1>());
73 EXPECT_EQ(nullptr, n.get<Int2>());
74 }
75
TEST_F(PointerSumTypeTest,Cast)76 TEST_F(PointerSumTypeTest, Cast) {
77 EXPECT_EQ(&f, a.cast<Float>());
78 EXPECT_EQ(&i1, b.cast<Int1>());
79 EXPECT_EQ(&i2, c.cast<Int2>());
80 }
81
TEST_F(PointerSumTypeTest,Assignment)82 TEST_F(PointerSumTypeTest, Assignment) {
83 b = SumType::create<Int2>(&i2);
84 EXPECT_EQ(nullptr, b.get<Float>());
85 EXPECT_EQ(nullptr, b.get<Int1>());
86 EXPECT_EQ(&i2, b.get<Int2>());
87
88 b = SumType::create<Int2>(&i1);
89 EXPECT_EQ(nullptr, b.get<Float>());
90 EXPECT_EQ(nullptr, b.get<Int1>());
91 EXPECT_EQ(&i1, b.get<Int2>());
92
93 float Local = 1.616f;
94 b = SumType::create<Float>(&Local);
95 EXPECT_EQ(&Local, b.get<Float>());
96 EXPECT_EQ(nullptr, b.get<Int1>());
97 EXPECT_EQ(nullptr, b.get<Int2>());
98
99 n = SumType::create<Int1>(&i2);
100 EXPECT_TRUE(n);
101 EXPECT_EQ(nullptr, n.get<Float>());
102 EXPECT_EQ(&i2, n.get<Int1>());
103 EXPECT_EQ(nullptr, n.get<Int2>());
104
105 n = SumType::create<Float>(nullptr);
106 EXPECT_FALSE(n);
107 EXPECT_EQ(nullptr, n.get<Float>());
108 EXPECT_EQ(nullptr, n.get<Int1>());
109 EXPECT_EQ(nullptr, n.get<Int2>());
110 }
111
112
113 } // end anonymous namespace
114