blob: 7ed5b2b080e5ed552237c7d5224db5d858c8e623 [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
54TEST(strings, split_null_char) {
55 std::vector<std::string> parts =
56 android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
57 ASSERT_EQ(2U, parts.size());
58 ASSERT_EQ("foo", parts[0]);
59 ASSERT_EQ("bar", parts[1]);
60}
61
62TEST(strings, split_any) {
63 std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
64 ASSERT_EQ(3U, parts.size());
65 ASSERT_EQ("foo", parts[0]);
66 ASSERT_EQ("bar", parts[1]);
67 ASSERT_EQ("baz", parts[2]);
68}
69
70TEST(strings, split_any_with_empty_part) {
71 std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
Elliott Hughes9964efc2015-04-24 23:02:00 -070072 ASSERT_EQ(3U, parts.size());
Dan Albert00716d72015-03-13 22:57:40 -070073 ASSERT_EQ("foo", parts[0]);
Elliott Hughes9964efc2015-04-24 23:02:00 -070074 ASSERT_EQ("", parts[1]);
75 ASSERT_EQ("bar", parts[2]);
Dan Albert00716d72015-03-13 22:57:40 -070076}
77
78TEST(strings, trim_empty) {
79 ASSERT_EQ("", android::base::Trim(""));
80}
81
82TEST(strings, trim_already_trimmed) {
83 ASSERT_EQ("foo", android::base::Trim("foo"));
84}
85
86TEST(strings, trim_left) {
87 ASSERT_EQ("foo", android::base::Trim(" foo"));
88}
89
90TEST(strings, trim_right) {
91 ASSERT_EQ("foo", android::base::Trim("foo "));
92}
93
94TEST(strings, trim_both) {
95 ASSERT_EQ("foo", android::base::Trim(" foo "));
96}
97
98TEST(strings, trim_no_trim_middle) {
99 ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
100}
101
102TEST(strings, trim_other_whitespace) {
103 ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
104}
105
106TEST(strings, join_nothing) {
107 std::vector<std::string> list = {};
108 ASSERT_EQ("", android::base::Join(list, ','));
109}
110
111TEST(strings, join_single) {
112 std::vector<std::string> list = {"foo"};
113 ASSERT_EQ("foo", android::base::Join(list, ','));
114}
115
116TEST(strings, join_simple) {
117 std::vector<std::string> list = {"foo", "bar", "baz"};
118 ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
119}
120
121TEST(strings, join_separator_in_vector) {
122 std::vector<std::string> list = {",", ","};
123 ASSERT_EQ(",,,", android::base::Join(list, ','));
124}
125
Dan Albert0d20cc92015-05-21 18:37:36 -0700126TEST(strings, join_simple_ints) {
127 std::set<int> list = {1, 2, 3};
128 ASSERT_EQ("1,2,3", android::base::Join(list, ','));
129}
130
131TEST(strings, join_unordered_set) {
132 std::unordered_set<int> list = {1, 2};
133 ASSERT_TRUE("1,2" == android::base::Join(list, ',') ||
134 "2,1" == android::base::Join(list, ','));
135}
136
Elliott Hughes85484272016-10-25 14:56:04 -0700137TEST(strings, StartsWith_empty) {
Dan Albert00716d72015-03-13 22:57:40 -0700138 ASSERT_FALSE(android::base::StartsWith("", "foo"));
139 ASSERT_TRUE(android::base::StartsWith("", ""));
140}
141
Elliott Hughes85484272016-10-25 14:56:04 -0700142TEST(strings, StartsWithIgnoreCase_empty) {
143 ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo"));
144 ASSERT_TRUE(android::base::StartsWithIgnoreCase("", ""));
145}
146
147TEST(strings, StartsWith_simple) {
Dan Albert00716d72015-03-13 22:57:40 -0700148 ASSERT_TRUE(android::base::StartsWith("foo", ""));
149 ASSERT_TRUE(android::base::StartsWith("foo", "f"));
150 ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
151 ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
152}
153
Elliott Hughes85484272016-10-25 14:56:04 -0700154TEST(strings, StartsWithIgnoreCase_simple) {
155 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", ""));
156 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f"));
157 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F"));
158 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo"));
159 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO"));
160 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo"));
161 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO"));
162 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo"));
163 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO"));
164 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo"));
165 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO"));
166 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo"));
167 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO"));
168 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo"));
169 ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO"));
170}
171
172TEST(strings, StartsWith_prefix_too_long) {
Dan Albert00716d72015-03-13 22:57:40 -0700173 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
174}
175
Elliott Hughes85484272016-10-25 14:56:04 -0700176TEST(strings, StartsWithIgnoreCase_prefix_too_long) {
177 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar"));
178 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR"));
179}
180
181TEST(strings, StartsWith_contains_prefix) {
Dan Albert00716d72015-03-13 22:57:40 -0700182 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
183 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
184}
185
Elliott Hughes85484272016-10-25 14:56:04 -0700186TEST(strings, StartsWithIgnoreCase_contains_prefix) {
187 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba"));
188 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA"));
189 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar"));
190 ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR"));
191}
192
193TEST(strings, EndsWith_empty) {
Dan Albert00716d72015-03-13 22:57:40 -0700194 ASSERT_FALSE(android::base::EndsWith("", "foo"));
195 ASSERT_TRUE(android::base::EndsWith("", ""));
196}
197
Elliott Hughes85484272016-10-25 14:56:04 -0700198TEST(strings, EndsWithIgnoreCase_empty) {
199 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo"));
200 ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO"));
201 ASSERT_TRUE(android::base::EndsWithIgnoreCase("", ""));
202}
203
204TEST(strings, EndsWith_simple) {
Dan Albert00716d72015-03-13 22:57:40 -0700205 ASSERT_TRUE(android::base::EndsWith("foo", ""));
206 ASSERT_TRUE(android::base::EndsWith("foo", "o"));
207 ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
208 ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
209}
210
Elliott Hughes85484272016-10-25 14:56:04 -0700211TEST(strings, EndsWithIgnoreCase_simple) {
212 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", ""));
213 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o"));
214 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O"));
215 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo"));
216 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO"));
217 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo"));
218 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO"));
219 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo"));
220 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO"));
221 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo"));
222 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO"));
223 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo"));
224 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO"));
225 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo"));
226 ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO"));
227}
228
229TEST(strings, EndsWith_prefix_too_long) {
Dan Albert00716d72015-03-13 22:57:40 -0700230 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
231}
232
Elliott Hughes85484272016-10-25 14:56:04 -0700233TEST(strings, EndsWithIgnoreCase_prefix_too_long) {
234 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar"));
235 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR"));
236}
237
238TEST(strings, EndsWith_contains_prefix) {
Dan Albert00716d72015-03-13 22:57:40 -0700239 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
240 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
241}
Elliott Hughes85484272016-10-25 14:56:04 -0700242
243TEST(strings, EndsWithIgnoreCase_contains_prefix) {
244 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA"));
245 ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO"));
246}
Elliott Hughes7872ff32017-01-13 18:51:32 -0800247
248TEST(strings, EqualsIgnoreCase) {
249 ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO"));
250 ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo"));
251 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar"));
252 ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool"));
253}
Elliott Hughesb2264792017-02-07 15:30:24 -0800254
255TEST(strings, ubsan_28729303) {
256 android::base::Split("/dev/null", ":");
257}