1 //===- llvm/unittest/ADT/PointerIntPairTest.cpp - 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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/PointerIntPair.h"
10 #include "gtest/gtest.h"
11 #include <limits>
12 using namespace llvm;
13
14 namespace {
15
TEST(PointerIntPairTest,GetSet)16 TEST(PointerIntPairTest, GetSet) {
17 struct S {
18 int i;
19 };
20 S s;
21
22 PointerIntPair<S *, 2> Pair(&s, 1U);
23 EXPECT_EQ(&s, Pair.getPointer());
24 EXPECT_EQ(1U, Pair.getInt());
25
26 Pair.setInt(2);
27 EXPECT_EQ(&s, Pair.getPointer());
28 EXPECT_EQ(2U, Pair.getInt());
29
30 Pair.setPointer(nullptr);
31 EXPECT_EQ(nullptr, Pair.getPointer());
32 EXPECT_EQ(2U, Pair.getInt());
33
34 Pair.setPointerAndInt(&s, 3U);
35 EXPECT_EQ(&s, Pair.getPointer());
36 EXPECT_EQ(3U, Pair.getInt());
37
38 // Make sure that we can perform all of our operations on enum classes.
39 //
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 {
45 Case1,
46 Case2,
47 Case3,
48 };
49 PointerIntPair<S *, 2, E> Pair2(&s, E::Case1);
50 EXPECT_EQ(&s, Pair2.getPointer());
51 EXPECT_EQ(E::Case1, Pair2.getInt());
52
53 Pair2.setInt(E::Case2);
54 EXPECT_EQ(&s, Pair2.getPointer());
55 EXPECT_EQ(E::Case2, Pair2.getInt());
56
57 Pair2.setPointer(nullptr);
58 EXPECT_EQ(nullptr, Pair2.getPointer());
59 EXPECT_EQ(E::Case2, Pair2.getInt());
60
61 Pair2.setPointerAndInt(&s, E::Case3);
62 EXPECT_EQ(&s, Pair2.getPointer());
63 EXPECT_EQ(E::Case3, Pair2.getInt());
64
65 static_assert(std::is_trivially_copyable<PointerIntPair<S *, 2, E>>::value,
66 "trivially copyable");
67 }
68
TEST(PointerIntPairTest,DefaultInitialize)69 TEST(PointerIntPairTest, DefaultInitialize) {
70 PointerIntPair<float *, 2> Pair;
71 EXPECT_EQ(nullptr, Pair.getPointer());
72 EXPECT_EQ(0U, Pair.getInt());
73 }
74
75 // In real code this would be a word-sized integer limited to 31 bits.
76 struct Fixnum31 {
77 uintptr_t Value;
78 };
79 struct FixnumPointerTraits {
getAsVoidPointer__anonc10497630111::FixnumPointerTraits80 static inline void *getAsVoidPointer(Fixnum31 Num) {
81 return reinterpret_cast<void *>(Num.Value << NumLowBitsAvailable);
82 }
getFromVoidPointer__anonc10497630111::FixnumPointerTraits83 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};
86 }
87 static constexpr int NumLowBitsAvailable =
88 std::numeric_limits<uintptr_t>::digits - 31;
89 };
TEST(PointerIntPairTest,ManyUnusedBits)90 TEST(PointerIntPairTest, ManyUnusedBits) {
91
92 PointerIntPair<Fixnum31, 1, bool, FixnumPointerTraits> pair;
93 EXPECT_EQ((uintptr_t)0, pair.getPointer().Value);
94 EXPECT_FALSE(pair.getInt());
95
96 pair.setPointerAndInt({ 0x7FFFFFFF }, true );
97 EXPECT_EQ((uintptr_t)0x7FFFFFFF, pair.getPointer().Value);
98 EXPECT_TRUE(pair.getInt());
99
100 EXPECT_EQ(FixnumPointerTraits::NumLowBitsAvailable - 1,
101 (int)PointerLikeTypeTraits<decltype(pair)>::NumLowBitsAvailable);
102
103 static_assert(
104 std::is_trivially_copyable<
105 PointerIntPair<Fixnum31, 1, bool, FixnumPointerTraits>>::value,
106 "trivially copyable");
107 }
108
109 } // end anonymous namespace
110