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 | |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 201 | TEST(strings, StartsWith_char) { |
| 202 | ASSERT_FALSE(android::base::StartsWith("", 'f')); |
| 203 | ASSERT_TRUE(android::base::StartsWith("foo", 'f')); |
| 204 | ASSERT_FALSE(android::base::StartsWith("foo", 'o')); |
| 205 | } |
| 206 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 207 | TEST(strings, EndsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 208 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 209 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 210 | } |
| 211 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 212 | TEST(strings, EndsWithIgnoreCase_empty) { |
| 213 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo")); |
| 214 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO")); |
| 215 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("", "")); |
| 216 | } |
| 217 | |
| 218 | TEST(strings, EndsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 219 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 220 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 221 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 222 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 223 | } |
| 224 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 225 | TEST(strings, EndsWithIgnoreCase_simple) { |
| 226 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "")); |
| 227 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o")); |
| 228 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O")); |
| 229 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo")); |
| 230 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO")); |
| 231 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo")); |
| 232 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO")); |
| 233 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo")); |
| 234 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO")); |
| 235 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo")); |
| 236 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO")); |
| 237 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo")); |
| 238 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO")); |
| 239 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo")); |
| 240 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO")); |
| 241 | } |
| 242 | |
| 243 | TEST(strings, EndsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 244 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 245 | } |
| 246 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 247 | TEST(strings, EndsWithIgnoreCase_prefix_too_long) { |
| 248 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar")); |
| 249 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR")); |
| 250 | } |
| 251 | |
| 252 | TEST(strings, EndsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 253 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 254 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 255 | } |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 256 | |
| 257 | TEST(strings, EndsWithIgnoreCase_contains_prefix) { |
| 258 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); |
| 259 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); |
| 260 | } |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 261 | |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 262 | TEST(strings, StartsWith_std_string) { |
| 263 | ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"})); |
| 264 | ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"})); |
| 265 | } |
| 266 | |
| 267 | TEST(strings, StartsWithIgnoreCase_std_string) { |
| 268 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"})); |
| 269 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"})); |
| 270 | } |
| 271 | |
| 272 | TEST(strings, EndsWith_std_string) { |
| 273 | ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"})); |
| 274 | ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"})); |
| 275 | } |
| 276 | |
| 277 | TEST(strings, EndsWithIgnoreCase_std_string) { |
| 278 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"})); |
| 279 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); |
| 280 | } |
| 281 | |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 282 | TEST(strings, EndsWith_char) { |
| 283 | ASSERT_FALSE(android::base::EndsWith("", 'o')); |
| 284 | ASSERT_TRUE(android::base::EndsWith("foo", 'o')); |
| 285 | ASSERT_FALSE(android::base::EndsWith("foo", "f")); |
| 286 | } |
| 287 | |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 288 | TEST(strings, EqualsIgnoreCase) { |
| 289 | ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); |
| 290 | ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); |
| 291 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar")); |
| 292 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool")); |
| 293 | } |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 294 | |
| 295 | TEST(strings, ubsan_28729303) { |
| 296 | android::base::Split("/dev/null", ":"); |
| 297 | } |
Elliott Hughes | 26a40e8 | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 298 | |
| 299 | TEST(strings, ConsumePrefix) { |
| 300 | std::string_view s{"foo.bar"}; |
| 301 | ASSERT_FALSE(android::base::ConsumePrefix(&s, "bar.")); |
| 302 | ASSERT_EQ("foo.bar", s); |
| 303 | ASSERT_TRUE(android::base::ConsumePrefix(&s, "foo.")); |
| 304 | ASSERT_EQ("bar", s); |
| 305 | } |
| 306 | |
| 307 | TEST(strings, ConsumeSuffix) { |
| 308 | std::string_view s{"foo.bar"}; |
| 309 | ASSERT_FALSE(android::base::ConsumeSuffix(&s, ".foo")); |
| 310 | ASSERT_EQ("foo.bar", s); |
| 311 | ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar")); |
| 312 | ASSERT_EQ("foo", s); |
| 313 | } |
Elliott Hughes | 80ac17b | 2019-11-18 16:01:21 -0800 | [diff] [blame] | 314 | |
| 315 | TEST(strings, StringReplace_false) { |
| 316 | // No change. |
| 317 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false)); |
| 318 | ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false)); |
| 319 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false)); |
| 320 | |
| 321 | // Equal lengths. |
| 322 | ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false)); |
| 323 | ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false)); |
| 324 | ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false)); |
| 325 | |
| 326 | // Longer replacement. |
| 327 | ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false)); |
| 328 | ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false)); |
| 329 | ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false)); |
| 330 | |
| 331 | // Shorter replacement. |
| 332 | ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false)); |
| 333 | ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false)); |
| 334 | ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false)); |
| 335 | } |
| 336 | |
| 337 | TEST(strings, StringReplace_true) { |
| 338 | // No change. |
| 339 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true)); |
| 340 | ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true)); |
| 341 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true)); |
| 342 | |
| 343 | // Equal lengths. |
| 344 | ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true)); |
| 345 | ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true)); |
| 346 | ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true)); |
| 347 | |
| 348 | // Longer replacement. |
| 349 | ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true)); |
| 350 | ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true)); |
| 351 | ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true)); |
| 352 | |
| 353 | // Shorter replacement. |
| 354 | ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true)); |
| 355 | ASSERT_EQ("<xx>", android::base::StringReplace("<abcabc>", "abc", "x", true)); |
| 356 | } |