blob: 5ae30944645661f354d18248364f4a6f6fb563f0 [file] [log] [blame]
Dan Albert00716d72015-03-13 22:57:40 -07001/*
2 * Copyright (C) 2015 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
Elliott Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/strings.h"
Dan Albert00716d72015-03-13 22:57:40 -070018
19#include <gtest/gtest.h>
20
21#include <string>
22#include <vector>
Dan Albert0d20cc92015-05-21 18:37:36 -070023#include <set>
24#include <unordered_set>
Dan Albert00716d72015-03-13 22:57:40 -070025
26TEST(strings, split_empty) {
Dan Albert0d716d02015-03-19 13:24:26 -070027 std::vector<std::string> parts = android::base::Split("", ",");
Elliott Hughes9964efc2015-04-24 23:02:00 -070028 ASSERT_EQ(1U, parts.size());
29 ASSERT_EQ("", parts[0]);
Dan Albert00716d72015-03-13 22:57:40 -070030}
31
32TEST(strings, split_single) {
Dan Albert0d716d02015-03-19 13:24:26 -070033 std::vector<std::string> parts = android::base::Split("foo", ",");
Dan Albert00716d72015-03-13 22:57:40 -070034 ASSERT_EQ(1U, parts.size());
35 ASSERT_EQ("foo", parts[0]);
36}
37
38TEST(strings, split_simple) {
Dan Albert0d716d02015-03-19 13:24:26 -070039 std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
Dan Albert00716d72015-03-13 22:57:40 -070040 ASSERT_EQ(3U, parts.size());
41 ASSERT_EQ("foo", parts[0]);
42 ASSERT_EQ("bar", parts[1]);
43 ASSERT_EQ("baz", parts[2]);
44}
45
46TEST(strings, split_with_empty_part) {
Dan Albert0d716d02015-03-19 13:24:26 -070047 std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
Elliott Hughes9964efc2015-04-24 23:02:00 -070048 ASSERT_EQ(3U, parts.size());
Dan Albert0d716d02015-03-19 13:24:26 -070049 ASSERT_EQ("foo", parts[0]);
Elliott Hughes9964efc2015-04-24 23:02:00 -070050 ASSERT_EQ("", parts[1]);
51 ASSERT_EQ("bar", parts[2]);
Dan Albert0d716d02015-03-19 13:24:26 -070052}
53
Tom Cherryf16b55c2017-10-26 10:50:20 -070054TEST(strings, split_with_trailing_empty_part) {
55 std::vector<std::string> parts = android::base::Split("foo,bar,", ",");
56 ASSERT_EQ(3U, parts.size());
57 ASSERT_EQ("foo", parts[0]);
58 ASSERT_EQ("bar", parts[1]);
59 ASSERT_EQ("", parts[2]);
60}
61
Dan Albert0d716d02015-03-19 13:24:26 -070062TEST(strings, split_null_char) {
63 std::vector<std::string> parts =
64 android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
65 ASSERT_EQ(2U, parts.size());
66 ASSERT_EQ("foo", parts[0]);
67 ASSERT_EQ("bar", parts[1]);
68}
69
70TEST(strings, split_any) {
71 std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
72 ASSERT_EQ(3U, parts.size());
73 ASSERT_EQ("foo", parts[0]);
74 ASSERT_EQ("bar", parts[1]);
75 ASSERT_EQ("baz", parts[2]);
76}
77
78TEST(strings, split_any_with_empty_part) {
79 std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
Elliott Hughes9964efc2015-04-24 23:02:00 -070080 ASSERT_EQ(3U, parts.size());
Dan Albert00716d72015-03-13 22:57:40 -070081 ASSERT_EQ("foo", parts[0]);
Elliott Hughes9964efc2015-04-24 23:02:00 -070082 ASSERT_EQ("", parts[1]);
83 ASSERT_EQ("bar", parts[2]);
Dan Albert00716d72015-03-13 22:57:40 -070084}
85
86TEST(strings, trim_empty) {
87 ASSERT_EQ("", android::base::Trim(""));
88}
89
90TEST(strings, trim_already_trimmed) {
91 ASSERT_EQ("foo", android::base::Trim("foo"));
92}
93
94TEST(strings, trim_left) {
95 ASSERT_EQ("foo", android::base::Trim(" foo"));
96}
97
98TEST(strings, trim_right) {
99 ASSERT_EQ("foo", android::base::Trim("foo "));
100}
101
102TEST(strings, trim_both) {
103 ASSERT_EQ("foo", android::base::Trim(" foo "));
104}
105
106TEST(strings, trim_no_trim_middle) {
107 ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
108}
109
110TEST(strings, trim_other_whitespace) {
111 ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
112}
113
114TEST(strings, join_nothing) {
115 std::vector<std::string> list = {};
116 ASSERT_EQ("", android::base::Join(list, ','));
117}
118
119TEST(strings, join_single) {
120 std::vector<std::string> list = {"foo"};
121 ASSERT_EQ("foo", android::base::Join(list, ','));
122}
123
124TEST(strings, join_simple) {
125 std::vector<std::string> list = {"foo", "bar", "baz"};
126 ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
127}
128
129TEST(strings, join_separator_in_vector) {
130 std::vector<std::string> list = {",", ","};
131 ASSERT_EQ(",,,", android::base::Join(list, ','));
132}
133
Dan Albert0d20cc92015-05-21 18:37:36 -0700134TEST(strings, join_simple_ints) {
135 std::set<int> list = {1, 2, 3};
136 ASSERT_EQ("1,2,3", android::base::Join(list, ','));
137}
138
139TEST(strings, join_unordered_set) {
140 std::unordered_set<int> list = {1, 2};
141 ASSERT_TRUE("1,2" == android::base::Join(list, ',') ||
142 "2,1" == android::base::Join(list, ','));
143}
144
Elliott Hughes85484272016-10-25 14:56:04 -0700145TEST(strings, StartsWith_empty) {
Dan Albert00716d72015-03-13 22:57:40 -0700146 ASSERT_FALSE(android::base::StartsWith("", "foo"));
147 ASSERT_TRUE(android::base::StartsWith("", ""));
148}
149
Elliott Hughes85484272016-10-25 14:56:04 -0700150TEST(strings, StartsWithIgnoreCase_empty) {
151 ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo"));
152 ASSERT_TRUE(android::base::StartsWithIgnoreCase("", ""));
153}
154
155TEST(strings, StartsWith_simple) {
Dan Albert00716d72015-03-13 22:57:40 -0700156 ASSERT_TRUE(android::base::StartsWith("foo", ""));
157 ASSERT_TRUE(android::base::StartsWith("foo", "f"));
158 ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
159 ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
160}
161
Elliott Hughes85484272016-10-25 14:56:04 -0700162TEST(strings, StartsWithIgnoreCase_simple) {
163 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", ""));
164 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f"));
165 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F"));
166 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo"));
167 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO"));
168 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo"));
169 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO"));
170 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo"));
171 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO"));
172 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo"));
173 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO"));
174 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo"));
175 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO"));
176 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo"));
177 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO"));
178}
179
180TEST(strings, StartsWith_prefix_too_long) {
Dan Albert00716d72015-03-13 22:57:40 -0700181 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
182}
183
Elliott Hughes85484272016-10-25 14:56:04 -0700184TEST(strings, StartsWithIgnoreCase_prefix_too_long) {
185 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar"));
186 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR"));
187}
188
189TEST(strings, StartsWith_contains_prefix) {
Dan Albert00716d72015-03-13 22:57:40 -0700190 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
191 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
192}
193
Elliott Hughes85484272016-10-25 14:56:04 -0700194TEST(strings, StartsWithIgnoreCase_contains_prefix) {
195 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba"));
196 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA"));
197 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar"));
198 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR"));
199}
200
Yabin Cuia87b3882018-10-30 15:49:53 -0700201TEST(strings, StartsWith_char) {
202 ASSERT_FALSE(android::base::StartsWith("", 'f'));
203 ASSERT_TRUE(android::base::StartsWith("foo", 'f'));
204 ASSERT_FALSE(android::base::StartsWith("foo", 'o'));
205}
206
Elliott Hughes85484272016-10-25 14:56:04 -0700207TEST(strings, EndsWith_empty) {
Dan Albert00716d72015-03-13 22:57:40 -0700208 ASSERT_FALSE(android::base::EndsWith("", "foo"));
209 ASSERT_TRUE(android::base::EndsWith("", ""));
210}
211
Elliott Hughes85484272016-10-25 14:56:04 -0700212TEST(strings, EndsWithIgnoreCase_empty) {
213 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo"));
214 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO"));
215 ASSERT_TRUE(android::base::EndsWithIgnoreCase("", ""));
216}
217
218TEST(strings, EndsWith_simple) {
Dan Albert00716d72015-03-13 22:57:40 -0700219 ASSERT_TRUE(android::base::EndsWith("foo", ""));
220 ASSERT_TRUE(android::base::EndsWith("foo", "o"));
221 ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
222 ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
223}
224
Elliott Hughes85484272016-10-25 14:56:04 -0700225TEST(strings, EndsWithIgnoreCase_simple) {
226 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", ""));
227 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o"));
228 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O"));
229 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo"));
230 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO"));
231 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo"));
232 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO"));
233 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo"));
234 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO"));
235 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo"));
236 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO"));
237 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo"));
238 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO"));
239 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo"));
240 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO"));
241}
242
243TEST(strings, EndsWith_prefix_too_long) {
Dan Albert00716d72015-03-13 22:57:40 -0700244 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
245}
246
Elliott Hughes85484272016-10-25 14:56:04 -0700247TEST(strings, EndsWithIgnoreCase_prefix_too_long) {
248 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar"));
249 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR"));
250}
251
252TEST(strings, EndsWith_contains_prefix) {
Dan Albert00716d72015-03-13 22:57:40 -0700253 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
254 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
255}
Elliott Hughes85484272016-10-25 14:56:04 -0700256
257TEST(strings, EndsWithIgnoreCase_contains_prefix) {
258 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA"));
259 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
260}
Elliott Hughes7872ff32017-01-13 18:51:32 -0800261
Elliott Hughes9955af22017-12-20 09:41:00 -0800262TEST(strings, StartsWith_std_string) {
263 ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"}));
264 ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"}));
265}
266
267TEST(strings, StartsWithIgnoreCase_std_string) {
268 ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"}));
269 ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"}));
270}
271
272TEST(strings, EndsWith_std_string) {
273 ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"}));
274 ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"}));
275}
276
277TEST(strings, EndsWithIgnoreCase_std_string) {
278 ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"}));
279 ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"}));
280}
281
Yabin Cuia87b3882018-10-30 15:49:53 -0700282TEST(strings, EndsWith_char) {
283 ASSERT_FALSE(android::base::EndsWith("", 'o'));
284 ASSERT_TRUE(android::base::EndsWith("foo", 'o'));
285 ASSERT_FALSE(android::base::EndsWith("foo", "f"));
286}
287
Elliott Hughes7872ff32017-01-13 18:51:32 -0800288TEST(strings, EqualsIgnoreCase) {
289 ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
290 ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));
291 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar"));
292 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool"));
293}
Elliott Hughesb2264792017-02-07 15:30:24 -0800294
295TEST(strings, ubsan_28729303) {
296 android::base::Split("/dev/null", ":");
297}
Elliott Hughes26a40e82019-05-03 09:02:45 -0700298
299TEST(strings, ConsumePrefix) {
300 std::string_view s{"foo.bar"};
301 ASSERT_FALSE(android::base::ConsumePrefix(&s, "bar."));
302 ASSERT_EQ("foo.bar", s);
303 ASSERT_TRUE(android::base::ConsumePrefix(&s, "foo."));
304 ASSERT_EQ("bar", s);
305}
306
307TEST(strings, ConsumeSuffix) {
308 std::string_view s{"foo.bar"};
309 ASSERT_FALSE(android::base::ConsumeSuffix(&s, ".foo"));
310 ASSERT_EQ("foo.bar", s);
311 ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar"));
312 ASSERT_EQ("foo", s);
313}
Elliott Hughes80ac17b2019-11-18 16:01:21 -0800314
315TEST(strings, StringReplace_false) {
316 // No change.
317 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false));
318 ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false));
319 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false));
320
321 // Equal lengths.
322 ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false));
323 ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false));
324 ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false));
325
326 // Longer replacement.
327 ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false));
328 ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false));
329 ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false));
330
331 // Shorter replacement.
332 ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false));
333 ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false));
334 ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false));
335}
336
337TEST(strings, StringReplace_true) {
338 // No change.
339 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true));
340 ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true));
341 ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true));
342
343 // Equal lengths.
344 ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true));
345 ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true));
346 ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true));
347
348 // Longer replacement.
349 ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true));
350 ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true));
351 ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true));
352
353 // Shorter replacement.
354 ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true));
355 ASSERT_EQ("<xx>", android::base::StringReplace("<abcabc>", "abc", "x", true));
356}