[llvm] ac1c0dd - [ADT] Add some basic APInt::isPowerOf2() unit test coverage

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 18 07:01:37 PDT 2021


Author: Simon Pilgrim
Date: 2021-10-18T15:01:28+01:00
New Revision: ac1c0dd3175aaf139b81762d7b06fa38ca55b9aa

URL: https://github.com/llvm/llvm-project/commit/ac1c0dd3175aaf139b81762d7b06fa38ca55b9aa
DIFF: https://github.com/llvm/llvm-project/commit/ac1c0dd3175aaf139b81762d7b06fa38ca55b9aa.diff

LOG: [ADT] Add some basic APInt::isPowerOf2() unit test coverage

Added: 
    

Modified: 
    llvm/unittests/ADT/APIntTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 58c17b49a1022..3631c126718a8 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1765,6 +1765,26 @@ TEST(APIntTest, isShiftedMask) {
   }
 }
 
+TEST(APIntTest, isPowerOf2) {
+  EXPECT_FALSE(APInt(5, 0x00).isPowerOf2());
+  EXPECT_FALSE(APInt(32, 0x11).isPowerOf2());
+  EXPECT_TRUE(APInt(17, 0x01).isPowerOf2());
+  EXPECT_TRUE(APInt(32, 0xff << 31).isPowerOf2());
+
+  for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) {
+    EXPECT_FALSE(APInt(N, 0).isPowerOf2());
+    EXPECT_TRUE(APInt::getSignedMinValue(N).isPowerOf2());
+
+    APInt One(N, 1);
+    for (int I = 1; I < N - 1; ++I) {
+      EXPECT_TRUE(APInt::getOneBitSet(N, I).isPowerOf2());
+
+      APInt MaskVal = One.shl(I);
+      EXPECT_TRUE(MaskVal.isPowerOf2());
+    }
+  }
+}
+
 // Test that self-move works with EXPENSIVE_CHECKS. It calls std::shuffle which
 // does self-move on some platforms.
 #ifdef EXPENSIVE_CHECKS


        


More information about the llvm-commits mailing list