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