1 /*
2 * Copyright (C) 2018 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
17 #define LOG_TAG "libhidl-gen-host-utils"
18
19 #include <hidl-util/StringHelper.h>
20
21 #include <gtest/gtest.h>
22 #include <vector>
23
24 using ::android::StringHelper;
25
26 class LibHidlGenUtilsTest : public ::testing::Test {};
27
TEST_F(LibHidlGenUtilsTest,EndsWithTest)28 TEST_F(LibHidlGenUtilsTest, EndsWithTest) {
29 EXPECT_TRUE(StringHelper::EndsWith("", ""));
30 EXPECT_TRUE(StringHelper::EndsWith("a", ""));
31 EXPECT_TRUE(StringHelper::EndsWith("b", "b"));
32 EXPECT_TRUE(StringHelper::EndsWith("ab", "b"));
33 EXPECT_TRUE(StringHelper::EndsWith("ab", "ab"));
34 EXPECT_TRUE(StringHelper::EndsWith("abcd", "bcd"));
35 EXPECT_TRUE(StringHelper::EndsWith("abcd", "abcd"));
36 EXPECT_TRUE(StringHelper::EndsWith("abcdefghijk", "ijk"));
37 EXPECT_TRUE(StringHelper::EndsWith("abcdefghijk", "bcdefghijk"));
38
39 EXPECT_FALSE(StringHelper::EndsWith("", "a"));
40 EXPECT_FALSE(StringHelper::EndsWith("b", "a"));
41 EXPECT_FALSE(StringHelper::EndsWith("abcd", "ab"));
42 }
43
TEST_F(LibHidlGenUtilsTest,StartsWithTest)44 TEST_F(LibHidlGenUtilsTest, StartsWithTest) {
45 EXPECT_TRUE(StringHelper::StartsWith("", ""));
46 EXPECT_TRUE(StringHelper::StartsWith("a", ""));
47 EXPECT_TRUE(StringHelper::StartsWith("b", "b"));
48 EXPECT_TRUE(StringHelper::StartsWith("ab", "a"));
49 EXPECT_TRUE(StringHelper::StartsWith("ab", "ab"));
50 EXPECT_TRUE(StringHelper::StartsWith("abcd", "abc"));
51 EXPECT_TRUE(StringHelper::StartsWith("abcd", "abcd"));
52 EXPECT_TRUE(StringHelper::StartsWith("abcdefghijk", "abc"));
53 EXPECT_TRUE(StringHelper::StartsWith("abcdefghijk", "abcdefghij"));
54
55 EXPECT_FALSE(StringHelper::StartsWith("", "a"));
56 EXPECT_FALSE(StringHelper::StartsWith("b", "a"));
57 EXPECT_FALSE(StringHelper::StartsWith("abcd", "cd"));
58 }
59
TEST_F(LibHidlGenUtilsTest,Trim)60 TEST_F(LibHidlGenUtilsTest, Trim) {
61 EXPECT_EQ("", StringHelper::LTrim("", ""));
62 EXPECT_EQ("", StringHelper::LTrim("", "a"));
63 EXPECT_EQ("", StringHelper::LTrim("a", "a"));
64 EXPECT_EQ("a", StringHelper::LTrim("a", ""));
65 EXPECT_EQ("a", StringHelper::LTrim("a", "b"));
66 EXPECT_EQ("a", StringHelper::LTrim("ba", "b"));
67 EXPECT_EQ("f", StringHelper::LTrim("abcdef", "abcde"));
68 EXPECT_EQ("cdef", StringHelper::LTrim("abcdef", "ab"));
69 EXPECT_EQ("abcdef", StringHelper::LTrim("abcdef", ""));
70
71 EXPECT_EQ("", StringHelper::RTrim("", ""));
72 EXPECT_EQ("", StringHelper::RTrim("", "a"));
73 EXPECT_EQ("", StringHelper::RTrim("a", "a"));
74 EXPECT_EQ("a", StringHelper::RTrim("a", ""));
75 EXPECT_EQ("a", StringHelper::RTrim("a", "b"));
76 EXPECT_EQ("a", StringHelper::RTrim("ab", "b"));
77 EXPECT_EQ("a", StringHelper::RTrim("abcdef", "bcdef"));
78 EXPECT_EQ("abcd", StringHelper::RTrim("abcdef", "ef"));
79 EXPECT_EQ("abcdef", StringHelper::RTrim("abcdef", ""));
80 }
81
TEST_F(LibHidlGenUtilsTest,TrimAll)82 TEST_F(LibHidlGenUtilsTest, TrimAll) {
83 EXPECT_EQ("", StringHelper::LTrimAll("", ""));
84 EXPECT_EQ("", StringHelper::LTrimAll("", "a"));
85 EXPECT_EQ("", StringHelper::LTrimAll("", "ab"));
86 EXPECT_EQ("", StringHelper::LTrimAll("a", "a"));
87 EXPECT_EQ("", StringHelper::LTrimAll("aa", "a"));
88 EXPECT_EQ("b", StringHelper::LTrimAll("b", "a"));
89 EXPECT_EQ("b", StringHelper::LTrimAll("aaab", "a"));
90 EXPECT_EQ("c", StringHelper::LTrimAll("ababc", "ab"));
91 EXPECT_EQ("ac", StringHelper::LTrimAll("abac", "ab"));
92
93 EXPECT_EQ("", StringHelper::RTrimAll("", ""));
94 EXPECT_EQ("", StringHelper::RTrimAll("", "a"));
95 EXPECT_EQ("", StringHelper::RTrimAll("", "ab"));
96 EXPECT_EQ("", StringHelper::RTrimAll("a", "a"));
97 EXPECT_EQ("", StringHelper::RTrimAll("aa", "a"));
98 EXPECT_EQ("b", StringHelper::RTrimAll("b", "a"));
99 EXPECT_EQ("b", StringHelper::RTrimAll("baaa", "a"));
100 EXPECT_EQ("c", StringHelper::RTrimAll("cabab", "ab"));
101 EXPECT_EQ("ca", StringHelper::RTrimAll("caba", "ba"));
102 }
103
TEST_F(LibHidlGenUtilsTest,SplitString)104 TEST_F(LibHidlGenUtilsTest, SplitString) {
105 std::vector<std::string> components;
106
107 StringHelper::SplitString("", '.', &components);
108 EXPECT_EQ(std::vector<std::string>({""}), components);
109 StringHelper::SplitString("a.", '.', &components);
110 EXPECT_EQ(std::vector<std::string>({"a", ""}), components);
111 StringHelper::SplitString(".a", '.', &components);
112 EXPECT_EQ(std::vector<std::string>({"", "a"}), components);
113 StringHelper::SplitString("..", '.', &components);
114 EXPECT_EQ(std::vector<std::string>({"", "", ""}), components);
115 StringHelper::SplitString("asdf.asdf", '.', &components);
116 EXPECT_EQ(std::vector<std::string>({"asdf", "asdf"}), components);
117 }
118
TEST_F(LibHidlGenUtilsTest,JoinStrings)119 TEST_F(LibHidlGenUtilsTest, JoinStrings) {
120 EXPECT_EQ("", StringHelper::JoinStrings({}, ""));
121 EXPECT_EQ("", StringHelper::JoinStrings({}, "a"));
122 EXPECT_EQ("a", StringHelper::JoinStrings({"a"}, ""));
123 EXPECT_EQ("a,b", StringHelper::JoinStrings({"a", "b"}, ","));
124 EXPECT_EQ("ab,", StringHelper::JoinStrings({"ab", ""}, ","));
125 EXPECT_EQ(",ab", StringHelper::JoinStrings({"", "ab"}, ","));
126 EXPECT_EQ("a.,b", StringHelper::JoinStrings({"a", "b"}, ".,"));
127 EXPECT_EQ("a,b,c", StringHelper::JoinStrings({"a", "b", "c"}, ","));
128 EXPECT_EQ("abc.,def.,ghi", StringHelper::JoinStrings({"abc", "def", "ghi"}, ".,"));
129 }
130
131 // This test checks if Tokenize is working correctly. Since that is private, this just converts
132 // everything to upper snake case.
TEST_F(LibHidlGenUtilsTest,TokenizeTest)133 TEST_F(LibHidlGenUtilsTest, TokenizeTest) {
134 // Basic cases
135 EXPECT_EQ("UPPER_SNAKE_CASE", StringHelper::ToUpperSnakeCase("UPPER_SNAKE_CASE"));
136 EXPECT_EQ("UPPER_SNAKE_CASE", StringHelper::ToUpperSnakeCase("upper_snake_case"));
137 EXPECT_EQ("UPPER_SNAKE_CASE", StringHelper::ToUpperSnakeCase("UpperSnakeCase"));
138 EXPECT_EQ("UPPER_SNAKE_CASE", StringHelper::ToUpperSnakeCase("upperSnakeCase"));
139
140 // Cases with numbers
141 // TODO: The following case "Some3DText" should be tokenized as SOME_3D_TEXT
142 // EXPECT_EQ("SOME3_D_TEXT", StringHelper::ToUpperSnakeCase("Some3DText"));
143 EXPECT_EQ("SOME_3D_TEXT", StringHelper::ToUpperSnakeCase("Some_3D_Text"));
144 EXPECT_EQ("IPV4_ADDR", StringHelper::ToUpperSnakeCase("Ipv4Addr"));
145 EXPECT_EQ("IPV4_ADDR", StringHelper::ToUpperSnakeCase("Ipv4_Addr"));
146
147 // Cases with incorrect PascalCase
148 EXPECT_EQ("UI_ERROR", StringHelper::ToUpperSnakeCase("UIError"));
149 EXPECT_EQ("UI_ERROR", StringHelper::ToUpperSnakeCase("UI_Error"));
150 EXPECT_EQ("PROTOCOL_B_PRIME", StringHelper::ToUpperSnakeCase("protocolBPrime"));
151 EXPECT_EQ("PROTOCOL_B_PRIME", StringHelper::ToUpperSnakeCase("protocol_B_Prime"));
152
153 // Fully uppercase
154 EXPECT_EQ("VAL2", StringHelper::ToUpperSnakeCase("VAL2"));
155 EXPECT_EQ("VAL2OTHER", StringHelper::ToUpperSnakeCase("VAL2OTHER"));
156 }
157
main(int argc,char ** argv)158 int main(int argc, char** argv) {
159 ::testing::InitGoogleTest(&argc, argv);
160 return RUN_ALL_TESTS();
161 }
162