blob: c2dd77364f0ee96b8f659053eea5227a656ee540 [file] [log] [blame]
Yifan Hong602b85a2016-10-24 13:40:01 -07001#define LOG_TAG "LibHidlTest"
2
3#include <android-base/logging.h>
4#include <gtest/gtest.h>
5#include <hidl/HidlSupport.h>
6#include <vector>
7
8#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
9
10template<typename T, typename S>
11static inline bool isArrayEqual(const T arr1, const S arr2, size_t size) {
12 for(size_t i = 0; i < size; i++)
13 if(arr1[i] != arr2[i])
14 return false;
15 return true;
16}
17
18class LibHidlTest : public ::testing::Test {
19public:
20 virtual void SetUp() override {
21 }
22 virtual void TearDown() override {
23 }
24};
25
26TEST_F(LibHidlTest, StringTest) {
27 using android::hardware::hidl_string;
28 hidl_string s; // empty constructor
29 EXPECT_STREQ(s.c_str(), "");
30 hidl_string s1 = "s1"; // copy = from cstr
31 EXPECT_STREQ(s1.c_str(), "s1");
32 hidl_string s2("s2"); // copy constructor from cstr
33 EXPECT_STREQ(s2.c_str(), "s2");
34 hidl_string s3 = hidl_string("s3"); // move =
35 EXPECT_STREQ(s3.c_str(), "s3");
36 hidl_string s4(hidl_string(hidl_string("s4"))); // move constructor
37 EXPECT_STREQ(s4.c_str(), "s4");
38 hidl_string s5(std::string("s5")); // copy constructor from std::string
39 EXPECT_STREQ(s5, "s5");
40 hidl_string s6 = std::string("s6"); // copy = from std::string
41 EXPECT_STREQ(s6, "s6");
42 hidl_string s7(s6); // copy constructor
43 EXPECT_STREQ(s7, "s6");
44 hidl_string s8 = s7; // copy =
45 EXPECT_STREQ(s8, "s6");
46 char myCString[20] = "myCString";
47 s.setToExternal(&myCString[0], strlen(myCString));
48 EXPECT_STREQ(s, "myCString");
49 myCString[2] = 'D';
50 EXPECT_STREQ(s, "myDString");
51 s.clear(); // should not affect myCString
52 EXPECT_STREQ(myCString, "myDString");
53 // casts
54 s = "great";
55 std::string myString = s;
56 const char *anotherCString = s;
57 EXPECT_EQ(myString, "great");
58 EXPECT_STREQ(anotherCString, "great");
59}
60
61TEST_F(LibHidlTest, VecTest) {
62 using android::hardware::hidl_vec;
63 using std::vector;
64 int32_t array[] = {5, 6, 7};
65 vector<int32_t> v(array, array + 3);
66
67 hidl_vec<int32_t> hv1 = v; // copy =
68 EXPECT_ARRAYEQ(hv1, array, 3);
69 EXPECT_ARRAYEQ(hv1, v, 3);
70 hidl_vec<int32_t> hv2(v); // copy constructor
71 EXPECT_ARRAYEQ(hv2, v, 3);
72
73 vector<int32_t> v2 = hv1; // cast
74 EXPECT_ARRAYEQ(v2, v, 3);
75}
76
77template <typename T>
78void great(android::hardware::hidl_vec<T>) {}
79
80TEST_F(LibHidlTest, VecCopyTest) {
81 android::hardware::hidl_vec<int32_t> v;
82 great(v);
83}
84
85int main(int argc, char **argv) {
86 ::testing::InitGoogleTest(&argc, argv);
87 return RUN_ALL_TESTS();
88}