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