blob: a7dc24395305d962912490077cf00891638acb0b [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
94 EXPECT_EQ(v3.size(), 3);
95 EXPECT_ARRAYEQ(v3, array, v3.size());
Yifan Hong602b85a2016-10-24 13:40:01 -070096}
97
Yifan Hong5e2318c2016-10-27 17:19:21 -070098TEST_F(LibHidlTest, TaskRunnerTest) {
99 using android::hardware::TaskRunner;
100 TaskRunner tr;
101 bool flag = false;
102 tr.push([&] {
103 usleep(1000);
104 flag = true;
105 });
106 usleep(500);
107 EXPECT_FALSE(flag);
108 usleep(1000);
109 EXPECT_TRUE(flag);
110}
111
Yifan Hong5708fb42016-10-26 17:50:29 -0700112TEST_F(LibHidlTest, StringCmpTest) {
113 using android::hardware::hidl_string;
114 const char * s = "good";
115 hidl_string hs(s);
116 EXPECT_NE(hs.c_str(), s);
117
118 EXPECT_TRUE(hs == s); // operator ==
119 EXPECT_TRUE(s == hs);
120
121 EXPECT_FALSE(hs != s); // operator ==
122 EXPECT_FALSE(s != hs);
123}
124
Yifan Hong602b85a2016-10-24 13:40:01 -0700125template <typename T>
126void great(android::hardware::hidl_vec<T>) {}
127
128TEST_F(LibHidlTest, VecCopyTest) {
129 android::hardware::hidl_vec<int32_t> v;
130 great(v);
131}
132
133int main(int argc, char **argv) {
134 ::testing::InitGoogleTest(&argc, argv);
135 return RUN_ALL_TESTS();
136}