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 | |
Tom Cherry | f16b55c | 2017-10-26 10:50:20 -0700 | [diff] [blame] | 54 | TEST(strings, split_with_trailing_empty_part) { |
| 55 | std::vector<std::string> parts = android::base::Split("foo,bar,", ","); |
| 56 | ASSERT_EQ(3U, parts.size()); |
| 57 | ASSERT_EQ("foo", parts[0]); |
| 58 | ASSERT_EQ("bar", parts[1]); |
| 59 | ASSERT_EQ("", parts[2]); |
| 60 | } |
| 61 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 62 | TEST(strings, split_null_char) { |
| 63 | std::vector<std::string> parts = |
| 64 | android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1)); |
| 65 | ASSERT_EQ(2U, parts.size()); |
| 66 | ASSERT_EQ("foo", parts[0]); |
| 67 | ASSERT_EQ("bar", parts[1]); |
| 68 | } |
| 69 | |
| 70 | TEST(strings, split_any) { |
| 71 | std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:"); |
| 72 | ASSERT_EQ(3U, parts.size()); |
| 73 | ASSERT_EQ("foo", parts[0]); |
| 74 | ASSERT_EQ("bar", parts[1]); |
| 75 | ASSERT_EQ("baz", parts[2]); |
| 76 | } |
| 77 | |
| 78 | TEST(strings, split_any_with_empty_part) { |
| 79 | std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 80 | ASSERT_EQ(3U, parts.size()); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 81 | ASSERT_EQ("foo", parts[0]); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame] | 82 | ASSERT_EQ("", parts[1]); |
| 83 | ASSERT_EQ("bar", parts[2]); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(strings, trim_empty) { |
| 87 | ASSERT_EQ("", android::base::Trim("")); |
| 88 | } |
| 89 | |
| 90 | TEST(strings, trim_already_trimmed) { |
| 91 | ASSERT_EQ("foo", android::base::Trim("foo")); |
| 92 | } |
| 93 | |
| 94 | TEST(strings, trim_left) { |
| 95 | ASSERT_EQ("foo", android::base::Trim(" foo")); |
| 96 | } |
| 97 | |
| 98 | TEST(strings, trim_right) { |
| 99 | ASSERT_EQ("foo", android::base::Trim("foo ")); |
| 100 | } |
| 101 | |
| 102 | TEST(strings, trim_both) { |
| 103 | ASSERT_EQ("foo", android::base::Trim(" foo ")); |
| 104 | } |
| 105 | |
| 106 | TEST(strings, trim_no_trim_middle) { |
| 107 | ASSERT_EQ("foo bar", android::base::Trim("foo bar")); |
| 108 | } |
| 109 | |
| 110 | TEST(strings, trim_other_whitespace) { |
| 111 | ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f")); |
| 112 | } |
| 113 | |
| 114 | TEST(strings, join_nothing) { |
| 115 | std::vector<std::string> list = {}; |
| 116 | ASSERT_EQ("", android::base::Join(list, ',')); |
| 117 | } |
| 118 | |
| 119 | TEST(strings, join_single) { |
| 120 | std::vector<std::string> list = {"foo"}; |
| 121 | ASSERT_EQ("foo", android::base::Join(list, ',')); |
| 122 | } |
| 123 | |
| 124 | TEST(strings, join_simple) { |
| 125 | std::vector<std::string> list = {"foo", "bar", "baz"}; |
| 126 | ASSERT_EQ("foo,bar,baz", android::base::Join(list, ',')); |
| 127 | } |
| 128 | |
| 129 | TEST(strings, join_separator_in_vector) { |
| 130 | std::vector<std::string> list = {",", ","}; |
| 131 | ASSERT_EQ(",,,", android::base::Join(list, ',')); |
| 132 | } |
| 133 | |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 134 | TEST(strings, join_simple_ints) { |
| 135 | std::set<int> list = {1, 2, 3}; |
| 136 | ASSERT_EQ("1,2,3", android::base::Join(list, ',')); |
| 137 | } |
| 138 | |
| 139 | TEST(strings, join_unordered_set) { |
| 140 | std::unordered_set<int> list = {1, 2}; |
| 141 | ASSERT_TRUE("1,2" == android::base::Join(list, ',') || |
| 142 | "2,1" == android::base::Join(list, ',')); |
| 143 | } |
| 144 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 145 | TEST(strings, StartsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 146 | ASSERT_FALSE(android::base::StartsWith("", "foo")); |
| 147 | ASSERT_TRUE(android::base::StartsWith("", "")); |
| 148 | } |
| 149 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 150 | TEST(strings, StartsWithIgnoreCase_empty) { |
| 151 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo")); |
| 152 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("", "")); |
| 153 | } |
| 154 | |
| 155 | TEST(strings, StartsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 156 | ASSERT_TRUE(android::base::StartsWith("foo", "")); |
| 157 | ASSERT_TRUE(android::base::StartsWith("foo", "f")); |
| 158 | ASSERT_TRUE(android::base::StartsWith("foo", "fo")); |
| 159 | ASSERT_TRUE(android::base::StartsWith("foo", "foo")); |
| 160 | } |
| 161 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 162 | TEST(strings, StartsWithIgnoreCase_simple) { |
| 163 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "")); |
| 164 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f")); |
| 165 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F")); |
| 166 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo")); |
| 167 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO")); |
| 168 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo")); |
| 169 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO")); |
| 170 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo")); |
| 171 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO")); |
| 172 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo")); |
| 173 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO")); |
| 174 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo")); |
| 175 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO")); |
| 176 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo")); |
| 177 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO")); |
| 178 | } |
| 179 | |
| 180 | TEST(strings, StartsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 181 | ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); |
| 182 | } |
| 183 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 184 | TEST(strings, StartsWithIgnoreCase_prefix_too_long) { |
| 185 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar")); |
| 186 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR")); |
| 187 | } |
| 188 | |
| 189 | TEST(strings, StartsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 190 | ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); |
| 191 | ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); |
| 192 | } |
| 193 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 194 | TEST(strings, StartsWithIgnoreCase_contains_prefix) { |
| 195 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba")); |
| 196 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA")); |
| 197 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar")); |
| 198 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); |
| 199 | } |
| 200 | |
| 201 | TEST(strings, EndsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 202 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 203 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 204 | } |
| 205 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 206 | TEST(strings, EndsWithIgnoreCase_empty) { |
| 207 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo")); |
| 208 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO")); |
| 209 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("", "")); |
| 210 | } |
| 211 | |
| 212 | TEST(strings, EndsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 213 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 214 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 215 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 216 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 217 | } |
| 218 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 219 | TEST(strings, EndsWithIgnoreCase_simple) { |
| 220 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "")); |
| 221 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o")); |
| 222 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O")); |
| 223 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo")); |
| 224 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO")); |
| 225 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo")); |
| 226 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO")); |
| 227 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo")); |
| 228 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO")); |
| 229 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo")); |
| 230 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO")); |
| 231 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo")); |
| 232 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO")); |
| 233 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo")); |
| 234 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO")); |
| 235 | } |
| 236 | |
| 237 | TEST(strings, EndsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 238 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 239 | } |
| 240 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 241 | TEST(strings, EndsWithIgnoreCase_prefix_too_long) { |
| 242 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar")); |
| 243 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR")); |
| 244 | } |
| 245 | |
| 246 | TEST(strings, EndsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 247 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 248 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 249 | } |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 250 | |
| 251 | TEST(strings, EndsWithIgnoreCase_contains_prefix) { |
| 252 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); |
| 253 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); |
| 254 | } |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 255 | |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame^] | 256 | TEST(strings, StartsWith_std_string) { |
| 257 | ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"})); |
| 258 | ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"})); |
| 259 | } |
| 260 | |
| 261 | TEST(strings, StartsWithIgnoreCase_std_string) { |
| 262 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"})); |
| 263 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"})); |
| 264 | } |
| 265 | |
| 266 | TEST(strings, EndsWith_std_string) { |
| 267 | ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"})); |
| 268 | ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"})); |
| 269 | } |
| 270 | |
| 271 | TEST(strings, EndsWithIgnoreCase_std_string) { |
| 272 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"})); |
| 273 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); |
| 274 | } |
| 275 | |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 276 | TEST(strings, EqualsIgnoreCase) { |
| 277 | ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); |
| 278 | ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); |
| 279 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar")); |
| 280 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool")); |
| 281 | } |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 282 | |
| 283 | TEST(strings, ubsan_28729303) { |
| 284 | android::base::Split("/dev/null", ":"); |
| 285 | } |