1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/StringRef.h" 10 #include "llvm/ADT/Hashing.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/SmallVector.h" 13 #include "llvm/ADT/StringExtras.h" 14 #include "llvm/Support/Allocator.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include "gtest/gtest.h" 17 using namespace llvm; 18 19 namespace llvm { 20 21 std::ostream &operator<<(std::ostream &OS, const StringRef &S) { 22 OS << S.str(); 23 return OS; 24 } 25 26 std::ostream &operator<<(std::ostream &OS, 27 const std::pair<StringRef, StringRef> &P) { 28 OS << "(" << P.first << ", " << P.second << ")"; 29 return OS; 30 } 31 32 } 33 34 // Check that we can't accidentally assign a temporary std::string to a 35 // StringRef. (Unfortunately we can't make use of the same thing with 36 // constructors.) 37 static_assert( 38 !std::is_assignable<StringRef&, std::string>::value, 39 "Assigning from prvalue std::string"); 40 static_assert( 41 !std::is_assignable<StringRef&, std::string &&>::value, 42 "Assigning from xvalue std::string"); 43 static_assert( 44 std::is_assignable<StringRef&, std::string &>::value, 45 "Assigning from lvalue std::string"); 46 static_assert( 47 std::is_assignable<StringRef&, const char *>::value, 48 "Assigning from prvalue C string"); 49 static_assert( 50 std::is_assignable<StringRef&, const char * &&>::value, 51 "Assigning from xvalue C string"); 52 static_assert( 53 std::is_assignable<StringRef&, const char * &>::value, 54 "Assigning from lvalue C string"); 55 56 namespace { 57 TEST(StringRefTest, Construction) { 58 EXPECT_EQ("", StringRef()); 59 EXPECT_EQ("hello", StringRef("hello")); 60 EXPECT_EQ("hello", StringRef("hello world", 5)); 61 EXPECT_EQ("hello", StringRef(std::string("hello"))); 62 } 63 64 TEST(StringRefTest, EmptyInitializerList) { 65 StringRef S = {}; 66 EXPECT_TRUE(S.empty()); 67 68 S = {}; 69 EXPECT_TRUE(S.empty()); 70 } 71 72 TEST(StringRefTest, Iteration) { 73 StringRef S("hello"); 74 const char *p = "hello"; 75 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p) 76 EXPECT_EQ(*it, *p); 77 } 78 79 TEST(StringRefTest, StringOps) { 80 const char *p = "hello"; 81 EXPECT_EQ(p, StringRef(p, 0).data()); 82 EXPECT_TRUE(StringRef().empty()); 83 EXPECT_EQ((size_t) 5, StringRef("hello").size()); 84 EXPECT_EQ(-1, StringRef("aab").compare("aad")); 85 EXPECT_EQ( 0, StringRef("aab").compare("aab")); 86 EXPECT_EQ( 1, StringRef("aab").compare("aaa")); 87 EXPECT_EQ(-1, StringRef("aab").compare("aabb")); 88 EXPECT_EQ( 1, StringRef("aab").compare("aa")); 89 EXPECT_EQ( 1, StringRef("\xFF").compare("\1")); 90 91 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd")); 92 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab")); 93 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA")); 94 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb")); 95 EXPECT_EQ(-1, StringRef("AaB").compare_lower("bb")); 96 EXPECT_EQ( 1, StringRef("aaBb").compare_lower("AaB")); 97 EXPECT_EQ( 1, StringRef("bb").compare_lower("AaB")); 98 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA")); 99 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1")); 100 101 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad")); 102 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab")); 103 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa")); 104 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb")); 105 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa")); 106 EXPECT_EQ(-1, StringRef("1").compare_numeric("10")); 107 EXPECT_EQ( 0, StringRef("10").compare_numeric("10")); 108 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a")); 109 EXPECT_EQ( 1, StringRef("2").compare_numeric("1")); 110 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty")); 111 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1")); 112 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0")); 113 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16")); 114 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16")); 115 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0")); 116 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0")); 117 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0")); 118 } 119 120 TEST(StringRefTest, Operators) { 121 EXPECT_EQ("", StringRef()); 122 EXPECT_TRUE(StringRef("aab") < StringRef("aad")); 123 EXPECT_FALSE(StringRef("aab") < StringRef("aab")); 124 EXPECT_TRUE(StringRef("aab") <= StringRef("aab")); 125 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa")); 126 EXPECT_TRUE(StringRef("aad") > StringRef("aab")); 127 EXPECT_FALSE(StringRef("aab") > StringRef("aab")); 128 EXPECT_TRUE(StringRef("aab") >= StringRef("aab")); 129 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab")); 130 EXPECT_EQ(StringRef("aab"), StringRef("aab")); 131 EXPECT_FALSE(StringRef("aab") == StringRef("aac")); 132 EXPECT_FALSE(StringRef("aab") != StringRef("aab")); 133 EXPECT_TRUE(StringRef("aab") != StringRef("aac")); 134 EXPECT_EQ('a', StringRef("aab")[1]); 135 } 136 137 TEST(StringRefTest, Substr) { 138 StringRef Str("hello"); 139 EXPECT_EQ("lo", Str.substr(3)); 140 EXPECT_EQ("", Str.substr(100)); 141 EXPECT_EQ("hello", Str.substr(0, 100)); 142 EXPECT_EQ("o", Str.substr(4, 10)); 143 } 144 145 TEST(StringRefTest, Slice) { 146 StringRef Str("hello"); 147 EXPECT_EQ("l", Str.slice(2, 3)); 148 EXPECT_EQ("ell", Str.slice(1, 4)); 149 EXPECT_EQ("llo", Str.slice(2, 100)); 150 EXPECT_EQ("", Str.slice(2, 1)); 151 EXPECT_EQ("", Str.slice(10, 20)); 152 } 153 154 TEST(StringRefTest, Split) { 155 StringRef Str("hello"); 156 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")), 157 Str.split('X')); 158 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")), 159 Str.split('e')); 160 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")), 161 Str.split('h')); 162 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")), 163 Str.split('l')); 164 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")), 165 Str.split('o')); 166 167 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")), 168 Str.rsplit('X')); 169 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")), 170 Str.rsplit('e')); 171 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")), 172 Str.rsplit('h')); 173 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")), 174 Str.rsplit('l')); 175 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")), 176 Str.rsplit('o')); 177 178 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("o")), 179 Str.rsplit("ll")); 180 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")), 181 Str.rsplit("h")); 182 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")), 183 Str.rsplit("o")); 184 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")), 185 Str.rsplit("::")); 186 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")), 187 Str.rsplit("l")); 188 } 189 190 TEST(StringRefTest, Split2) { 191 SmallVector<StringRef, 5> parts; 192 SmallVector<StringRef, 5> expected; 193 194 expected.push_back("ab"); expected.push_back("c"); 195 StringRef(",ab,,c,").split(parts, ",", -1, false); 196 EXPECT_TRUE(parts == expected); 197 198 expected.clear(); parts.clear(); 199 expected.push_back(""); expected.push_back("ab"); expected.push_back(""); 200 expected.push_back("c"); expected.push_back(""); 201 StringRef(",ab,,c,").split(parts, ",", -1, true); 202 EXPECT_TRUE(parts == expected); 203 204 expected.clear(); parts.clear(); 205 expected.push_back(""); 206 StringRef("").split(parts, ",", -1, true); 207 EXPECT_TRUE(parts == expected); 208 209 expected.clear(); parts.clear(); 210 StringRef("").split(parts, ",", -1, false); 211 EXPECT_TRUE(parts == expected); 212 213 expected.clear(); parts.clear(); 214 StringRef(",").split(parts, ",", -1, false); 215 EXPECT_TRUE(parts == expected); 216 217 expected.clear(); parts.clear(); 218 expected.push_back(""); expected.push_back(""); 219 StringRef(",").split(parts, ",", -1, true); 220 EXPECT_TRUE(parts == expected); 221 222 expected.clear(); parts.clear(); 223 expected.push_back("a"); expected.push_back("b"); 224 StringRef("a,b").split(parts, ",", -1, true); 225 EXPECT_TRUE(parts == expected); 226 227 // Test MaxSplit 228 expected.clear(); parts.clear(); 229 expected.push_back("a,,b,c"); 230 StringRef("a,,b,c").split(parts, ",", 0, true); 231 EXPECT_TRUE(parts == expected); 232 233 expected.clear(); parts.clear(); 234 expected.push_back("a,,b,c"); 235 StringRef("a,,b,c").split(parts, ",", 0, false); 236 EXPECT_TRUE(parts == expected); 237 238 expected.clear(); parts.clear(); 239 expected.push_back("a"); expected.push_back(",b,c"); 240 StringRef("a,,b,c").split(parts, ",", 1, true); 241 EXPECT_TRUE(parts == expected); 242 243 expected.clear(); parts.clear(); 244 expected.push_back("a"); expected.push_back(",b,c"); 245 StringRef("a,,b,c").split(parts, ",", 1, false); 246 EXPECT_TRUE(parts == expected); 247 248 expected.clear(); parts.clear(); 249 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c"); 250 StringRef("a,,b,c").split(parts, ",", 2, true); 251 EXPECT_TRUE(parts == expected); 252 253 expected.clear(); parts.clear(); 254 expected.push_back("a"); expected.push_back("b,c"); 255 StringRef("a,,b,c").split(parts, ",", 2, false); 256 EXPECT_TRUE(parts == expected); 257 258 expected.clear(); parts.clear(); 259 expected.push_back("a"); expected.push_back(""); expected.push_back("b"); 260 expected.push_back("c"); 261 StringRef("a,,b,c").split(parts, ",", 3, true); 262 EXPECT_TRUE(parts == expected); 263 264 expected.clear(); parts.clear(); 265 expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); 266 StringRef("a,,b,c").split(parts, ",", 3, false); 267 EXPECT_TRUE(parts == expected); 268 269 expected.clear(); parts.clear(); 270 expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); 271 StringRef("a,,b,c").split(parts, ',', 3, false); 272 EXPECT_TRUE(parts == expected); 273 274 expected.clear(); parts.clear(); 275 expected.push_back(""); 276 StringRef().split(parts, ",", 0, true); 277 EXPECT_TRUE(parts == expected); 278 279 expected.clear(); parts.clear(); 280 expected.push_back(StringRef()); 281 StringRef("").split(parts, ",", 0, true); 282 EXPECT_TRUE(parts == expected); 283 284 expected.clear(); parts.clear(); 285 StringRef("").split(parts, ",", 0, false); 286 EXPECT_TRUE(parts == expected); 287 StringRef().split(parts, ",", 0, false); 288 EXPECT_TRUE(parts == expected); 289 290 expected.clear(); parts.clear(); 291 expected.push_back("a"); 292 expected.push_back(""); 293 expected.push_back("b"); 294 expected.push_back("c,d"); 295 StringRef("a,,b,c,d").split(parts, ",", 3, true); 296 EXPECT_TRUE(parts == expected); 297 298 expected.clear(); parts.clear(); 299 expected.push_back(""); 300 StringRef().split(parts, ',', 0, true); 301 EXPECT_TRUE(parts == expected); 302 303 expected.clear(); parts.clear(); 304 expected.push_back(StringRef()); 305 StringRef("").split(parts, ',', 0, true); 306 EXPECT_TRUE(parts == expected); 307 308 expected.clear(); parts.clear(); 309 StringRef("").split(parts, ',', 0, false); 310 EXPECT_TRUE(parts == expected); 311 StringRef().split(parts, ',', 0, false); 312 EXPECT_TRUE(parts == expected); 313 314 expected.clear(); parts.clear(); 315 expected.push_back("a"); 316 expected.push_back(""); 317 expected.push_back("b"); 318 expected.push_back("c,d"); 319 StringRef("a,,b,c,d").split(parts, ',', 3, true); 320 EXPECT_TRUE(parts == expected); 321 } 322 323 TEST(StringRefTest, Trim) { 324 StringRef Str0("hello"); 325 StringRef Str1(" hello "); 326 StringRef Str2(" hello "); 327 StringRef Str3("\t\n\v\f\r hello \t\n\v\f\r"); 328 329 EXPECT_EQ(StringRef("hello"), Str0.rtrim()); 330 EXPECT_EQ(StringRef(" hello"), Str1.rtrim()); 331 EXPECT_EQ(StringRef(" hello"), Str2.rtrim()); 332 EXPECT_EQ(StringRef("\t\n\v\f\r hello"), Str3.rtrim()); 333 EXPECT_EQ(StringRef("hello"), Str0.ltrim()); 334 EXPECT_EQ(StringRef("hello "), Str1.ltrim()); 335 EXPECT_EQ(StringRef("hello "), Str2.ltrim()); 336 EXPECT_EQ(StringRef("hello \t\n\v\f\r"), Str3.ltrim()); 337 EXPECT_EQ(StringRef("hello"), Str0.trim()); 338 EXPECT_EQ(StringRef("hello"), Str1.trim()); 339 EXPECT_EQ(StringRef("hello"), Str2.trim()); 340 EXPECT_EQ(StringRef("hello"), Str3.trim()); 341 342 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh")); 343 344 EXPECT_EQ(StringRef(""), StringRef("").trim()); 345 EXPECT_EQ(StringRef(""), StringRef(" ").trim()); 346 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim()); 347 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim()); 348 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0')); 349 } 350 351 TEST(StringRefTest, StartsWith) { 352 StringRef Str("hello"); 353 EXPECT_TRUE(Str.startswith("")); 354 EXPECT_TRUE(Str.startswith("he")); 355 EXPECT_FALSE(Str.startswith("helloworld")); 356 EXPECT_FALSE(Str.startswith("hi")); 357 } 358 359 TEST(StringRefTest, StartsWithLower) { 360 StringRef Str("heLLo"); 361 EXPECT_TRUE(Str.startswith_lower("")); 362 EXPECT_TRUE(Str.startswith_lower("he")); 363 EXPECT_TRUE(Str.startswith_lower("hell")); 364 EXPECT_TRUE(Str.startswith_lower("HELlo")); 365 EXPECT_FALSE(Str.startswith_lower("helloworld")); 366 EXPECT_FALSE(Str.startswith_lower("hi")); 367 } 368 369 TEST(StringRefTest, ConsumeFront) { 370 StringRef Str("hello"); 371 EXPECT_TRUE(Str.consume_front("")); 372 EXPECT_EQ("hello", Str); 373 EXPECT_TRUE(Str.consume_front("he")); 374 EXPECT_EQ("llo", Str); 375 EXPECT_FALSE(Str.consume_front("lloworld")); 376 EXPECT_EQ("llo", Str); 377 EXPECT_FALSE(Str.consume_front("lol")); 378 EXPECT_EQ("llo", Str); 379 EXPECT_TRUE(Str.consume_front("llo")); 380 EXPECT_EQ("", Str); 381 EXPECT_FALSE(Str.consume_front("o")); 382 EXPECT_TRUE(Str.consume_front("")); 383 } 384 385 TEST(StringRefTest, EndsWith) { 386 StringRef Str("hello"); 387 EXPECT_TRUE(Str.endswith("")); 388 EXPECT_TRUE(Str.endswith("lo")); 389 EXPECT_FALSE(Str.endswith("helloworld")); 390 EXPECT_FALSE(Str.endswith("worldhello")); 391 EXPECT_FALSE(Str.endswith("so")); 392 } 393 394 TEST(StringRefTest, EndsWithLower) { 395 StringRef Str("heLLo"); 396 EXPECT_TRUE(Str.endswith_lower("")); 397 EXPECT_TRUE(Str.endswith_lower("lo")); 398 EXPECT_TRUE(Str.endswith_lower("LO")); 399 EXPECT_TRUE(Str.endswith_lower("ELlo")); 400 EXPECT_FALSE(Str.endswith_lower("helloworld")); 401 EXPECT_FALSE(Str.endswith_lower("hi")); 402 } 403 404 TEST(StringRefTest, ConsumeBack) { 405 StringRef Str("hello"); 406 EXPECT_TRUE(Str.consume_back("")); 407 EXPECT_EQ("hello", Str); 408 EXPECT_TRUE(Str.consume_back("lo")); 409 EXPECT_EQ("hel", Str); 410 EXPECT_FALSE(Str.consume_back("helhel")); 411 EXPECT_EQ("hel", Str); 412 EXPECT_FALSE(Str.consume_back("hle")); 413 EXPECT_EQ("hel", Str); 414 EXPECT_TRUE(Str.consume_back("hel")); 415 EXPECT_EQ("", Str); 416 EXPECT_FALSE(Str.consume_back("h")); 417 EXPECT_TRUE(Str.consume_back("")); 418 } 419 420 TEST(StringRefTest, Find) { 421 StringRef Str("helloHELLO"); 422 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO"); 423 424 struct { 425 StringRef Str; 426 char C; 427 std::size_t From; 428 std::size_t Pos; 429 std::size_t LowerPos; 430 } CharExpectations[] = { 431 {Str, 'h', 0U, 0U, 0U}, 432 {Str, 'e', 0U, 1U, 1U}, 433 {Str, 'l', 0U, 2U, 2U}, 434 {Str, 'l', 3U, 3U, 3U}, 435 {Str, 'o', 0U, 4U, 4U}, 436 {Str, 'L', 0U, 7U, 2U}, 437 {Str, 'z', 0U, StringRef::npos, StringRef::npos}, 438 }; 439 440 struct { 441 StringRef Str; 442 llvm::StringRef S; 443 std::size_t From; 444 std::size_t Pos; 445 std::size_t LowerPos; 446 } StrExpectations[] = { 447 {Str, "helloword", 0, StringRef::npos, StringRef::npos}, 448 {Str, "hello", 0, 0U, 0U}, 449 {Str, "ello", 0, 1U, 1U}, 450 {Str, "zz", 0, StringRef::npos, StringRef::npos}, 451 {Str, "ll", 2U, 2U, 2U}, 452 {Str, "ll", 3U, StringRef::npos, 7U}, 453 {Str, "LL", 2U, 7U, 2U}, 454 {Str, "LL", 3U, 7U, 7U}, 455 {Str, "", 0U, 0U, 0U}, 456 {LongStr, "hello", 0U, 36U, 36U}, 457 {LongStr, "foo", 0U, 28U, 28U}, 458 {LongStr, "hell", 2U, 12U, 12U}, 459 {LongStr, "HELL", 2U, 42U, 12U}, 460 {LongStr, "", 0U, 0U, 0U}}; 461 462 for (auto &E : CharExpectations) { 463 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From)); 464 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From)); 465 EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From)); 466 } 467 468 for (auto &E : StrExpectations) { 469 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From)); 470 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From)); 471 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From)); 472 } 473 474 EXPECT_EQ(3U, Str.rfind('l')); 475 EXPECT_EQ(StringRef::npos, Str.rfind('z')); 476 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld")); 477 EXPECT_EQ(0U, Str.rfind("hello")); 478 EXPECT_EQ(1U, Str.rfind("ello")); 479 EXPECT_EQ(StringRef::npos, Str.rfind("zz")); 480 481 EXPECT_EQ(8U, Str.rfind_lower('l')); 482 EXPECT_EQ(8U, Str.rfind_lower('L')); 483 EXPECT_EQ(StringRef::npos, Str.rfind_lower('z')); 484 EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD")); 485 EXPECT_EQ(5U, Str.rfind("HELLO")); 486 EXPECT_EQ(6U, Str.rfind("ELLO")); 487 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ")); 488 489 EXPECT_EQ(2U, Str.find_first_of('l')); 490 EXPECT_EQ(1U, Str.find_first_of("el")); 491 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz")); 492 493 Str = "hello"; 494 EXPECT_EQ(1U, Str.find_first_not_of('h')); 495 EXPECT_EQ(4U, Str.find_first_not_of("hel")); 496 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello")); 497 498 EXPECT_EQ(3U, Str.find_last_not_of('o')); 499 EXPECT_EQ(1U, Str.find_last_not_of("lo")); 500 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo")); 501 } 502 503 TEST(StringRefTest, Count) { 504 StringRef Str("hello"); 505 EXPECT_EQ(2U, Str.count('l')); 506 EXPECT_EQ(1U, Str.count('o')); 507 EXPECT_EQ(0U, Str.count('z')); 508 EXPECT_EQ(0U, Str.count("helloworld")); 509 EXPECT_EQ(1U, Str.count("hello")); 510 EXPECT_EQ(1U, Str.count("ello")); 511 EXPECT_EQ(0U, Str.count("zz")); 512 } 513 514 TEST(StringRefTest, EditDistance) { 515 StringRef Hello("hello"); 516 EXPECT_EQ(2U, Hello.edit_distance("hill")); 517 518 StringRef Industry("industry"); 519 EXPECT_EQ(6U, Industry.edit_distance("interest")); 520 521 StringRef Soylent("soylent green is people"); 522 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green")); 523 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green", 524 /* allow replacements = */ false)); 525 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green", 526 /* allow replacements = */ true, 527 /* max edit distance = */ 8)); 528 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green " 529 "people soiled our green " 530 "people soiled our green ")); 531 } 532 533 TEST(StringRefTest, Misc) { 534 std::string Storage; 535 raw_string_ostream OS(Storage); 536 OS << StringRef("hello"); 537 EXPECT_EQ("hello", OS.str()); 538 } 539 540 TEST(StringRefTest, Hashing) { 541 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef())); 542 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef(""))); 543 std::string S = "hello world"; 544 hash_code H = hash_value(S); 545 EXPECT_EQ(H, hash_value(StringRef("hello world"))); 546 EXPECT_EQ(H, hash_value(StringRef(S))); 547 EXPECT_NE(H, hash_value(StringRef("hello worl"))); 548 EXPECT_EQ(hash_value(std::string("hello worl")), 549 hash_value(StringRef("hello worl"))); 550 EXPECT_NE(H, hash_value(StringRef("hello world "))); 551 EXPECT_EQ(hash_value(std::string("hello world ")), 552 hash_value(StringRef("hello world "))); 553 EXPECT_EQ(H, hash_value(StringRef("hello world\0"))); 554 EXPECT_NE(hash_value(std::string("ello worl")), 555 hash_value(StringRef("hello world").slice(1, -1))); 556 } 557 558 struct UnsignedPair { 559 const char *Str; 560 uint64_t Expected; 561 } Unsigned[] = 562 { {"0", 0} 563 , {"255", 255} 564 , {"256", 256} 565 , {"65535", 65535} 566 , {"65536", 65536} 567 , {"4294967295", 4294967295ULL} 568 , {"4294967296", 4294967296ULL} 569 , {"18446744073709551615", 18446744073709551615ULL} 570 , {"042", 34} 571 , {"0x42", 66} 572 , {"0b101010", 42} 573 }; 574 575 struct SignedPair { 576 const char *Str; 577 int64_t Expected; 578 } Signed[] = 579 { {"0", 0} 580 , {"-0", 0} 581 , {"127", 127} 582 , {"128", 128} 583 , {"-128", -128} 584 , {"-129", -129} 585 , {"32767", 32767} 586 , {"32768", 32768} 587 , {"-32768", -32768} 588 , {"-32769", -32769} 589 , {"2147483647", 2147483647LL} 590 , {"2147483648", 2147483648LL} 591 , {"-2147483648", -2147483648LL} 592 , {"-2147483649", -2147483649LL} 593 , {"-9223372036854775808", -(9223372036854775807LL) - 1} 594 , {"042", 34} 595 , {"0x42", 66} 596 , {"0b101010", 42} 597 , {"-042", -34} 598 , {"-0x42", -66} 599 , {"-0b101010", -42} 600 }; 601 602 TEST(StringRefTest, getAsInteger) { 603 uint8_t U8; 604 uint16_t U16; 605 uint32_t U32; 606 uint64_t U64; 607 608 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) { 609 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8); 610 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 611 ASSERT_FALSE(U8Success); 612 EXPECT_EQ(U8, Unsigned[i].Expected); 613 } else { 614 ASSERT_TRUE(U8Success); 615 } 616 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16); 617 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 618 ASSERT_FALSE(U16Success); 619 EXPECT_EQ(U16, Unsigned[i].Expected); 620 } else { 621 ASSERT_TRUE(U16Success); 622 } 623 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32); 624 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 625 ASSERT_FALSE(U32Success); 626 EXPECT_EQ(U32, Unsigned[i].Expected); 627 } else { 628 ASSERT_TRUE(U32Success); 629 } 630 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64); 631 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 632 ASSERT_FALSE(U64Success); 633 EXPECT_EQ(U64, Unsigned[i].Expected); 634 } else { 635 ASSERT_TRUE(U64Success); 636 } 637 } 638 639 int8_t S8; 640 int16_t S16; 641 int32_t S32; 642 int64_t S64; 643 644 for (size_t i = 0; i < array_lengthof(Signed); ++i) { 645 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8); 646 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) { 647 ASSERT_FALSE(S8Success); 648 EXPECT_EQ(S8, Signed[i].Expected); 649 } else { 650 ASSERT_TRUE(S8Success); 651 } 652 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16); 653 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) { 654 ASSERT_FALSE(S16Success); 655 EXPECT_EQ(S16, Signed[i].Expected); 656 } else { 657 ASSERT_TRUE(S16Success); 658 } 659 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32); 660 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) { 661 ASSERT_FALSE(S32Success); 662 EXPECT_EQ(S32, Signed[i].Expected); 663 } else { 664 ASSERT_TRUE(S32Success); 665 } 666 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64); 667 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) { 668 ASSERT_FALSE(S64Success); 669 EXPECT_EQ(S64, Signed[i].Expected); 670 } else { 671 ASSERT_TRUE(S64Success); 672 } 673 } 674 } 675 676 677 static const char* BadStrings[] = { 678 "" // empty string 679 , "18446744073709551617" // value just over max 680 , "123456789012345678901" // value way too large 681 , "4t23v" // illegal decimal characters 682 , "0x123W56" // illegal hex characters 683 , "0b2" // illegal bin characters 684 , "08" // illegal oct characters 685 , "0o8" // illegal oct characters 686 , "-123" // negative unsigned value 687 , "0x" 688 , "0b" 689 }; 690 691 692 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) { 693 unsigned long long U64; 694 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) { 695 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64); 696 ASSERT_TRUE(IsBadNumber); 697 } 698 } 699 700 struct ConsumeUnsignedPair { 701 const char *Str; 702 uint64_t Expected; 703 const char *Leftover; 704 } ConsumeUnsigned[] = { 705 {"0", 0, ""}, 706 {"255", 255, ""}, 707 {"256", 256, ""}, 708 {"65535", 65535, ""}, 709 {"65536", 65536, ""}, 710 {"4294967295", 4294967295ULL, ""}, 711 {"4294967296", 4294967296ULL, ""}, 712 {"255A376", 255, "A376"}, 713 {"18446744073709551615", 18446744073709551615ULL, ""}, 714 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"}, 715 {"042", 34, ""}, 716 {"0x42", 66, ""}, 717 {"0x42-0x34", 66, "-0x34"}, 718 {"0b101010", 42, ""}, 719 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit 720 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit 721 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit. 722 723 struct ConsumeSignedPair { 724 const char *Str; 725 int64_t Expected; 726 const char *Leftover; 727 } ConsumeSigned[] = { 728 {"0", 0, ""}, 729 {"-0", 0, ""}, 730 {"0-1", 0, "-1"}, 731 {"-0-1", 0, "-1"}, 732 {"127", 127, ""}, 733 {"128", 128, ""}, 734 {"127-1", 127, "-1"}, 735 {"128-1", 128, "-1"}, 736 {"-128", -128, ""}, 737 {"-129", -129, ""}, 738 {"-128-1", -128, "-1"}, 739 {"-129-1", -129, "-1"}, 740 {"32767", 32767, ""}, 741 {"32768", 32768, ""}, 742 {"32767-1", 32767, "-1"}, 743 {"32768-1", 32768, "-1"}, 744 {"-32768", -32768, ""}, 745 {"-32769", -32769, ""}, 746 {"-32768-1", -32768, "-1"}, 747 {"-32769-1", -32769, "-1"}, 748 {"2147483647", 2147483647LL, ""}, 749 {"2147483648", 2147483648LL, ""}, 750 {"2147483647-1", 2147483647LL, "-1"}, 751 {"2147483648-1", 2147483648LL, "-1"}, 752 {"-2147483648", -2147483648LL, ""}, 753 {"-2147483649", -2147483649LL, ""}, 754 {"-2147483648-1", -2147483648LL, "-1"}, 755 {"-2147483649-1", -2147483649LL, "-1"}, 756 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""}, 757 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"}, 758 {"042", 34, ""}, 759 {"042-1", 34, "-1"}, 760 {"0x42", 66, ""}, 761 {"0x42-1", 66, "-1"}, 762 {"0b101010", 42, ""}, 763 {"0b101010-1", 42, "-1"}, 764 {"-042", -34, ""}, 765 {"-042-1", -34, "-1"}, 766 {"-0x42", -66, ""}, 767 {"-0x42-1", -66, "-1"}, 768 {"-0b101010", -42, ""}, 769 {"-0b101010-1", -42, "-1"}}; 770 771 TEST(StringRefTest, consumeIntegerUnsigned) { 772 uint8_t U8; 773 uint16_t U16; 774 uint32_t U32; 775 uint64_t U64; 776 777 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) { 778 StringRef Str = ConsumeUnsigned[i].Str; 779 bool U8Success = Str.consumeInteger(0, U8); 780 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) == 781 ConsumeUnsigned[i].Expected) { 782 ASSERT_FALSE(U8Success); 783 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected); 784 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 785 } else { 786 ASSERT_TRUE(U8Success); 787 } 788 789 Str = ConsumeUnsigned[i].Str; 790 bool U16Success = Str.consumeInteger(0, U16); 791 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) == 792 ConsumeUnsigned[i].Expected) { 793 ASSERT_FALSE(U16Success); 794 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected); 795 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 796 } else { 797 ASSERT_TRUE(U16Success); 798 } 799 800 Str = ConsumeUnsigned[i].Str; 801 bool U32Success = Str.consumeInteger(0, U32); 802 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) == 803 ConsumeUnsigned[i].Expected) { 804 ASSERT_FALSE(U32Success); 805 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected); 806 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 807 } else { 808 ASSERT_TRUE(U32Success); 809 } 810 811 Str = ConsumeUnsigned[i].Str; 812 bool U64Success = Str.consumeInteger(0, U64); 813 if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) == 814 ConsumeUnsigned[i].Expected) { 815 ASSERT_FALSE(U64Success); 816 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected); 817 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 818 } else { 819 ASSERT_TRUE(U64Success); 820 } 821 } 822 } 823 824 TEST(StringRefTest, consumeIntegerSigned) { 825 int8_t S8; 826 int16_t S16; 827 int32_t S32; 828 int64_t S64; 829 830 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) { 831 StringRef Str = ConsumeSigned[i].Str; 832 bool S8Success = Str.consumeInteger(0, S8); 833 if (static_cast<int8_t>(ConsumeSigned[i].Expected) == 834 ConsumeSigned[i].Expected) { 835 ASSERT_FALSE(S8Success); 836 EXPECT_EQ(S8, ConsumeSigned[i].Expected); 837 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 838 } else { 839 ASSERT_TRUE(S8Success); 840 } 841 842 Str = ConsumeSigned[i].Str; 843 bool S16Success = Str.consumeInteger(0, S16); 844 if (static_cast<int16_t>(ConsumeSigned[i].Expected) == 845 ConsumeSigned[i].Expected) { 846 ASSERT_FALSE(S16Success); 847 EXPECT_EQ(S16, ConsumeSigned[i].Expected); 848 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 849 } else { 850 ASSERT_TRUE(S16Success); 851 } 852 853 Str = ConsumeSigned[i].Str; 854 bool S32Success = Str.consumeInteger(0, S32); 855 if (static_cast<int32_t>(ConsumeSigned[i].Expected) == 856 ConsumeSigned[i].Expected) { 857 ASSERT_FALSE(S32Success); 858 EXPECT_EQ(S32, ConsumeSigned[i].Expected); 859 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 860 } else { 861 ASSERT_TRUE(S32Success); 862 } 863 864 Str = ConsumeSigned[i].Str; 865 bool S64Success = Str.consumeInteger(0, S64); 866 if (static_cast<int64_t>(ConsumeSigned[i].Expected) == 867 ConsumeSigned[i].Expected) { 868 ASSERT_FALSE(S64Success); 869 EXPECT_EQ(S64, ConsumeSigned[i].Expected); 870 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 871 } else { 872 ASSERT_TRUE(S64Success); 873 } 874 } 875 } 876 877 struct GetDoubleStrings { 878 const char *Str; 879 bool AllowInexact; 880 bool ShouldFail; 881 double D; 882 } DoubleStrings[] = {{"0", false, false, 0.0}, 883 {"0.0", false, false, 0.0}, 884 {"-0.0", false, false, -0.0}, 885 {"123.45", false, true, 123.45}, 886 {"123.45", true, false, 123.45}, 887 {"1.8e308", true, false, std::numeric_limits<double>::infinity()}, 888 {"1.8e308", false, true, std::numeric_limits<double>::infinity()}, 889 {"0x0.0000000000001P-1023", false, true, 0.0}, 890 {"0x0.0000000000001P-1023", true, false, 0.0}, 891 }; 892 893 TEST(StringRefTest, getAsDouble) { 894 for (const auto &Entry : DoubleStrings) { 895 double Result; 896 StringRef S(Entry.Str); 897 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact)); 898 if (!Entry.ShouldFail) { 899 EXPECT_EQ(Result, Entry.D); 900 } 901 } 902 } 903 904 static const char *join_input[] = { "a", "b", "c" }; 905 static const char join_result1[] = "a"; 906 static const char join_result2[] = "a:b:c"; 907 static const char join_result3[] = "a::b::c"; 908 909 TEST(StringRefTest, joinStrings) { 910 std::vector<StringRef> v1; 911 std::vector<std::string> v2; 912 for (size_t i = 0; i < array_lengthof(join_input); ++i) { 913 v1.push_back(join_input[i]); 914 v2.push_back(join_input[i]); 915 } 916 917 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1; 918 EXPECT_TRUE(v1_join1); 919 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2; 920 EXPECT_TRUE(v1_join2); 921 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3; 922 EXPECT_TRUE(v1_join3); 923 924 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1; 925 EXPECT_TRUE(v2_join1); 926 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2; 927 EXPECT_TRUE(v2_join2); 928 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3; 929 EXPECT_TRUE(v2_join3); 930 v2_join3 = join(v2, "::") == join_result3; 931 EXPECT_TRUE(v2_join3); 932 } 933 934 935 TEST(StringRefTest, AllocatorCopy) { 936 BumpPtrAllocator Alloc; 937 // First test empty strings. We don't want these to allocate anything on the 938 // allocator. 939 StringRef StrEmpty = ""; 940 StringRef StrEmptyc = StrEmpty.copy(Alloc); 941 EXPECT_TRUE(StrEmpty.equals(StrEmptyc)); 942 EXPECT_EQ(StrEmptyc.data(), nullptr); 943 EXPECT_EQ(StrEmptyc.size(), 0u); 944 EXPECT_EQ(Alloc.getTotalMemory(), 0u); 945 946 StringRef Str1 = "hello"; 947 StringRef Str2 = "bye"; 948 StringRef Str1c = Str1.copy(Alloc); 949 StringRef Str2c = Str2.copy(Alloc); 950 EXPECT_TRUE(Str1.equals(Str1c)); 951 EXPECT_NE(Str1.data(), Str1c.data()); 952 EXPECT_TRUE(Str2.equals(Str2c)); 953 EXPECT_NE(Str2.data(), Str2c.data()); 954 } 955 956 TEST(StringRefTest, Drop) { 957 StringRef Test("StringRefTest::Drop"); 958 959 StringRef Dropped = Test.drop_front(5); 960 EXPECT_EQ(Dropped, "gRefTest::Drop"); 961 962 Dropped = Test.drop_back(5); 963 EXPECT_EQ(Dropped, "StringRefTest:"); 964 965 Dropped = Test.drop_front(0); 966 EXPECT_EQ(Dropped, Test); 967 968 Dropped = Test.drop_back(0); 969 EXPECT_EQ(Dropped, Test); 970 971 Dropped = Test.drop_front(Test.size()); 972 EXPECT_TRUE(Dropped.empty()); 973 974 Dropped = Test.drop_back(Test.size()); 975 EXPECT_TRUE(Dropped.empty()); 976 } 977 978 TEST(StringRefTest, Take) { 979 StringRef Test("StringRefTest::Take"); 980 981 StringRef Taken = Test.take_front(5); 982 EXPECT_EQ(Taken, "Strin"); 983 984 Taken = Test.take_back(5); 985 EXPECT_EQ(Taken, ":Take"); 986 987 Taken = Test.take_front(Test.size()); 988 EXPECT_EQ(Taken, Test); 989 990 Taken = Test.take_back(Test.size()); 991 EXPECT_EQ(Taken, Test); 992 993 Taken = Test.take_front(0); 994 EXPECT_TRUE(Taken.empty()); 995 996 Taken = Test.take_back(0); 997 EXPECT_TRUE(Taken.empty()); 998 } 999 1000 TEST(StringRefTest, FindIf) { 1001 StringRef Punct("Test.String"); 1002 StringRef NoPunct("ABCDEFG"); 1003 StringRef Empty; 1004 1005 auto IsPunct = [](char c) { return ::ispunct(c); }; 1006 auto IsAlpha = [](char c) { return ::isalpha(c); }; 1007 EXPECT_EQ(4U, Punct.find_if(IsPunct)); 1008 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct)); 1009 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct)); 1010 1011 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha)); 1012 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha)); 1013 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha)); 1014 } 1015 1016 TEST(StringRefTest, TakeWhileUntil) { 1017 StringRef Test("String With 1 Number"); 1018 1019 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); }); 1020 EXPECT_EQ("", Taken); 1021 1022 Taken = Test.take_until([](char c) { return ::isdigit(c); }); 1023 EXPECT_EQ("String With ", Taken); 1024 1025 Taken = Test.take_while([](char c) { return true; }); 1026 EXPECT_EQ(Test, Taken); 1027 1028 Taken = Test.take_until([](char c) { return true; }); 1029 EXPECT_EQ("", Taken); 1030 1031 Test = ""; 1032 Taken = Test.take_while([](char c) { return true; }); 1033 EXPECT_EQ("", Taken); 1034 } 1035 1036 TEST(StringRefTest, DropWhileUntil) { 1037 StringRef Test("String With 1 Number"); 1038 1039 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); }); 1040 EXPECT_EQ(Test, Taken); 1041 1042 Taken = Test.drop_until([](char c) { return ::isdigit(c); }); 1043 EXPECT_EQ("1 Number", Taken); 1044 1045 Taken = Test.drop_while([](char c) { return true; }); 1046 EXPECT_EQ("", Taken); 1047 1048 Taken = Test.drop_until([](char c) { return true; }); 1049 EXPECT_EQ(Test, Taken); 1050 1051 StringRef EmptyString = ""; 1052 Taken = EmptyString.drop_while([](char c) { return true; }); 1053 EXPECT_EQ("", Taken); 1054 } 1055 1056 TEST(StringRefTest, StringLiteral) { 1057 constexpr StringRef StringRefs[] = {"Foo", "Bar"}; 1058 EXPECT_EQ(StringRef("Foo"), StringRefs[0]); 1059 EXPECT_EQ(StringRef("Bar"), StringRefs[1]); 1060 1061 constexpr StringLiteral Strings[] = {"Foo", "Bar"}; 1062 EXPECT_EQ(StringRef("Foo"), Strings[0]); 1063 EXPECT_EQ(StringRef("Bar"), Strings[1]); 1064 } 1065 1066 // Check gtest prints StringRef as a string instead of a container of chars. 1067 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h 1068 TEST(StringRefTest, GTestPrinter) { 1069 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo"))); 1070 } 1071 1072 static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable"); 1073 1074 } // end anonymous namespace 1075