1 //===- llvm/unittest/ADT/PointerEmbeddedIntTest.cpp -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/PointerEmbeddedInt.h"
10 #include "gtest/gtest.h"
15 TEST(PointerEmbeddedIntTest
, Basic
) {
16 PointerEmbeddedInt
<int, CHAR_BIT
> I
= 42, J
= 43;
20 EXPECT_EQ((int)sizeof(uintptr_t) * CHAR_BIT
- CHAR_BIT
,
21 (int)PointerLikeTypeTraits
<decltype(I
)>::NumLowBitsAvailable
);
30 EXPECT_FALSE(I
== 43);
35 EXPECT_FALSE(I
>= 43);
37 EXPECT_FALSE(42 == J
);
42 EXPECT_FALSE(42 >= J
);
45 TEST(PointerEmbeddedIntTest
, intptr_t) {
46 PointerEmbeddedInt
<intptr_t, CHAR_BIT
> IPos
= 42, INeg
= -42;
50 PointerEmbeddedInt
<uintptr_t, CHAR_BIT
> U
= 42, USaturated
= 255;
52 EXPECT_EQ(255U, USaturated
);
54 PointerEmbeddedInt
<intptr_t, std::numeric_limits
<intptr_t>::digits
>
55 IMax
= std::numeric_limits
<intptr_t>::max() >> 1,
56 IMin
= std::numeric_limits
<intptr_t>::min() >> 1;
57 EXPECT_EQ(std::numeric_limits
<intptr_t>::max() >> 1, IMax
);
58 EXPECT_EQ(std::numeric_limits
<intptr_t>::min() >> 1, IMin
);
60 PointerEmbeddedInt
<uintptr_t, std::numeric_limits
<uintptr_t>::digits
- 1>
61 UMax
= std::numeric_limits
<uintptr_t>::max() >> 1,
62 UMin
= std::numeric_limits
<uintptr_t>::min() >> 1;
63 EXPECT_EQ(std::numeric_limits
<uintptr_t>::max() >> 1, UMax
);
64 EXPECT_EQ(std::numeric_limits
<uintptr_t>::min() >> 1, UMin
);
67 TEST(PointerEmbeddedIntTest
, PointerLikeTypeTraits
) {
68 PointerEmbeddedInt
<int, CHAR_BIT
> I
= 42;
69 using ITraits
= PointerLikeTypeTraits
<decltype(I
)>;
70 EXPECT_EQ(42, ITraits::getFromVoidPointer(ITraits::getAsVoidPointer(I
)));
72 PointerEmbeddedInt
<uintptr_t, std::numeric_limits
<uintptr_t>::digits
- 1>
73 Max
= std::numeric_limits
<uintptr_t>::max() >> 1;
74 using MaxTraits
= PointerLikeTypeTraits
<decltype(Max
)>;
75 EXPECT_EQ(std::numeric_limits
<uintptr_t>::max() >> 1,
76 MaxTraits::getFromVoidPointer(MaxTraits::getAsVoidPointer(Max
)));
79 } // end anonymous namespace