blob: 648f0c77c65148378f32821f1e56b74d9275f724 [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>
22#include <vector>
23
24#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
25
26template<typename T, typename S>
27static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
28 for(size_t i = 0; i < size; i++)
29 if(arr1[i] != arr2[i])
30 return false;
31 return true;
32}
33
34class LibHidlTest : public ::testing::Test {
35public:
36 virtual void SetUp() override {
37 }
38 virtual void TearDown() override {
39 }
40};
41
42TEST_F(LibHidlTest, StringTest) {
43 using android::hardware::hidl_string;
44 hidl_string s; // empty constructor
45 EXPECT_STREQ(s.c_str(), "");
46 hidl_string s1 = "s1"; // copy = from cstr
47 EXPECT_STREQ(s1.c_str(), "s1");
48 hidl_string s2("s2"); // copy constructor from cstr
49 EXPECT_STREQ(s2.c_str(), "s2");
50 hidl_string s3 = hidl_string("s3"); // move =
51 EXPECT_STREQ(s3.c_str(), "s3");
52 hidl_string s4(hidl_string(hidl_string("s4"))); // move constructor
53 EXPECT_STREQ(s4.c_str(), "s4");
54 hidl_string s5(std::string("s5")); // copy constructor from std::string
55 EXPECT_STREQ(s5, "s5");
56 hidl_string s6 = std::string("s6"); // copy = from std::string
57 EXPECT_STREQ(s6, "s6");
58 hidl_string s7(s6); // copy constructor
59 EXPECT_STREQ(s7, "s6");
60 hidl_string s8 = s7; // copy =
61 EXPECT_STREQ(s8, "s6");
62 char myCString[20] = "myCString";
63 s.setToExternal(&myCString[0], strlen(myCString));
64 EXPECT_STREQ(s, "myCString");
65 myCString[2] = 'D';
66 EXPECT_STREQ(s, "myDString");
67 s.clear(); // should not affect myCString
68 EXPECT_STREQ(myCString, "myDString");
69 // casts
70 s = "great";
71 std::string myString = s;
72 const char *anotherCString = s;
73 EXPECT_EQ(myString, "great");
74 EXPECT_STREQ(anotherCString, "great");
75}
76
77TEST_F(LibHidlTest, VecTest) {
78 using android::hardware::hidl_vec;
79 using std::vector;
80 int32_t array[] = {5, 6, 7};
81 vector<int32_t> v(array, array + 3);
82
83 hidl_vec<int32_t> hv1 = v; // copy =
84 EXPECT_ARRAYEQ(hv1, array, 3);
85 EXPECT_ARRAYEQ(hv1, v, 3);
86 hidl_vec<int32_t> hv2(v); // copy constructor
87 EXPECT_ARRAYEQ(hv2, v, 3);
88
89 vector<int32_t> v2 = hv1; // cast
90 EXPECT_ARRAYEQ(v2, v, 3);
91}
92
93template <typename T>
94void great(android::hardware::hidl_vec<T>) {}
95
96TEST_F(LibHidlTest, VecCopyTest) {
97 android::hardware::hidl_vec<int32_t> v;
98 great(v);
99}
100
101int main(int argc, char **argv) {
102 ::testing::InitGoogleTest(&argc, argv);
103 return RUN_ALL_TESTS();
104}