[ARM] MVE big endian loads/stores
[llvm-complete.git] / unittests / ADT / PointerIntPairTest.cpp
blob6b3a4c055c9e7d4adcbd1d04116655afa80d2507
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/PointerIntPair.h"
10 #include "gtest/gtest.h"
11 #include <limits>
12 using namespace llvm;
14 namespace {
16 TEST(PointerIntPairTest, GetSet) {
17 struct S {
18 int i;
20 S s;
22 PointerIntPair<S *, 2> Pair(&s, 1U);
23 EXPECT_EQ(&s, Pair.getPointer());
24 EXPECT_EQ(1U, Pair.getInt());
26 Pair.setInt(2);
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 {
45 Case1,
46 Case2,
47 Case3,
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 static_assert(is_trivially_copyable<PointerIntPair<S *, 2, E>>::value,
66 "trivially copyable");
69 TEST(PointerIntPairTest, DefaultInitialize) {
70 PointerIntPair<float *, 2> Pair;
71 EXPECT_EQ(nullptr, Pair.getPointer());
72 EXPECT_EQ(0U, Pair.getInt());
75 TEST(PointerIntPairTest, ManyUnusedBits) {
76 // In real code this would be a word-sized integer limited to 31 bits.
77 struct Fixnum31 {
78 uintptr_t Value;
80 class FixnumPointerTraits {
81 public:
82 static inline void *getAsVoidPointer(Fixnum31 Num) {
83 return reinterpret_cast<void *>(Num.Value << NumLowBitsAvailable);
85 static inline Fixnum31 getFromVoidPointer(void *P) {
86 // In real code this would assert that the value is in range.
87 return { reinterpret_cast<uintptr_t>(P) >> NumLowBitsAvailable };
89 enum { NumLowBitsAvailable = std::numeric_limits<uintptr_t>::digits - 31 };
92 PointerIntPair<Fixnum31, 1, bool, FixnumPointerTraits> pair;
93 EXPECT_EQ((uintptr_t)0, pair.getPointer().Value);
94 EXPECT_FALSE(pair.getInt());
96 pair.setPointerAndInt({ 0x7FFFFFFF }, true );
97 EXPECT_EQ((uintptr_t)0x7FFFFFFF, pair.getPointer().Value);
98 EXPECT_TRUE(pair.getInt());
100 EXPECT_EQ(FixnumPointerTraits::NumLowBitsAvailable - 1,
101 PointerLikeTypeTraits<decltype(pair)>::NumLowBitsAvailable);
103 static_assert(
104 is_trivially_copyable<
105 PointerIntPair<Fixnum31, 1, bool, FixnumPointerTraits>>::value,
106 "trivially copyable");
109 } // end anonymous namespace