1 //===- llvm/unittest/ADT/PointerEmbeddedIntTest.cpp -----------------------===//
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/PointerEmbeddedInt.h"
12 using namespace llvm;
13
14 namespace {
15
TEST(PointerEmbeddedIntTest,Basic)16 TEST(PointerEmbeddedIntTest, Basic) {
17 PointerEmbeddedInt<int, CHAR_BIT> I = 42, J = 43;
18
19 EXPECT_EQ(42, I);
20 EXPECT_EQ(43, I + 1);
21 EXPECT_EQ(sizeof(uintptr_t) * CHAR_BIT - CHAR_BIT,
22 PointerLikeTypeTraits<decltype(I)>::NumLowBitsAvailable);
23
24 EXPECT_FALSE(I == J);
25 EXPECT_TRUE(I != J);
26 EXPECT_TRUE(I < J);
27 EXPECT_FALSE(I > J);
28 EXPECT_TRUE(I <= J);
29 EXPECT_FALSE(I >= J);
30
31 EXPECT_FALSE(I == 43);
32 EXPECT_TRUE(I != 43);
33 EXPECT_TRUE(I < 43);
34 EXPECT_FALSE(I > 43);
35 EXPECT_TRUE(I <= 43);
36 EXPECT_FALSE(I >= 43);
37
38 EXPECT_FALSE(42 == J);
39 EXPECT_TRUE(42 != J);
40 EXPECT_TRUE(42 < J);
41 EXPECT_FALSE(42 > J);
42 EXPECT_TRUE(42 <= J);
43 EXPECT_FALSE(42 >= J);
44 }
45
TEST(PointerEmbeddedIntTest,intptr_t)46 TEST(PointerEmbeddedIntTest, intptr_t) {
47 PointerEmbeddedInt<intptr_t, CHAR_BIT> IPos = 42, INeg = -42;
48 EXPECT_EQ(42, IPos);
49 EXPECT_EQ(-42, INeg);
50
51 PointerEmbeddedInt<uintptr_t, CHAR_BIT> U = 42, USaturated = 255;
52 EXPECT_EQ(42U, U);
53 EXPECT_EQ(255U, USaturated);
54
55 PointerEmbeddedInt<intptr_t, std::numeric_limits<intptr_t>::digits>
56 IMax = std::numeric_limits<intptr_t>::max() >> 1,
57 IMin = std::numeric_limits<intptr_t>::min() >> 1;
58 EXPECT_EQ(std::numeric_limits<intptr_t>::max() >> 1, IMax);
59 EXPECT_EQ(std::numeric_limits<intptr_t>::min() >> 1, IMin);
60
61 PointerEmbeddedInt<uintptr_t, std::numeric_limits<uintptr_t>::digits - 1>
62 UMax = std::numeric_limits<uintptr_t>::max() >> 1,
63 UMin = std::numeric_limits<uintptr_t>::min() >> 1;
64 EXPECT_EQ(std::numeric_limits<uintptr_t>::max() >> 1, UMax);
65 EXPECT_EQ(std::numeric_limits<uintptr_t>::min() >> 1, UMin);
66 }
67
TEST(PointerEmbeddedIntTest,PointerLikeTypeTraits)68 TEST(PointerEmbeddedIntTest, PointerLikeTypeTraits) {
69 PointerEmbeddedInt<int, CHAR_BIT> I = 42;
70 using ITraits = PointerLikeTypeTraits<decltype(I)>;
71 EXPECT_EQ(42, ITraits::getFromVoidPointer(ITraits::getAsVoidPointer(I)));
72
73 PointerEmbeddedInt<uintptr_t, std::numeric_limits<uintptr_t>::digits - 1>
74 Max = std::numeric_limits<uintptr_t>::max() >> 1;
75 using MaxTraits = PointerLikeTypeTraits<decltype(Max)>;
76 EXPECT_EQ(std::numeric_limits<uintptr_t>::max() >> 1,
77 MaxTraits::getFromVoidPointer(MaxTraits::getAsVoidPointer(Max)));
78 }
79
80 } // end anonymous namespace
81