Skip to content

Commit 8fbf9ac

Browse files
committed
Add missing comparison operators to SmallVector
Differential Revision: https://reviews.llvm.org/D124407
1 parent 0c99575 commit 8fbf9ac

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
949949
return std::lexicographical_compare(this->begin(), this->end(),
950950
RHS.begin(), RHS.end());
951951
}
952+
bool operator>(const SmallVectorImpl &RHS) const { return RHS < *this; }
953+
bool operator<=(const SmallVectorImpl &RHS) const { return !(*this > RHS); }
954+
bool operator>=(const SmallVectorImpl &RHS) const { return !(*this < RHS); }
952955
};
953956

954957
template <typename T>

llvm/unittests/ADT/SmallVectorTest.cpp

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,30 @@ class Constructable {
123123
return numCopyAssignmentCalls;
124124
}
125125

126-
friend bool operator==(const Constructable & c0, const Constructable & c1) {
126+
friend bool operator==(const Constructable &c0, const Constructable &c1) {
127127
return c0.getValue() == c1.getValue();
128128
}
129129

130-
friend bool LLVM_ATTRIBUTE_UNUSED
131-
operator!=(const Constructable & c0, const Constructable & c1) {
130+
friend bool LLVM_ATTRIBUTE_UNUSED operator!=(const Constructable &c0,
131+
const Constructable &c1) {
132132
return c0.getValue() != c1.getValue();
133133
}
134+
135+
friend bool operator<(const Constructable &c0, const Constructable &c1) {
136+
return c0.getValue() < c1.getValue();
137+
}
138+
friend bool LLVM_ATTRIBUTE_UNUSED operator<=(const Constructable &c0,
139+
const Constructable &c1) {
140+
return c0.getValue() <= c1.getValue();
141+
}
142+
friend bool LLVM_ATTRIBUTE_UNUSED operator>(const Constructable &c0,
143+
const Constructable &c1) {
144+
return c0.getValue() > c1.getValue();
145+
}
146+
friend bool LLVM_ATTRIBUTE_UNUSED operator>=(const Constructable &c0,
147+
const Constructable &c1) {
148+
return c0.getValue() >= c1.getValue();
149+
}
134150
};
135151

136152
int Constructable::numConstructorCalls;
@@ -766,8 +782,8 @@ TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
766782
}
767783

768784
// Comparison tests.
769-
TYPED_TEST(SmallVectorTest, ComparisonTest) {
770-
SCOPED_TRACE("ComparisonTest");
785+
TYPED_TEST(SmallVectorTest, ComparisonEqualityTest) {
786+
SCOPED_TRACE("ComparisonEqualityTest");
771787

772788
this->makeSequence(this->theVector, 1, 3);
773789
this->makeSequence(this->otherVector, 1, 3);
@@ -782,6 +798,36 @@ TYPED_TEST(SmallVectorTest, ComparisonTest) {
782798
EXPECT_TRUE(this->theVector != this->otherVector);
783799
}
784800

801+
// Comparison tests.
802+
TYPED_TEST(SmallVectorTest, ComparisonLessThanTest) {
803+
SCOPED_TRACE("ComparisonLessThanTest");
804+
805+
this->theVector = {1, 2, 4};
806+
this->otherVector = {1, 4};
807+
808+
EXPECT_TRUE(this->theVector < this->otherVector);
809+
EXPECT_TRUE(this->theVector <= this->otherVector);
810+
EXPECT_FALSE(this->theVector > this->otherVector);
811+
EXPECT_FALSE(this->theVector >= this->otherVector);
812+
813+
EXPECT_FALSE(this->otherVector < this->theVector);
814+
EXPECT_FALSE(this->otherVector <= this->theVector);
815+
EXPECT_TRUE(this->otherVector > this->theVector);
816+
EXPECT_TRUE(this->otherVector >= this->theVector);
817+
818+
this->otherVector = {1, 2, 4};
819+
820+
EXPECT_FALSE(this->theVector < this->otherVector);
821+
EXPECT_TRUE(this->theVector <= this->otherVector);
822+
EXPECT_FALSE(this->theVector > this->otherVector);
823+
EXPECT_TRUE(this->theVector >= this->otherVector);
824+
825+
EXPECT_FALSE(this->otherVector < this->theVector);
826+
EXPECT_TRUE(this->otherVector <= this->theVector);
827+
EXPECT_FALSE(this->otherVector > this->theVector);
828+
EXPECT_TRUE(this->otherVector >= this->theVector);
829+
}
830+
785831
// Constant vector tests.
786832
TYPED_TEST(SmallVectorTest, ConstVectorTest) {
787833
const TypeParam constVector;

0 commit comments

Comments
 (0)