[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / unittests / ADT / PointerUnionTest.cpp
blob0a88114ede852c874a7ca929e9c9d26200fb1c19
1 //===- llvm/unittest/ADT/PointerUnionTest.cpp - Optional unit 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/ADT/PointerUnion.h"
10 #include "gtest/gtest.h"
11 using namespace llvm;
13 namespace {
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 {
20 float f;
21 int i;
22 double d;
23 long long l;
25 PU a, b, c, n;
26 PU3 i3, f3, l3;
27 PU4 i4, f4, l4, d4;
28 PU4 i4null, f4null, l4null, d4null;
30 PointerUnionTest()
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) {
38 EXPECT_TRUE(a == a);
39 EXPECT_FALSE(a != a);
40 EXPECT_TRUE(a != b);
41 EXPECT_FALSE(a == b);
42 EXPECT_TRUE(b == c);
43 EXPECT_FALSE(b != c);
44 EXPECT_TRUE(b != n);
45 EXPECT_FALSE(b == n);
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());
65 EXPECT_FALSE(!a);
66 EXPECT_FALSE(!b);
67 EXPECT_TRUE(!n);
68 // workaround an issue with EXPECT macros and explicit bool
69 EXPECT_TRUE((bool)a);
70 EXPECT_TRUE((bool)b);
71 EXPECT_FALSE(n);
73 EXPECT_NE(n, b);
74 EXPECT_EQ(b, c);
75 b = nullptr;
76 EXPECT_EQ(n, b);
77 EXPECT_NE(b, c);
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> *>
121 PU8;
123 TEST_F(PointerUnionTest, ManyElements) {
124 Aligned<0> a0;
125 Aligned<7> a7;
127 PU8 a = &a0;
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);
139 a = &a7;
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