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>
diff --git a/src/tint/utils/vector.h b/src/tint/utils/vector.h
index f0cbf58..705817a 100644
--- a/src/tint/utils/vector.h
+++ b/src/tint/utils/vector.h
@@ -463,13 +463,15 @@
/// 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 @@
/// @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)
diff --git a/src/tint/utils/vector_test.cc b/src/tint/utils/vector_test.cc
index b4e9fb5..a3cc1f7 100644
--- a/src/tint/utils/vector_test.cc
+++ b/src/tint/utils/vector_test.cc
@@ -2060,6 +2060,15 @@
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