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