Yifan Hong | b93f050 | 2016-10-28 10:42:57 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 17 | #define LOG_TAG "LibHidlTest" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <hidl/HidlSupport.h> |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 22 | #include <hidl/TaskRunner.h> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__)) |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 26 | #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \ |
| 27 | EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__)) |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 28 | |
| 29 | template<typename T, typename S> |
| 30 | static 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 Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 37 | template<typename T, typename S> |
| 38 | static 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 Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 46 | class LibHidlTest : public ::testing::Test { |
| 47 | public: |
| 48 | virtual void SetUp() override { |
| 49 | } |
| 50 | virtual void TearDown() override { |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | TEST_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 Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 81 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 82 | // 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 Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 88 | |
| 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 Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 113 | TEST_F(LibHidlTest, VecInitTest) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 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 Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 127 | |
| 128 | hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 129 | EXPECT_EQ(v3.size(), 3ul); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 130 | EXPECT_ARRAYEQ(v3, array, v3.size()); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(LibHidlTest, VecIterTest) { |
| 134 | int32_t array[] = {5, 6, 7}; |
| 135 | android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 136 | |
| 137 | auto iter = hv1.begin(); // iterator begin() |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 138 | EXPECT_EQ(*iter++, 5); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 139 | EXPECT_EQ(*iter, 6); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 140 | EXPECT_EQ(*++iter, 7); |
| 141 | EXPECT_EQ(*iter--, 7); |
| 142 | EXPECT_EQ(*iter, 6); |
| 143 | EXPECT_EQ(*--iter, 5); |
| 144 | |
| 145 | iter += 2; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 146 | EXPECT_EQ(*iter, 7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 147 | iter -= 2; |
| 148 | EXPECT_EQ(*iter, 5); |
| 149 | |
| 150 | iter++; |
| 151 | EXPECT_EQ(*(iter + 1), 7); |
| 152 | EXPECT_EQ(*(1 + iter), 7); |
| 153 | EXPECT_EQ(*(iter - 1), 5); |
| 154 | EXPECT_EQ(*iter, 6); |
| 155 | |
| 156 | auto five = iter - 1; |
| 157 | auto seven = iter + 1; |
| 158 | EXPECT_EQ(seven - five, 2); |
| 159 | EXPECT_EQ(five - seven, -2); |
| 160 | |
| 161 | EXPECT_LT(five, seven); |
| 162 | EXPECT_LE(five, seven); |
| 163 | EXPECT_GT(seven, five); |
| 164 | EXPECT_GE(seven, five); |
| 165 | |
| 166 | EXPECT_EQ(seven[0], 7); |
| 167 | EXPECT_EQ(five[1], 6); |
| 168 | } |
| 169 | |
| 170 | TEST_F(LibHidlTest, VecIterForTest) { |
| 171 | using android::hardware::hidl_vec; |
| 172 | int32_t array[] = {5, 6, 7}; |
| 173 | hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 174 | |
| 175 | int32_t sum = 0; // range based for loop interoperability |
| 176 | for (auto &&i: hv1) { |
| 177 | sum += i; |
| 178 | } |
| 179 | EXPECT_EQ(sum, 5+6+7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 180 | |
| 181 | for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) { |
| 182 | *iter += 10; |
| 183 | } |
| 184 | const hidl_vec<int32_t> &v4 = hv1; |
| 185 | sum = 0; |
| 186 | for (const auto &i : v4) { |
| 187 | sum += i; |
| 188 | } |
| 189 | EXPECT_EQ(sum, 15+16+17); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 192 | TEST_F(LibHidlTest, ArrayTest) { |
| 193 | using android::hardware::hidl_array; |
| 194 | int32_t array[] = {5, 6, 7}; |
| 195 | |
| 196 | hidl_array<int32_t, 3> ha(array); |
| 197 | EXPECT_ARRAYEQ(ha, array, 3); |
| 198 | } |
| 199 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 200 | TEST_F(LibHidlTest, TaskRunnerTest) { |
| 201 | using android::hardware::TaskRunner; |
| 202 | TaskRunner tr; |
| 203 | bool flag = false; |
| 204 | tr.push([&] { |
| 205 | usleep(1000); |
| 206 | flag = true; |
| 207 | }); |
| 208 | usleep(500); |
| 209 | EXPECT_FALSE(flag); |
| 210 | usleep(1000); |
| 211 | EXPECT_TRUE(flag); |
| 212 | } |
| 213 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 214 | TEST_F(LibHidlTest, StringCmpTest) { |
| 215 | using android::hardware::hidl_string; |
| 216 | const char * s = "good"; |
| 217 | hidl_string hs(s); |
| 218 | EXPECT_NE(hs.c_str(), s); |
| 219 | |
| 220 | EXPECT_TRUE(hs == s); // operator == |
| 221 | EXPECT_TRUE(s == hs); |
| 222 | |
| 223 | EXPECT_FALSE(hs != s); // operator == |
| 224 | EXPECT_FALSE(s != hs); |
| 225 | } |
| 226 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 227 | template <typename T> |
| 228 | void great(android::hardware::hidl_vec<T>) {} |
| 229 | |
| 230 | TEST_F(LibHidlTest, VecCopyTest) { |
| 231 | android::hardware::hidl_vec<int32_t> v; |
| 232 | great(v); |
| 233 | } |
| 234 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 235 | TEST_F(LibHidlTest, StdArrayTest) { |
| 236 | using android::hardware::hidl_array; |
| 237 | hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}}; |
| 238 | std::array<int32_t, 5> stdArray = array; |
| 239 | EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5); |
| 240 | hidl_array<int32_t, 5> array2 = stdArray; |
| 241 | EXPECT_ARRAYEQ(array.data(), array2.data(), 5); |
| 242 | } |
| 243 | |
| 244 | TEST_F(LibHidlTest, MultiDimStdArrayTest) { |
| 245 | using android::hardware::hidl_array; |
| 246 | hidl_array<int32_t, 2, 3> array; |
| 247 | for (size_t i = 0; i < 2; i++) { |
| 248 | for (size_t j = 0; j < 3; j++) { |
| 249 | array[i][j] = i + j + i * j; |
| 250 | } |
| 251 | } |
| 252 | std::array<std::array<int32_t, 3>, 2> stdArray = array; |
| 253 | EXPECT_2DARRAYEQ(array, stdArray, 2, 3); |
| 254 | hidl_array<int32_t, 2, 3> array2 = stdArray; |
| 255 | EXPECT_2DARRAYEQ(array, array2, 2, 3); |
| 256 | } |
| 257 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 258 | TEST_F(LibHidlTest, HidlVersionTest) { |
| 259 | using android::hardware::hidl_version; |
| 260 | hidl_version v1_0{1, 0}; |
| 261 | EXPECT_EQ(1, v1_0.get_major()); |
| 262 | EXPECT_EQ(0, v1_0.get_minor()); |
| 263 | hidl_version v2_0{2, 0}; |
| 264 | hidl_version v2_1{2, 1}; |
| 265 | hidl_version v2_2{2, 2}; |
| 266 | hidl_version v3_0{3, 0}; |
| 267 | hidl_version v3_0b{3,0}; |
| 268 | |
| 269 | EXPECT_TRUE(v1_0 < v2_0); |
| 270 | EXPECT_TRUE(v2_0 < v2_1); |
| 271 | EXPECT_TRUE(v2_1 < v3_0); |
| 272 | EXPECT_TRUE(v2_0 > v1_0); |
| 273 | EXPECT_TRUE(v2_1 > v2_0); |
| 274 | EXPECT_TRUE(v3_0 > v2_1); |
| 275 | EXPECT_TRUE(v3_0 == v3_0b); |
| 276 | EXPECT_TRUE(v3_0 <= v3_0b); |
| 277 | EXPECT_TRUE(v2_2 <= v3_0); |
| 278 | EXPECT_TRUE(v3_0 >= v3_0b); |
| 279 | EXPECT_TRUE(v3_0 >= v2_2); |
| 280 | } |
| 281 | |
| 282 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 283 | int main(int argc, char **argv) { |
| 284 | ::testing::InitGoogleTest(&argc, argv); |
| 285 | return RUN_ALL_TESTS(); |
| 286 | } |