[LLVM][Alignment] Fix AlignmentTest on platform where size_t != uint64_t
[llvm-core.git] / unittests / ADT / PointerUnionTest.cpp
blobcd6e980e9a261159d9bf0734e88f704159a09b87
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;
17 struct PointerUnionTest : public testing::Test {
18 float f;
19 int i;
21 PU a, b, c, n;
23 PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), c(&i), n() {}
26 TEST_F(PointerUnionTest, Comparison) {
27 EXPECT_TRUE(a == a);
28 EXPECT_FALSE(a != a);
29 EXPECT_TRUE(a != b);
30 EXPECT_FALSE(a == b);
31 EXPECT_TRUE(b == c);
32 EXPECT_FALSE(b != c);
33 EXPECT_TRUE(b != n);
34 EXPECT_FALSE(b == n);
37 TEST_F(PointerUnionTest, Null) {
38 EXPECT_FALSE(a.isNull());
39 EXPECT_FALSE(b.isNull());
40 EXPECT_TRUE(n.isNull());
41 EXPECT_FALSE(!a);
42 EXPECT_FALSE(!b);
43 EXPECT_TRUE(!n);
44 // workaround an issue with EXPECT macros and explicit bool
45 EXPECT_TRUE((bool)a);
46 EXPECT_TRUE((bool)b);
47 EXPECT_FALSE(n);
49 EXPECT_NE(n, b);
50 EXPECT_EQ(b, c);
51 b = nullptr;
52 EXPECT_EQ(n, b);
53 EXPECT_NE(b, c);
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> *>
75 PU8;
77 TEST_F(PointerUnionTest, ManyElements) {
78 Aligned<0> a0;
79 Aligned<7> a7;
81 PU8 a = &a0;
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);
93 a = &a7;
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