test_main.cpp revision 53120f70b7d429c81fe47718182e829660ed5ef9
1b93f0504e861885734d954f716da0331af4aa438Yifan Hong/*
2b93f0504e861885734d954f716da0331af4aa438Yifan Hong * Copyright (C) 2016 The Android Open Source Project
3b93f0504e861885734d954f716da0331af4aa438Yifan Hong *
4b93f0504e861885734d954f716da0331af4aa438Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License");
5b93f0504e861885734d954f716da0331af4aa438Yifan Hong * you may not use this file except in compliance with the License.
6b93f0504e861885734d954f716da0331af4aa438Yifan Hong * You may obtain a copy of the License at
7b93f0504e861885734d954f716da0331af4aa438Yifan Hong *
8b93f0504e861885734d954f716da0331af4aa438Yifan Hong *      http://www.apache.org/licenses/LICENSE-2.0
9b93f0504e861885734d954f716da0331af4aa438Yifan Hong *
10b93f0504e861885734d954f716da0331af4aa438Yifan Hong * Unless required by applicable law or agreed to in writing, software
11b93f0504e861885734d954f716da0331af4aa438Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS,
12b93f0504e861885734d954f716da0331af4aa438Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b93f0504e861885734d954f716da0331af4aa438Yifan Hong * See the License for the specific language governing permissions and
14b93f0504e861885734d954f716da0331af4aa438Yifan Hong * limitations under the License.
15b93f0504e861885734d954f716da0331af4aa438Yifan Hong */
16b93f0504e861885734d954f716da0331af4aa438Yifan Hong
17602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#define LOG_TAG "LibHidlTest"
18602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
19602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#include <android-base/logging.h>
20602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#include <gtest/gtest.h>
21602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#include <hidl/HidlSupport.h>
225e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong#include <hidl/TaskRunner.h>
23602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#include <vector>
24602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
25602b85a846834d466e1f545eace06df87cf94a1eYifan Hong#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
2644ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
2744ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong        EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
28602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
29602b85a846834d466e1f545eace06df87cf94a1eYifan Hongtemplate<typename T, typename S>
30602b85a846834d466e1f545eace06df87cf94a1eYifan Hongstatic inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
31602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    for(size_t i = 0; i < size; i++)
32602b85a846834d466e1f545eace06df87cf94a1eYifan Hong        if(arr1[i] != arr2[i])
33602b85a846834d466e1f545eace06df87cf94a1eYifan Hong            return false;
34602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    return true;
35602b85a846834d466e1f545eace06df87cf94a1eYifan Hong}
36602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
3744ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hongtemplate<typename T, typename S>
3844ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hongstatic inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
3944ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    for(size_t i = 0; i < size1; i++)
4044ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong        for (size_t j = 0; j < size2; j++)
4144ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong            if(arr1[i][j] != arr2[i][j])
4244ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong                return false;
4344ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    return true;
4444ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong}
4544ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong
46602b85a846834d466e1f545eace06df87cf94a1eYifan Hongclass LibHidlTest : public ::testing::Test {
47602b85a846834d466e1f545eace06df87cf94a1eYifan Hongpublic:
48602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    virtual void SetUp() override {
49602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    }
50602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    virtual void TearDown() override {
51602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    }
52602b85a846834d466e1f545eace06df87cf94a1eYifan Hong};
53602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
54602b85a846834d466e1f545eace06df87cf94a1eYifan HongTEST_F(LibHidlTest, StringTest) {
55602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    using android::hardware::hidl_string;
56602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_string s; // empty constructor
57602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s.c_str(), "");
58602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_string s1 = "s1"; // copy = from cstr
59602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s1.c_str(), "s1");
60602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_string s2("s2"); // copy constructor from cstr
61602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s2.c_str(), "s2");
62602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_string s3 = hidl_string("s3"); // move =
63602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s3.c_str(), "s3");
6453120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
6553120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    EXPECT_STREQ(s4.c_str(), "123");
6653120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
6753120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    EXPECT_STREQ(s5.c_str(), "s5");
6853120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s6(std::string("s6")); // copy constructor from std::string
69602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s6, "s6");
7053120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s7 = std::string("s7"); // copy = from std::string
7153120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    EXPECT_STREQ(s7, "s7");
7253120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s8(s7); // copy constructor
7353120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    EXPECT_STREQ(s8, "s7");
7453120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    hidl_string s9 = s8; // copy =
7553120f70b7d429c81fe47718182e829660ed5ef9Steven Moreland    EXPECT_STREQ(s9, "s7");
76602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    char myCString[20] = "myCString";
77602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    s.setToExternal(&myCString[0], strlen(myCString));
78602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s, "myCString");
79602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    myCString[2] = 'D';
80602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(s, "myDString");
81602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    s.clear(); // should not affect myCString
82602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(myCString, "myDString");
83bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph
84602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    // casts
85602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    s = "great";
86602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    std::string myString = s;
87602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    const char *anotherCString = s;
88602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_EQ(myString, "great");
89602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_STREQ(anotherCString, "great");
90bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph
91bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    // Comparisons
92bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    const char * cstr1 = "abc";
93bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    std::string string1(cstr1);
94bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    hidl_string hs1(cstr1);
95bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    const char * cstrE = "abc";
96bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    std::string stringE(cstrE);
97bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    hidl_string hsE(cstrE);
98bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    const char * cstrNE = "ABC";
99bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    std::string stringNE(cstrNE);
100bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    hidl_string hsNE(cstrNE);
101bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  == hsE);
102bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 != hsE);
103bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  != hsNE);
104bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 == hsNE);
105bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  == cstrE);
106bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 != cstrE);
107bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  != cstrNE);
108bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 == cstrNE);
109bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  == stringE);
110bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 != stringE);
111bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_TRUE(hs1  != stringNE);
112bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_FALSE(hs1 == stringNE);
113602b85a846834d466e1f545eace06df87cf94a1eYifan Hong}
114602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
1150f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff TinkerTEST_F(LibHidlTest, MemoryTest) {
1160f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    using android::hardware::hidl_memory;
1170f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1180f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem1 = hidl_memory(); // default constructor
1190f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem2 = mem1; // copy constructor (nullptr)
1200f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1210f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_EQ(nullptr, mem2.handle());
1220f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1230f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
1240f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1250f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
1260f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem4 = mem3; // copy constructor (regular handle)
1270f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1280f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_EQ(mem3.name(), mem4.name());
1290f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_EQ(mem3.size(), mem4.size());
1300f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_NE(nullptr, mem4.handle());
1310f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
1320f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
1330f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
1340f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    hidl_memory mem6 = mem5;
1350f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_EQ(nullptr, mem5.handle());
1360f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker    EXPECT_EQ(nullptr, mem6.handle());
1370f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker}
1380f3461db91b75d9acd7e0bec8b1e97cbb3bafdf7Jeff Tinker
13964fdf4d1bdea86202c7526482d4d329724d33dcbYifan HongTEST_F(LibHidlTest, VecInitTest) {
140602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    using android::hardware::hidl_vec;
141602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    using std::vector;
142602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    int32_t array[] = {5, 6, 7};
143602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    vector<int32_t> v(array, array + 3);
144602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
145602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_vec<int32_t> hv1 = v; // copy =
146602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_ARRAYEQ(hv1, array, 3);
147602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_ARRAYEQ(hv1, v, 3);
148602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    hidl_vec<int32_t> hv2(v); // copy constructor
149602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_ARRAYEQ(hv2, v, 3);
150602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
151602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    vector<int32_t> v2 = hv1; // cast
152602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    EXPECT_ARRAYEQ(v2, v, 3);
1539fbfe475d933ecd29c9c2af7e35e9b13fbdf905dSteven Moreland
1549fbfe475d933ecd29c9c2af7e35e9b13fbdf905dSteven Moreland    hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
155b69926a728aa472be47f51dc5fc374571d7da5a8Steven Moreland    EXPECT_EQ(v3.size(), 3ul);
1569fbfe475d933ecd29c9c2af7e35e9b13fbdf905dSteven Moreland    EXPECT_ARRAYEQ(v3, array, v3.size());
15764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong}
15864fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
15964fdf4d1bdea86202c7526482d4d329724d33dcbYifan HongTEST_F(LibHidlTest, VecIterTest) {
16064fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    int32_t array[] = {5, 6, 7};
16164fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
162bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph
163bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    auto iter = hv1.begin();    // iterator begin()
16464fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*iter++, 5);
16564fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*iter, 6);
16664fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*++iter, 7);
16764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*iter--, 7);
168bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_EQ(*iter, 6);
16964fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*--iter, 5);
17064fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
17164fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    iter += 2;
172bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_EQ(*iter, 7);
17364fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    iter -= 2;
17464fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*iter, 5);
17564fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
17664fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    iter++;
17764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*(iter + 1), 7);
17864fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*(1 + iter), 7);
17964fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*(iter - 1), 5);
18064fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(*iter, 6);
18164fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
18264fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    auto five = iter - 1;
18364fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    auto seven = iter + 1;
18464fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(seven - five, 2);
18564fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(five - seven, -2);
18664fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
18764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_LT(five, seven);
18864fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_LE(five, seven);
18964fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_GT(seven, five);
19064fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_GE(seven, five);
19164fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
19264fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(seven[0], 7);
19364fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(five[1], 6);
19464fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong}
19564fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
19664fdf4d1bdea86202c7526482d4d329724d33dcbYifan HongTEST_F(LibHidlTest, VecIterForTest) {
19764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    using android::hardware::hidl_vec;
19864fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    int32_t array[] = {5, 6, 7};
19964fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
200bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph
201bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    int32_t sum = 0;            // range based for loop interoperability
202bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    for (auto &&i: hv1) {
203bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph        sum += i;
204bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    }
205bb840f73dc33c5de153cc6959672cf05c55ae4e6Scott Randolph    EXPECT_EQ(sum, 5+6+7);
20664fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong
20764fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
20864fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong        *iter += 10;
20964fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    }
21064fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    const hidl_vec<int32_t> &v4 = hv1;
21164fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    sum = 0;
21264fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    for (const auto &i : v4) {
21364fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong        sum += i;
21464fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    }
21564fdf4d1bdea86202c7526482d4d329724d33dcbYifan Hong    EXPECT_EQ(sum, 15+16+17);
216602b85a846834d466e1f545eace06df87cf94a1eYifan Hong}
217602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
2189fcbb36250a4392b12012cf5177c4669ef10e654Yifan HongTEST_F(LibHidlTest, VecEqTest) {
2199fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
2209fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
2219fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
2229fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong
2239fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    // use the == and != operator intentionally here
2249fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    EXPECT_TRUE(hv1 == hv2);
2259fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong    EXPECT_TRUE(hv1 != hv3);
2269fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong}
2279fcbb36250a4392b12012cf5177c4669ef10e654Yifan Hong
2283da68488e12ae8d487f43700582b775a4cdb6ec1Sasha LevitskiyTEST_F(LibHidlTest, ArrayTest) {
2293da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy    using android::hardware::hidl_array;
2303da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy    int32_t array[] = {5, 6, 7};
2313da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy
2323da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy    hidl_array<int32_t, 3> ha(array);
2333da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy    EXPECT_ARRAYEQ(ha, array, 3);
2343da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy}
2353da68488e12ae8d487f43700582b775a4cdb6ec1Sasha Levitskiy
2365e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan HongTEST_F(LibHidlTest, TaskRunnerTest) {
2375e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    using android::hardware::TaskRunner;
2385e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    TaskRunner tr;
2395e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    bool flag = false;
2405e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    tr.push([&] {
2415e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong        usleep(1000);
2425e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong        flag = true;
2435e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    });
2445e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    usleep(500);
2455e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    EXPECT_FALSE(flag);
2465e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    usleep(1000);
2475e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong    EXPECT_TRUE(flag);
2485e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong}
2495e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong
2505708fb4e117f06f8b867eb623d73fd3e5d969155Yifan HongTEST_F(LibHidlTest, StringCmpTest) {
2515708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    using android::hardware::hidl_string;
2525708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    const char * s = "good";
2535708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    hidl_string hs(s);
2545708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    EXPECT_NE(hs.c_str(), s);
2555708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong
2565708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    EXPECT_TRUE(hs == s); // operator ==
2575708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    EXPECT_TRUE(s == hs);
2585708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong
2595708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    EXPECT_FALSE(hs != s); // operator ==
2605708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong    EXPECT_FALSE(s != hs);
2615708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong}
2625708fb4e117f06f8b867eb623d73fd3e5d969155Yifan Hong
263602b85a846834d466e1f545eace06df87cf94a1eYifan Hongtemplate <typename T>
264602b85a846834d466e1f545eace06df87cf94a1eYifan Hongvoid great(android::hardware::hidl_vec<T>) {}
265602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
266602b85a846834d466e1f545eace06df87cf94a1eYifan HongTEST_F(LibHidlTest, VecCopyTest) {
267602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    android::hardware::hidl_vec<int32_t> v;
268602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    great(v);
269602b85a846834d466e1f545eace06df87cf94a1eYifan Hong}
270602b85a846834d466e1f545eace06df87cf94a1eYifan Hong
27144ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan HongTEST_F(LibHidlTest, StdArrayTest) {
27244ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    using android::hardware::hidl_array;
27344ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
27444ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    std::array<int32_t, 5> stdArray = array;
27544ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
27644ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    hidl_array<int32_t, 5> array2 = stdArray;
27744ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
27844ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong}
27944ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong
28044ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan HongTEST_F(LibHidlTest, MultiDimStdArrayTest) {
28144ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    using android::hardware::hidl_array;
28244ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    hidl_array<int32_t, 2, 3> array;
28344ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    for (size_t i = 0; i < 2; i++) {
28444ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong        for (size_t j = 0; j < 3; j++) {
28544ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong            array[i][j] = i + j + i * j;
28644ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong        }
28744ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    }
28844ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    std::array<std::array<int32_t, 3>, 2> stdArray = array;
28944ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
29044ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    hidl_array<int32_t, 2, 3> array2 = stdArray;
29144ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong    EXPECT_2DARRAYEQ(array, array2, 2, 3);
29244ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong}
29344ab623ccf386f0e470bdc6aa22f261b7dc047cdYifan Hong
29419f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville TalvalaTEST_F(LibHidlTest, HidlVersionTest) {
29519f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    using android::hardware::hidl_version;
29619f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v1_0{1, 0};
29719f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_EQ(1, v1_0.get_major());
29819f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_EQ(0, v1_0.get_minor());
29919f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v2_0{2, 0};
30019f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v2_1{2, 1};
30119f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v2_2{2, 2};
30219f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v3_0{3, 0};
30319f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    hidl_version v3_0b{3,0};
30419f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala
30519f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v1_0 < v2_0);
30619f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v2_0 < v2_1);
30719f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v2_1 < v3_0);
30819f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v2_0 > v1_0);
30919f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v2_1 > v2_0);
31019f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v3_0 > v2_1);
31119f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v3_0 == v3_0b);
31219f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v3_0 <= v3_0b);
31319f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v2_2 <= v3_0);
31419f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v3_0 >= v3_0b);
31519f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala    EXPECT_TRUE(v3_0 >= v2_2);
31619f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala}
31719f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala
31819f4db58674560ebe6b85dad77af0155a3dddecfEino-Ville Talvala
319602b85a846834d466e1f545eace06df87cf94a1eYifan Hongint main(int argc, char **argv) {
320602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    ::testing::InitGoogleTest(&argc, argv);
321602b85a846834d466e1f545eace06df87cf94a1eYifan Hong    return RUN_ALL_TESTS();
322602b85a846834d466e1f545eace06df87cf94a1eYifan Hong}
323