Implement hidl_vec::find and hidl_vec::contains
Also fixed formatting of nearby line.
Bug: 135918744
Test: atest libhidl_test
Change-Id: I3743115bdacdbf56b6e68d57cf7a8f38f99cb182
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 44691e6..056f177 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -569,8 +569,11 @@
iterator end() { return data()+mSize; }
const_iterator begin() const { return data(); }
const_iterator end() const { return data()+mSize; }
+ iterator find(const T& v) { return std::find(begin(), end(), v); }
+ const_iterator find(const T& v) const { return std::find(begin(), end(), v); }
+ bool contains(const T& v) const { return find(v) != end(); }
-private:
+ private:
details::hidl_pointer<T> mBuffer;
uint32_t mSize;
bool mOwnsBuffer;
diff --git a/test_main.cpp b/test_main.cpp
index 6e96a42..7851ddb 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -350,6 +350,41 @@
EXPECT_NE(oldPointer, noCopies.data());
}
+TEST_F(LibHidlTest, VecFindTest) {
+ using android::hardware::hidl_vec;
+ hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
+ const hidl_vec<int32_t> hv2 = {1, 2, 3, 4};
+
+ auto it = hv1.find(20);
+ EXPECT_EQ(20, *it);
+ *it = 21;
+ EXPECT_EQ(21, *it);
+ it = hv1.find(20);
+ EXPECT_EQ(hv1.end(), it);
+ it = hv1.find(21);
+ EXPECT_EQ(21, *it);
+
+ auto cit = hv2.find(4);
+ EXPECT_EQ(4, *cit);
+}
+
+TEST_F(LibHidlTest, VecContainsTest) {
+ using android::hardware::hidl_vec;
+ hidl_vec<int32_t> hv1 = {10, 20, 30, 40};
+ const hidl_vec<int32_t> hv2 = {0, 1, 2, 3, 4};
+
+ EXPECT_TRUE(hv1.contains(10));
+ EXPECT_TRUE(hv1.contains(40));
+ EXPECT_FALSE(hv1.contains(1));
+ EXPECT_FALSE(hv1.contains(0));
+ EXPECT_TRUE(hv2.contains(0));
+ EXPECT_FALSE(hv2.contains(10));
+
+ hv1[0] = 11;
+ EXPECT_FALSE(hv1.contains(10));
+ EXPECT_TRUE(hv1.contains(11));
+}
+
TEST_F(LibHidlTest, ArrayTest) {
using android::hardware::hidl_array;
int32_t array[] = {5, 6, 7};