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 #include "perfetto/ext/base/string_utils.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 #include "perfetto/ext/base/optional.h"
22
23 namespace perfetto {
24 namespace base {
25 namespace {
26
27 using testing::ElementsAre;
28
TEST(StringUtilsTest,Lowercase)29 TEST(StringUtilsTest, Lowercase) {
30 EXPECT_EQ(Lowercase('A'), 'a');
31 EXPECT_EQ(Lowercase('a'), 'a');
32 EXPECT_EQ(Lowercase('Z'), 'z');
33 EXPECT_EQ(Lowercase('z'), 'z');
34 EXPECT_EQ(Lowercase('!'), '!');
35 }
36
TEST(StringUtilsTest,Uppercase)37 TEST(StringUtilsTest, Uppercase) {
38 EXPECT_EQ(Uppercase('A'), 'A');
39 EXPECT_EQ(Uppercase('a'), 'A');
40 EXPECT_EQ(Uppercase('Z'), 'Z');
41 EXPECT_EQ(Uppercase('z'), 'Z');
42 EXPECT_EQ(Uppercase('!'), '!');
43 }
44
TEST(StringUtilsTest,CStringToUInt32)45 TEST(StringUtilsTest, CStringToUInt32) {
46 EXPECT_EQ(CStringToUInt32("0"), make_optional<uint32_t>(0U));
47 EXPECT_EQ(CStringToUInt32("1"), make_optional<uint32_t>(1U));
48 EXPECT_EQ(CStringToUInt32("42"), make_optional<uint32_t>(42U));
49 EXPECT_EQ(CStringToUInt32(""), nullopt);
50 EXPECT_EQ(CStringToUInt32("!?"), nullopt);
51 EXPECT_EQ(CStringToUInt32("abc"), nullopt);
52 EXPECT_EQ(CStringToUInt32("123 abc"), nullopt);
53 }
54
TEST(StringUtilsTest,CStringToInt32)55 TEST(StringUtilsTest, CStringToInt32) {
56 EXPECT_EQ(CStringToInt32("0"), make_optional<int32_t>(0));
57 EXPECT_EQ(CStringToInt32("1"), make_optional<int32_t>(1));
58 EXPECT_EQ(CStringToInt32("-42"), make_optional<int32_t>(-42));
59 EXPECT_EQ(CStringToInt32(""), nullopt);
60 EXPECT_EQ(CStringToInt32("!?"), nullopt);
61 EXPECT_EQ(CStringToInt32("abc"), nullopt);
62 EXPECT_EQ(CStringToInt32("123 abc"), nullopt);
63 }
64
TEST(StringUtilsTest,CStringToDouble)65 TEST(StringUtilsTest, CStringToDouble) {
66 EXPECT_DOUBLE_EQ(CStringToDouble("0").value(), 0l);
67 EXPECT_DOUBLE_EQ(CStringToDouble("1").value(), 1l);
68 EXPECT_DOUBLE_EQ(CStringToDouble("-42").value(), -42l);
69 EXPECT_DOUBLE_EQ(CStringToDouble("-42.5").value(), -42.5l);
70 EXPECT_EQ(CStringToDouble(""), nullopt);
71 EXPECT_EQ(CStringToDouble("!?"), nullopt);
72 EXPECT_EQ(CStringToDouble("abc"), nullopt);
73 EXPECT_EQ(CStringToDouble("123 abc"), nullopt);
74 }
75
TEST(StringUtilsTest,StringToUInt32)76 TEST(StringUtilsTest, StringToUInt32) {
77 EXPECT_EQ(StringToUInt32("0"), make_optional<uint32_t>(0U));
78 EXPECT_EQ(StringToUInt32("1"), make_optional<uint32_t>(1U));
79 EXPECT_EQ(StringToUInt32("42"), make_optional<uint32_t>(42U));
80 EXPECT_EQ(StringToUInt32("a", 16), make_optional<uint32_t>(10U));
81 EXPECT_EQ(StringToUInt32("fffffff0", 16),
82 make_optional<uint32_t>(0xfffffff0));
83 EXPECT_EQ(StringToUInt32(""), nullopt);
84 EXPECT_EQ(StringToUInt32("!?"), nullopt);
85 EXPECT_EQ(StringToUInt32("abc"), nullopt);
86 EXPECT_EQ(StringToUInt32("123 abc"), nullopt);
87 EXPECT_EQ(StringToUInt32("beefz", 16), nullopt);
88 }
89
TEST(StringUtilsTest,StringToInt32)90 TEST(StringUtilsTest, StringToInt32) {
91 EXPECT_EQ(StringToInt32("0"), make_optional<int32_t>(0));
92 EXPECT_EQ(StringToInt32("1"), make_optional<int32_t>(1));
93 EXPECT_EQ(StringToInt32("-42"), make_optional<int32_t>(-42));
94 EXPECT_EQ(StringToInt32("42", 16), make_optional<int32_t>(0x42));
95 EXPECT_EQ(StringToInt32("7ffffffe", 16), make_optional<int32_t>(0x7ffffffe));
96 EXPECT_EQ(StringToInt32(""), nullopt);
97 EXPECT_EQ(StringToInt32("!?"), nullopt);
98 EXPECT_EQ(StringToInt32("abc"), nullopt);
99 EXPECT_EQ(StringToInt32("123 abc"), nullopt);
100 EXPECT_EQ(StringToInt32("beefz", 16), nullopt);
101 }
102
TEST(StringUtilsTest,StringToUInt64)103 TEST(StringUtilsTest, StringToUInt64) {
104 EXPECT_EQ(StringToUInt64("0"), make_optional<uint64_t>(0u));
105 EXPECT_EQ(StringToUInt64("1"), make_optional<uint64_t>(1u));
106 EXPECT_EQ(StringToUInt64("5000000000"),
107 make_optional<uint64_t>(5000000000ULL));
108 EXPECT_EQ(StringToUInt64("7ffffffffffffffe", 16),
109 make_optional<uint64_t>(0x7ffffffffffffffeULL));
110 EXPECT_EQ(StringToUInt64("9ffffffffffffffe", 16),
111 make_optional<uint64_t>(0x9ffffffffffffffeULL));
112 EXPECT_EQ(StringToUInt64(""), nullopt);
113 EXPECT_EQ(StringToUInt64("abc"), nullopt);
114 EXPECT_EQ(StringToUInt64("beefz", 16), nullopt);
115 }
116
TEST(StringUtilsTest,StringToInt64)117 TEST(StringUtilsTest, StringToInt64) {
118 EXPECT_EQ(StringToInt64("0"), make_optional<int64_t>(0));
119 EXPECT_EQ(StringToInt64("1"), make_optional<int64_t>(1));
120 EXPECT_EQ(StringToInt64("-5000000000"),
121 make_optional<int64_t>(-5000000000LL));
122 EXPECT_EQ(StringToInt64("5000000000"), make_optional<int64_t>(5000000000LL));
123 EXPECT_EQ(StringToInt64("7ffffffffffffffe", 16),
124 make_optional<int64_t>(0x7ffffffffffffffeLL));
125 EXPECT_EQ(StringToInt64("9ffffffe", 16),
126 make_optional<int64_t>(0x9ffffffeLL));
127 EXPECT_EQ(StringToInt64(""), nullopt);
128 EXPECT_EQ(StringToInt64("abc"), nullopt);
129 EXPECT_EQ(StringToInt64("beefz", 16), nullopt);
130 }
131
TEST(StringUtilsTest,StringToDouble)132 TEST(StringUtilsTest, StringToDouble) {
133 EXPECT_DOUBLE_EQ(StringToDouble("0").value(), 0l);
134 EXPECT_DOUBLE_EQ(StringToDouble("1").value(), 1l);
135 EXPECT_DOUBLE_EQ(StringToDouble("-42").value(), -42l);
136 EXPECT_DOUBLE_EQ(StringToDouble("-42.5").value(), -42.5l);
137 EXPECT_EQ(StringToDouble(""), nullopt);
138 EXPECT_EQ(StringToDouble("!?"), nullopt);
139 EXPECT_EQ(StringToDouble("abc"), nullopt);
140 EXPECT_EQ(StringToDouble("123 abc"), nullopt);
141 }
142
TEST(StringUtilsTest,StartsWith)143 TEST(StringUtilsTest, StartsWith) {
144 EXPECT_TRUE(StartsWith("", ""));
145 EXPECT_TRUE(StartsWith("abc", ""));
146 EXPECT_TRUE(StartsWith("abc", "a"));
147 EXPECT_TRUE(StartsWith("abc", "ab"));
148 EXPECT_TRUE(StartsWith("abc", "abc"));
149 EXPECT_FALSE(StartsWith("abc", "abcd"));
150 EXPECT_FALSE(StartsWith("aa", "ab"));
151 EXPECT_FALSE(StartsWith("", "ab"));
152 }
153
TEST(StringUtilsTest,EndsWith)154 TEST(StringUtilsTest, EndsWith) {
155 EXPECT_TRUE(EndsWith("", ""));
156 EXPECT_TRUE(EndsWith("abc", ""));
157 EXPECT_TRUE(EndsWith("abc", "c"));
158 EXPECT_TRUE(EndsWith("abc", "bc"));
159 EXPECT_TRUE(EndsWith("abc", "abc"));
160 EXPECT_FALSE(EndsWith("bcd", "abcd"));
161 EXPECT_FALSE(EndsWith("abc", "abd"));
162 EXPECT_FALSE(EndsWith("", "c"));
163 }
164
TEST(StringUtilsTest,ToHex)165 TEST(StringUtilsTest, ToHex) {
166 EXPECT_EQ(ToHex(""), "");
167 EXPECT_EQ(ToHex("abc123"), "616263313233");
168 }
169
TEST(StringUtilsTest,intToHex)170 TEST(StringUtilsTest, intToHex) {
171 EXPECT_EQ(IntToHexString(0), "0x00");
172 EXPECT_EQ(IntToHexString(1), "0x01");
173 EXPECT_EQ(IntToHexString(16), "0x10");
174 EXPECT_EQ(IntToHexString(4294967295), "0xffffffff");
175 }
176
TEST(StringUtilsTest,CaseInsensitiveEqual)177 TEST(StringUtilsTest, CaseInsensitiveEqual) {
178 EXPECT_TRUE(CaseInsensitiveEqual("", ""));
179 EXPECT_TRUE(CaseInsensitiveEqual("abc", "abc"));
180 EXPECT_TRUE(CaseInsensitiveEqual("ABC", "abc"));
181 EXPECT_TRUE(CaseInsensitiveEqual("abc", "ABC"));
182 EXPECT_FALSE(CaseInsensitiveEqual("abc", "AB"));
183 EXPECT_FALSE(CaseInsensitiveEqual("ab", "ABC"));
184 }
185
TEST(StringUtilsTest,SplitString)186 TEST(StringUtilsTest, SplitString) {
187 EXPECT_THAT(SplitString("", ":"), ElementsAre());
188 EXPECT_THAT(SplitString("a:b:c", ":"), ElementsAre("a", "b", "c"));
189 EXPECT_THAT(SplitString("a::b::c", "::"), ElementsAre("a", "b", "c"));
190 EXPECT_THAT(SplitString("::::a::b::::c::", "::"), ElementsAre("a", "b", "c"));
191 EXPECT_THAT(SplitString("abc", ":"), ElementsAre("abc"));
192 EXPECT_THAT(SplitString("abc", "::"), ElementsAre("abc"));
193 EXPECT_THAT(SplitString("abc", ":"), ElementsAre("abc"));
194 EXPECT_THAT(SplitString("abc", "::"), ElementsAre("abc"));
195 }
196
TEST(StringUtilsTest,Strip)197 TEST(StringUtilsTest, Strip) {
198 EXPECT_EQ(StripPrefix("abc", ""), "abc");
199 EXPECT_EQ(StripPrefix("abc", "a"), "bc");
200 EXPECT_EQ(StripPrefix("abc", "ab"), "c");
201 EXPECT_EQ(StripPrefix("abc", "abc"), "");
202 EXPECT_EQ(StripPrefix("abc", "abcd"), "abc");
203
204 EXPECT_EQ(StripSuffix("abc", ""), "abc");
205 EXPECT_EQ(StripSuffix("abc", "c"), "ab");
206 EXPECT_EQ(StripSuffix("abc", "bc"), "a");
207 EXPECT_EQ(StripSuffix("abc", "abc"), "");
208 EXPECT_EQ(StripSuffix("abc", "ebcd"), "abc");
209
210 EXPECT_EQ(StripChars("foobar", "", '_'), "foobar");
211 EXPECT_EQ(StripChars("foobar", "x", '_'), "foobar");
212 EXPECT_EQ(StripChars("foobar", "f", '_'), "_oobar");
213 EXPECT_EQ(StripChars("foobar", "o", '_'), "f__bar");
214 EXPECT_EQ(StripChars("foobar", "oa", '_'), "f__b_r");
215 EXPECT_EQ(StripChars("foobar", "fbr", '_'), "_oo_a_");
216 EXPECT_EQ(StripChars("foobar", "froab", '_'), "______");
217 }
218
TEST(StringUtilsTest,Contains)219 TEST(StringUtilsTest, Contains) {
220 EXPECT_TRUE(Contains("", ""));
221 EXPECT_TRUE(Contains("abc", ""));
222 EXPECT_TRUE(Contains("abc", "a"));
223 EXPECT_TRUE(Contains("abc", "b"));
224 EXPECT_TRUE(Contains("abc", "c"));
225 EXPECT_TRUE(Contains("abc", "ab"));
226 EXPECT_TRUE(Contains("abc", "bc"));
227 EXPECT_TRUE(Contains("abc", "abc"));
228 EXPECT_FALSE(Contains("abc", "d"));
229 EXPECT_FALSE(Contains("abc", "ac"));
230 EXPECT_FALSE(Contains("abc", "abcd"));
231 EXPECT_FALSE(Contains("", "a"));
232 EXPECT_FALSE(Contains("", "abc"));
233 }
234
TEST(StringUtilsTest,Find)235 TEST(StringUtilsTest, Find) {
236 EXPECT_EQ(Find("", ""), 0u);
237 EXPECT_EQ(Find("", "abc"), 0u);
238 EXPECT_EQ(Find("a", "abc"), 0u);
239 EXPECT_EQ(Find("b", "abc"), 1u);
240 EXPECT_EQ(Find("c", "abc"), 2u);
241 EXPECT_EQ(Find("ab", "abc"), 0u);
242 EXPECT_EQ(Find("bc", "abc"), 1u);
243 EXPECT_EQ(Find("abc", "abc"), 0u);
244 EXPECT_EQ(Find("d", "abc"), std::string::npos);
245 EXPECT_EQ(Find("ac", "abc"), std::string::npos);
246 EXPECT_EQ(Find("abcd", "abc"), std::string::npos);
247 EXPECT_EQ(Find("a", ""), std::string::npos);
248 EXPECT_EQ(Find("abc", ""), std::string::npos);
249 }
250
251 } // namespace
252 } // namespace base
253 } // namespace perfetto
254