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