Make BitVector::operator== return false for different-sized vectors.
authorBrad Moody <brad.moody@oracle.com>
Fri, 10 Apr 2020 01:15:49 +0000 (20:15 -0500)
committerMichael Kruse <llvm-project@meinersbur.de>
Fri, 10 Apr 2020 01:28:55 +0000 (20:28 -0500)
This behaviour is in line with SmallBitVector and other vector-like
types.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D77027

llvm/include/llvm/ADT/BitVector.h
llvm/unittests/ADT/BitVectorTest.cpp

index 02e01effc0fc9f85500cf8e3549b237082d4a300..89158f213110d7931e4bc814d912a553dcce1e0f 100644 (file)
@@ -532,24 +532,10 @@ public:
 
   // Comparison operators.
   bool operator==(const BitVector &RHS) const {
-    unsigned ThisWords = NumBitWords(size());
-    unsigned RHSWords  = NumBitWords(RHS.size());
-    unsigned i;
-    for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
-      if (Bits[i] != RHS.Bits[i])
-        return false;
-
-    // Verify that any extra words are all zeros.
-    if (i != ThisWords) {
-      for (; i != ThisWords; ++i)
-        if (Bits[i])
-          return false;
-    } else if (i != RHSWords) {
-      for (; i != RHSWords; ++i)
-        if (RHS.Bits[i])
-          return false;
-    }
-    return true;
+    if (size() != RHS.size())
+      return false;
+    unsigned NumWords = NumBitWords(size());
+    return Bits.take_front(NumWords) == RHS.Bits.take_front(NumWords);
   }
 
   bool operator!=(const BitVector &RHS) const {
index c933388361411682bcf489c70ef44db9d0b783b4..7c9417966b163c4980bd3c8ecde2846e5bd26f8f 100644 (file)
@@ -179,6 +179,24 @@ TYPED_TEST(BitVectorTest, TrivialOperation) {
   EXPECT_TRUE(Vec.empty());
 }
 
+TYPED_TEST(BitVectorTest, Equality) {
+  TypeParam A;
+  TypeParam B;
+  EXPECT_TRUE(A == B);
+  A.resize(10);
+  EXPECT_FALSE(A == B);
+  B.resize(10);
+  EXPECT_TRUE(A == B);
+  A.set(5);
+  EXPECT_FALSE(A == B);
+  B.set(5);
+  EXPECT_TRUE(A == B);
+  A.resize(20);
+  EXPECT_FALSE(A == B);
+  B.resize(20);
+  EXPECT_TRUE(A == B);
+}
+
 TYPED_TEST(BitVectorTest, SimpleFindOpsMultiWord) {
   TypeParam A;