• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/strings.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <string>
22 #include <vector>
23 
TEST(strings,split_empty)24 TEST(strings, split_empty) {
25   std::vector<std::string> parts = android::base::Split("", ",");
26   ASSERT_EQ(1U, parts.size());
27   ASSERT_EQ("", parts[0]);
28 }
29 
TEST(strings,split_single)30 TEST(strings, split_single) {
31   std::vector<std::string> parts = android::base::Split("foo", ",");
32   ASSERT_EQ(1U, parts.size());
33   ASSERT_EQ("foo", parts[0]);
34 }
35 
TEST(strings,split_simple)36 TEST(strings, split_simple) {
37   std::vector<std::string> parts = android::base::Split("foo,bar,baz", ",");
38   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 
TEST(strings,split_with_empty_part)44 TEST(strings, split_with_empty_part) {
45   std::vector<std::string> parts = android::base::Split("foo,,bar", ",");
46   ASSERT_EQ(3U, parts.size());
47   ASSERT_EQ("foo", parts[0]);
48   ASSERT_EQ("", parts[1]);
49   ASSERT_EQ("bar", parts[2]);
50 }
51 
TEST(strings,split_null_char)52 TEST(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 
TEST(strings,split_any)60 TEST(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 
TEST(strings,split_any_with_empty_part)68 TEST(strings, split_any_with_empty_part) {
69   std::vector<std::string> parts = android::base::Split("foo:,bar", ",:");
70   ASSERT_EQ(3U, parts.size());
71   ASSERT_EQ("foo", parts[0]);
72   ASSERT_EQ("", parts[1]);
73   ASSERT_EQ("bar", parts[2]);
74 }
75 
TEST(strings,trim_empty)76 TEST(strings, trim_empty) {
77   ASSERT_EQ("", android::base::Trim(""));
78 }
79 
TEST(strings,trim_already_trimmed)80 TEST(strings, trim_already_trimmed) {
81   ASSERT_EQ("foo", android::base::Trim("foo"));
82 }
83 
TEST(strings,trim_left)84 TEST(strings, trim_left) {
85   ASSERT_EQ("foo", android::base::Trim(" foo"));
86 }
87 
TEST(strings,trim_right)88 TEST(strings, trim_right) {
89   ASSERT_EQ("foo", android::base::Trim("foo "));
90 }
91 
TEST(strings,trim_both)92 TEST(strings, trim_both) {
93   ASSERT_EQ("foo", android::base::Trim(" foo "));
94 }
95 
TEST(strings,trim_no_trim_middle)96 TEST(strings, trim_no_trim_middle) {
97   ASSERT_EQ("foo bar", android::base::Trim("foo bar"));
98 }
99 
TEST(strings,trim_other_whitespace)100 TEST(strings, trim_other_whitespace) {
101   ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f"));
102 }
103 
TEST(strings,join_nothing)104 TEST(strings, join_nothing) {
105   std::vector<std::string> list = {};
106   ASSERT_EQ("", android::base::Join(list, ','));
107 }
108 
TEST(strings,join_single)109 TEST(strings, join_single) {
110   std::vector<std::string> list = {"foo"};
111   ASSERT_EQ("foo", android::base::Join(list, ','));
112 }
113 
TEST(strings,join_simple)114 TEST(strings, join_simple) {
115   std::vector<std::string> list = {"foo", "bar", "baz"};
116   ASSERT_EQ("foo,bar,baz", android::base::Join(list, ','));
117 }
118 
TEST(strings,join_separator_in_vector)119 TEST(strings, join_separator_in_vector) {
120   std::vector<std::string> list = {",", ","};
121   ASSERT_EQ(",,,", android::base::Join(list, ','));
122 }
123 
TEST(strings,startswith_empty)124 TEST(strings, startswith_empty) {
125   ASSERT_FALSE(android::base::StartsWith("", "foo"));
126   ASSERT_TRUE(android::base::StartsWith("", ""));
127 }
128 
TEST(strings,startswith_simple)129 TEST(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 
TEST(strings,startswith_prefix_too_long)136 TEST(strings, startswith_prefix_too_long) {
137   ASSERT_FALSE(android::base::StartsWith("foo", "foobar"));
138 }
139 
TEST(strings,startswith_contains_prefix)140 TEST(strings, startswith_contains_prefix) {
141   ASSERT_FALSE(android::base::StartsWith("foobar", "oba"));
142   ASSERT_FALSE(android::base::StartsWith("foobar", "bar"));
143 }
144 
TEST(strings,endswith_empty)145 TEST(strings, endswith_empty) {
146   ASSERT_FALSE(android::base::EndsWith("", "foo"));
147   ASSERT_TRUE(android::base::EndsWith("", ""));
148 }
149 
TEST(strings,endswith_simple)150 TEST(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 
TEST(strings,endswith_prefix_too_long)157 TEST(strings, endswith_prefix_too_long) {
158   ASSERT_FALSE(android::base::EndsWith("foo", "foobar"));
159 }
160 
TEST(strings,endswith_contains_prefix)161 TEST(strings, endswith_contains_prefix) {
162   ASSERT_FALSE(android::base::EndsWith("foobar", "oba"));
163   ASSERT_FALSE(android::base::EndsWith("foobar", "foo"));
164 }
165