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 328 EXPECT_EQ(StringRef("hello"), Str0.rtrim()); 329 EXPECT_EQ(StringRef(" hello"), Str1.rtrim()); 330 EXPECT_EQ(StringRef(" hello"), Str2.rtrim()); 331 EXPECT_EQ(StringRef("hello"), Str0.ltrim()); 332 EXPECT_EQ(StringRef("hello "), Str1.ltrim()); 333 EXPECT_EQ(StringRef("hello "), Str2.ltrim()); 334 EXPECT_EQ(StringRef("hello"), Str0.trim()); 335 EXPECT_EQ(StringRef("hello"), Str1.trim()); 336 EXPECT_EQ(StringRef("hello"), Str2.trim()); 337 338 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh")); 339 340 EXPECT_EQ(StringRef(""), StringRef("").trim()); 341 EXPECT_EQ(StringRef(""), StringRef(" ").trim()); 342 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim()); 343 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim()); 344 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim('\0')); 345 } 346 347 TEST(StringRefTest, StartsWith) { 348 StringRef Str("hello"); 349 EXPECT_TRUE(Str.startswith("")); 350 EXPECT_TRUE(Str.startswith("he")); 351 EXPECT_FALSE(Str.startswith("helloworld")); 352 EXPECT_FALSE(Str.startswith("hi")); 353 } 354 355 TEST(StringRefTest, StartsWithLower) { 356 StringRef Str("heLLo"); 357 EXPECT_TRUE(Str.startswith_lower("")); 358 EXPECT_TRUE(Str.startswith_lower("he")); 359 EXPECT_TRUE(Str.startswith_lower("hell")); 360 EXPECT_TRUE(Str.startswith_lower("HELlo")); 361 EXPECT_FALSE(Str.startswith_lower("helloworld")); 362 EXPECT_FALSE(Str.startswith_lower("hi")); 363 } 364 365 TEST(StringRefTest, ConsumeFront) { 366 StringRef Str("hello"); 367 EXPECT_TRUE(Str.consume_front("")); 368 EXPECT_EQ("hello", Str); 369 EXPECT_TRUE(Str.consume_front("he")); 370 EXPECT_EQ("llo", Str); 371 EXPECT_FALSE(Str.consume_front("lloworld")); 372 EXPECT_EQ("llo", Str); 373 EXPECT_FALSE(Str.consume_front("lol")); 374 EXPECT_EQ("llo", Str); 375 EXPECT_TRUE(Str.consume_front("llo")); 376 EXPECT_EQ("", Str); 377 EXPECT_FALSE(Str.consume_front("o")); 378 EXPECT_TRUE(Str.consume_front("")); 379 } 380 381 TEST(StringRefTest, EndsWith) { 382 StringRef Str("hello"); 383 EXPECT_TRUE(Str.endswith("")); 384 EXPECT_TRUE(Str.endswith("lo")); 385 EXPECT_FALSE(Str.endswith("helloworld")); 386 EXPECT_FALSE(Str.endswith("worldhello")); 387 EXPECT_FALSE(Str.endswith("so")); 388 } 389 390 TEST(StringRefTest, EndsWithLower) { 391 StringRef Str("heLLo"); 392 EXPECT_TRUE(Str.endswith_lower("")); 393 EXPECT_TRUE(Str.endswith_lower("lo")); 394 EXPECT_TRUE(Str.endswith_lower("LO")); 395 EXPECT_TRUE(Str.endswith_lower("ELlo")); 396 EXPECT_FALSE(Str.endswith_lower("helloworld")); 397 EXPECT_FALSE(Str.endswith_lower("hi")); 398 } 399 400 TEST(StringRefTest, ConsumeBack) { 401 StringRef Str("hello"); 402 EXPECT_TRUE(Str.consume_back("")); 403 EXPECT_EQ("hello", Str); 404 EXPECT_TRUE(Str.consume_back("lo")); 405 EXPECT_EQ("hel", Str); 406 EXPECT_FALSE(Str.consume_back("helhel")); 407 EXPECT_EQ("hel", Str); 408 EXPECT_FALSE(Str.consume_back("hle")); 409 EXPECT_EQ("hel", Str); 410 EXPECT_TRUE(Str.consume_back("hel")); 411 EXPECT_EQ("", Str); 412 EXPECT_FALSE(Str.consume_back("h")); 413 EXPECT_TRUE(Str.consume_back("")); 414 } 415 416 TEST(StringRefTest, Find) { 417 StringRef Str("helloHELLO"); 418 StringRef LongStr("hellx xello hell ello world foo bar hello HELLO"); 419 420 struct { 421 StringRef Str; 422 char C; 423 std::size_t From; 424 std::size_t Pos; 425 std::size_t LowerPos; 426 } CharExpectations[] = { 427 {Str, 'h', 0U, 0U, 0U}, 428 {Str, 'e', 0U, 1U, 1U}, 429 {Str, 'l', 0U, 2U, 2U}, 430 {Str, 'l', 3U, 3U, 3U}, 431 {Str, 'o', 0U, 4U, 4U}, 432 {Str, 'L', 0U, 7U, 2U}, 433 {Str, 'z', 0U, StringRef::npos, StringRef::npos}, 434 }; 435 436 struct { 437 StringRef Str; 438 llvm::StringRef S; 439 std::size_t From; 440 std::size_t Pos; 441 std::size_t LowerPos; 442 } StrExpectations[] = { 443 {Str, "helloword", 0, StringRef::npos, StringRef::npos}, 444 {Str, "hello", 0, 0U, 0U}, 445 {Str, "ello", 0, 1U, 1U}, 446 {Str, "zz", 0, StringRef::npos, StringRef::npos}, 447 {Str, "ll", 2U, 2U, 2U}, 448 {Str, "ll", 3U, StringRef::npos, 7U}, 449 {Str, "LL", 2U, 7U, 2U}, 450 {Str, "LL", 3U, 7U, 7U}, 451 {Str, "", 0U, 0U, 0U}, 452 {LongStr, "hello", 0U, 36U, 36U}, 453 {LongStr, "foo", 0U, 28U, 28U}, 454 {LongStr, "hell", 2U, 12U, 12U}, 455 {LongStr, "HELL", 2U, 42U, 12U}, 456 {LongStr, "", 0U, 0U, 0U}}; 457 458 for (auto &E : CharExpectations) { 459 EXPECT_EQ(E.Pos, E.Str.find(E.C, E.From)); 460 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.C, E.From)); 461 EXPECT_EQ(E.LowerPos, E.Str.find_lower(toupper(E.C), E.From)); 462 } 463 464 for (auto &E : StrExpectations) { 465 EXPECT_EQ(E.Pos, E.Str.find(E.S, E.From)); 466 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S, E.From)); 467 EXPECT_EQ(E.LowerPos, E.Str.find_lower(E.S.upper(), E.From)); 468 } 469 470 EXPECT_EQ(3U, Str.rfind('l')); 471 EXPECT_EQ(StringRef::npos, Str.rfind('z')); 472 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld")); 473 EXPECT_EQ(0U, Str.rfind("hello")); 474 EXPECT_EQ(1U, Str.rfind("ello")); 475 EXPECT_EQ(StringRef::npos, Str.rfind("zz")); 476 477 EXPECT_EQ(8U, Str.rfind_lower('l')); 478 EXPECT_EQ(8U, Str.rfind_lower('L')); 479 EXPECT_EQ(StringRef::npos, Str.rfind_lower('z')); 480 EXPECT_EQ(StringRef::npos, Str.rfind_lower("HELLOWORLD")); 481 EXPECT_EQ(5U, Str.rfind("HELLO")); 482 EXPECT_EQ(6U, Str.rfind("ELLO")); 483 EXPECT_EQ(StringRef::npos, Str.rfind("ZZ")); 484 485 EXPECT_EQ(2U, Str.find_first_of('l')); 486 EXPECT_EQ(1U, Str.find_first_of("el")); 487 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz")); 488 489 Str = "hello"; 490 EXPECT_EQ(1U, Str.find_first_not_of('h')); 491 EXPECT_EQ(4U, Str.find_first_not_of("hel")); 492 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello")); 493 494 EXPECT_EQ(3U, Str.find_last_not_of('o')); 495 EXPECT_EQ(1U, Str.find_last_not_of("lo")); 496 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo")); 497 } 498 499 TEST(StringRefTest, Count) { 500 StringRef Str("hello"); 501 EXPECT_EQ(2U, Str.count('l')); 502 EXPECT_EQ(1U, Str.count('o')); 503 EXPECT_EQ(0U, Str.count('z')); 504 EXPECT_EQ(0U, Str.count("helloworld")); 505 EXPECT_EQ(1U, Str.count("hello")); 506 EXPECT_EQ(1U, Str.count("ello")); 507 EXPECT_EQ(0U, Str.count("zz")); 508 } 509 510 TEST(StringRefTest, EditDistance) { 511 StringRef Hello("hello"); 512 EXPECT_EQ(2U, Hello.edit_distance("hill")); 513 514 StringRef Industry("industry"); 515 EXPECT_EQ(6U, Industry.edit_distance("interest")); 516 517 StringRef Soylent("soylent green is people"); 518 EXPECT_EQ(19U, Soylent.edit_distance("people soiled our green")); 519 EXPECT_EQ(26U, Soylent.edit_distance("people soiled our green", 520 /* allow replacements = */ false)); 521 EXPECT_EQ(9U, Soylent.edit_distance("people soiled our green", 522 /* allow replacements = */ true, 523 /* max edit distance = */ 8)); 524 EXPECT_EQ(53U, Soylent.edit_distance("people soiled our green " 525 "people soiled our green " 526 "people soiled our green ")); 527 } 528 529 TEST(StringRefTest, Misc) { 530 std::string Storage; 531 raw_string_ostream OS(Storage); 532 OS << StringRef("hello"); 533 EXPECT_EQ("hello", OS.str()); 534 } 535 536 TEST(StringRefTest, Hashing) { 537 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef())); 538 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef(""))); 539 std::string S = "hello world"; 540 hash_code H = hash_value(S); 541 EXPECT_EQ(H, hash_value(StringRef("hello world"))); 542 EXPECT_EQ(H, hash_value(StringRef(S))); 543 EXPECT_NE(H, hash_value(StringRef("hello worl"))); 544 EXPECT_EQ(hash_value(std::string("hello worl")), 545 hash_value(StringRef("hello worl"))); 546 EXPECT_NE(H, hash_value(StringRef("hello world "))); 547 EXPECT_EQ(hash_value(std::string("hello world ")), 548 hash_value(StringRef("hello world "))); 549 EXPECT_EQ(H, hash_value(StringRef("hello world\0"))); 550 EXPECT_NE(hash_value(std::string("ello worl")), 551 hash_value(StringRef("hello world").slice(1, -1))); 552 } 553 554 struct UnsignedPair { 555 const char *Str; 556 uint64_t Expected; 557 } Unsigned[] = 558 { {"0", 0} 559 , {"255", 255} 560 , {"256", 256} 561 , {"65535", 65535} 562 , {"65536", 65536} 563 , {"4294967295", 4294967295ULL} 564 , {"4294967296", 4294967296ULL} 565 , {"18446744073709551615", 18446744073709551615ULL} 566 , {"042", 34} 567 , {"0x42", 66} 568 , {"0b101010", 42} 569 }; 570 571 struct SignedPair { 572 const char *Str; 573 int64_t Expected; 574 } Signed[] = 575 { {"0", 0} 576 , {"-0", 0} 577 , {"127", 127} 578 , {"128", 128} 579 , {"-128", -128} 580 , {"-129", -129} 581 , {"32767", 32767} 582 , {"32768", 32768} 583 , {"-32768", -32768} 584 , {"-32769", -32769} 585 , {"2147483647", 2147483647LL} 586 , {"2147483648", 2147483648LL} 587 , {"-2147483648", -2147483648LL} 588 , {"-2147483649", -2147483649LL} 589 , {"-9223372036854775808", -(9223372036854775807LL) - 1} 590 , {"042", 34} 591 , {"0x42", 66} 592 , {"0b101010", 42} 593 , {"-042", -34} 594 , {"-0x42", -66} 595 , {"-0b101010", -42} 596 }; 597 598 TEST(StringRefTest, getAsInteger) { 599 uint8_t U8; 600 uint16_t U16; 601 uint32_t U32; 602 uint64_t U64; 603 604 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) { 605 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8); 606 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 607 ASSERT_FALSE(U8Success); 608 EXPECT_EQ(U8, Unsigned[i].Expected); 609 } else { 610 ASSERT_TRUE(U8Success); 611 } 612 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16); 613 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 614 ASSERT_FALSE(U16Success); 615 EXPECT_EQ(U16, Unsigned[i].Expected); 616 } else { 617 ASSERT_TRUE(U16Success); 618 } 619 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32); 620 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 621 ASSERT_FALSE(U32Success); 622 EXPECT_EQ(U32, Unsigned[i].Expected); 623 } else { 624 ASSERT_TRUE(U32Success); 625 } 626 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64); 627 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 628 ASSERT_FALSE(U64Success); 629 EXPECT_EQ(U64, Unsigned[i].Expected); 630 } else { 631 ASSERT_TRUE(U64Success); 632 } 633 } 634 635 int8_t S8; 636 int16_t S16; 637 int32_t S32; 638 int64_t S64; 639 640 for (size_t i = 0; i < array_lengthof(Signed); ++i) { 641 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8); 642 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) { 643 ASSERT_FALSE(S8Success); 644 EXPECT_EQ(S8, Signed[i].Expected); 645 } else { 646 ASSERT_TRUE(S8Success); 647 } 648 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16); 649 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) { 650 ASSERT_FALSE(S16Success); 651 EXPECT_EQ(S16, Signed[i].Expected); 652 } else { 653 ASSERT_TRUE(S16Success); 654 } 655 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32); 656 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) { 657 ASSERT_FALSE(S32Success); 658 EXPECT_EQ(S32, Signed[i].Expected); 659 } else { 660 ASSERT_TRUE(S32Success); 661 } 662 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64); 663 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) { 664 ASSERT_FALSE(S64Success); 665 EXPECT_EQ(S64, Signed[i].Expected); 666 } else { 667 ASSERT_TRUE(S64Success); 668 } 669 } 670 } 671 672 673 static const char* BadStrings[] = { 674 "" // empty string 675 , "18446744073709551617" // value just over max 676 , "123456789012345678901" // value way too large 677 , "4t23v" // illegal decimal characters 678 , "0x123W56" // illegal hex characters 679 , "0b2" // illegal bin characters 680 , "08" // illegal oct characters 681 , "0o8" // illegal oct characters 682 , "-123" // negative unsigned value 683 , "0x" 684 , "0b" 685 }; 686 687 688 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) { 689 unsigned long long U64; 690 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) { 691 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64); 692 ASSERT_TRUE(IsBadNumber); 693 } 694 } 695 696 struct ConsumeUnsignedPair { 697 const char *Str; 698 uint64_t Expected; 699 const char *Leftover; 700 } ConsumeUnsigned[] = { 701 {"0", 0, ""}, 702 {"255", 255, ""}, 703 {"256", 256, ""}, 704 {"65535", 65535, ""}, 705 {"65536", 65536, ""}, 706 {"4294967295", 4294967295ULL, ""}, 707 {"4294967296", 4294967296ULL, ""}, 708 {"255A376", 255, "A376"}, 709 {"18446744073709551615", 18446744073709551615ULL, ""}, 710 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"}, 711 {"042", 34, ""}, 712 {"0x42", 66, ""}, 713 {"0x42-0x34", 66, "-0x34"}, 714 {"0b101010", 42, ""}, 715 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit 716 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit 717 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit. 718 719 struct ConsumeSignedPair { 720 const char *Str; 721 int64_t Expected; 722 const char *Leftover; 723 } ConsumeSigned[] = { 724 {"0", 0, ""}, 725 {"-0", 0, ""}, 726 {"0-1", 0, "-1"}, 727 {"-0-1", 0, "-1"}, 728 {"127", 127, ""}, 729 {"128", 128, ""}, 730 {"127-1", 127, "-1"}, 731 {"128-1", 128, "-1"}, 732 {"-128", -128, ""}, 733 {"-129", -129, ""}, 734 {"-128-1", -128, "-1"}, 735 {"-129-1", -129, "-1"}, 736 {"32767", 32767, ""}, 737 {"32768", 32768, ""}, 738 {"32767-1", 32767, "-1"}, 739 {"32768-1", 32768, "-1"}, 740 {"-32768", -32768, ""}, 741 {"-32769", -32769, ""}, 742 {"-32768-1", -32768, "-1"}, 743 {"-32769-1", -32769, "-1"}, 744 {"2147483647", 2147483647LL, ""}, 745 {"2147483648", 2147483648LL, ""}, 746 {"2147483647-1", 2147483647LL, "-1"}, 747 {"2147483648-1", 2147483648LL, "-1"}, 748 {"-2147483648", -2147483648LL, ""}, 749 {"-2147483649", -2147483649LL, ""}, 750 {"-2147483648-1", -2147483648LL, "-1"}, 751 {"-2147483649-1", -2147483649LL, "-1"}, 752 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""}, 753 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"}, 754 {"042", 34, ""}, 755 {"042-1", 34, "-1"}, 756 {"0x42", 66, ""}, 757 {"0x42-1", 66, "-1"}, 758 {"0b101010", 42, ""}, 759 {"0b101010-1", 42, "-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 767 TEST(StringRefTest, consumeIntegerUnsigned) { 768 uint8_t U8; 769 uint16_t U16; 770 uint32_t U32; 771 uint64_t U64; 772 773 for (size_t i = 0; i < array_lengthof(ConsumeUnsigned); ++i) { 774 StringRef Str = ConsumeUnsigned[i].Str; 775 bool U8Success = Str.consumeInteger(0, U8); 776 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) == 777 ConsumeUnsigned[i].Expected) { 778 ASSERT_FALSE(U8Success); 779 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected); 780 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 781 } else { 782 ASSERT_TRUE(U8Success); 783 } 784 785 Str = ConsumeUnsigned[i].Str; 786 bool U16Success = Str.consumeInteger(0, U16); 787 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) == 788 ConsumeUnsigned[i].Expected) { 789 ASSERT_FALSE(U16Success); 790 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected); 791 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 792 } else { 793 ASSERT_TRUE(U16Success); 794 } 795 796 Str = ConsumeUnsigned[i].Str; 797 bool U32Success = Str.consumeInteger(0, U32); 798 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) == 799 ConsumeUnsigned[i].Expected) { 800 ASSERT_FALSE(U32Success); 801 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected); 802 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 803 } else { 804 ASSERT_TRUE(U32Success); 805 } 806 807 Str = ConsumeUnsigned[i].Str; 808 bool U64Success = Str.consumeInteger(0, U64); 809 if (static_cast<uint64_t>(ConsumeUnsigned[i].Expected) == 810 ConsumeUnsigned[i].Expected) { 811 ASSERT_FALSE(U64Success); 812 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected); 813 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover); 814 } else { 815 ASSERT_TRUE(U64Success); 816 } 817 } 818 } 819 820 TEST(StringRefTest, consumeIntegerSigned) { 821 int8_t S8; 822 int16_t S16; 823 int32_t S32; 824 int64_t S64; 825 826 for (size_t i = 0; i < array_lengthof(ConsumeSigned); ++i) { 827 StringRef Str = ConsumeSigned[i].Str; 828 bool S8Success = Str.consumeInteger(0, S8); 829 if (static_cast<int8_t>(ConsumeSigned[i].Expected) == 830 ConsumeSigned[i].Expected) { 831 ASSERT_FALSE(S8Success); 832 EXPECT_EQ(S8, ConsumeSigned[i].Expected); 833 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 834 } else { 835 ASSERT_TRUE(S8Success); 836 } 837 838 Str = ConsumeSigned[i].Str; 839 bool S16Success = Str.consumeInteger(0, S16); 840 if (static_cast<int16_t>(ConsumeSigned[i].Expected) == 841 ConsumeSigned[i].Expected) { 842 ASSERT_FALSE(S16Success); 843 EXPECT_EQ(S16, ConsumeSigned[i].Expected); 844 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 845 } else { 846 ASSERT_TRUE(S16Success); 847 } 848 849 Str = ConsumeSigned[i].Str; 850 bool S32Success = Str.consumeInteger(0, S32); 851 if (static_cast<int32_t>(ConsumeSigned[i].Expected) == 852 ConsumeSigned[i].Expected) { 853 ASSERT_FALSE(S32Success); 854 EXPECT_EQ(S32, ConsumeSigned[i].Expected); 855 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 856 } else { 857 ASSERT_TRUE(S32Success); 858 } 859 860 Str = ConsumeSigned[i].Str; 861 bool S64Success = Str.consumeInteger(0, S64); 862 if (static_cast<int64_t>(ConsumeSigned[i].Expected) == 863 ConsumeSigned[i].Expected) { 864 ASSERT_FALSE(S64Success); 865 EXPECT_EQ(S64, ConsumeSigned[i].Expected); 866 EXPECT_EQ(Str, ConsumeSigned[i].Leftover); 867 } else { 868 ASSERT_TRUE(S64Success); 869 } 870 } 871 } 872 873 struct GetDoubleStrings { 874 const char *Str; 875 bool AllowInexact; 876 bool ShouldFail; 877 double D; 878 } DoubleStrings[] = {{"0", false, false, 0.0}, 879 {"0.0", false, false, 0.0}, 880 {"-0.0", false, false, -0.0}, 881 {"123.45", false, true, 123.45}, 882 {"123.45", true, false, 123.45}, 883 {"1.8e308", true, false, std::numeric_limits<double>::infinity()}, 884 {"1.8e308", false, true, std::numeric_limits<double>::infinity()}, 885 {"0x0.0000000000001P-1023", false, true, 0.0}, 886 {"0x0.0000000000001P-1023", true, false, 0.0}, 887 }; 888 889 TEST(StringRefTest, getAsDouble) { 890 for (const auto &Entry : DoubleStrings) { 891 double Result; 892 StringRef S(Entry.Str); 893 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact)); 894 if (!Entry.ShouldFail) { 895 EXPECT_EQ(Result, Entry.D); 896 } 897 } 898 } 899 900 static const char *join_input[] = { "a", "b", "c" }; 901 static const char join_result1[] = "a"; 902 static const char join_result2[] = "a:b:c"; 903 static const char join_result3[] = "a::b::c"; 904 905 TEST(StringRefTest, joinStrings) { 906 std::vector<StringRef> v1; 907 std::vector<std::string> v2; 908 for (size_t i = 0; i < array_lengthof(join_input); ++i) { 909 v1.push_back(join_input[i]); 910 v2.push_back(join_input[i]); 911 } 912 913 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1; 914 EXPECT_TRUE(v1_join1); 915 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2; 916 EXPECT_TRUE(v1_join2); 917 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3; 918 EXPECT_TRUE(v1_join3); 919 920 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1; 921 EXPECT_TRUE(v2_join1); 922 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2; 923 EXPECT_TRUE(v2_join2); 924 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3; 925 EXPECT_TRUE(v2_join3); 926 v2_join3 = join(v2, "::") == join_result3; 927 EXPECT_TRUE(v2_join3); 928 } 929 930 931 TEST(StringRefTest, AllocatorCopy) { 932 BumpPtrAllocator Alloc; 933 // First test empty strings. We don't want these to allocate anything on the 934 // allocator. 935 StringRef StrEmpty = ""; 936 StringRef StrEmptyc = StrEmpty.copy(Alloc); 937 EXPECT_TRUE(StrEmpty.equals(StrEmptyc)); 938 EXPECT_EQ(StrEmptyc.data(), nullptr); 939 EXPECT_EQ(StrEmptyc.size(), 0u); 940 EXPECT_EQ(Alloc.getTotalMemory(), 0u); 941 942 StringRef Str1 = "hello"; 943 StringRef Str2 = "bye"; 944 StringRef Str1c = Str1.copy(Alloc); 945 StringRef Str2c = Str2.copy(Alloc); 946 EXPECT_TRUE(Str1.equals(Str1c)); 947 EXPECT_NE(Str1.data(), Str1c.data()); 948 EXPECT_TRUE(Str2.equals(Str2c)); 949 EXPECT_NE(Str2.data(), Str2c.data()); 950 } 951 952 TEST(StringRefTest, Drop) { 953 StringRef Test("StringRefTest::Drop"); 954 955 StringRef Dropped = Test.drop_front(5); 956 EXPECT_EQ(Dropped, "gRefTest::Drop"); 957 958 Dropped = Test.drop_back(5); 959 EXPECT_EQ(Dropped, "StringRefTest:"); 960 961 Dropped = Test.drop_front(0); 962 EXPECT_EQ(Dropped, Test); 963 964 Dropped = Test.drop_back(0); 965 EXPECT_EQ(Dropped, Test); 966 967 Dropped = Test.drop_front(Test.size()); 968 EXPECT_TRUE(Dropped.empty()); 969 970 Dropped = Test.drop_back(Test.size()); 971 EXPECT_TRUE(Dropped.empty()); 972 } 973 974 TEST(StringRefTest, Take) { 975 StringRef Test("StringRefTest::Take"); 976 977 StringRef Taken = Test.take_front(5); 978 EXPECT_EQ(Taken, "Strin"); 979 980 Taken = Test.take_back(5); 981 EXPECT_EQ(Taken, ":Take"); 982 983 Taken = Test.take_front(Test.size()); 984 EXPECT_EQ(Taken, Test); 985 986 Taken = Test.take_back(Test.size()); 987 EXPECT_EQ(Taken, Test); 988 989 Taken = Test.take_front(0); 990 EXPECT_TRUE(Taken.empty()); 991 992 Taken = Test.take_back(0); 993 EXPECT_TRUE(Taken.empty()); 994 } 995 996 TEST(StringRefTest, FindIf) { 997 StringRef Punct("Test.String"); 998 StringRef NoPunct("ABCDEFG"); 999 StringRef Empty; 1000 1001 auto IsPunct = [](char c) { return ::ispunct(c); }; 1002 auto IsAlpha = [](char c) { return ::isalpha(c); }; 1003 EXPECT_EQ(4U, Punct.find_if(IsPunct)); 1004 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct)); 1005 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct)); 1006 1007 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha)); 1008 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha)); 1009 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha)); 1010 } 1011 1012 TEST(StringRefTest, TakeWhileUntil) { 1013 StringRef Test("String With 1 Number"); 1014 1015 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); }); 1016 EXPECT_EQ("", Taken); 1017 1018 Taken = Test.take_until([](char c) { return ::isdigit(c); }); 1019 EXPECT_EQ("String With ", Taken); 1020 1021 Taken = Test.take_while([](char c) { return true; }); 1022 EXPECT_EQ(Test, Taken); 1023 1024 Taken = Test.take_until([](char c) { return true; }); 1025 EXPECT_EQ("", Taken); 1026 1027 Test = ""; 1028 Taken = Test.take_while([](char c) { return true; }); 1029 EXPECT_EQ("", Taken); 1030 } 1031 1032 TEST(StringRefTest, DropWhileUntil) { 1033 StringRef Test("String With 1 Number"); 1034 1035 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); }); 1036 EXPECT_EQ(Test, Taken); 1037 1038 Taken = Test.drop_until([](char c) { return ::isdigit(c); }); 1039 EXPECT_EQ("1 Number", Taken); 1040 1041 Taken = Test.drop_while([](char c) { return true; }); 1042 EXPECT_EQ("", Taken); 1043 1044 Taken = Test.drop_until([](char c) { return true; }); 1045 EXPECT_EQ(Test, Taken); 1046 1047 StringRef EmptyString = ""; 1048 Taken = EmptyString.drop_while([](char c) { return true; }); 1049 EXPECT_EQ("", Taken); 1050 } 1051 1052 TEST(StringRefTest, StringLiteral) { 1053 constexpr StringRef StringRefs[] = {"Foo", "Bar"}; 1054 EXPECT_EQ(StringRef("Foo"), StringRefs[0]); 1055 EXPECT_EQ(StringRef("Bar"), StringRefs[1]); 1056 1057 constexpr StringLiteral Strings[] = {"Foo", "Bar"}; 1058 EXPECT_EQ(StringRef("Foo"), Strings[0]); 1059 EXPECT_EQ(StringRef("Bar"), Strings[1]); 1060 } 1061 1062 // Check gtest prints StringRef as a string instead of a container of chars. 1063 // The code is in utils/unittest/googletest/internal/custom/gtest-printers.h 1064 TEST(StringRefTest, GTestPrinter) { 1065 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo"))); 1066 } 1067 1068 static_assert(is_trivially_copyable<StringRef>::value, "trivially copyable"); 1069 1070 } // end anonymous namespace 1071