Dan Albert | 00716d7 | 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 | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/strings.h" |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <unordered_set> |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 25 | |
| 26 | TEST(strings, split_empty) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 27 | std::vector<std::string> parts = android::base::Split("", ","); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 28 | ASSERT_EQ(1U, parts.size()); |
| 29 | ASSERT_EQ("", parts[0]); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | TEST(strings, split_single) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 33 | std::vector<std::string> parts = android::base::Split("foo", ","); |
Dan Albert | 00716d7 | 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 | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 39 | std::vector<std::string> parts = android::base::Split("foo,bar,baz", ","); |
Dan Albert | 00716d7 | 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 | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 47 | std::vector<std::string> parts = android::base::Split("foo,,bar", ","); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 48 | ASSERT_EQ(3U, parts.size()); |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 49 | ASSERT_EQ("foo", parts[0]); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 50 | ASSERT_EQ("", parts[1]); |
| 51 | ASSERT_EQ("bar", parts[2]); |
Dan Albert | 0d716d0 | 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 | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 72 | ASSERT_EQ(3U, parts.size()); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 73 | ASSERT_EQ("foo", parts[0]); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 74 | ASSERT_EQ("", parts[1]); |
| 75 | ASSERT_EQ("bar", parts[2]); |
Dan Albert | 00716d7 | 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 | 0d20cc9 | 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 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 137 | TEST(strings, StartsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 138 | ASSERT_FALSE(android::base::StartsWith("", "foo")); |
| 139 | ASSERT_TRUE(android::base::StartsWith("", "")); |
| 140 | } |
| 141 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 142 | TEST(strings, StartsWithIgnoreCase_empty) { |
| 143 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo")); |
| 144 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("", "")); |
| 145 | } |
| 146 | |
| 147 | TEST(strings, StartsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 148 | ASSERT_TRUE(android::base::StartsWith("foo", "")); |
| 149 | ASSERT_TRUE(android::base::StartsWith("foo", "f")); |
| 150 | ASSERT_TRUE(android::base::StartsWith("foo", "fo")); |
| 151 | ASSERT_TRUE(android::base::StartsWith("foo", "foo")); |
| 152 | } |
| 153 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 154 | TEST(strings, StartsWithIgnoreCase_simple) { |
| 155 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "")); |
| 156 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f")); |
| 157 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F")); |
| 158 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo")); |
| 159 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO")); |
| 160 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo")); |
| 161 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO")); |
| 162 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo")); |
| 163 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO")); |
| 164 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo")); |
| 165 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO")); |
| 166 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo")); |
| 167 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO")); |
| 168 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo")); |
| 169 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO")); |
| 170 | } |
| 171 | |
| 172 | TEST(strings, StartsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 173 | ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); |
| 174 | } |
| 175 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 176 | TEST(strings, StartsWithIgnoreCase_prefix_too_long) { |
| 177 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar")); |
| 178 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR")); |
| 179 | } |
| 180 | |
| 181 | TEST(strings, StartsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 182 | ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); |
| 183 | ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); |
| 184 | } |
| 185 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 186 | TEST(strings, StartsWithIgnoreCase_contains_prefix) { |
| 187 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba")); |
| 188 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA")); |
| 189 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar")); |
| 190 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); |
| 191 | } |
| 192 | |
| 193 | TEST(strings, EndsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 194 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 195 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 196 | } |
| 197 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 198 | TEST(strings, EndsWithIgnoreCase_empty) { |
| 199 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo")); |
| 200 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO")); |
| 201 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("", "")); |
| 202 | } |
| 203 | |
| 204 | TEST(strings, EndsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 205 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 206 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 207 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 208 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 209 | } |
| 210 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 211 | TEST(strings, EndsWithIgnoreCase_simple) { |
| 212 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "")); |
| 213 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o")); |
| 214 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O")); |
| 215 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo")); |
| 216 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO")); |
| 217 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo")); |
| 218 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO")); |
| 219 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo")); |
| 220 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO")); |
| 221 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo")); |
| 222 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO")); |
| 223 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo")); |
| 224 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO")); |
| 225 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo")); |
| 226 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO")); |
| 227 | } |
| 228 | |
| 229 | TEST(strings, EndsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 230 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 231 | } |
| 232 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 233 | TEST(strings, EndsWithIgnoreCase_prefix_too_long) { |
| 234 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar")); |
| 235 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR")); |
| 236 | } |
| 237 | |
| 238 | TEST(strings, EndsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 239 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 240 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 241 | } |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame^] | 242 | |
| 243 | TEST(strings, EndsWithIgnoreCase_contains_prefix) { |
| 244 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); |
| 245 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); |
| 246 | } |