1 //===- llvm/unittest/ADT/PointerIntPairTest.cpp - Unit tests --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/PointerIntPair.h"
11 #include "gtest/gtest.h"
17 TEST(PointerIntPairTest
, GetSet
) {
23 PointerIntPair
<S
*, 2> Pair(&s
, 1U);
24 EXPECT_EQ(&s
, Pair
.getPointer());
25 EXPECT_EQ(1U, Pair
.getInt());
28 EXPECT_EQ(&s
, Pair
.getPointer());
29 EXPECT_EQ(2U, Pair
.getInt());
31 Pair
.setPointer(nullptr);
32 EXPECT_EQ(nullptr, Pair
.getPointer());
33 EXPECT_EQ(2U, Pair
.getInt());
35 Pair
.setPointerAndInt(&s
, 3U);
36 EXPECT_EQ(&s
, Pair
.getPointer());
37 EXPECT_EQ(3U, Pair
.getInt());
39 // Make sure that we can perform all of our operations on enum classes.
41 // The concern is that enum classes are only explicitly convertible to
42 // integers. This means that if we assume in PointerIntPair this, a
43 // compilation error will result. This group of tests exercises the enum class
44 // code to make sure that we do not run into such issues in the future.
45 enum class E
: unsigned {
50 PointerIntPair
<S
*, 2, E
> Pair2(&s
, E::Case1
);
51 EXPECT_EQ(&s
, Pair2
.getPointer());
52 EXPECT_EQ(E::Case1
, Pair2
.getInt());
54 Pair2
.setInt(E::Case2
);
55 EXPECT_EQ(&s
, Pair2
.getPointer());
56 EXPECT_EQ(E::Case2
, Pair2
.getInt());
58 Pair2
.setPointer(nullptr);
59 EXPECT_EQ(nullptr, Pair2
.getPointer());
60 EXPECT_EQ(E::Case2
, Pair2
.getInt());
62 Pair2
.setPointerAndInt(&s
, E::Case3
);
63 EXPECT_EQ(&s
, Pair2
.getPointer());
64 EXPECT_EQ(E::Case3
, Pair2
.getInt());
67 TEST(PointerIntPairTest
, DefaultInitialize
) {
68 PointerIntPair
<float *, 2> Pair
;
69 EXPECT_EQ(nullptr, Pair
.getPointer());
70 EXPECT_EQ(0U, Pair
.getInt());
73 TEST(PointerIntPairTest
, ManyUnusedBits
) {
74 // In real code this would be a word-sized integer limited to 31 bits.
78 class FixnumPointerTraits
{
80 static inline void *getAsVoidPointer(Fixnum31 Num
) {
81 return reinterpret_cast<void *>(Num
.Value
<< NumLowBitsAvailable
);
83 static inline Fixnum31
getFromVoidPointer(void *P
) {
84 // In real code this would assert that the value is in range.
85 return { reinterpret_cast<uintptr_t>(P
) >> NumLowBitsAvailable
};
87 enum { NumLowBitsAvailable
= std::numeric_limits
<uintptr_t>::digits
- 31 };
90 PointerIntPair
<Fixnum31
, 1, bool, FixnumPointerTraits
> pair
;
91 EXPECT_EQ((uintptr_t)0, pair
.getPointer().Value
);
92 EXPECT_FALSE(pair
.getInt());
94 pair
.setPointerAndInt({ 0x7FFFFFFF }, true );
95 EXPECT_EQ((uintptr_t)0x7FFFFFFF, pair
.getPointer().Value
);
96 EXPECT_TRUE(pair
.getInt());
98 EXPECT_EQ(FixnumPointerTraits::NumLowBitsAvailable
- 1,
99 PointerLikeTypeTraits
<decltype(pair
)>::NumLowBitsAvailable
);
102 } // end anonymous namespace