operator== and != for hidl_vec.

Test: libhidl_test

Bug: 32834072
Change-Id: Iae9b454f5c1e9d8b3b10394ef6de9d4ba4cdd959
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 1a4fd3c..1b0184a 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -382,6 +382,24 @@
         return v;
     }
 
+    // equality check, assuming that T::operator== is defined.
+    bool operator==(const hidl_vec &other) const {
+        if (mSize != other.size()) {
+            return false;
+        }
+        for (size_t i = 0; i < mSize; ++i) {
+            if (!(mBuffer[i] == other.mBuffer[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    // inequality check, assuming that T::operator== is defined.
+    inline bool operator!=(const hidl_vec &other) const {
+        return !((*this) == other);
+    }
+
     size_t size() const {
         return mSize;
     }
@@ -649,6 +667,20 @@
                 &mBuffer[index * details::product<SIZES...>::value]);
     }
 
+    // equality check, assuming that T::operator== is defined.
+    bool operator==(const hidl_array &other) const {
+        for (size_t i = 0; i < elementCount(); ++i) {
+            if (!(mBuffer[i] == other.mBuffer[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    inline bool operator!=(const hidl_array &other) const {
+        return !((*this) == other);
+    }
+
     using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>;
 
     static constexpr size_tuple_type size() {
@@ -696,6 +728,20 @@
         return mBuffer[index];
     }
 
+    // equality check, assuming that T::operator== is defined.
+    bool operator==(const hidl_array &other) const {
+        for (size_t i = 0; i < elementCount(); ++i) {
+            if (!(mBuffer[i] == other.mBuffer[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    inline bool operator!=(const hidl_array &other) const {
+        return !((*this) == other);
+    }
+
     static constexpr size_t size() { return SIZE1; }
     static constexpr size_t elementCount() { return SIZE1; }
 
diff --git a/test_main.cpp b/test_main.cpp
index ca5d017..bdf4b49 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -189,6 +189,16 @@
     EXPECT_EQ(sum, 15+16+17);
 }
 
+TEST_F(LibHidlTest, VecEqTest) {
+    android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
+    android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
+    android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
+
+    // use the == and != operator intentionally here
+    EXPECT_TRUE(hv1 == hv2);
+    EXPECT_TRUE(hv1 != hv3);
+}
+
 TEST_F(LibHidlTest, ArrayTest) {
     using android::hardware::hidl_array;
     int32_t array[] = {5, 6, 7};