blob: f964ce5525572e2f9af60028fb87549ba3b8f985 [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__))
26
27template<typename T, typename S>
28static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
29 for(size_t i = 0; i < size; i++)
30 if(arr1[i] != arr2[i])
31 return false;
32 return true;
33}
34
35class LibHidlTest : public ::testing::Test {
36public:
37 virtual void SetUp() override {
38 }
39 virtual void TearDown() override {
40 }
41};
42
43TEST_F(LibHidlTest, StringTest) {
44 using android::hardware::hidl_string;
45 hidl_string s; // empty constructor
46 EXPECT_STREQ(s.c_str(), "");
47 hidl_string s1 = "s1"; // copy = from cstr
48 EXPECT_STREQ(s1.c_str(), "s1");
49 hidl_string s2("s2"); // copy constructor from cstr
50 EXPECT_STREQ(s2.c_str(), "s2");
51 hidl_string s3 = hidl_string("s3"); // move =
52 EXPECT_STREQ(s3.c_str(), "s3");
53 hidl_string s4(hidl_string(hidl_string("s4"))); // move constructor
54 EXPECT_STREQ(s4.c_str(), "s4");
55 hidl_string s5(std::string("s5")); // copy constructor from std::string
56 EXPECT_STREQ(s5, "s5");
57 hidl_string s6 = std::string("s6"); // copy = from std::string
58 EXPECT_STREQ(s6, "s6");
59 hidl_string s7(s6); // copy constructor
60 EXPECT_STREQ(s7, "s6");
61 hidl_string s8 = s7; // copy =
62 EXPECT_STREQ(s8, "s6");
63 char myCString[20] = "myCString";
64 s.setToExternal(&myCString[0], strlen(myCString));
65 EXPECT_STREQ(s, "myCString");
66 myCString[2] = 'D';
67 EXPECT_STREQ(s, "myDString");
68 s.clear(); // should not affect myCString
69 EXPECT_STREQ(myCString, "myDString");
70 // casts
71 s = "great";
72 std::string myString = s;
73 const char *anotherCString = s;
74 EXPECT_EQ(myString, "great");
75 EXPECT_STREQ(anotherCString, "great");
76}
77
78TEST_F(LibHidlTest, VecTest) {
79 using android::hardware::hidl_vec;
80 using std::vector;
81 int32_t array[] = {5, 6, 7};
82 vector<int32_t> v(array, array + 3);
83
84 hidl_vec<int32_t> hv1 = v; // copy =
85 EXPECT_ARRAYEQ(hv1, array, 3);
86 EXPECT_ARRAYEQ(hv1, v, 3);
87 hidl_vec<int32_t> hv2(v); // copy constructor
88 EXPECT_ARRAYEQ(hv2, v, 3);
89
90 vector<int32_t> v2 = hv1; // cast
91 EXPECT_ARRAYEQ(v2, v, 3);
Steven Moreland9fbfe472016-11-14 16:49:17 -080092
93 hidl_vec<int32_t> v3 = {5, 6, 7}; // initializer_list
Steven Morelandb69926a2016-11-15 10:02:57 -080094 EXPECT_EQ(v3.size(), 3ul);
Steven Moreland9fbfe472016-11-14 16:49:17 -080095 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong602b85a2016-10-24 13:40:01 -070096}
97
Sasha Levitskiy3da68482016-11-17 16:48:43 -080098TEST_F(LibHidlTest, ArrayTest) {
99 using android::hardware::hidl_array;
100 int32_t array[] = {5, 6, 7};
101
102 hidl_array<int32_t, 3> ha(array);
103 EXPECT_ARRAYEQ(ha, array, 3);
104}
105
Yifan Hong5e2318c2016-10-27 17:19:21 -0700106TEST_F(LibHidlTest, TaskRunnerTest) {
107 using android::hardware::TaskRunner;
108 TaskRunner tr;
109 bool flag = false;
110 tr.push([&] {
111 usleep(1000);
112 flag = true;
113 });
114 usleep(500);
115 EXPECT_FALSE(flag);
116 usleep(1000);
117 EXPECT_TRUE(flag);
118}
119
Yifan Hong5708fb42016-10-26 17:50:29 -0700120TEST_F(LibHidlTest, StringCmpTest) {
121 using android::hardware::hidl_string;
122 const char * s = "good";
123 hidl_string hs(s);
124 EXPECT_NE(hs.c_str(), s);
125
126 EXPECT_TRUE(hs == s); // operator ==
127 EXPECT_TRUE(s == hs);
128
129 EXPECT_FALSE(hs != s); // operator ==
130 EXPECT_FALSE(s != hs);
131}
132
Yifan Hong602b85a2016-10-24 13:40:01 -0700133template <typename T>
134void great(android::hardware::hidl_vec<T>) {}
135
136TEST_F(LibHidlTest, VecCopyTest) {
137 android::hardware::hidl_vec<int32_t> v;
138 great(v);
139}
140
141int main(int argc, char **argv) {
142 ::testing::InitGoogleTest(&argc, argv);
143 return RUN_ALL_TESTS();
144}