1 //===- llvm/unittest/ADT/PointerEmbeddedIntTest.cpp -----------------------===//
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 "gtest/gtest.h"
11 #include "llvm/ADT/PointerEmbeddedInt.h"
16 TEST(PointerEmbeddedIntTest
, Basic
) {
17 PointerEmbeddedInt
<int, CHAR_BIT
> I
= 42, J
= 43;
21 EXPECT_EQ(sizeof(uintptr_t) * CHAR_BIT
- CHAR_BIT
,
22 PointerLikeTypeTraits
<decltype(I
)>::NumLowBitsAvailable
);
31 EXPECT_FALSE(I
== 43);
36 EXPECT_FALSE(I
>= 43);
38 EXPECT_FALSE(42 == J
);
43 EXPECT_FALSE(42 >= J
);
46 TEST(PointerEmbeddedIntTest
, intptr_t) {
47 PointerEmbeddedInt
<intptr_t, CHAR_BIT
> IPos
= 42, INeg
= -42;
51 PointerEmbeddedInt
<uintptr_t, CHAR_BIT
> U
= 42, USaturated
= 255;
53 EXPECT_EQ(255U, USaturated
);
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
);
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
);
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
)));
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
)));
80 } // end anonymous namespace