Convert (by copying) hidl_array from and to std::array.
* For example, hidl_array<T, 2, 3> can be converted to
std::array<std::array<T, 3>, 2>.
* Uses operator= for copying.
* Also uses operator= when initializing an hidl_array
(instead of memcpy'ing). This fixes potential memory
issues for hidl_array<hidl_string>, for example.
Bug: 32883329
Test: libhidl_test
Change-Id: Idf7d080433aaed2c585fd4875f3411e5544a9e72
diff --git a/test_main.cpp b/test_main.cpp
index 276b3a4..3ee8bc9 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -23,6 +23,8 @@
#include <vector>
#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
+#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
+ EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
template<typename T, typename S>
static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
@@ -32,6 +34,15 @@
return true;
}
+template<typename T, typename S>
+static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
+ for(size_t i = 0; i < size1; i++)
+ for (size_t j = 0; j < size2; j++)
+ if(arr1[i][j] != arr2[i][j])
+ return false;
+ return true;
+}
+
class LibHidlTest : public ::testing::Test {
public:
virtual void SetUp() override {
@@ -175,6 +186,29 @@
great(v);
}
+TEST_F(LibHidlTest, StdArrayTest) {
+ using android::hardware::hidl_array;
+ hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
+ std::array<int32_t, 5> stdArray = array;
+ EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
+ hidl_array<int32_t, 5> array2 = stdArray;
+ EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
+}
+
+TEST_F(LibHidlTest, MultiDimStdArrayTest) {
+ using android::hardware::hidl_array;
+ hidl_array<int32_t, 2, 3> array;
+ for (size_t i = 0; i < 2; i++) {
+ for (size_t j = 0; j < 3; j++) {
+ array[i][j] = i + j + i * j;
+ }
+ }
+ std::array<std::array<int32_t, 3>, 2> stdArray = array;
+ EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
+ hidl_array<int32_t, 2, 3> array2 = stdArray;
+ EXPECT_2DARRAYEQ(array, array2, 2, 3);
+}
+
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();