| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/strings.h" | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 18 |  | 
 | 19 | #include <gtest/gtest.h> | 
 | 20 |  | 
 | 21 | #include <string> | 
 | 22 | #include <vector> | 
| Dan Albert | e0da8a1 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 23 | #include <set> | 
 | 24 | #include <unordered_set> | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 25 |  | 
 | 26 | TEST(strings, split_empty) { | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 27 |   std::vector<std::string> parts = android::base::Split("", ","); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 28 |   ASSERT_EQ(1U, parts.size()); | 
 | 29 |   ASSERT_EQ("", parts[0]); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 30 | } | 
 | 31 |  | 
 | 32 | TEST(strings, split_single) { | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 33 |   std::vector<std::string> parts = android::base::Split("foo", ","); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 34 |   ASSERT_EQ(1U, parts.size()); | 
 | 35 |   ASSERT_EQ("foo", parts[0]); | 
 | 36 | } | 
 | 37 |  | 
 | 38 | TEST(strings, split_simple) { | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 39 |   std::vector<std::string> parts = android::base::Split("foo,bar,baz", ","); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 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 |  | 
 | 46 | TEST(strings, split_with_empty_part) { | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 47 |   std::vector<std::string> parts = android::base::Split("foo,,bar", ","); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 48 |   ASSERT_EQ(3U, parts.size()); | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 49 |   ASSERT_EQ("foo", parts[0]); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 50 |   ASSERT_EQ("", parts[1]); | 
 | 51 |   ASSERT_EQ("bar", parts[2]); | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 52 | } | 
 | 53 |  | 
 | 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 |  | 
 | 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 |  | 
 | 70 | TEST(strings, split_any_with_empty_part) { | 
 | 71 |   std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 72 |   ASSERT_EQ(3U, parts.size()); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 73 |   ASSERT_EQ("foo", parts[0]); | 
| Elliott Hughes | 8d5fa6d | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 74 |   ASSERT_EQ("", parts[1]); | 
 | 75 |   ASSERT_EQ("bar", parts[2]); | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 76 | } | 
 | 77 |  | 
 | 78 | TEST(strings, trim_empty) { | 
 | 79 |   ASSERT_EQ("", android::base::Trim("")); | 
 | 80 | } | 
 | 81 |  | 
 | 82 | TEST(strings, trim_already_trimmed) { | 
 | 83 |   ASSERT_EQ("foo", android::base::Trim("foo")); | 
 | 84 | } | 
 | 85 |  | 
 | 86 | TEST(strings, trim_left) { | 
 | 87 |   ASSERT_EQ("foo", android::base::Trim(" foo")); | 
 | 88 | } | 
 | 89 |  | 
 | 90 | TEST(strings, trim_right) { | 
 | 91 |   ASSERT_EQ("foo", android::base::Trim("foo ")); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | TEST(strings, trim_both) { | 
 | 95 |   ASSERT_EQ("foo", android::base::Trim(" foo ")); | 
 | 96 | } | 
 | 97 |  | 
 | 98 | TEST(strings, trim_no_trim_middle) { | 
 | 99 |   ASSERT_EQ("foo bar", android::base::Trim("foo bar")); | 
 | 100 | } | 
 | 101 |  | 
 | 102 | TEST(strings, trim_other_whitespace) { | 
 | 103 |   ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f")); | 
 | 104 | } | 
 | 105 |  | 
 | 106 | TEST(strings, join_nothing) { | 
 | 107 |   std::vector<std::string> list = {}; | 
 | 108 |   ASSERT_EQ("", android::base::Join(list, ',')); | 
 | 109 | } | 
 | 110 |  | 
 | 111 | TEST(strings, join_single) { | 
 | 112 |   std::vector<std::string> list = {"foo"}; | 
 | 113 |   ASSERT_EQ("foo", android::base::Join(list, ',')); | 
 | 114 | } | 
 | 115 |  | 
 | 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 |  | 
 | 121 | TEST(strings, join_separator_in_vector) { | 
 | 122 |   std::vector<std::string> list = {",", ","}; | 
 | 123 |   ASSERT_EQ(",,,", android::base::Join(list, ',')); | 
 | 124 | } | 
 | 125 |  | 
| Dan Albert | e0da8a1 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 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 |  | 
 | 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 |  | 
| Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 137 | TEST(strings, startswith_empty) { | 
 | 138 |   ASSERT_FALSE(android::base::StartsWith("", "foo")); | 
 | 139 |   ASSERT_TRUE(android::base::StartsWith("", "")); | 
 | 140 | } | 
 | 141 |  | 
 | 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 |  | 
 | 149 | TEST(strings, startswith_prefix_too_long) { | 
 | 150 |   ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); | 
 | 151 | } | 
 | 152 |  | 
 | 153 | TEST(strings, startswith_contains_prefix) { | 
 | 154 |   ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); | 
 | 155 |   ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); | 
 | 156 | } | 
 | 157 |  | 
 | 158 | TEST(strings, endswith_empty) { | 
 | 159 |   ASSERT_FALSE(android::base::EndsWith("", "foo")); | 
 | 160 |   ASSERT_TRUE(android::base::EndsWith("", "")); | 
 | 161 | } | 
 | 162 |  | 
 | 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 |  | 
 | 170 | TEST(strings, endswith_prefix_too_long) { | 
 | 171 |   ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); | 
 | 172 | } | 
 | 173 |  | 
 | 174 | TEST(strings, endswith_contains_prefix) { | 
 | 175 |   ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); | 
 | 176 |   ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); | 
 | 177 | } |