tint: Fix vector equality operator

Change-Id: Ic8e66016beb8fd6a3046beaccbb9c07acbd765d5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/105260
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-10-11 15:31:46 +00:00 committed by Dawn LUCI CQ
parent 8d3d4f6fd1
commit ab2fa8be87
2 changed files with 21 additions and 7 deletions

View File

@ -463,15 +463,17 @@ class Vector {
/// Equality operator /// Equality operator
/// @param other the other vector /// @param other the other vector
/// @returns true if this vector is the same length as `other`, and all elements are equal. /// @returns true if this vector is the same length as `other`, and all elements are equal.
bool operator==(const Vector& other) const { template <typename T2, size_t N2>
bool operator==(const Vector<T2, N2>& other) const {
const size_t len = Length(); const size_t len = Length();
if (len == other.Length()) { if (len != other.Length()) {
return false;
}
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
if ((*this)[i] != other[i]) { if ((*this)[i] != other[i]) {
return false; return false;
} }
} }
}
return true; return true;
} }
@ -479,7 +481,10 @@ class Vector {
/// @param other the other vector /// @param other the other vector
/// @returns true if this vector is not the same length as `other`, or all elements are not /// @returns true if this vector is not the same length as `other`, or all elements are not
/// equal. /// equal.
bool operator!=(const Vector& other) const { return !(*this == other); } template <typename T2, size_t N2>
bool operator!=(const Vector<T2, N2>& other) const {
return !(*this == other);
}
private: private:
/// Friend class (differing specializations of this class) /// Friend class (differing specializations of this class)

View File

@ -2060,6 +2060,15 @@ TEST(TintVectorRefTest, ConstBeginEnd) {
EXPECT_EQ(vec_ref.end(), &vec[0] + 3); EXPECT_EQ(vec_ref.end(), &vec[0] + 3);
} }
TEST(TintVectorTest, Equality) {
EXPECT_EQ((Vector<int, 2>{1, 2}), (Vector<int, 2>{1, 2}));
EXPECT_EQ((Vector<int, 1>{1, 2}), (Vector<int, 3>{1, 2}));
EXPECT_NE((Vector{1, 2}), (Vector{1}));
EXPECT_NE((Vector{1}), (Vector{1, 2}));
EXPECT_NE((Vector{1, 2}), (Vector{2, 1}));
EXPECT_NE((Vector{2, 1}), (Vector{1, 2}));
}
} // namespace } // namespace
} // namespace tint::utils } // namespace tint::utils