blob: 5f675750c5ae114080170624cfa4b8ede4eabedc [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
17#include "base/strings.h"
18
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
Dan Albert00716d72015-03-13 22:57:40 -0700137TEST(strings, startswith_empty) {
138 ASSERT_FALSE(android::base::StartsWith("", "foo"));
139 ASSERT_TRUE(android::base::StartsWith("", ""));
140}
141
142TEST(strings, startswith_simple) {
143 ASSERT_TRUE(android::base::StartsWith("foo", ""));
144 ASSERT_TRUE(android::base::StartsWith("foo", "f"));
145 ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
146 ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
147}
148
149TEST(strings, startswith_prefix_too_long) {
150 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
151}
152
153TEST(strings, startswith_contains_prefix) {
154 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
155 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
156}
157
158TEST(strings, endswith_empty) {
159 ASSERT_FALSE(android::base::EndsWith("", "foo"));
160 ASSERT_TRUE(android::base::EndsWith("", ""));
161}
162
163TEST(strings, endswith_simple) {
164 ASSERT_TRUE(android::base::EndsWith("foo", ""));
165 ASSERT_TRUE(android::base::EndsWith("foo", "o"));
166 ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
167 ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
168}
169
170TEST(strings, endswith_prefix_too_long) {
171 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
172}
173
174TEST(strings, endswith_contains_prefix) {
175 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
176 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
177}