Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/PointerEmbeddedIntTest.cpp -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/PointerEmbeddedInt.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 11 | using namespace llvm; |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | TEST(PointerEmbeddedIntTest, Basic) { |
| 16 | PointerEmbeddedInt<int, CHAR_BIT> I = 42, J = 43; |
| 17 | |
| 18 | EXPECT_EQ(42, I); |
| 19 | EXPECT_EQ(43, I + 1); |
David Blaikie | 65eb74e | 2020-01-15 12:36:20 -0800 | [diff] [blame] | 20 | EXPECT_EQ((int)sizeof(uintptr_t) * CHAR_BIT - CHAR_BIT, |
| 21 | (int)PointerLikeTypeTraits<decltype(I)>::NumLowBitsAvailable); |
Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 22 | |
| 23 | EXPECT_FALSE(I == J); |
| 24 | EXPECT_TRUE(I != J); |
| 25 | EXPECT_TRUE(I < J); |
| 26 | EXPECT_FALSE(I > J); |
| 27 | EXPECT_TRUE(I <= J); |
| 28 | EXPECT_FALSE(I >= J); |
| 29 | |
| 30 | EXPECT_FALSE(I == 43); |
| 31 | EXPECT_TRUE(I != 43); |
| 32 | EXPECT_TRUE(I < 43); |
| 33 | EXPECT_FALSE(I > 43); |
| 34 | EXPECT_TRUE(I <= 43); |
| 35 | EXPECT_FALSE(I >= 43); |
| 36 | |
| 37 | EXPECT_FALSE(42 == J); |
| 38 | EXPECT_TRUE(42 != J); |
| 39 | EXPECT_TRUE(42 < J); |
| 40 | EXPECT_FALSE(42 > J); |
| 41 | EXPECT_TRUE(42 <= J); |
| 42 | EXPECT_FALSE(42 >= J); |
| 43 | } |
| 44 | |
Jordan Rose | 3c81d62 | 2016-02-18 21:00:08 +0000 | [diff] [blame] | 45 | TEST(PointerEmbeddedIntTest, intptr_t) { |
| 46 | PointerEmbeddedInt<intptr_t, CHAR_BIT> IPos = 42, INeg = -42; |
| 47 | EXPECT_EQ(42, IPos); |
| 48 | EXPECT_EQ(-42, INeg); |
| 49 | |
| 50 | PointerEmbeddedInt<uintptr_t, CHAR_BIT> U = 42, USaturated = 255; |
| 51 | EXPECT_EQ(42U, U); |
| 52 | EXPECT_EQ(255U, USaturated); |
| 53 | |
| 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); |
| 59 | |
| 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); |
| 65 | } |
| 66 | |
| 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))); |
| 71 | |
| 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))); |
| 77 | } |
| 78 | |
Chandler Carruth | 6145f7d | 2016-01-10 09:40:13 +0000 | [diff] [blame] | 79 | } // end anonymous namespace |