blob: 46a1ab54374a70d97f4b366323193170e2ef2380 [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>
23
24TEST(strings, split_empty) {
Dan Albert0d716d02015-03-19 13:24:26 -070025 std::vector<std::string> parts = android::base::Split("", ",");
Elliott Hughes9964efc2015-04-24 23:02:00 -070026 ASSERT_EQ(1U, parts.size());
27 ASSERT_EQ("", parts[0]);
Dan Albert00716d72015-03-13 22:57:40 -070028}
29
30TEST(strings, split_single) {
Dan Albert0d716d02015-03-19 13:24:26 -070031 std::vector<std::string> parts = android::base::Split("foo", ",");
Dan Albert00716d72015-03-13 22:57:40 -070032 ASSERT_EQ(1U, parts.size());
33 ASSERT_EQ("foo", parts[0]);
34}
35
36TEST(strings, split_simple) {
Dan Albert0d716d02015-03-19 13:24:26 -070037 std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
Dan Albert00716d72015-03-13 22:57:40 -070038 ASSERT_EQ(3U, parts.size());
39 ASSERT_EQ("foo", parts[0]);
40 ASSERT_EQ("bar", parts[1]);
41 ASSERT_EQ("baz", parts[2]);
42}
43
44TEST(strings, split_with_empty_part) {
Dan Albert0d716d02015-03-19 13:24:26 -070045 std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
Elliott Hughes9964efc2015-04-24 23:02:00 -070046 ASSERT_EQ(3U, parts.size());
Dan Albert0d716d02015-03-19 13:24:26 -070047 ASSERT_EQ("foo", parts[0]);
Elliott Hughes9964efc2015-04-24 23:02:00 -070048 ASSERT_EQ("", parts[1]);
49 ASSERT_EQ("bar", parts[2]);
Dan Albert0d716d02015-03-19 13:24:26 -070050}
51
52TEST(strings, split_null_char) {
53 std::vector<std::string> parts =
54 android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1));
55 ASSERT_EQ(2U, parts.size());
56 ASSERT_EQ("foo", parts[0]);
57 ASSERT_EQ("bar", parts[1]);
58}
59
60TEST(strings, split_any) {
61 std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:");
62 ASSERT_EQ(3U, parts.size());
63 ASSERT_EQ("foo", parts[0]);
64 ASSERT_EQ("bar", parts[1]);
65 ASSERT_EQ("baz", parts[2]);
66}
67
68TEST(strings, split_any_with_empty_part) {
69 std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
Elliott Hughes9964efc2015-04-24 23:02:00 -070070 ASSERT_EQ(3U, parts.size());
Dan Albert00716d72015-03-13 22:57:40 -070071 ASSERT_EQ("foo", parts[0]);
Elliott Hughes9964efc2015-04-24 23:02:00 -070072 ASSERT_EQ("", parts[1]);
73 ASSERT_EQ("bar", parts[2]);
Dan Albert00716d72015-03-13 22:57:40 -070074}
75
76TEST(strings, trim_empty) {
77 ASSERT_EQ("", android::base::Trim(""));
78}
79
80TEST(strings, trim_already_trimmed) {
81 ASSERT_EQ("foo", android::base::Trim("foo"));
82}
83
84TEST(strings, trim_left) {
85 ASSERT_EQ("foo", android::base::Trim(" foo"));
86}
87
88TEST(strings, trim_right) {
89 ASSERT_EQ("foo", android::base::Trim("foo "));
90}
91
92TEST(strings, trim_both) {
93 ASSERT_EQ("foo", android::base::Trim(" foo "));
94}
95
96TEST(strings, trim_no_trim_middle) {
97 ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
98}
99
100TEST(strings, trim_other_whitespace) {
101 ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
102}
103
104TEST(strings, join_nothing) {
105 std::vector<std::string> list = {};
106 ASSERT_EQ("", android::base::Join(list, ','));
107}
108
109TEST(strings, join_single) {
110 std::vector<std::string> list = {"foo"};
111 ASSERT_EQ("foo", android::base::Join(list, ','));
112}
113
114TEST(strings, join_simple) {
115 std::vector<std::string> list = {"foo", "bar", "baz"};
116 ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
117}
118
119TEST(strings, join_separator_in_vector) {
120 std::vector<std::string> list = {",", ","};
121 ASSERT_EQ(",,,", android::base::Join(list, ','));
122}
123
124TEST(strings, startswith_empty) {
125 ASSERT_FALSE(android::base::StartsWith("", "foo"));
126 ASSERT_TRUE(android::base::StartsWith("", ""));
127}
128
129TEST(strings, startswith_simple) {
130 ASSERT_TRUE(android::base::StartsWith("foo", ""));
131 ASSERT_TRUE(android::base::StartsWith("foo", "f"));
132 ASSERT_TRUE(android::base::StartsWith("foo", "fo"));
133 ASSERT_TRUE(android::base::StartsWith("foo", "foo"));
134}
135
136TEST(strings, startswith_prefix_too_long) {
137 ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
138}
139
140TEST(strings, startswith_contains_prefix) {
141 ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
142 ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
143}
144
145TEST(strings, endswith_empty) {
146 ASSERT_FALSE(android::base::EndsWith("", "foo"));
147 ASSERT_TRUE(android::base::EndsWith("", ""));
148}
149
150TEST(strings, endswith_simple) {
151 ASSERT_TRUE(android::base::EndsWith("foo", ""));
152 ASSERT_TRUE(android::base::EndsWith("foo", "o"));
153 ASSERT_TRUE(android::base::EndsWith("foo", "oo"));
154 ASSERT_TRUE(android::base::EndsWith("foo", "foo"));
155}
156
157TEST(strings, endswith_prefix_too_long) {
158 ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
159}
160
161TEST(strings, endswith_contains_prefix) {
162 ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
163 ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
164}