Skip to content

Commit ac1c0dd

Browse files
committed
[ADT] Add some basic APInt::isPowerOf2() unit test coverage
1 parent d5429a1 commit ac1c0dd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

llvm/unittests/ADT/APIntTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,26 @@ TEST(APIntTest, isShiftedMask) {
17651765
}
17661766
}
17671767

1768+
TEST(APIntTest, isPowerOf2) {
1769+
EXPECT_FALSE(APInt(5, 0x00).isPowerOf2());
1770+
EXPECT_FALSE(APInt(32, 0x11).isPowerOf2());
1771+
EXPECT_TRUE(APInt(17, 0x01).isPowerOf2());
1772+
EXPECT_TRUE(APInt(32, 0xff << 31).isPowerOf2());
1773+
1774+
for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
1775+
EXPECT_FALSE(APInt(N, 0).isPowerOf2());
1776+
EXPECT_TRUE(APInt::getSignedMinValue(N).isPowerOf2());
1777+
1778+
APInt One(N, 1);
1779+
for (int I = 1; I < N - 1; ++I) {
1780+
EXPECT_TRUE(APInt::getOneBitSet(N, I).isPowerOf2());
1781+
1782+
APInt MaskVal = One.shl(I);
1783+
EXPECT_TRUE(MaskVal.isPowerOf2());
1784+
}
1785+
}
1786+
}
1787+
17681788
// Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which
17691789
// does self-move on some platforms.
17701790
#ifdef EXPENSIVE_CHECKS

0 commit comments

Comments
 (0)