test_main.cpp revision 53120f70b7d429c81fe47718182e829660ed5ef9
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "LibHidlTest"
18
19#include <android-base/logging.h>
20#include <gtest/gtest.h>
21#include <hidl/HidlSupport.h>
22#include <hidl/TaskRunner.h>
23#include <vector>
24
25#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
26#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
27        EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
28
29template<typename T, typename S>
30static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
31    for(size_t i = 0; i < size; i++)
32        if(arr1[i] != arr2[i])
33            return false;
34    return true;
35}
36
37template<typename T, typename S>
38static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) {
39    for(size_t i = 0; i < size1; i++)
40        for (size_t j = 0; j < size2; j++)
41            if(arr1[i][j] != arr2[i][j])
42                return false;
43    return true;
44}
45
46class LibHidlTest : public ::testing::Test {
47public:
48    virtual void SetUp() override {
49    }
50    virtual void TearDown() override {
51    }
52};
53
54TEST_F(LibHidlTest, StringTest) {
55    using android::hardware::hidl_string;
56    hidl_string s; // empty constructor
57    EXPECT_STREQ(s.c_str(), "");
58    hidl_string s1 = "s1"; // copy = from cstr
59    EXPECT_STREQ(s1.c_str(), "s1");
60    hidl_string s2("s2"); // copy constructor from cstr
61    EXPECT_STREQ(s2.c_str(), "s2");
62    hidl_string s3 = hidl_string("s3"); // move =
63    EXPECT_STREQ(s3.c_str(), "s3");
64    hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length
65    EXPECT_STREQ(s4.c_str(), "123");
66    hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
67    EXPECT_STREQ(s5.c_str(), "s5");
68    hidl_string s6(std::string("s6")); // copy constructor from std::string
69    EXPECT_STREQ(s6, "s6");
70    hidl_string s7 = std::string("s7"); // copy = from std::string
71    EXPECT_STREQ(s7, "s7");
72    hidl_string s8(s7); // copy constructor
73    EXPECT_STREQ(s8, "s7");
74    hidl_string s9 = s8; // copy =
75    EXPECT_STREQ(s9, "s7");
76    char myCString[20] = "myCString";
77    s.setToExternal(&myCString[0], strlen(myCString));
78    EXPECT_STREQ(s, "myCString");
79    myCString[2] = 'D';
80    EXPECT_STREQ(s, "myDString");
81    s.clear(); // should not affect myCString
82    EXPECT_STREQ(myCString, "myDString");
83
84    // casts
85    s = "great";
86    std::string myString = s;
87    const char *anotherCString = s;
88    EXPECT_EQ(myString, "great");
89    EXPECT_STREQ(anotherCString, "great");
90
91    // Comparisons
92    const char * cstr1 = "abc";
93    std::string string1(cstr1);
94    hidl_string hs1(cstr1);
95    const char * cstrE = "abc";
96    std::string stringE(cstrE);
97    hidl_string hsE(cstrE);
98    const char * cstrNE = "ABC";
99    std::string stringNE(cstrNE);
100    hidl_string hsNE(cstrNE);
101    EXPECT_TRUE(hs1  == hsE);
102    EXPECT_FALSE(hs1 != hsE);
103    EXPECT_TRUE(hs1  != hsNE);
104    EXPECT_FALSE(hs1 == hsNE);
105    EXPECT_TRUE(hs1  == cstrE);
106    EXPECT_FALSE(hs1 != cstrE);
107    EXPECT_TRUE(hs1  != cstrNE);
108    EXPECT_FALSE(hs1 == cstrNE);
109    EXPECT_TRUE(hs1  == stringE);
110    EXPECT_FALSE(hs1 != stringE);
111    EXPECT_TRUE(hs1  != stringNE);
112    EXPECT_FALSE(hs1 == stringNE);
113}
114
115TEST_F(LibHidlTest, MemoryTest) {
116    using android::hardware::hidl_memory;
117
118    hidl_memory mem1 = hidl_memory(); // default constructor
119    hidl_memory mem2 = mem1; // copy constructor (nullptr)
120
121    EXPECT_EQ(nullptr, mem2.handle());
122
123    native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
124
125    hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
126    hidl_memory mem4 = mem3; // copy constructor (regular handle)
127
128    EXPECT_EQ(mem3.name(), mem4.name());
129    EXPECT_EQ(mem3.size(), mem4.size());
130    EXPECT_NE(nullptr, mem4.handle());
131    EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
132
133    hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
134    hidl_memory mem6 = mem5;
135    EXPECT_EQ(nullptr, mem5.handle());
136    EXPECT_EQ(nullptr, mem6.handle());
137}
138
139TEST_F(LibHidlTest, VecInitTest) {
140    using android::hardware::hidl_vec;
141    using std::vector;
142    int32_t array[] = {5, 6, 7};
143    vector<int32_t> v(array, array + 3);
144
145    hidl_vec<int32_t> hv1 = v; // copy =
146    EXPECT_ARRAYEQ(hv1, array, 3);
147    EXPECT_ARRAYEQ(hv1, v, 3);
148    hidl_vec<int32_t> hv2(v); // copy constructor
149    EXPECT_ARRAYEQ(hv2, v, 3);
150
151    vector<int32_t> v2 = hv1; // cast
152    EXPECT_ARRAYEQ(v2, v, 3);
153
154    hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
155    EXPECT_EQ(v3.size(), 3ul);
156    EXPECT_ARRAYEQ(v3, array, v3.size());
157}
158
159TEST_F(LibHidlTest, VecIterTest) {
160    int32_t array[] = {5, 6, 7};
161    android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
162
163    auto iter = hv1.begin();    // iterator begin()
164    EXPECT_EQ(*iter++, 5);
165    EXPECT_EQ(*iter, 6);
166    EXPECT_EQ(*++iter, 7);
167    EXPECT_EQ(*iter--, 7);
168    EXPECT_EQ(*iter, 6);
169    EXPECT_EQ(*--iter, 5);
170
171    iter += 2;
172    EXPECT_EQ(*iter, 7);
173    iter -= 2;
174    EXPECT_EQ(*iter, 5);
175
176    iter++;
177    EXPECT_EQ(*(iter + 1), 7);
178    EXPECT_EQ(*(1 + iter), 7);
179    EXPECT_EQ(*(iter - 1), 5);
180    EXPECT_EQ(*iter, 6);
181
182    auto five = iter - 1;
183    auto seven = iter + 1;
184    EXPECT_EQ(seven - five, 2);
185    EXPECT_EQ(five - seven, -2);
186
187    EXPECT_LT(five, seven);
188    EXPECT_LE(five, seven);
189    EXPECT_GT(seven, five);
190    EXPECT_GE(seven, five);
191
192    EXPECT_EQ(seven[0], 7);
193    EXPECT_EQ(five[1], 6);
194}
195
196TEST_F(LibHidlTest, VecIterForTest) {
197    using android::hardware::hidl_vec;
198    int32_t array[] = {5, 6, 7};
199    hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
200
201    int32_t sum = 0;            // range based for loop interoperability
202    for (auto &&i: hv1) {
203        sum += i;
204    }
205    EXPECT_EQ(sum, 5+6+7);
206
207    for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) {
208        *iter += 10;
209    }
210    const hidl_vec<int32_t> &v4 = hv1;
211    sum = 0;
212    for (const auto &i : v4) {
213        sum += i;
214    }
215    EXPECT_EQ(sum, 15+16+17);
216}
217
218TEST_F(LibHidlTest, VecEqTest) {
219    android::hardware::hidl_vec<int32_t> hv1{5, 6, 7};
220    android::hardware::hidl_vec<int32_t> hv2{5, 6, 7};
221    android::hardware::hidl_vec<int32_t> hv3{5, 6, 8};
222
223    // use the == and != operator intentionally here
224    EXPECT_TRUE(hv1 == hv2);
225    EXPECT_TRUE(hv1 != hv3);
226}
227
228TEST_F(LibHidlTest, ArrayTest) {
229    using android::hardware::hidl_array;
230    int32_t array[] = {5, 6, 7};
231
232    hidl_array<int32_t, 3> ha(array);
233    EXPECT_ARRAYEQ(ha, array, 3);
234}
235
236TEST_F(LibHidlTest, TaskRunnerTest) {
237    using android::hardware::TaskRunner;
238    TaskRunner tr;
239    bool flag = false;
240    tr.push([&] {
241        usleep(1000);
242        flag = true;
243    });
244    usleep(500);
245    EXPECT_FALSE(flag);
246    usleep(1000);
247    EXPECT_TRUE(flag);
248}
249
250TEST_F(LibHidlTest, StringCmpTest) {
251    using android::hardware::hidl_string;
252    const char * s = "good";
253    hidl_string hs(s);
254    EXPECT_NE(hs.c_str(), s);
255
256    EXPECT_TRUE(hs == s); // operator ==
257    EXPECT_TRUE(s == hs);
258
259    EXPECT_FALSE(hs != s); // operator ==
260    EXPECT_FALSE(s != hs);
261}
262
263template <typename T>
264void great(android::hardware::hidl_vec<T>) {}
265
266TEST_F(LibHidlTest, VecCopyTest) {
267    android::hardware::hidl_vec<int32_t> v;
268    great(v);
269}
270
271TEST_F(LibHidlTest, StdArrayTest) {
272    using android::hardware::hidl_array;
273    hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
274    std::array<int32_t, 5> stdArray = array;
275    EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
276    hidl_array<int32_t, 5> array2 = stdArray;
277    EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
278}
279
280TEST_F(LibHidlTest, MultiDimStdArrayTest) {
281    using android::hardware::hidl_array;
282    hidl_array<int32_t, 2, 3> array;
283    for (size_t i = 0; i < 2; i++) {
284        for (size_t j = 0; j < 3; j++) {
285            array[i][j] = i + j + i * j;
286        }
287    }
288    std::array<std::array<int32_t, 3>, 2> stdArray = array;
289    EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
290    hidl_array<int32_t, 2, 3> array2 = stdArray;
291    EXPECT_2DARRAYEQ(array, array2, 2, 3);
292}
293
294TEST_F(LibHidlTest, HidlVersionTest) {
295    using android::hardware::hidl_version;
296    hidl_version v1_0{1, 0};
297    EXPECT_EQ(1, v1_0.get_major());
298    EXPECT_EQ(0, v1_0.get_minor());
299    hidl_version v2_0{2, 0};
300    hidl_version v2_1{2, 1};
301    hidl_version v2_2{2, 2};
302    hidl_version v3_0{3, 0};
303    hidl_version v3_0b{3,0};
304
305    EXPECT_TRUE(v1_0 < v2_0);
306    EXPECT_TRUE(v2_0 < v2_1);
307    EXPECT_TRUE(v2_1 < v3_0);
308    EXPECT_TRUE(v2_0 > v1_0);
309    EXPECT_TRUE(v2_1 > v2_0);
310    EXPECT_TRUE(v3_0 > v2_1);
311    EXPECT_TRUE(v3_0 == v3_0b);
312    EXPECT_TRUE(v3_0 <= v3_0b);
313    EXPECT_TRUE(v2_2 <= v3_0);
314    EXPECT_TRUE(v3_0 >= v3_0b);
315    EXPECT_TRUE(v3_0 >= v2_2);
316}
317
318
319int main(int argc, char **argv) {
320    ::testing::InitGoogleTest(&argc, argv);
321    return RUN_ALL_TESTS();
322}
323