blob: 3ee8bc9a8da9c62369faa288f64f6fcd0e28ba1e [file] [log] [blame]
Yifan Hongb93f0502016-10-28 10:42:57 -07001/*
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
Yifan Hong602b85a2016-10-24 13:40:01 -070017#define LOG_TAG "LibHidlTest"
18
19#include <android-base/logging.h>
20#include <gtest/gtest.h>
21#include <hidl/HidlSupport.h>
Yifan Hong5e2318c2016-10-27 17:19:21 -070022#include <hidl/TaskRunner.h>
Yifan Hong602b85a2016-10-24 13:40:01 -070023#include <vector>
24
25#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
Yifan Hong44ab6232016-11-22 16:28:24 -080026#define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \
27 EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__))
Yifan Hong602b85a2016-10-24 13:40:01 -070028
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
Yifan Hong44ab6232016-11-22 16:28:24 -080037template<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
Yifan Hong602b85a2016-10-24 13:40:01 -070046class 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(hidl_string("s4"))); // move constructor
65 EXPECT_STREQ(s4.c_str(), "s4");
66 hidl_string s5(std::string("s5")); // copy constructor from std::string
67 EXPECT_STREQ(s5, "s5");
68 hidl_string s6 = std::string("s6"); // copy = from std::string
69 EXPECT_STREQ(s6, "s6");
70 hidl_string s7(s6); // copy constructor
71 EXPECT_STREQ(s7, "s6");
72 hidl_string s8 = s7; // copy =
73 EXPECT_STREQ(s8, "s6");
74 char myCString[20] = "myCString";
75 s.setToExternal(&myCString[0], strlen(myCString));
76 EXPECT_STREQ(s, "myCString");
77 myCString[2] = 'D';
78 EXPECT_STREQ(s, "myDString");
79 s.clear(); // should not affect myCString
80 EXPECT_STREQ(myCString, "myDString");
Scott Randolphbb840f72016-11-21 14:39:26 -080081
Yifan Hong602b85a2016-10-24 13:40:01 -070082 // casts
83 s = "great";
84 std::string myString = s;
85 const char *anotherCString = s;
86 EXPECT_EQ(myString, "great");
87 EXPECT_STREQ(anotherCString, "great");
Scott Randolphbb840f72016-11-21 14:39:26 -080088
89 // Comparisons
90 const char * cstr1 = "abc";
91 std::string string1(cstr1);
92 hidl_string hs1(cstr1);
93 const char * cstrE = "abc";
94 std::string stringE(cstrE);
95 hidl_string hsE(cstrE);
96 const char * cstrNE = "ABC";
97 std::string stringNE(cstrNE);
98 hidl_string hsNE(cstrNE);
99 EXPECT_TRUE(hs1 == hsE);
100 EXPECT_FALSE(hs1 != hsE);
101 EXPECT_TRUE(hs1 != hsNE);
102 EXPECT_FALSE(hs1 == hsNE);
103 EXPECT_TRUE(hs1 == cstrE);
104 EXPECT_FALSE(hs1 != cstrE);
105 EXPECT_TRUE(hs1 != cstrNE);
106 EXPECT_FALSE(hs1 == cstrNE);
107 EXPECT_TRUE(hs1 == stringE);
108 EXPECT_FALSE(hs1 != stringE);
109 EXPECT_TRUE(hs1 != stringNE);
110 EXPECT_FALSE(hs1 == stringNE);
Yifan Hong602b85a2016-10-24 13:40:01 -0700111}
112
113TEST_F(LibHidlTest, VecTest) {
114 using android::hardware::hidl_vec;
115 using std::vector;
116 int32_t array[] = {5, 6, 7};
117 vector<int32_t> v(array, array + 3);
118
119 hidl_vec<int32_t> hv1 = v; // copy =
120 EXPECT_ARRAYEQ(hv1, array, 3);
121 EXPECT_ARRAYEQ(hv1, v, 3);
122 hidl_vec<int32_t> hv2(v); // copy constructor
123 EXPECT_ARRAYEQ(hv2, v, 3);
124
125 vector<int32_t> v2 = hv1; // cast
126 EXPECT_ARRAYEQ(v2, v, 3);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800127
128 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -0800129 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -0800130 EXPECT_ARRAYEQ(v3, array, v3.size());
Scott Randolphbb840f72016-11-21 14:39:26 -0800131
132 auto iter = hv1.begin(); // iterator begin()
133 EXPECT_EQ(*iter, 5);
134 iter++; // post increment
135 EXPECT_EQ(*iter, 6);
136 ++iter; // pre increment
137 EXPECT_EQ(*iter, 7);
138
139 int32_t sum = 0; // range based for loop interoperability
140 for (auto &&i: hv1) {
141 sum += i;
142 }
143 EXPECT_EQ(sum, 5+6+7);
Yifan Hong602b85a2016-10-24 13:40:01 -0700144}
145
Sasha Levitskiy3da68482016-11-17 16:48:43 -0800146TEST_F(LibHidlTest, ArrayTest) {
147 using android::hardware::hidl_array;
148 int32_t array[] = {5, 6, 7};
149
150 hidl_array<int32_t, 3> ha(array);
151 EXPECT_ARRAYEQ(ha, array, 3);
152}
153
Yifan Hong5e2318c2016-10-27 17:19:21 -0700154TEST_F(LibHidlTest, TaskRunnerTest) {
155 using android::hardware::TaskRunner;
156 TaskRunner tr;
157 bool flag = false;
158 tr.push([&] {
159 usleep(1000);
160 flag = true;
161 });
162 usleep(500);
163 EXPECT_FALSE(flag);
164 usleep(1000);
165 EXPECT_TRUE(flag);
166}
167
Yifan Hong5708fb42016-10-26 17:50:29 -0700168TEST_F(LibHidlTest, StringCmpTest) {
169 using android::hardware::hidl_string;
170 const char * s = "good";
171 hidl_string hs(s);
172 EXPECT_NE(hs.c_str(), s);
173
174 EXPECT_TRUE(hs == s); // operator ==
175 EXPECT_TRUE(s == hs);
176
177 EXPECT_FALSE(hs != s); // operator ==
178 EXPECT_FALSE(s != hs);
179}
180
Yifan Hong602b85a2016-10-24 13:40:01 -0700181template <typename T>
182void great(android::hardware::hidl_vec<T>) {}
183
184TEST_F(LibHidlTest, VecCopyTest) {
185 android::hardware::hidl_vec<int32_t> v;
186 great(v);
187}
188
Yifan Hong44ab6232016-11-22 16:28:24 -0800189TEST_F(LibHidlTest, StdArrayTest) {
190 using android::hardware::hidl_array;
191 hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}};
192 std::array<int32_t, 5> stdArray = array;
193 EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5);
194 hidl_array<int32_t, 5> array2 = stdArray;
195 EXPECT_ARRAYEQ(array.data(), array2.data(), 5);
196}
197
198TEST_F(LibHidlTest, MultiDimStdArrayTest) {
199 using android::hardware::hidl_array;
200 hidl_array<int32_t, 2, 3> array;
201 for (size_t i = 0; i < 2; i++) {
202 for (size_t j = 0; j < 3; j++) {
203 array[i][j] = i + j + i * j;
204 }
205 }
206 std::array<std::array<int32_t, 3>, 2> stdArray = array;
207 EXPECT_2DARRAYEQ(array, stdArray, 2, 3);
208 hidl_array<int32_t, 2, 3> array2 = stdArray;
209 EXPECT_2DARRAYEQ(array, array2, 2, 3);
210}
211
Yifan Hong602b85a2016-10-24 13:40:01 -0700212int main(int argc, char **argv) {
213 ::testing::InitGoogleTest(&argc, argv);
214 return RUN_ALL_TESTS();
215}