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,13 +463,15 @@ class Vector {
/// Equality operator
/// @param other the other vector
/// @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();
if (len == other.Length()) {
for (size_t i = 0; i < len; i++) {
if ((*this)[i] != other[i]) {
return false;
}
if (len != other.Length()) {
return false;
}
for (size_t i = 0; i < len; i++) {
if ((*this)[i] != other[i]) {
return false;
}
}
return true;
@ -479,7 +481,10 @@ class Vector {
/// @param other the other vector
/// @returns true if this vector is not the same length as `other`, or all elements are not
/// 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:
/// Friend class (differing specializations of this class)

View File

@ -2060,6 +2060,15 @@ TEST(TintVectorRefTest, ConstBeginEnd) {
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 tint::utils