1 //===- llvm/unittest/ADT/PointerIntPairTest.cpp - 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/PointerIntPair.h"
10 #include "gtest/gtest.h"
16 TEST(PointerIntPairTest
, GetSet
) {
22 PointerIntPair
<S
*, 2> Pair(&s
, 1U);
23 EXPECT_EQ(&s
, Pair
.getPointer());
24 EXPECT_EQ(1U, Pair
.getInt());
27 EXPECT_EQ(&s
, Pair
.getPointer());
28 EXPECT_EQ(2U, Pair
.getInt());
30 Pair
.setPointer(nullptr);
31 EXPECT_EQ(nullptr, Pair
.getPointer());
32 EXPECT_EQ(2U, Pair
.getInt());
34 Pair
.setPointerAndInt(&s
, 3U);
35 EXPECT_EQ(&s
, Pair
.getPointer());
36 EXPECT_EQ(3U, Pair
.getInt());
38 // Make sure that we can perform all of our operations on enum classes.
40 // The concern is that enum classes are only explicitly convertible to
41 // integers. This means that if we assume in PointerIntPair this, a
42 // compilation error will result. This group of tests exercises the enum class
43 // code to make sure that we do not run into such issues in the future.
44 enum class E
: unsigned {
49 PointerIntPair
<S
*, 2, E
> Pair2(&s
, E::Case1
);
50 EXPECT_EQ(&s
, Pair2
.getPointer());
51 EXPECT_EQ(E::Case1
, Pair2
.getInt());
53 Pair2
.setInt(E::Case2
);
54 EXPECT_EQ(&s
, Pair2
.getPointer());
55 EXPECT_EQ(E::Case2
, Pair2
.getInt());
57 Pair2
.setPointer(nullptr);
58 EXPECT_EQ(nullptr, Pair2
.getPointer());
59 EXPECT_EQ(E::Case2
, Pair2
.getInt());
61 Pair2
.setPointerAndInt(&s
, E::Case3
);
62 EXPECT_EQ(&s
, Pair2
.getPointer());
63 EXPECT_EQ(E::Case3
, Pair2
.getInt());
65 auto [Pointer2
, Int2
] = Pair2
;
66 EXPECT_EQ(Pair2
.getPointer(), Pointer2
);
67 EXPECT_EQ(Pair2
.getInt(), Int2
);
69 static_assert(std::is_trivially_copyable_v
<PointerIntPair
<S
*, 2, E
>>,
70 "trivially copyable");
73 TEST(PointerIntPairTest
, DefaultInitialize
) {
74 PointerIntPair
<float *, 2> Pair
;
75 EXPECT_EQ(nullptr, Pair
.getPointer());
76 EXPECT_EQ(0U, Pair
.getInt());
79 // In real code this would be a word-sized integer limited to 31 bits.
83 struct FixnumPointerTraits
{
84 static inline void *getAsVoidPointer(Fixnum31 Num
) {
85 return reinterpret_cast<void *>(Num
.Value
<< NumLowBitsAvailable
);
87 static inline Fixnum31
getFromVoidPointer(void *P
) {
88 // In real code this would assert that the value is in range.
89 return {reinterpret_cast<uintptr_t>(P
) >> NumLowBitsAvailable
};
91 static constexpr int NumLowBitsAvailable
=
92 std::numeric_limits
<uintptr_t>::digits
- 31;
94 TEST(PointerIntPairTest
, ManyUnusedBits
) {
96 PointerIntPair
<Fixnum31
, 1, bool, FixnumPointerTraits
> pair
;
97 EXPECT_EQ((uintptr_t)0, pair
.getPointer().Value
);
98 EXPECT_FALSE(pair
.getInt());
100 pair
.setPointerAndInt({ 0x7FFFFFFF }, true );
101 EXPECT_EQ((uintptr_t)0x7FFFFFFF, pair
.getPointer().Value
);
102 EXPECT_TRUE(pair
.getInt());
104 EXPECT_EQ(FixnumPointerTraits::NumLowBitsAvailable
- 1,
105 (int)PointerLikeTypeTraits
<decltype(pair
)>::NumLowBitsAvailable
);
107 static_assert(std::is_trivially_copyable_v
<
108 PointerIntPair
<Fixnum31
, 1, bool, FixnumPointerTraits
>>,
109 "trivially copyable");
112 TEST(PointerIntPairTest
, TypePunning
) {
116 int **IntPtrBegin
= &IntPtr
;
117 int **IntPtrEnd
= IntPtrBegin
+ 1;
119 PointerIntPair
<int *, 1> Pair
;
120 int **PairAddr
= Pair
.getAddrOfPointer();
122 while (IntPtrBegin
!= IntPtrEnd
) {
123 *PairAddr
= *IntPtrBegin
;
127 EXPECT_EQ(Pair
.getPointer(), IntPtr
);
130 } // end anonymous namespace