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