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
;
16 typedef PointerUnion3
<int *, float *, long long *> PU3
;
17 typedef PointerUnion4
<int *, float *, long long *, double *> PU4
;
19 struct PointerUnionTest
: public testing::Test
{
28 PU4 i4null
, f4null
, l4null
, d4null
;
31 : f(3.14f
), i(42), d(3.14), l(42), a(&f
), b(&i
), c(&i
), n(), i3(&i
),
32 f3(&f
), l3(&l
), i4(&i
), f4(&f
), l4(&l
), d4(&d
), i4null((int *)nullptr),
33 f4null((float *)nullptr), l4null((long long *)nullptr),
34 d4null((double *)nullptr) {}
37 TEST_F(PointerUnionTest
, Comparison
) {
46 EXPECT_TRUE(i3
== i3
);
47 EXPECT_FALSE(i3
!= i3
);
48 EXPECT_TRUE(i3
!= f3
);
49 EXPECT_TRUE(f3
!= l3
);
50 EXPECT_TRUE(i4
== i4
);
51 EXPECT_FALSE(i4
!= i4
);
52 EXPECT_TRUE(i4
!= f4
);
53 EXPECT_TRUE(i4
!= l4
);
54 EXPECT_TRUE(f4
!= l4
);
55 EXPECT_TRUE(l4
!= d4
);
56 EXPECT_TRUE(i4null
!= f4null
);
57 EXPECT_TRUE(i4null
!= l4null
);
58 EXPECT_TRUE(i4null
!= d4null
);
61 TEST_F(PointerUnionTest
, Null
) {
62 EXPECT_FALSE(a
.isNull());
63 EXPECT_FALSE(b
.isNull());
64 EXPECT_TRUE(n
.isNull());
68 // workaround an issue with EXPECT macros and explicit bool
78 EXPECT_FALSE(i3
.isNull());
79 EXPECT_FALSE(f3
.isNull());
80 EXPECT_FALSE(l3
.isNull());
81 EXPECT_FALSE(i4
.isNull());
82 EXPECT_FALSE(f4
.isNull());
83 EXPECT_FALSE(l4
.isNull());
84 EXPECT_FALSE(d4
.isNull());
85 EXPECT_TRUE(i4null
.isNull());
86 EXPECT_TRUE(f4null
.isNull());
87 EXPECT_TRUE(l4null
.isNull());
88 EXPECT_TRUE(d4null
.isNull());
91 TEST_F(PointerUnionTest
, Is
) {
92 EXPECT_FALSE(a
.is
<int *>());
93 EXPECT_TRUE(a
.is
<float *>());
94 EXPECT_TRUE(b
.is
<int *>());
95 EXPECT_FALSE(b
.is
<float *>());
96 EXPECT_TRUE(n
.is
<int *>());
97 EXPECT_FALSE(n
.is
<float *>());
98 EXPECT_TRUE(i3
.is
<int *>());
99 EXPECT_TRUE(f3
.is
<float *>());
100 EXPECT_TRUE(l3
.is
<long long *>());
101 EXPECT_TRUE(i4
.is
<int *>());
102 EXPECT_TRUE(f4
.is
<float *>());
103 EXPECT_TRUE(l4
.is
<long long *>());
104 EXPECT_TRUE(d4
.is
<double *>());
105 EXPECT_TRUE(i4null
.is
<int *>());
106 EXPECT_TRUE(f4null
.is
<float *>());
107 EXPECT_TRUE(l4null
.is
<long long *>());
108 EXPECT_TRUE(d4null
.is
<double *>());
111 TEST_F(PointerUnionTest
, Get
) {
112 EXPECT_EQ(a
.get
<float *>(), &f
);
113 EXPECT_EQ(b
.get
<int *>(), &i
);
114 EXPECT_EQ(n
.get
<int *>(), (int *)nullptr);
117 template<int I
> struct alignas(8) Aligned
{};
119 typedef PointerUnion
<Aligned
<0> *, Aligned
<1> *, Aligned
<2> *, Aligned
<3> *,
120 Aligned
<4> *, Aligned
<5> *, Aligned
<6> *, Aligned
<7> *>
123 TEST_F(PointerUnionTest
, ManyElements
) {
128 EXPECT_TRUE(a
.is
<Aligned
<0>*>());
129 EXPECT_FALSE(a
.is
<Aligned
<1>*>());
130 EXPECT_FALSE(a
.is
<Aligned
<2>*>());
131 EXPECT_FALSE(a
.is
<Aligned
<3>*>());
132 EXPECT_FALSE(a
.is
<Aligned
<4>*>());
133 EXPECT_FALSE(a
.is
<Aligned
<5>*>());
134 EXPECT_FALSE(a
.is
<Aligned
<6>*>());
135 EXPECT_FALSE(a
.is
<Aligned
<7>*>());
136 EXPECT_EQ(a
.dyn_cast
<Aligned
<0>*>(), &a0
);
137 EXPECT_EQ(*a
.getAddrOfPtr1(), &a0
);
140 EXPECT_FALSE(a
.is
<Aligned
<0>*>());
141 EXPECT_FALSE(a
.is
<Aligned
<1>*>());
142 EXPECT_FALSE(a
.is
<Aligned
<2>*>());
143 EXPECT_FALSE(a
.is
<Aligned
<3>*>());
144 EXPECT_FALSE(a
.is
<Aligned
<4>*>());
145 EXPECT_FALSE(a
.is
<Aligned
<5>*>());
146 EXPECT_FALSE(a
.is
<Aligned
<6>*>());
147 EXPECT_TRUE(a
.is
<Aligned
<7>*>());
148 EXPECT_EQ(a
.dyn_cast
<Aligned
<7>*>(), &a7
);
150 EXPECT_TRUE(a
== PU8(&a7
));
151 EXPECT_TRUE(a
!= PU8(&a0
));
154 TEST_F(PointerUnionTest
, GetAddrOfPtr1
) {
155 EXPECT_TRUE((void *)b
.getAddrOfPtr1() == (void *)&b
);
156 EXPECT_TRUE((void *)n
.getAddrOfPtr1() == (void *)&n
);
159 } // end anonymous namespace