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> |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 20 | #include <condition_variable> |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 21 | #include <gmock/gmock.h> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | #include <hidl/HidlSupport.h> |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 24 | #include <hidl/Status.h> |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 25 | #include <hidl/TaskRunner.h> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | #define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__)) |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 29 | #define EXPECT_2DARRAYEQ(__a1__, __a2__, __size1__, __size2__) \ |
| 30 | EXPECT_TRUE(is2dArrayEqual(__a1__, __a2__, __size1__, __size2__)) |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 31 | |
| 32 | template<typename T, typename S> |
| 33 | static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) { |
| 34 | for(size_t i = 0; i < size; i++) |
| 35 | if(arr1[i] != arr2[i]) |
| 36 | return false; |
| 37 | return true; |
| 38 | } |
| 39 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 40 | template<typename T, typename S> |
| 41 | static inline bool is2dArrayEqual(const T arr1, const S arr2, size_t size1, size_t size2) { |
| 42 | for(size_t i = 0; i < size1; i++) |
| 43 | for (size_t j = 0; j < size2; j++) |
| 44 | if(arr1[i][j] != arr2[i][j]) |
| 45 | return false; |
| 46 | return true; |
| 47 | } |
| 48 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 49 | class LibHidlTest : public ::testing::Test { |
| 50 | public: |
| 51 | virtual void SetUp() override { |
| 52 | } |
| 53 | virtual void TearDown() override { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | TEST_F(LibHidlTest, StringTest) { |
| 58 | using android::hardware::hidl_string; |
| 59 | hidl_string s; // empty constructor |
| 60 | EXPECT_STREQ(s.c_str(), ""); |
| 61 | hidl_string s1 = "s1"; // copy = from cstr |
| 62 | EXPECT_STREQ(s1.c_str(), "s1"); |
| 63 | hidl_string s2("s2"); // copy constructor from cstr |
| 64 | EXPECT_STREQ(s2.c_str(), "s2"); |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 65 | hidl_string s2a(nullptr); // copy constructor from null cstr |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 66 | EXPECT_STREQ("", s2a.c_str()); |
Steven Moreland | 153f87a | 2017-02-28 09:42:26 -0800 | [diff] [blame] | 67 | s2a = nullptr; // = from nullptr cstr |
| 68 | EXPECT_STREQ(s2a.c_str(), ""); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 69 | hidl_string s3 = hidl_string("s3"); // move = |
| 70 | EXPECT_STREQ(s3.c_str(), "s3"); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 71 | hidl_string s4 = hidl_string("12345", 3); // copy constructor from cstr w/ length |
| 72 | EXPECT_STREQ(s4.c_str(), "123"); |
| 73 | hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor |
| 74 | EXPECT_STREQ(s5.c_str(), "s5"); |
| 75 | hidl_string s6(std::string("s6")); // copy constructor from std::string |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 76 | EXPECT_STREQ(s6.c_str(), "s6"); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 77 | hidl_string s7 = std::string("s7"); // copy = from std::string |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 78 | EXPECT_STREQ(s7.c_str(), "s7"); |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 79 | hidl_string s8(s7); // copy constructor // NOLINT, test the copy constructor |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 80 | EXPECT_STREQ(s8.c_str(), "s7"); |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 81 | hidl_string s9 = s8; // copy = // NOLINT, test the copy operator |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 82 | EXPECT_STREQ(s9.c_str(), "s7"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 83 | char myCString[20] = "myCString"; |
| 84 | s.setToExternal(&myCString[0], strlen(myCString)); |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 85 | EXPECT_STREQ(s.c_str(), "myCString"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 86 | myCString[2] = 'D'; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 87 | EXPECT_STREQ(s.c_str(), "myDString"); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 88 | s.clear(); // should not affect myCString |
| 89 | EXPECT_STREQ(myCString, "myDString"); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 90 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 91 | // casts |
| 92 | s = "great"; |
| 93 | std::string myString = s; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 94 | const char *anotherCString = s.c_str(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 95 | EXPECT_EQ(myString, "great"); |
| 96 | EXPECT_STREQ(anotherCString, "great"); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 97 | |
Steven Moreland | 727e3dd | 2017-03-23 11:19:00 -0700 | [diff] [blame] | 98 | const hidl_string t = "not so great"; |
| 99 | std::string myTString = t; |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 100 | const char * anotherTCString = t.c_str(); |
Steven Moreland | 727e3dd | 2017-03-23 11:19:00 -0700 | [diff] [blame] | 101 | EXPECT_EQ(myTString, "not so great"); |
| 102 | EXPECT_STREQ(anotherTCString, "not so great"); |
| 103 | |
Scott Randolph | eb0c337 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 104 | // Assignment from hidl_string to std::string |
| 105 | std::string tgt; |
| 106 | hidl_string src("some stuff"); |
| 107 | tgt = src; |
| 108 | EXPECT_STREQ(tgt.c_str(), "some stuff"); |
| 109 | |
| 110 | // Stream output operator |
| 111 | hidl_string msg("hidl_string works with operator<<"); |
| 112 | std::cout << msg; |
| 113 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 114 | // Comparisons |
| 115 | const char * cstr1 = "abc"; |
| 116 | std::string string1(cstr1); |
| 117 | hidl_string hs1(cstr1); |
| 118 | const char * cstrE = "abc"; |
| 119 | std::string stringE(cstrE); |
| 120 | hidl_string hsE(cstrE); |
| 121 | const char * cstrNE = "ABC"; |
| 122 | std::string stringNE(cstrNE); |
| 123 | hidl_string hsNE(cstrNE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 124 | const char * cstr2 = "def"; |
| 125 | std::string string2(cstr2); |
| 126 | hidl_string hs2(cstr2); |
| 127 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 128 | EXPECT_TRUE(hs1 == hsE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 129 | EXPECT_FALSE(hs1 == hsNE); |
| 130 | EXPECT_TRUE(hs1 == cstrE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 131 | EXPECT_FALSE(hs1 == cstrNE); |
| 132 | EXPECT_TRUE(hs1 == stringE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 133 | EXPECT_FALSE(hs1 == stringNE); |
| 134 | EXPECT_FALSE(hs1 != hsE); |
| 135 | EXPECT_TRUE(hs1 != hsNE); |
| 136 | EXPECT_FALSE(hs1 != cstrE); |
| 137 | EXPECT_TRUE(hs1 != cstrNE); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 138 | EXPECT_FALSE(hs1 != stringE); |
| 139 | EXPECT_TRUE(hs1 != stringNE); |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 140 | |
| 141 | EXPECT_TRUE(hs1 < hs2); |
| 142 | EXPECT_FALSE(hs2 < hs1); |
| 143 | EXPECT_TRUE(hs2 > hs1); |
| 144 | EXPECT_FALSE(hs1 > hs2); |
| 145 | EXPECT_TRUE(hs1 <= hs1); |
| 146 | EXPECT_TRUE(hs1 <= hs2); |
| 147 | EXPECT_FALSE(hs2 <= hs1); |
| 148 | EXPECT_TRUE(hs1 >= hs1); |
| 149 | EXPECT_TRUE(hs2 >= hs1); |
| 150 | EXPECT_FALSE(hs2 <= hs1); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 153 | TEST_F(LibHidlTest, MemoryTest) { |
| 154 | using android::hardware::hidl_memory; |
| 155 | |
| 156 | hidl_memory mem1 = hidl_memory(); // default constructor |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 157 | hidl_memory mem2 = mem1; // copy constructor (nullptr), NOLINT |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 158 | |
| 159 | EXPECT_EQ(nullptr, mem2.handle()); |
| 160 | |
| 161 | native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */); |
| 162 | |
| 163 | hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 164 | hidl_memory mem4 = mem3; // copy constructor (regular handle), NOLINT |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 165 | |
| 166 | EXPECT_EQ(mem3.name(), mem4.name()); |
| 167 | EXPECT_EQ(mem3.size(), mem4.size()); |
| 168 | EXPECT_NE(nullptr, mem4.handle()); |
| 169 | EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned |
| 170 | |
| 171 | hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 172 | hidl_memory mem6 = mem5; // NOLINT, test copying |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 173 | EXPECT_EQ(nullptr, mem5.handle()); |
| 174 | EXPECT_EQ(nullptr, mem6.handle()); |
| 175 | } |
| 176 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 177 | TEST_F(LibHidlTest, VecInitTest) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 178 | using android::hardware::hidl_vec; |
| 179 | using std::vector; |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 180 | int32_t empty[3] = {0}; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 181 | int32_t array[] = {5, 6, 7}; |
| 182 | vector<int32_t> v(array, array + 3); |
| 183 | |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 184 | hidl_vec<int32_t> hv0(3); // size |
| 185 | EXPECT_EQ(hv0.size(), 3ul); |
| 186 | EXPECT_ARRAYEQ(hv0, empty, 3); |
| 187 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 188 | hidl_vec<int32_t> hv1 = v; // copy = |
| 189 | EXPECT_ARRAYEQ(hv1, array, 3); |
| 190 | EXPECT_ARRAYEQ(hv1, v, 3); |
| 191 | hidl_vec<int32_t> hv2(v); // copy constructor |
| 192 | EXPECT_ARRAYEQ(hv2, v, 3); |
| 193 | |
| 194 | vector<int32_t> v2 = hv1; // cast |
| 195 | EXPECT_ARRAYEQ(v2, v, 3); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 196 | |
| 197 | hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 198 | EXPECT_EQ(v3.size(), 3ul); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 199 | EXPECT_ARRAYEQ(v3, array, v3.size()); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_F(LibHidlTest, VecIterTest) { |
| 203 | int32_t array[] = {5, 6, 7}; |
| 204 | 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] | 205 | |
| 206 | auto iter = hv1.begin(); // iterator begin() |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 207 | EXPECT_EQ(*iter++, 5); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 208 | EXPECT_EQ(*iter, 6); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 209 | EXPECT_EQ(*++iter, 7); |
| 210 | EXPECT_EQ(*iter--, 7); |
| 211 | EXPECT_EQ(*iter, 6); |
| 212 | EXPECT_EQ(*--iter, 5); |
| 213 | |
| 214 | iter += 2; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 215 | EXPECT_EQ(*iter, 7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 216 | iter -= 2; |
| 217 | EXPECT_EQ(*iter, 5); |
| 218 | |
| 219 | iter++; |
| 220 | EXPECT_EQ(*(iter + 1), 7); |
| 221 | EXPECT_EQ(*(1 + iter), 7); |
| 222 | EXPECT_EQ(*(iter - 1), 5); |
| 223 | EXPECT_EQ(*iter, 6); |
| 224 | |
| 225 | auto five = iter - 1; |
| 226 | auto seven = iter + 1; |
| 227 | EXPECT_EQ(seven - five, 2); |
| 228 | EXPECT_EQ(five - seven, -2); |
| 229 | |
| 230 | EXPECT_LT(five, seven); |
| 231 | EXPECT_LE(five, seven); |
| 232 | EXPECT_GT(seven, five); |
| 233 | EXPECT_GE(seven, five); |
| 234 | |
| 235 | EXPECT_EQ(seven[0], 7); |
| 236 | EXPECT_EQ(five[1], 6); |
| 237 | } |
| 238 | |
| 239 | TEST_F(LibHidlTest, VecIterForTest) { |
| 240 | using android::hardware::hidl_vec; |
| 241 | int32_t array[] = {5, 6, 7}; |
| 242 | hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3); |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 243 | |
| 244 | int32_t sum = 0; // range based for loop interoperability |
| 245 | for (auto &&i: hv1) { |
| 246 | sum += i; |
| 247 | } |
| 248 | EXPECT_EQ(sum, 5+6+7); |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 249 | |
| 250 | for (auto iter = hv1.begin(); iter < hv1.end(); ++iter) { |
| 251 | *iter += 10; |
| 252 | } |
| 253 | const hidl_vec<int32_t> &v4 = hv1; |
| 254 | sum = 0; |
| 255 | for (const auto &i : v4) { |
| 256 | sum += i; |
| 257 | } |
| 258 | EXPECT_EQ(sum, 15+16+17); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 261 | TEST_F(LibHidlTest, VecEqTest) { |
| 262 | android::hardware::hidl_vec<int32_t> hv1{5, 6, 7}; |
| 263 | android::hardware::hidl_vec<int32_t> hv2{5, 6, 7}; |
| 264 | android::hardware::hidl_vec<int32_t> hv3{5, 6, 8}; |
| 265 | |
| 266 | // use the == and != operator intentionally here |
| 267 | EXPECT_TRUE(hv1 == hv2); |
| 268 | EXPECT_TRUE(hv1 != hv3); |
| 269 | } |
| 270 | |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 271 | TEST_F(LibHidlTest, VecRangeCtorTest) { |
| 272 | struct ConvertibleType { |
| 273 | int val; |
| 274 | |
| 275 | explicit ConvertibleType(int val) : val(val) {} |
| 276 | explicit operator int() const { return val; } |
| 277 | bool operator==(const int& other) const { return val == other; } |
| 278 | }; |
| 279 | |
| 280 | std::vector<ConvertibleType> input{ |
| 281 | ConvertibleType(1), ConvertibleType(2), ConvertibleType(3), |
| 282 | }; |
| 283 | |
| 284 | android::hardware::hidl_vec<int> hv(input.begin(), input.end()); |
| 285 | |
| 286 | EXPECT_EQ(input.size(), hv.size()); |
| 287 | int sum = 0; |
| 288 | for (unsigned i = 0; i < input.size(); i++) { |
| 289 | EXPECT_EQ(input[i], hv[i]); |
| 290 | sum += hv[i]; |
| 291 | } |
| 292 | EXPECT_EQ(sum, 1 + 2 + 3); |
| 293 | } |
| 294 | |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 295 | TEST_F(LibHidlTest, ArrayTest) { |
| 296 | using android::hardware::hidl_array; |
| 297 | int32_t array[] = {5, 6, 7}; |
| 298 | |
| 299 | hidl_array<int32_t, 3> ha(array); |
| 300 | EXPECT_ARRAYEQ(ha, array, 3); |
| 301 | } |
| 302 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 303 | TEST_F(LibHidlTest, TaskRunnerTest) { |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 304 | using android::hardware::details::TaskRunner; |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 305 | using namespace std::chrono_literals; |
| 306 | |
| 307 | std::condition_variable cv; |
| 308 | std::mutex m; |
| 309 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 310 | TaskRunner tr; |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 311 | tr.start(1 /* limit */); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 312 | bool flag = false; |
| 313 | tr.push([&] { |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 314 | flag = true; |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 315 | cv.notify_all(); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 316 | }); |
Steven Moreland | 86fd2e1 | 2017-08-02 13:26:59 -0700 | [diff] [blame] | 317 | |
| 318 | std::unique_lock<std::mutex> lock(m); |
| 319 | |
| 320 | // 1s so this doesn't deadlock. This isn't a performance test. |
| 321 | EXPECT_TRUE(cv.wait_for(lock, 1s, [&]{return flag;})); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 322 | EXPECT_TRUE(flag); |
| 323 | } |
| 324 | |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 325 | TEST_F(LibHidlTest, StringCmpTest) { |
| 326 | using android::hardware::hidl_string; |
| 327 | const char * s = "good"; |
| 328 | hidl_string hs(s); |
| 329 | EXPECT_NE(hs.c_str(), s); |
| 330 | |
| 331 | EXPECT_TRUE(hs == s); // operator == |
| 332 | EXPECT_TRUE(s == hs); |
| 333 | |
| 334 | EXPECT_FALSE(hs != s); // operator == |
| 335 | EXPECT_FALSE(s != hs); |
| 336 | } |
| 337 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 338 | template <typename T> |
| 339 | void great(android::hardware::hidl_vec<T>) {} |
| 340 | |
| 341 | TEST_F(LibHidlTest, VecCopyTest) { |
| 342 | android::hardware::hidl_vec<int32_t> v; |
| 343 | great(v); |
| 344 | } |
| 345 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 346 | TEST_F(LibHidlTest, StdArrayTest) { |
| 347 | using android::hardware::hidl_array; |
| 348 | hidl_array<int32_t, 5> array{(int32_t[5]){1, 2, 3, 4, 5}}; |
| 349 | std::array<int32_t, 5> stdArray = array; |
| 350 | EXPECT_ARRAYEQ(array.data(), stdArray.data(), 5); |
| 351 | hidl_array<int32_t, 5> array2 = stdArray; |
| 352 | EXPECT_ARRAYEQ(array.data(), array2.data(), 5); |
| 353 | } |
| 354 | |
| 355 | TEST_F(LibHidlTest, MultiDimStdArrayTest) { |
| 356 | using android::hardware::hidl_array; |
| 357 | hidl_array<int32_t, 2, 3> array; |
| 358 | for (size_t i = 0; i < 2; i++) { |
| 359 | for (size_t j = 0; j < 3; j++) { |
| 360 | array[i][j] = i + j + i * j; |
| 361 | } |
| 362 | } |
| 363 | std::array<std::array<int32_t, 3>, 2> stdArray = array; |
| 364 | EXPECT_2DARRAYEQ(array, stdArray, 2, 3); |
| 365 | hidl_array<int32_t, 2, 3> array2 = stdArray; |
| 366 | EXPECT_2DARRAYEQ(array, array2, 2, 3); |
| 367 | } |
| 368 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 369 | TEST_F(LibHidlTest, HidlVersionTest) { |
| 370 | using android::hardware::hidl_version; |
| 371 | hidl_version v1_0{1, 0}; |
| 372 | EXPECT_EQ(1, v1_0.get_major()); |
| 373 | EXPECT_EQ(0, v1_0.get_minor()); |
| 374 | hidl_version v2_0{2, 0}; |
| 375 | hidl_version v2_1{2, 1}; |
| 376 | hidl_version v2_2{2, 2}; |
| 377 | hidl_version v3_0{3, 0}; |
| 378 | hidl_version v3_0b{3,0}; |
| 379 | |
| 380 | EXPECT_TRUE(v1_0 < v2_0); |
| 381 | EXPECT_TRUE(v2_0 < v2_1); |
| 382 | EXPECT_TRUE(v2_1 < v3_0); |
| 383 | EXPECT_TRUE(v2_0 > v1_0); |
| 384 | EXPECT_TRUE(v2_1 > v2_0); |
| 385 | EXPECT_TRUE(v3_0 > v2_1); |
| 386 | EXPECT_TRUE(v3_0 == v3_0b); |
| 387 | EXPECT_TRUE(v3_0 <= v3_0b); |
| 388 | EXPECT_TRUE(v2_2 <= v3_0); |
| 389 | EXPECT_TRUE(v3_0 >= v3_0b); |
| 390 | EXPECT_TRUE(v3_0 >= v2_2); |
| 391 | } |
| 392 | |
Yifan Hong | af4e43c | 2017-03-03 19:10:52 -0800 | [diff] [blame] | 393 | TEST_F(LibHidlTest, ReturnMoveTest) { |
| 394 | using namespace ::android; |
| 395 | using ::android::hardware::Return; |
| 396 | using ::android::hardware::Status; |
| 397 | Return<void> ret{Status::fromStatusT(DEAD_OBJECT)}; |
| 398 | ret.isOk(); |
| 399 | ret = {Status::fromStatusT(DEAD_OBJECT)}; |
| 400 | ret.isOk(); |
| 401 | } |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 402 | |
Steven Moreland | 4917296 | 2017-04-28 16:08:43 -0700 | [diff] [blame] | 403 | TEST_F(LibHidlTest, ReturnTest) { |
| 404 | using ::android::DEAD_OBJECT; |
| 405 | using ::android::hardware::Return; |
| 406 | using ::android::hardware::Status; |
| 407 | using ::android::hardware::hidl_string; |
| 408 | |
| 409 | EXPECT_FALSE(Return<void>(Status::fromStatusT(DEAD_OBJECT)).isOk()); |
| 410 | EXPECT_TRUE(Return<void>(Status::ok()).isOk()); |
| 411 | |
| 412 | hidl_string one = "1"; |
| 413 | hidl_string two = "2"; |
| 414 | Return<hidl_string> ret = Return<hidl_string>(Status::fromStatusT(DEAD_OBJECT)); |
| 415 | |
| 416 | EXPECT_EQ(one, Return<hidl_string>(one).withDefault(two)); |
| 417 | EXPECT_EQ(two, ret.withDefault(two)); |
| 418 | |
| 419 | hidl_string&& moved = ret.withDefault(std::move(two)); |
| 420 | EXPECT_EQ("2", moved); |
| 421 | |
| 422 | const hidl_string three = "3"; |
| 423 | EXPECT_EQ(three, ret.withDefault(three)); |
| 424 | } |
| 425 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 426 | std::string toString(const ::android::hardware::Status &s) { |
| 427 | using ::android::hardware::operator<<; |
| 428 | std::ostringstream oss; |
| 429 | oss << s; |
| 430 | return oss.str(); |
| 431 | } |
| 432 | |
| 433 | TEST_F(LibHidlTest, StatusStringTest) { |
| 434 | using namespace ::android; |
| 435 | using ::android::hardware::Status; |
| 436 | using ::testing::HasSubstr; |
| 437 | |
| 438 | EXPECT_EQ(toString(Status::ok()), "No error"); |
| 439 | |
| 440 | EXPECT_THAT(toString(Status::fromStatusT(DEAD_OBJECT)), HasSubstr("DEAD_OBJECT")); |
| 441 | |
| 442 | EXPECT_THAT(toString(Status::fromStatusT(-EBUSY)), HasSubstr("busy")); |
| 443 | |
Yifan Hong | 2be9418 | 2017-03-03 19:07:38 -0800 | [diff] [blame] | 444 | EXPECT_THAT(toString(Status::fromExceptionCode(Status::EX_NULL_POINTER)), |
| 445 | HasSubstr("EX_NULL_POINTER")); |
| 446 | |
| 447 | } |
| 448 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 449 | int main(int argc, char **argv) { |
| 450 | ::testing::InitGoogleTest(&argc, argv); |
| 451 | return RUN_ALL_TESTS(); |
| 452 | } |