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 | |
Yi-Yo Chiang | 5ad131f | 2021-11-09 19:48:09 +0800 | [diff] [blame] | 86 | TEST(strings, tokenize_empty) { |
| 87 | std::vector<std::string> parts = android::base::Tokenize("", " "); |
| 88 | ASSERT_EQ(0U, parts.size()); |
| 89 | } |
| 90 | |
| 91 | TEST(strings, tokenize_all_delimiter) { |
| 92 | std::vector<std::string> parts = android::base::Tokenize(" \t ", " \t"); |
| 93 | ASSERT_EQ(0U, parts.size()); |
| 94 | } |
| 95 | |
| 96 | TEST(strings, tokenize_trivial) { |
| 97 | std::vector<std::string> parts = android::base::Tokenize("foo", "\t"); |
| 98 | ASSERT_EQ(1U, parts.size()); |
| 99 | ASSERT_EQ("foo", parts[0]); |
| 100 | } |
| 101 | |
| 102 | TEST(strings, tokenize_single) { |
| 103 | std::vector<std::string> parts = android::base::Tokenize("foo\t", "\t"); |
| 104 | ASSERT_EQ(1U, parts.size()); |
| 105 | ASSERT_EQ("foo", parts[0]); |
| 106 | } |
| 107 | |
| 108 | TEST(strings, tokenize_simple) { |
| 109 | std::vector<std::string> parts = android::base::Tokenize("foo bar baz", " "); |
| 110 | ASSERT_EQ(3U, parts.size()); |
| 111 | ASSERT_EQ("foo", parts[0]); |
| 112 | ASSERT_EQ("bar", parts[1]); |
| 113 | ASSERT_EQ("baz", parts[2]); |
| 114 | } |
| 115 | |
| 116 | TEST(strings, tokenize_any) { |
| 117 | std::vector<std::string> parts = android::base::Tokenize("foo \tbar\t\t baz", " \t"); |
| 118 | ASSERT_EQ(3U, parts.size()); |
| 119 | ASSERT_EQ("foo", parts[0]); |
| 120 | ASSERT_EQ("bar", parts[1]); |
| 121 | ASSERT_EQ("baz", parts[2]); |
| 122 | } |
| 123 | |
| 124 | TEST(strings, tokenize_beginning_trailing_delimiters) { |
| 125 | std::vector<std::string> parts = android::base::Tokenize(" foo bar baz \t", " \t"); |
| 126 | ASSERT_EQ(3U, parts.size()); |
| 127 | ASSERT_EQ("foo", parts[0]); |
| 128 | ASSERT_EQ("bar", parts[1]); |
| 129 | ASSERT_EQ("baz", parts[2]); |
| 130 | } |
| 131 | |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 132 | TEST(strings, trim_empty) { |
| 133 | ASSERT_EQ("", android::base::Trim("")); |
| 134 | } |
| 135 | |
| 136 | TEST(strings, trim_already_trimmed) { |
| 137 | ASSERT_EQ("foo", android::base::Trim("foo")); |
| 138 | } |
| 139 | |
| 140 | TEST(strings, trim_left) { |
| 141 | ASSERT_EQ("foo", android::base::Trim(" foo")); |
| 142 | } |
| 143 | |
| 144 | TEST(strings, trim_right) { |
| 145 | ASSERT_EQ("foo", android::base::Trim("foo ")); |
| 146 | } |
| 147 | |
| 148 | TEST(strings, trim_both) { |
| 149 | ASSERT_EQ("foo", android::base::Trim(" foo ")); |
| 150 | } |
| 151 | |
| 152 | TEST(strings, trim_no_trim_middle) { |
| 153 | ASSERT_EQ("foo bar", android::base::Trim("foo bar")); |
| 154 | } |
| 155 | |
| 156 | TEST(strings, trim_other_whitespace) { |
| 157 | ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f")); |
| 158 | } |
| 159 | |
| 160 | TEST(strings, join_nothing) { |
| 161 | std::vector<std::string> list = {}; |
| 162 | ASSERT_EQ("", android::base::Join(list, ',')); |
| 163 | } |
| 164 | |
| 165 | TEST(strings, join_single) { |
| 166 | std::vector<std::string> list = {"foo"}; |
| 167 | ASSERT_EQ("foo", android::base::Join(list, ',')); |
| 168 | } |
| 169 | |
| 170 | TEST(strings, join_simple) { |
| 171 | std::vector<std::string> list = {"foo", "bar", "baz"}; |
| 172 | ASSERT_EQ("foo,bar,baz", android::base::Join(list, ',')); |
| 173 | } |
| 174 | |
| 175 | TEST(strings, join_separator_in_vector) { |
| 176 | std::vector<std::string> list = {",", ","}; |
| 177 | ASSERT_EQ(",,,", android::base::Join(list, ',')); |
| 178 | } |
| 179 | |
Dan Albert | 0d20cc9 | 2015-05-21 18:37:36 -0700 | [diff] [blame] | 180 | TEST(strings, join_simple_ints) { |
| 181 | std::set<int> list = {1, 2, 3}; |
| 182 | ASSERT_EQ("1,2,3", android::base::Join(list, ',')); |
| 183 | } |
| 184 | |
| 185 | TEST(strings, join_unordered_set) { |
| 186 | std::unordered_set<int> list = {1, 2}; |
| 187 | ASSERT_TRUE("1,2" == android::base::Join(list, ',') || |
| 188 | "2,1" == android::base::Join(list, ',')); |
| 189 | } |
| 190 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 191 | TEST(strings, StartsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 192 | ASSERT_FALSE(android::base::StartsWith("", "foo")); |
| 193 | ASSERT_TRUE(android::base::StartsWith("", "")); |
| 194 | } |
| 195 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 196 | TEST(strings, StartsWithIgnoreCase_empty) { |
| 197 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("", "foo")); |
| 198 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("", "")); |
| 199 | } |
| 200 | |
| 201 | TEST(strings, StartsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 202 | ASSERT_TRUE(android::base::StartsWith("foo", "")); |
| 203 | ASSERT_TRUE(android::base::StartsWith("foo", "f")); |
| 204 | ASSERT_TRUE(android::base::StartsWith("foo", "fo")); |
| 205 | ASSERT_TRUE(android::base::StartsWith("foo", "foo")); |
| 206 | } |
| 207 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 208 | TEST(strings, StartsWithIgnoreCase_simple) { |
| 209 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "")); |
| 210 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "f")); |
| 211 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "F")); |
| 212 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fo")); |
| 213 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fO")); |
| 214 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Fo")); |
| 215 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FO")); |
| 216 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foo")); |
| 217 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "foO")); |
| 218 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOo")); |
| 219 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "fOO")); |
| 220 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "Foo")); |
| 221 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FoO")); |
| 222 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOo")); |
| 223 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("foo", "FOO")); |
| 224 | } |
| 225 | |
| 226 | TEST(strings, StartsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 227 | ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); |
| 228 | } |
| 229 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 230 | TEST(strings, StartsWithIgnoreCase_prefix_too_long) { |
| 231 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "foobar")); |
| 232 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foo", "FOOBAR")); |
| 233 | } |
| 234 | |
| 235 | TEST(strings, StartsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 236 | ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); |
| 237 | ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); |
| 238 | } |
| 239 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 240 | TEST(strings, StartsWithIgnoreCase_contains_prefix) { |
| 241 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "oba")); |
| 242 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "OBA")); |
| 243 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "bar")); |
| 244 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("foobar", "BAR")); |
| 245 | } |
| 246 | |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 247 | TEST(strings, StartsWith_char) { |
| 248 | ASSERT_FALSE(android::base::StartsWith("", 'f')); |
| 249 | ASSERT_TRUE(android::base::StartsWith("foo", 'f')); |
| 250 | ASSERT_FALSE(android::base::StartsWith("foo", 'o')); |
| 251 | } |
| 252 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 253 | TEST(strings, EndsWith_empty) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 254 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 255 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 256 | } |
| 257 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 258 | TEST(strings, EndsWithIgnoreCase_empty) { |
| 259 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "foo")); |
| 260 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("", "FOO")); |
| 261 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("", "")); |
| 262 | } |
| 263 | |
| 264 | TEST(strings, EndsWith_simple) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 265 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 266 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 267 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 268 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 269 | } |
| 270 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 271 | TEST(strings, EndsWithIgnoreCase_simple) { |
| 272 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "")); |
| 273 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "o")); |
| 274 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "O")); |
| 275 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oo")); |
| 276 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "oO")); |
| 277 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Oo")); |
| 278 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "OO")); |
| 279 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foo")); |
| 280 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "foO")); |
| 281 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOo")); |
| 282 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "fOO")); |
| 283 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "Foo")); |
| 284 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FoO")); |
| 285 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOo")); |
| 286 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("foo", "FOO")); |
| 287 | } |
| 288 | |
| 289 | TEST(strings, EndsWith_prefix_too_long) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 290 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 291 | } |
| 292 | |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 293 | TEST(strings, EndsWithIgnoreCase_prefix_too_long) { |
| 294 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "foobar")); |
| 295 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foo", "FOOBAR")); |
| 296 | } |
| 297 | |
| 298 | TEST(strings, EndsWith_contains_prefix) { |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 299 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 300 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 301 | } |
Elliott Hughes | 8548427 | 2016-10-25 14:56:04 -0700 | [diff] [blame] | 302 | |
| 303 | TEST(strings, EndsWithIgnoreCase_contains_prefix) { |
| 304 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); |
| 305 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); |
| 306 | } |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 307 | |
Elliott Hughes | 9955af2 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 308 | TEST(strings, StartsWith_std_string) { |
| 309 | ASSERT_TRUE(android::base::StartsWith("hello", std::string{"hell"})); |
| 310 | ASSERT_FALSE(android::base::StartsWith("goodbye", std::string{"hell"})); |
| 311 | } |
| 312 | |
| 313 | TEST(strings, StartsWithIgnoreCase_std_string) { |
| 314 | ASSERT_TRUE(android::base::StartsWithIgnoreCase("HeLlO", std::string{"hell"})); |
| 315 | ASSERT_FALSE(android::base::StartsWithIgnoreCase("GoOdByE", std::string{"hell"})); |
| 316 | } |
| 317 | |
| 318 | TEST(strings, EndsWith_std_string) { |
| 319 | ASSERT_TRUE(android::base::EndsWith("hello", std::string{"lo"})); |
| 320 | ASSERT_FALSE(android::base::EndsWith("goodbye", std::string{"lo"})); |
| 321 | } |
| 322 | |
| 323 | TEST(strings, EndsWithIgnoreCase_std_string) { |
| 324 | ASSERT_TRUE(android::base::EndsWithIgnoreCase("HeLlO", std::string{"lo"})); |
| 325 | ASSERT_FALSE(android::base::EndsWithIgnoreCase("GoOdByE", std::string{"lo"})); |
| 326 | } |
| 327 | |
Yabin Cui | a87b388 | 2018-10-30 15:49:53 -0700 | [diff] [blame] | 328 | TEST(strings, EndsWith_char) { |
| 329 | ASSERT_FALSE(android::base::EndsWith("", 'o')); |
| 330 | ASSERT_TRUE(android::base::EndsWith("foo", 'o')); |
| 331 | ASSERT_FALSE(android::base::EndsWith("foo", "f")); |
| 332 | } |
| 333 | |
Elliott Hughes | 7872ff3 | 2017-01-13 18:51:32 -0800 | [diff] [blame] | 334 | TEST(strings, EqualsIgnoreCase) { |
| 335 | ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); |
| 336 | ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); |
| 337 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar")); |
| 338 | ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool")); |
| 339 | } |
Elliott Hughes | b226479 | 2017-02-07 15:30:24 -0800 | [diff] [blame] | 340 | |
| 341 | TEST(strings, ubsan_28729303) { |
| 342 | android::base::Split("/dev/null", ":"); |
| 343 | } |
Elliott Hughes | 26a40e8 | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 344 | |
| 345 | TEST(strings, ConsumePrefix) { |
| 346 | std::string_view s{"foo.bar"}; |
| 347 | ASSERT_FALSE(android::base::ConsumePrefix(&s, "bar.")); |
| 348 | ASSERT_EQ("foo.bar", s); |
| 349 | ASSERT_TRUE(android::base::ConsumePrefix(&s, "foo.")); |
| 350 | ASSERT_EQ("bar", s); |
| 351 | } |
| 352 | |
| 353 | TEST(strings, ConsumeSuffix) { |
| 354 | std::string_view s{"foo.bar"}; |
| 355 | ASSERT_FALSE(android::base::ConsumeSuffix(&s, ".foo")); |
| 356 | ASSERT_EQ("foo.bar", s); |
| 357 | ASSERT_TRUE(android::base::ConsumeSuffix(&s, ".bar")); |
| 358 | ASSERT_EQ("foo", s); |
| 359 | } |
Elliott Hughes | 80ac17b | 2019-11-18 16:01:21 -0800 | [diff] [blame] | 360 | |
| 361 | TEST(strings, StringReplace_false) { |
| 362 | // No change. |
| 363 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", false)); |
| 364 | ASSERT_EQ("", android::base::StringReplace("", "z", "Z", false)); |
| 365 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", false)); |
| 366 | |
| 367 | // Equal lengths. |
| 368 | ASSERT_EQ("Abcabc", android::base::StringReplace("abcabc", "a", "A", false)); |
| 369 | ASSERT_EQ("aBcabc", android::base::StringReplace("abcabc", "b", "B", false)); |
| 370 | ASSERT_EQ("abCabc", android::base::StringReplace("abcabc", "c", "C", false)); |
| 371 | |
| 372 | // Longer replacement. |
| 373 | ASSERT_EQ("foobcabc", android::base::StringReplace("abcabc", "a", "foo", false)); |
| 374 | ASSERT_EQ("afoocabc", android::base::StringReplace("abcabc", "b", "foo", false)); |
| 375 | ASSERT_EQ("abfooabc", android::base::StringReplace("abcabc", "c", "foo", false)); |
| 376 | |
| 377 | // Shorter replacement. |
| 378 | ASSERT_EQ("xxyz", android::base::StringReplace("abcxyz", "abc", "x", false)); |
| 379 | ASSERT_EQ("axyz", android::base::StringReplace("abcxyz", "bcx", "x", false)); |
| 380 | ASSERT_EQ("abcx", android::base::StringReplace("abcxyz", "xyz", "x", false)); |
| 381 | } |
| 382 | |
| 383 | TEST(strings, StringReplace_true) { |
| 384 | // No change. |
| 385 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "z", "Z", true)); |
| 386 | ASSERT_EQ("", android::base::StringReplace("", "z", "Z", true)); |
| 387 | ASSERT_EQ("abcabc", android::base::StringReplace("abcabc", "", "Z", true)); |
| 388 | |
| 389 | // Equal lengths. |
| 390 | ASSERT_EQ("AbcAbc", android::base::StringReplace("abcabc", "a", "A", true)); |
| 391 | ASSERT_EQ("aBcaBc", android::base::StringReplace("abcabc", "b", "B", true)); |
| 392 | ASSERT_EQ("abCabC", android::base::StringReplace("abcabc", "c", "C", true)); |
| 393 | |
| 394 | // Longer replacement. |
| 395 | ASSERT_EQ("foobcfoobc", android::base::StringReplace("abcabc", "a", "foo", true)); |
| 396 | ASSERT_EQ("afoocafooc", android::base::StringReplace("abcabc", "b", "foo", true)); |
| 397 | ASSERT_EQ("abfooabfoo", android::base::StringReplace("abcabc", "c", "foo", true)); |
| 398 | |
| 399 | // Shorter replacement. |
| 400 | ASSERT_EQ("xxyzx", android::base::StringReplace("abcxyzabc", "abc", "x", true)); |
| 401 | ASSERT_EQ("<xx>", android::base::StringReplace("<abcabc>", "abc", "x", true)); |
| 402 | } |
Colin Cross | 4bea655 | 2021-12-21 13:54:40 -0800 | [diff] [blame^] | 403 | |
| 404 | TEST(strings, ErrnoNumberAsString) { |
| 405 | EXPECT_EQ("No such file or directory", android::base::ErrnoNumberAsString(ENOENT)); |
| 406 | } |