[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / ADT / PointerSumTypeTest.cpp
blobfbf59f3a2fda581ad03e828b567104acd55f9184
1 //===- llvm/unittest/ADT/PointerSumTypeTest.cpp ---------------------------===//
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/ADT/PointerSumType.h"
10 #include "gtest/gtest.h"
11 using namespace llvm;
13 namespace {
15 struct PointerSumTypeTest : public testing::Test {
16 enum Kinds { Float, Int1, Int2 };
17 float f;
18 int i1, i2;
20 typedef PointerSumType<Kinds, PointerSumTypeMember<Float, float *>,
21 PointerSumTypeMember<Int1, int *>,
22 PointerSumTypeMember<Int2, int *>>
23 SumType;
24 SumType a, b, c, n;
26 PointerSumTypeTest()
27 : f(3.14f), i1(42), i2(-1), a(SumType::create<Float>(&f)),
28 b(SumType::create<Int1>(&i1)), c(SumType::create<Int2>(&i2)), n() {}
31 TEST_F(PointerSumTypeTest, NullTest) {
32 EXPECT_TRUE(a);
33 EXPECT_TRUE(b);
34 EXPECT_TRUE(c);
35 EXPECT_FALSE(n);
38 TEST_F(PointerSumTypeTest, GetTag) {
39 EXPECT_EQ(Float, a.getTag());
40 EXPECT_EQ(Int1, b.getTag());
41 EXPECT_EQ(Int2, c.getTag());
42 EXPECT_EQ((Kinds)0, n.getTag());
45 TEST_F(PointerSumTypeTest, Is) {
46 EXPECT_TRUE(a.is<Float>());
47 EXPECT_FALSE(a.is<Int1>());
48 EXPECT_FALSE(a.is<Int2>());
49 EXPECT_FALSE(b.is<Float>());
50 EXPECT_TRUE(b.is<Int1>());
51 EXPECT_FALSE(b.is<Int2>());
52 EXPECT_FALSE(c.is<Float>());
53 EXPECT_FALSE(c.is<Int1>());
54 EXPECT_TRUE(c.is<Int2>());
57 TEST_F(PointerSumTypeTest, Get) {
58 EXPECT_EQ(&f, a.get<Float>());
59 EXPECT_EQ(nullptr, a.get<Int1>());
60 EXPECT_EQ(nullptr, a.get<Int2>());
61 EXPECT_EQ(nullptr, b.get<Float>());
62 EXPECT_EQ(&i1, b.get<Int1>());
63 EXPECT_EQ(nullptr, b.get<Int2>());
64 EXPECT_EQ(nullptr, c.get<Float>());
65 EXPECT_EQ(nullptr, c.get<Int1>());
66 EXPECT_EQ(&i2, c.get<Int2>());
68 // Note that we can use .get even on a null sum type. It just always produces
69 // a null pointer, even if one of the discriminants is null.
70 EXPECT_EQ(nullptr, n.get<Float>());
71 EXPECT_EQ(nullptr, n.get<Int1>());
72 EXPECT_EQ(nullptr, n.get<Int2>());
75 TEST_F(PointerSumTypeTest, Cast) {
76 EXPECT_EQ(&f, a.cast<Float>());
77 EXPECT_EQ(&i1, b.cast<Int1>());
78 EXPECT_EQ(&i2, c.cast<Int2>());
81 TEST_F(PointerSumTypeTest, Assignment) {
82 b = SumType::create<Int2>(&i2);
83 EXPECT_EQ(nullptr, b.get<Float>());
84 EXPECT_EQ(nullptr, b.get<Int1>());
85 EXPECT_EQ(&i2, b.get<Int2>());
87 b = SumType::create<Int2>(&i1);
88 EXPECT_EQ(nullptr, b.get<Float>());
89 EXPECT_EQ(nullptr, b.get<Int1>());
90 EXPECT_EQ(&i1, b.get<Int2>());
92 float Local = 1.616f;
93 b = SumType::create<Float>(&Local);
94 EXPECT_EQ(&Local, b.get<Float>());
95 EXPECT_EQ(nullptr, b.get<Int1>());
96 EXPECT_EQ(nullptr, b.get<Int2>());
98 n = SumType::create<Int1>(&i2);
99 EXPECT_TRUE(n);
100 EXPECT_EQ(nullptr, n.get<Float>());
101 EXPECT_EQ(&i2, n.get<Int1>());
102 EXPECT_EQ(nullptr, n.get<Int2>());
104 n = SumType::create<Float>(nullptr);
105 EXPECT_FALSE(n);
106 EXPECT_EQ(nullptr, n.get<Float>());
107 EXPECT_EQ(nullptr, n.get<Int1>());
108 EXPECT_EQ(nullptr, n.get<Int2>());
112 } // end anonymous namespace