Add missing comparison operators to SmallVector
authorFrederik Gossen <frgossen@google.com>
Mon, 25 Apr 2022 21:44:53 +0000 (17:44 -0400)
committerFrederik Gossen <frgossen@google.com>
Mon, 25 Apr 2022 22:18:14 +0000 (18:18 -0400)
Differential Revision: https://reviews.llvm.org/D124407

llvm/include/llvm/ADT/SmallVector.h
llvm/unittests/ADT/SmallVectorTest.cpp

index 701d10fc2bf097105946b47bf5e14d0b9b393f00..e34702bdbb3c1a3dbc4ea6c2725abaffec583ba6 100644 (file)
@@ -949,6 +949,9 @@ public:
     return std::lexicographical_compare(this->begin(), this->end(),
                                         RHS.begin(), RHS.end());
   }
+  bool operator>(const SmallVectorImpl &RHS) const { return RHS < *this; }
+  bool operator<=(const SmallVectorImpl &RHS) const { return !(*this > RHS); }
+  bool operator>=(const SmallVectorImpl &RHS) const { return !(*this < RHS); }
 };
 
 template <typename T>
index 3fbea5299501bdb2c36ceb2caf8f2cbda40efdde..07479ecdf5f8a206498f92443d7e3e8e382910f0 100644 (file)
@@ -123,14 +123,30 @@ public:
     return numCopyAssignmentCalls;
   }
 
-  friend bool operator==(const Constructable & c0, const Constructable & c1) {
+  friend bool operator==(const Constructable &c0, const Constructable &c1) {
     return c0.getValue() == c1.getValue();
   }
 
-  friend bool LLVM_ATTRIBUTE_UNUSED
-  operator!=(const Constructable & c0, const Constructable & c1) {
+  friend bool LLVM_ATTRIBUTE_UNUSED operator!=(const Constructable &c0,
+                                               const Constructable &c1) {
     return c0.getValue() != c1.getValue();
   }
+
+  friend bool operator<(const Constructable &c0, const Constructable &c1) {
+    return c0.getValue() < c1.getValue();
+  }
+  friend bool LLVM_ATTRIBUTE_UNUSED operator<=(const Constructable &c0,
+                                               const Constructable &c1) {
+    return c0.getValue() <= c1.getValue();
+  }
+  friend bool LLVM_ATTRIBUTE_UNUSED operator>(const Constructable &c0,
+                                              const Constructable &c1) {
+    return c0.getValue() > c1.getValue();
+  }
+  friend bool LLVM_ATTRIBUTE_UNUSED operator>=(const Constructable &c0,
+                                               const Constructable &c1) {
+    return c0.getValue() >= c1.getValue();
+  }
 };
 
 int Constructable::numConstructorCalls;
@@ -766,8 +782,8 @@ TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
 }
 
 // Comparison tests.
-TYPED_TEST(SmallVectorTest, ComparisonTest) {
-  SCOPED_TRACE("ComparisonTest");
+TYPED_TEST(SmallVectorTest, ComparisonEqualityTest) {
+  SCOPED_TRACE("ComparisonEqualityTest");
 
   this->makeSequence(this->theVector, 1, 3);
   this->makeSequence(this->otherVector, 1, 3);
@@ -782,6 +798,36 @@ TYPED_TEST(SmallVectorTest, ComparisonTest) {
   EXPECT_TRUE(this->theVector != this->otherVector);
 }
 
+// Comparison tests.
+TYPED_TEST(SmallVectorTest, ComparisonLessThanTest) {
+  SCOPED_TRACE("ComparisonLessThanTest");
+
+  this->theVector = {1, 2, 4};
+  this->otherVector = {1, 4};
+
+  EXPECT_TRUE(this->theVector < this->otherVector);
+  EXPECT_TRUE(this->theVector <= this->otherVector);
+  EXPECT_FALSE(this->theVector > this->otherVector);
+  EXPECT_FALSE(this->theVector >= this->otherVector);
+
+  EXPECT_FALSE(this->otherVector < this->theVector);
+  EXPECT_FALSE(this->otherVector <= this->theVector);
+  EXPECT_TRUE(this->otherVector > this->theVector);
+  EXPECT_TRUE(this->otherVector >= this->theVector);
+
+  this->otherVector = {1, 2, 4};
+
+  EXPECT_FALSE(this->theVector < this->otherVector);
+  EXPECT_TRUE(this->theVector <= this->otherVector);
+  EXPECT_FALSE(this->theVector > this->otherVector);
+  EXPECT_TRUE(this->theVector >= this->otherVector);
+
+  EXPECT_FALSE(this->otherVector < this->theVector);
+  EXPECT_TRUE(this->otherVector <= this->theVector);
+  EXPECT_FALSE(this->otherVector > this->theVector);
+  EXPECT_TRUE(this->otherVector >= this->theVector);
+}
+
 // Constant vector tests.
 TYPED_TEST(SmallVectorTest, ConstVectorTest) {
   const TypeParam constVector;