1 /*
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 "android-base/strings.h"
18
19 #include <gtest/gtest.h>
20
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <unordered_set>
25
TEST(strings,split_empty)26 TEST(strings, split_empty) {
27 std::vector<std::string> parts = android::base::Split("", ",");
28 ASSERT_EQ(1U, parts.size());
29 ASSERT_EQ("", parts[0]);
30 }
31
TEST(strings,split_single)32 TEST(strings, split_single) {
33 std::vector<std::string> parts = android::base::Split("foo", ",");
34 ASSERT_EQ(1U, parts.size());
35 ASSERT_EQ("foo", parts[0]);
36 }
37
TEST(strings,split_simple)38 TEST(strings, split_simple) {
39 std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
40 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
TEST(strings,split_with_empty_part)46 TEST(strings, split_with_empty_part) {
47 std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
48 ASSERT_EQ(3U, parts.size());
49 ASSERT_EQ("foo", parts[0]);
50 ASSERT_EQ("", parts[1]);
51 ASSERT_EQ("bar", parts[2]);
52 }
53
TEST(strings,split_null_char)54 TEST(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
TEST(strings,split_any)62 TEST(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
TEST(strings,split_any_with_empty_part)70 TEST(strings, split_any_with_empty_part) {
71 std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
72 ASSERT_EQ(3U, parts.size());
73 ASSERT_EQ("foo", parts[0]);
74 ASSERT_EQ("", parts[1]);
75 ASSERT_EQ("bar", parts[2]);
76 }
77
TEST(strings,trim_empty)78 TEST(strings, trim_empty) {
79 ASSERT_EQ("", android::base::Trim(""));
80 }
81
TEST(strings,trim_already_trimmed)82 TEST(strings, trim_already_trimmed) {
83 ASSERT_EQ("foo", android::base::Trim("foo"));
84 }
85
TEST(strings,trim_left)86 TEST(strings, trim_left) {
87 ASSERT_EQ("foo", android::base::Trim(" foo"));
88 }
89
TEST(strings,trim_right)90 TEST(strings, trim_right) {
91 ASSERT_EQ("foo", android::base::Trim("foo "));
92 }
93
TEST(strings,trim_both)94 TEST(strings, trim_both) {
95 ASSERT_EQ("foo", android::base::Trim(" foo "));
96 }
97
TEST(strings,trim_no_trim_middle)98 TEST(strings, trim_no_trim_middle) {
99 ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
100 }
101
TEST(strings,trim_other_whitespace)102 TEST(strings, trim_other_whitespace) {
103 ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
104 }
105
TEST(strings,join_nothing)106 TEST(strings, join_nothing) {
107 std::vector<std::string> list = {};
108 ASSERT_EQ("", android::base::Join(list, ','));
109 }
110
TEST(strings,join_single)111 TEST(strings, join_single) {
112 std::vector<std::string> list = {"foo"};
113 ASSERT_EQ("foo", android::base::Join(list, ','));
114 }
115
TEST(strings,join_simple)116 TEST(strings, join_simple) {
117 std::vector<std::string> list = {"foo", "bar", "baz"};
118 ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
119 }
120
TEST(strings,join_separator_in_vector)121 TEST(strings, join_separator_in_vector) {
122 std::vector<std::string> list = {",", ","};
123 ASSERT_EQ(",,,", android::base::Join(list, ','));
124 }
125
TEST(strings,join_simple_ints)126 TEST(strings, join_simple_ints) {
127 std::set<int> list = {1, 2, 3};
128 ASSERT_EQ("1,2,3", android::base::Join(list, ','));
129 }
130
TEST(strings,join_unordered_set)131 TEST(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
TEST(strings,startswith_empty)137 TEST(strings, startswith_empty) {
138 ASSERT_FALSE(android::base::StartsWith("", "foo"));
139 ASSERT_TRUE(android::base::StartsWith("", ""));
140 }
141
TEST(strings,startswith_simple)142 TEST(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
TEST(strings,startswith_prefix_too_long)149 TEST(strings, startswith_prefix_too_long) {
150 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
151 }
152
TEST(strings,startswith_contains_prefix)153 TEST(strings, startswith_contains_prefix) {
154 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
155 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
156 }
157
TEST(strings,endswith_empty)158 TEST(strings, endswith_empty) {
159 ASSERT_FALSE(android::base::EndsWith("", "foo"));
160 ASSERT_TRUE(android::base::EndsWith("", ""));
161 }
162
TEST(strings,endswith_simple)163 TEST(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
TEST(strings,endswith_prefix_too_long)170 TEST(strings, endswith_prefix_too_long) {
171 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
172 }
173
TEST(strings,endswith_contains_prefix)174 TEST(strings, endswith_contains_prefix) {
175 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
176 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
177 }
178