1 //===- llvm/unittest/ADT/PointerUnionTest.cpp - Optional unit 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/ADT/PointerUnion.h"
10 #include "gtest/gtest.h"
15 typedef PointerUnion
<int *, float *> PU
;
17 struct PointerUnionTest
: public testing::Test
{
23 PointerUnionTest() : f(3.14f
), i(42), a(&f
), b(&i
), c(&i
), n() {}
26 TEST_F(PointerUnionTest
, Comparison
) {
37 TEST_F(PointerUnionTest
, Null
) {
38 EXPECT_FALSE(a
.isNull());
39 EXPECT_FALSE(b
.isNull());
40 EXPECT_TRUE(n
.isNull());
44 // workaround an issue with EXPECT macros and explicit bool
56 TEST_F(PointerUnionTest
, Is
) {
57 EXPECT_FALSE(a
.is
<int *>());
58 EXPECT_TRUE(a
.is
<float *>());
59 EXPECT_TRUE(b
.is
<int *>());
60 EXPECT_FALSE(b
.is
<float *>());
61 EXPECT_TRUE(n
.is
<int *>());
62 EXPECT_FALSE(n
.is
<float *>());
65 TEST_F(PointerUnionTest
, Get
) {
66 EXPECT_EQ(a
.get
<float *>(), &f
);
67 EXPECT_EQ(b
.get
<int *>(), &i
);
68 EXPECT_EQ(n
.get
<int *>(), (int *)nullptr);
71 template<int I
> struct alignas(8) Aligned
{};
73 typedef PointerUnion
<Aligned
<0> *, Aligned
<1> *, Aligned
<2> *, Aligned
<3> *,
74 Aligned
<4> *, Aligned
<5> *, Aligned
<6> *, Aligned
<7> *>
77 TEST_F(PointerUnionTest
, ManyElements
) {
82 EXPECT_TRUE(a
.is
<Aligned
<0>*>());
83 EXPECT_FALSE(a
.is
<Aligned
<1>*>());
84 EXPECT_FALSE(a
.is
<Aligned
<2>*>());
85 EXPECT_FALSE(a
.is
<Aligned
<3>*>());
86 EXPECT_FALSE(a
.is
<Aligned
<4>*>());
87 EXPECT_FALSE(a
.is
<Aligned
<5>*>());
88 EXPECT_FALSE(a
.is
<Aligned
<6>*>());
89 EXPECT_FALSE(a
.is
<Aligned
<7>*>());
90 EXPECT_EQ(a
.dyn_cast
<Aligned
<0>*>(), &a0
);
91 EXPECT_EQ(*a
.getAddrOfPtr1(), &a0
);
94 EXPECT_FALSE(a
.is
<Aligned
<0>*>());
95 EXPECT_FALSE(a
.is
<Aligned
<1>*>());
96 EXPECT_FALSE(a
.is
<Aligned
<2>*>());
97 EXPECT_FALSE(a
.is
<Aligned
<3>*>());
98 EXPECT_FALSE(a
.is
<Aligned
<4>*>());
99 EXPECT_FALSE(a
.is
<Aligned
<5>*>());
100 EXPECT_FALSE(a
.is
<Aligned
<6>*>());
101 EXPECT_TRUE(a
.is
<Aligned
<7>*>());
102 EXPECT_EQ(a
.dyn_cast
<Aligned
<7>*>(), &a7
);
104 EXPECT_TRUE(a
== PU8(&a7
));
105 EXPECT_TRUE(a
!= PU8(&a0
));
108 } // end anonymous namespace