• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DOUBLE_EQ(StringToDouble("0.5").value(), .5l);
138   EXPECT_DOUBLE_EQ(StringToDouble(".5").value(), .5l);
139   EXPECT_EQ(StringToDouble(""), nullopt);
140   EXPECT_EQ(StringToDouble("!?"), nullopt);
141   EXPECT_EQ(StringToDouble("abc"), nullopt);
142   EXPECT_EQ(StringToDouble("123 abc"), nullopt);
143   EXPECT_EQ(StringToDouble("124,456"), nullopt);
144   EXPECT_EQ(StringToDouble("4 2"), nullopt);
145   EXPECT_EQ(StringToDouble(" - 42"), nullopt);
146 }
147 
TEST(StringUtilsTest,StartsWith)148 TEST(StringUtilsTest, StartsWith) {
149   EXPECT_TRUE(StartsWith("", ""));
150   EXPECT_TRUE(StartsWith("abc", ""));
151   EXPECT_TRUE(StartsWith("abc", "a"));
152   EXPECT_TRUE(StartsWith("abc", "ab"));
153   EXPECT_TRUE(StartsWith("abc", "abc"));
154   EXPECT_FALSE(StartsWith("abc", "abcd"));
155   EXPECT_FALSE(StartsWith("aa", "ab"));
156   EXPECT_FALSE(StartsWith("", "ab"));
157 }
158 
TEST(StringUtilsTest,EndsWith)159 TEST(StringUtilsTest, EndsWith) {
160   EXPECT_TRUE(EndsWith("", ""));
161   EXPECT_TRUE(EndsWith("abc", ""));
162   EXPECT_TRUE(EndsWith("abc", "c"));
163   EXPECT_TRUE(EndsWith("abc", "bc"));
164   EXPECT_TRUE(EndsWith("abc", "abc"));
165   EXPECT_FALSE(EndsWith("bcd", "abcd"));
166   EXPECT_FALSE(EndsWith("abc", "abd"));
167   EXPECT_FALSE(EndsWith("", "c"));
168 }
169 
TEST(StringUtilsTest,ToHex)170 TEST(StringUtilsTest, ToHex) {
171   EXPECT_EQ(ToHex(""), "");
172   EXPECT_EQ(ToHex("abc123"), "616263313233");
173 }
174 
TEST(StringUtilsTest,IntToHex)175 TEST(StringUtilsTest, IntToHex) {
176   EXPECT_EQ(IntToHexString(0), "0x00");
177   EXPECT_EQ(IntToHexString(1), "0x01");
178   EXPECT_EQ(IntToHexString(16), "0x10");
179   EXPECT_EQ(IntToHexString(4294967295), "0xffffffff");
180 }
181 
TEST(StringUtilsTest,Uint64ToHex)182 TEST(StringUtilsTest, Uint64ToHex) {
183   EXPECT_EQ(Uint64ToHexString(0), "0x0");
184   EXPECT_EQ(Uint64ToHexString(1), "0x1");
185   EXPECT_EQ(Uint64ToHexString(16), "0x10");
186   EXPECT_EQ(Uint64ToHexString(18446744073709551615UL), "0xffffffffffffffff");
187 }
188 
TEST(StringUtilsTest,Uint64ToHexNoPrefix)189 TEST(StringUtilsTest, Uint64ToHexNoPrefix) {
190   EXPECT_EQ(Uint64ToHexStringNoPrefix(0), "0");
191   EXPECT_EQ(Uint64ToHexStringNoPrefix(1), "1");
192   EXPECT_EQ(Uint64ToHexStringNoPrefix(16), "10");
193   EXPECT_EQ(Uint64ToHexStringNoPrefix(18446744073709551615UL),
194             "ffffffffffffffff");
195 }
196 
TEST(StringUtilsTest,CaseInsensitiveEqual)197 TEST(StringUtilsTest, CaseInsensitiveEqual) {
198   EXPECT_TRUE(CaseInsensitiveEqual("", ""));
199   EXPECT_TRUE(CaseInsensitiveEqual("abc", "abc"));
200   EXPECT_TRUE(CaseInsensitiveEqual("ABC", "abc"));
201   EXPECT_TRUE(CaseInsensitiveEqual("abc", "ABC"));
202   EXPECT_FALSE(CaseInsensitiveEqual("abc", "AB"));
203   EXPECT_FALSE(CaseInsensitiveEqual("ab", "ABC"));
204 }
205 
TEST(StringUtilsTest,SplitString)206 TEST(StringUtilsTest, SplitString) {
207   EXPECT_THAT(SplitString("", ":"), ElementsAre());
208   EXPECT_THAT(SplitString("a:b:c", ":"), ElementsAre("a", "b", "c"));
209   EXPECT_THAT(SplitString("a::b::c", "::"), ElementsAre("a", "b", "c"));
210   EXPECT_THAT(SplitString("::::a::b::::c::", "::"), ElementsAre("a", "b", "c"));
211   EXPECT_THAT(SplitString("abc", ":"), ElementsAre("abc"));
212   EXPECT_THAT(SplitString("abc", "::"), ElementsAre("abc"));
213   EXPECT_THAT(SplitString("abc", ":"), ElementsAre("abc"));
214   EXPECT_THAT(SplitString("abc", "::"), ElementsAre("abc"));
215 }
216 
TEST(StringUtilsTest,Strip)217 TEST(StringUtilsTest, Strip) {
218   EXPECT_EQ(StripPrefix("abc", ""), "abc");
219   EXPECT_EQ(StripPrefix("abc", "a"), "bc");
220   EXPECT_EQ(StripPrefix("abc", "ab"), "c");
221   EXPECT_EQ(StripPrefix("abc", "abc"), "");
222   EXPECT_EQ(StripPrefix("abc", "abcd"), "abc");
223 
224   EXPECT_EQ(StripSuffix("abc", ""), "abc");
225   EXPECT_EQ(StripSuffix("abc", "c"), "ab");
226   EXPECT_EQ(StripSuffix("abc", "bc"), "a");
227   EXPECT_EQ(StripSuffix("abc", "abc"), "");
228   EXPECT_EQ(StripSuffix("abc", "ebcd"), "abc");
229 
230   EXPECT_EQ(StripChars("foobar", "", '_'), "foobar");
231   EXPECT_EQ(StripChars("foobar", "x", '_'), "foobar");
232   EXPECT_EQ(StripChars("foobar", "f", '_'), "_oobar");
233   EXPECT_EQ(StripChars("foobar", "o", '_'), "f__bar");
234   EXPECT_EQ(StripChars("foobar", "oa", '_'), "f__b_r");
235   EXPECT_EQ(StripChars("foobar", "fbr", '_'), "_oo_a_");
236   EXPECT_EQ(StripChars("foobar", "froab", '_'), "______");
237 }
238 
TEST(StringUtilsTest,Contains)239 TEST(StringUtilsTest, Contains) {
240   EXPECT_TRUE(Contains("", ""));
241   EXPECT_TRUE(Contains("abc", ""));
242   EXPECT_TRUE(Contains("abc", "a"));
243   EXPECT_TRUE(Contains("abc", "b"));
244   EXPECT_TRUE(Contains("abc", "c"));
245   EXPECT_TRUE(Contains("abc", "ab"));
246   EXPECT_TRUE(Contains("abc", "bc"));
247   EXPECT_TRUE(Contains("abc", "abc"));
248   EXPECT_FALSE(Contains("abc", "d"));
249   EXPECT_FALSE(Contains("abc", "ac"));
250   EXPECT_FALSE(Contains("abc", "abcd"));
251   EXPECT_FALSE(Contains("", "a"));
252   EXPECT_FALSE(Contains("", "abc"));
253 }
254 
TEST(StringUtilsTest,Find)255 TEST(StringUtilsTest, Find) {
256   EXPECT_EQ(Find("", ""), 0u);
257   EXPECT_EQ(Find("", "abc"), 0u);
258   EXPECT_EQ(Find("a", "abc"), 0u);
259   EXPECT_EQ(Find("b", "abc"), 1u);
260   EXPECT_EQ(Find("c", "abc"), 2u);
261   EXPECT_EQ(Find("ab", "abc"), 0u);
262   EXPECT_EQ(Find("bc", "abc"), 1u);
263   EXPECT_EQ(Find("abc", "abc"), 0u);
264   EXPECT_EQ(Find("d", "abc"), std::string::npos);
265   EXPECT_EQ(Find("ac", "abc"), std::string::npos);
266   EXPECT_EQ(Find("abcd", "abc"), std::string::npos);
267   EXPECT_EQ(Find("a", ""), std::string::npos);
268   EXPECT_EQ(Find("abc", ""), std::string::npos);
269 }
270 
TEST(StringUtilsTest,ReplaceAll)271 TEST(StringUtilsTest, ReplaceAll) {
272   EXPECT_EQ(ReplaceAll("", "a", ""), "");
273   EXPECT_EQ(ReplaceAll("", "a", "b"), "");
274   EXPECT_EQ(ReplaceAll("a", "a", "b"), "b");
275   EXPECT_EQ(ReplaceAll("aaaa", "a", "b"), "bbbb");
276   EXPECT_EQ(ReplaceAll("aaaa", "aa", "b"), "bb");
277   EXPECT_EQ(ReplaceAll("aa", "aa", "bb"), "bb");
278   EXPECT_EQ(ReplaceAll("aa", "a", "bb"), "bbbb");
279   EXPECT_EQ(ReplaceAll("abc", "a", "b"), "bbc");
280   EXPECT_EQ(ReplaceAll("abc", "c", "b"), "abb");
281   EXPECT_EQ(ReplaceAll("abc", "c", "bbb"), "abbbb");
282 }
283 
TEST(StringUtilsTest,TrimLeading)284 TEST(StringUtilsTest, TrimLeading) {
285   EXPECT_EQ(TrimLeading(""), "");
286   EXPECT_EQ(TrimLeading("a"), "a");
287   EXPECT_EQ(TrimLeading(" aaaa"), "aaaa");
288   EXPECT_EQ(TrimLeading(" aaaaa     "), "aaaaa     ");
289 }
290 
TEST(StringUtilsTest,Base64Encode)291 TEST(StringUtilsTest, Base64Encode) {
292   auto base64_encode = [](const std::string& str) {
293     return Base64Encode(str.c_str(), str.size());
294   };
295 
296   EXPECT_EQ(base64_encode(""), "");
297   EXPECT_EQ(base64_encode("f"), "Zg==");
298   EXPECT_EQ(base64_encode("fo"), "Zm8=");
299   EXPECT_EQ(base64_encode("foo"), "Zm9v");
300   EXPECT_EQ(base64_encode("foob"), "Zm9vYg==");
301   EXPECT_EQ(base64_encode("fooba"), "Zm9vYmE=");
302   EXPECT_EQ(base64_encode("foobar"), "Zm9vYmFy");
303 
304   EXPECT_EQ(Base64Encode("foo\0bar", 7), "Zm9vAGJhcg==");
305 
306   std::vector<uint8_t> buffer = {0x04, 0x53, 0x42, 0x35,
307                                  0x32, 0xFF, 0x00, 0xFE};
308   EXPECT_EQ(Base64Encode(buffer.data(), buffer.size()), "BFNCNTL/AP4=");
309 
310   buffer = {0xfb, 0xf0, 0x3e, 0x07, 0xfc};
311   EXPECT_EQ(Base64Encode(buffer.data(), buffer.size()), "+/A+B/w=");
312 }
313 
314 }  // namespace
315 }  // namespace base
316 }  // namespace perfetto
317