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