[PATCH] D124407: Add missing comparison operators to SmallVector
Frederik Gossen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 25 14:45:08 PDT 2022
frgossen updated this revision to Diff 425037.
frgossen added a comment.
Add tests
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D124407/new/
https://reviews.llvm.org/D124407
Files:
llvm/include/llvm/ADT/SmallVector.h
llvm/unittests/ADT/SmallVectorTest.cpp
Index: llvm/unittests/ADT/SmallVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallVectorTest.cpp
+++ llvm/unittests/ADT/SmallVectorTest.cpp
@@ -123,14 +123,30 @@
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 @@
}
// 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 @@
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;
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -949,6 +949,9 @@
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>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124407.425037.patch
Type: text/x-patch
Size: 3762 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220425/82b77353/attachment.bin>
More information about the llvm-commits
mailing list