1176 lines · cpp
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-exception6//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"17using namespace llvm;18 19// Check that we can't accidentally assign a temporary std::string to a20// StringRef. (Unfortunately we can't make use of the same thing with21// constructors.)22static_assert(!std::is_assignable_v<StringRef &, std::string>,23 "Assigning from prvalue std::string");24static_assert(!std::is_assignable_v<StringRef &, std::string &&>,25 "Assigning from xvalue std::string");26static_assert(std::is_assignable_v<StringRef &, std::string &>,27 "Assigning from lvalue std::string");28static_assert(std::is_assignable_v<StringRef &, const char *>,29 "Assigning from prvalue C string");30static_assert(std::is_assignable_v<StringRef &, const char *&&>,31 "Assigning from xvalue C string");32static_assert(std::is_assignable_v<StringRef &, const char *&>,33 "Assigning from lvalue C string");34 35namespace {36TEST(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 44TEST(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 50TEST(StringRefTest, EmptyInitializerList) {51 StringRef S = {};52 EXPECT_TRUE(S.empty());53 54 S = {};55 EXPECT_TRUE(S.empty());56}57 58TEST(StringRefTest, Iteration) {59 StringRef S("hello");60 constexpr StringLiteral CS("hello");61 62 // Note: Cannot use literal strings in equal() as iteration over a literal63 // 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 73TEST(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 127TEST(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 144TEST(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 152TEST(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 161TEST(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 197TEST(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 MaxSplit235 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 330TEST(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 358TEST(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 368TEST(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 378TEST(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 394TEST(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 412TEST(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 423TEST(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 433TEST(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 449TEST(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 467TEST(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 550TEST(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 569TEST(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 588TEST(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 597TEST(StringRefTest, Misc) {598 std::string Storage;599 raw_string_ostream OS(Storage);600 OS << StringRef("hello");601 EXPECT_EQ("hello", OS.str());602}603 604TEST(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 622TEST(StringRefTest, getAutoSenseRadix) {623 struct RadixPair {624 const char *Str;625 unsigned Expected;626 } RadixNumbers[] = {{"123", 10}, {"1", 10}, {"0b1", 2}, {"01", 8}, {"0o1", 8},627 {"0x1", 16}, {"0", 10}, {"00", 8}, {"", 10}};628 for (size_t i = 0; i < std::size(RadixNumbers); ++i) {629 StringRef number = RadixNumbers[i].Str;630 unsigned radix = getAutoSenseRadix(number);631 EXPECT_EQ(radix, RadixNumbers[i].Expected);632 }633}634 635struct UnsignedPair {636 const char *Str;637 uint64_t Expected;638} Unsigned[] =639 { {"0", 0}640 , {"255", 255}641 , {"256", 256}642 , {"65535", 65535}643 , {"65536", 65536}644 , {"4294967295", 4294967295ULL}645 , {"4294967296", 4294967296ULL}646 , {"18446744073709551615", 18446744073709551615ULL}647 , {"042", 34}648 , {"0x42", 66}649 , {"0b101010", 42}650 };651 652struct SignedPair {653 const char *Str;654 int64_t Expected;655} Signed[] =656 { {"0", 0}657 , {"-0", 0}658 , {"127", 127}659 , {"128", 128}660 , {"-128", -128}661 , {"-129", -129}662 , {"32767", 32767}663 , {"32768", 32768}664 , {"-32768", -32768}665 , {"-32769", -32769}666 , {"2147483647", 2147483647LL}667 , {"2147483648", 2147483648LL}668 , {"-2147483648", -2147483648LL}669 , {"-2147483649", -2147483649LL}670 , {"-9223372036854775808", -(9223372036854775807LL) - 1}671 , {"042", 34}672 , {"0x42", 66}673 , {"0b101010", 42}674 , {"-042", -34}675 , {"-0x42", -66}676 , {"-0b101010", -42}677 };678 679TEST(StringRefTest, getAsInteger) {680 uint8_t U8;681 uint16_t U16;682 uint32_t U32;683 uint64_t U64;684 685 for (size_t i = 0; i < std::size(Unsigned); ++i) {686 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8);687 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {688 ASSERT_FALSE(U8Success);689 EXPECT_EQ(U8, Unsigned[i].Expected);690 } else {691 ASSERT_TRUE(U8Success);692 }693 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16);694 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {695 ASSERT_FALSE(U16Success);696 EXPECT_EQ(U16, Unsigned[i].Expected);697 } else {698 ASSERT_TRUE(U16Success);699 }700 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32);701 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) {702 ASSERT_FALSE(U32Success);703 EXPECT_EQ(U32, Unsigned[i].Expected);704 } else {705 ASSERT_TRUE(U32Success);706 }707 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64);708 ASSERT_FALSE(U64Success);709 EXPECT_EQ(U64, Unsigned[i].Expected);710 }711 712 int8_t S8;713 int16_t S16;714 int32_t S32;715 int64_t S64;716 717 for (size_t i = 0; i < std::size(Signed); ++i) {718 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8);719 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) {720 ASSERT_FALSE(S8Success);721 EXPECT_EQ(S8, Signed[i].Expected);722 } else {723 ASSERT_TRUE(S8Success);724 }725 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16);726 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) {727 ASSERT_FALSE(S16Success);728 EXPECT_EQ(S16, Signed[i].Expected);729 } else {730 ASSERT_TRUE(S16Success);731 }732 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32);733 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) {734 ASSERT_FALSE(S32Success);735 EXPECT_EQ(S32, Signed[i].Expected);736 } else {737 ASSERT_TRUE(S32Success);738 }739 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64);740 ASSERT_FALSE(S64Success);741 EXPECT_EQ(S64, Signed[i].Expected);742 }743}744 745 746static const char* BadStrings[] = {747 "" // empty string748 , "18446744073709551617" // value just over max749 , "123456789012345678901" // value way too large750 , "4t23v" // illegal decimal characters751 , "0x123W56" // illegal hex characters752 , "0b2" // illegal bin characters753 , "08" // illegal oct characters754 , "0o8" // illegal oct characters755 , "-123" // negative unsigned value756 , "0x"757 , "0b"758};759 760 761TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {762 unsigned long long U64;763 for (size_t i = 0; i < std::size(BadStrings); ++i) {764 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64);765 ASSERT_TRUE(IsBadNumber);766 }767}768 769struct ConsumeUnsignedPair {770 const char *Str;771 uint64_t Expected;772 const char *Leftover;773} ConsumeUnsigned[] = {774 {"0", 0, ""},775 {"255", 255, ""},776 {"256", 256, ""},777 {"65535", 65535, ""},778 {"65536", 65536, ""},779 {"4294967295", 4294967295ULL, ""},780 {"4294967296", 4294967296ULL, ""},781 {"255A376", 255, "A376"},782 {"18446744073709551615", 18446744073709551615ULL, ""},783 {"18446744073709551615ABC", 18446744073709551615ULL, "ABC"},784 {"042", 34, ""},785 {"0x42", 66, ""},786 {"0x42-0x34", 66, "-0x34"},787 {"0b101010", 42, ""},788 {"0429F", 042, "9F"}, // Auto-sensed octal radix, invalid digit789 {"0x42G12", 0x42, "G12"}, // Auto-sensed hex radix, invalid digit790 {"0b10101020101", 42, "20101"}}; // Auto-sensed binary radix, invalid digit.791 792struct ConsumeSignedPair {793 const char *Str;794 int64_t Expected;795 const char *Leftover;796} ConsumeSigned[] = {797 {"0", 0, ""},798 {"-0", 0, ""},799 {"0-1", 0, "-1"},800 {"-0-1", 0, "-1"},801 {"127", 127, ""},802 {"128", 128, ""},803 {"127-1", 127, "-1"},804 {"128-1", 128, "-1"},805 {"-128", -128, ""},806 {"-129", -129, ""},807 {"-128-1", -128, "-1"},808 {"-129-1", -129, "-1"},809 {"32767", 32767, ""},810 {"32768", 32768, ""},811 {"32767-1", 32767, "-1"},812 {"32768-1", 32768, "-1"},813 {"-32768", -32768, ""},814 {"-32769", -32769, ""},815 {"-32768-1", -32768, "-1"},816 {"-32769-1", -32769, "-1"},817 {"2147483647", 2147483647LL, ""},818 {"2147483648", 2147483648LL, ""},819 {"2147483647-1", 2147483647LL, "-1"},820 {"2147483648-1", 2147483648LL, "-1"},821 {"-2147483648", -2147483648LL, ""},822 {"-2147483649", -2147483649LL, ""},823 {"-2147483648-1", -2147483648LL, "-1"},824 {"-2147483649-1", -2147483649LL, "-1"},825 {"-9223372036854775808", -(9223372036854775807LL) - 1, ""},826 {"-9223372036854775808-1", -(9223372036854775807LL) - 1, "-1"},827 {"042", 34, ""},828 {"042-1", 34, "-1"},829 {"0x42", 66, ""},830 {"0x42-1", 66, "-1"},831 {"0b101010", 42, ""},832 {"0b101010-1", 42, "-1"},833 {"-042", -34, ""},834 {"-042-1", -34, "-1"},835 {"-0x42", -66, ""},836 {"-0x42-1", -66, "-1"},837 {"-0b101010", -42, ""},838 {"-0b101010-1", -42, "-1"}};839 840TEST(StringRefTest, consumeIntegerUnsigned) {841 uint8_t U8;842 uint16_t U16;843 uint32_t U32;844 uint64_t U64;845 APInt U;846 847 for (size_t i = 0; i < std::size(ConsumeUnsigned); ++i) {848 StringRef Str = ConsumeUnsigned[i].Str;849 bool U8Success = Str.consumeInteger(0, U8);850 if (static_cast<uint8_t>(ConsumeUnsigned[i].Expected) ==851 ConsumeUnsigned[i].Expected) {852 ASSERT_FALSE(U8Success);853 EXPECT_EQ(U8, ConsumeUnsigned[i].Expected);854 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);855 } else {856 ASSERT_TRUE(U8Success);857 }858 859 Str = ConsumeUnsigned[i].Str;860 bool U16Success = Str.consumeInteger(0, U16);861 if (static_cast<uint16_t>(ConsumeUnsigned[i].Expected) ==862 ConsumeUnsigned[i].Expected) {863 ASSERT_FALSE(U16Success);864 EXPECT_EQ(U16, ConsumeUnsigned[i].Expected);865 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);866 } else {867 ASSERT_TRUE(U16Success);868 }869 870 Str = ConsumeUnsigned[i].Str;871 bool U32Success = Str.consumeInteger(0, U32);872 if (static_cast<uint32_t>(ConsumeUnsigned[i].Expected) ==873 ConsumeUnsigned[i].Expected) {874 ASSERT_FALSE(U32Success);875 EXPECT_EQ(U32, ConsumeUnsigned[i].Expected);876 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);877 } else {878 ASSERT_TRUE(U32Success);879 }880 881 Str = ConsumeUnsigned[i].Str;882 bool U64Success = Str.consumeInteger(0, U64);883 ASSERT_FALSE(U64Success);884 EXPECT_EQ(U64, ConsumeUnsigned[i].Expected);885 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);886 887 Str = ConsumeUnsigned[i].Str;888 U64Success = Str.consumeInteger(0, U);889 ASSERT_FALSE(U64Success);890 EXPECT_EQ(U.getZExtValue(), ConsumeUnsigned[i].Expected);891 EXPECT_EQ(Str, ConsumeUnsigned[i].Leftover);892 }893}894 895TEST(StringRefTest, consumeIntegerSigned) {896 int8_t S8;897 int16_t S16;898 int32_t S32;899 int64_t S64;900 901 for (size_t i = 0; i < std::size(ConsumeSigned); ++i) {902 StringRef Str = ConsumeSigned[i].Str;903 bool S8Success = Str.consumeInteger(0, S8);904 if (static_cast<int8_t>(ConsumeSigned[i].Expected) ==905 ConsumeSigned[i].Expected) {906 ASSERT_FALSE(S8Success);907 EXPECT_EQ(S8, ConsumeSigned[i].Expected);908 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);909 } else {910 ASSERT_TRUE(S8Success);911 }912 913 Str = ConsumeSigned[i].Str;914 bool S16Success = Str.consumeInteger(0, S16);915 if (static_cast<int16_t>(ConsumeSigned[i].Expected) ==916 ConsumeSigned[i].Expected) {917 ASSERT_FALSE(S16Success);918 EXPECT_EQ(S16, ConsumeSigned[i].Expected);919 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);920 } else {921 ASSERT_TRUE(S16Success);922 }923 924 Str = ConsumeSigned[i].Str;925 bool S32Success = Str.consumeInteger(0, S32);926 if (static_cast<int32_t>(ConsumeSigned[i].Expected) ==927 ConsumeSigned[i].Expected) {928 ASSERT_FALSE(S32Success);929 EXPECT_EQ(S32, ConsumeSigned[i].Expected);930 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);931 } else {932 ASSERT_TRUE(S32Success);933 }934 935 Str = ConsumeSigned[i].Str;936 bool S64Success = Str.consumeInteger(0, S64);937 ASSERT_FALSE(S64Success);938 EXPECT_EQ(S64, ConsumeSigned[i].Expected);939 EXPECT_EQ(Str, ConsumeSigned[i].Leftover);940 }941}942 943struct GetDoubleStrings {944 const char *Str;945 bool AllowInexact;946 bool ShouldFail;947 double D;948} DoubleStrings[] = {949 {"0", false, false, 0.0},950 {"0.0", false, false, 0.0},951 {"-0.0", false, false, -0.0},952 {"123.45", false, true, 123.45},953 {"123.45", true, false, 123.45},954 {"1.8e308", true, false, std::numeric_limits<double>::infinity()},955 {"1.8e308", false, true, std::numeric_limits<double>::infinity()},956 {"0x0.0000000000001P-1023", false, true, 0.0},957 {"0x0.0000000000001P-1023", true, false, 0.0},958};959 960TEST(StringRefTest, getAsDouble) {961 for (const auto &Entry : DoubleStrings) {962 double Result;963 StringRef S(Entry.Str);964 EXPECT_EQ(Entry.ShouldFail, S.getAsDouble(Result, Entry.AllowInexact));965 if (!Entry.ShouldFail) {966 EXPECT_EQ(Result, Entry.D);967 }968 }969}970 971static const char *join_input[] = { "a", "b", "c" };972static const char join_result1[] = "a";973static const char join_result2[] = "a:b:c";974static const char join_result3[] = "a::b::c";975 976TEST(StringRefTest, joinStrings) {977 std::vector<StringRef> v1;978 std::vector<std::string> v2;979 for (size_t i = 0; i < std::size(join_input); ++i) {980 v1.push_back(join_input[i]);981 v2.push_back(join_input[i]);982 }983 984 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1;985 EXPECT_TRUE(v1_join1);986 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2;987 EXPECT_TRUE(v1_join2);988 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3;989 EXPECT_TRUE(v1_join3);990 991 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1;992 EXPECT_TRUE(v2_join1);993 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2;994 EXPECT_TRUE(v2_join2);995 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3;996 EXPECT_TRUE(v2_join3);997 v2_join3 = join(v2, "::") == join_result3;998 EXPECT_TRUE(v2_join3);999}1000 1001 1002TEST(StringRefTest, AllocatorCopy) {1003 BumpPtrAllocator Alloc;1004 // First test empty strings. We don't want these to allocate anything on the1005 // allocator.1006 StringRef StrEmpty = "";1007 StringRef StrEmptyc = StrEmpty.copy(Alloc);1008 EXPECT_TRUE(StrEmpty == StrEmptyc);1009 EXPECT_EQ(StrEmptyc.data(), nullptr);1010 EXPECT_EQ(StrEmptyc.size(), 0u);1011 EXPECT_EQ(Alloc.getTotalMemory(), 0u);1012 1013 StringRef Str1 = "hello";1014 StringRef Str2 = "bye";1015 StringRef Str1c = Str1.copy(Alloc);1016 StringRef Str2c = Str2.copy(Alloc);1017 EXPECT_TRUE(Str1 == Str1c);1018 EXPECT_NE(Str1.data(), Str1c.data());1019 EXPECT_TRUE(Str2 == Str2c);1020 EXPECT_NE(Str2.data(), Str2c.data());1021}1022 1023TEST(StringRefTest, Drop) {1024 StringRef Test("StringRefTest::Drop");1025 1026 StringRef Dropped = Test.drop_front(5);1027 EXPECT_EQ(Dropped, "gRefTest::Drop");1028 1029 Dropped = Test.drop_back(5);1030 EXPECT_EQ(Dropped, "StringRefTest:");1031 1032 Dropped = Test.drop_front(0);1033 EXPECT_EQ(Dropped, Test);1034 1035 Dropped = Test.drop_back(0);1036 EXPECT_EQ(Dropped, Test);1037 1038 Dropped = Test.drop_front(Test.size());1039 EXPECT_TRUE(Dropped.empty());1040 1041 Dropped = Test.drop_back(Test.size());1042 EXPECT_TRUE(Dropped.empty());1043}1044 1045TEST(StringRefTest, Take) {1046 StringRef Test("StringRefTest::Take");1047 1048 StringRef Taken = Test.take_front(5);1049 EXPECT_EQ(Taken, "Strin");1050 1051 Taken = Test.take_back(5);1052 EXPECT_EQ(Taken, ":Take");1053 1054 Taken = Test.take_front(Test.size());1055 EXPECT_EQ(Taken, Test);1056 1057 Taken = Test.take_back(Test.size());1058 EXPECT_EQ(Taken, Test);1059 1060 Taken = Test.take_front(0);1061 EXPECT_TRUE(Taken.empty());1062 1063 Taken = Test.take_back(0);1064 EXPECT_TRUE(Taken.empty());1065}1066 1067TEST(StringRefTest, FindIf) {1068 StringRef Punct("Test.String");1069 StringRef NoPunct("ABCDEFG");1070 StringRef Empty;1071 1072 auto IsPunct = [](char c) { return ::ispunct(c); };1073 auto IsAlpha = [](char c) { return ::isalpha(c); };1074 EXPECT_EQ(4U, Punct.find_if(IsPunct));1075 EXPECT_EQ(StringRef::npos, NoPunct.find_if(IsPunct));1076 EXPECT_EQ(StringRef::npos, Empty.find_if(IsPunct));1077 1078 EXPECT_EQ(4U, Punct.find_if_not(IsAlpha));1079 EXPECT_EQ(StringRef::npos, NoPunct.find_if_not(IsAlpha));1080 EXPECT_EQ(StringRef::npos, Empty.find_if_not(IsAlpha));1081}1082 1083TEST(StringRefTest, TakeWhileUntil) {1084 StringRef Test("String With 1 Number");1085 1086 StringRef Taken = Test.take_while([](char c) { return ::isdigit(c); });1087 EXPECT_EQ("", Taken);1088 1089 Taken = Test.take_until([](char c) { return ::isdigit(c); });1090 EXPECT_EQ("String With ", Taken);1091 1092 Taken = Test.take_while([](char c) { return true; });1093 EXPECT_EQ(Test, Taken);1094 1095 Taken = Test.take_until([](char c) { return true; });1096 EXPECT_EQ("", Taken);1097 1098 Test = "";1099 Taken = Test.take_while([](char c) { return true; });1100 EXPECT_EQ("", Taken);1101}1102 1103TEST(StringRefTest, DropWhileUntil) {1104 StringRef Test("String With 1 Number");1105 1106 StringRef Taken = Test.drop_while([](char c) { return ::isdigit(c); });1107 EXPECT_EQ(Test, Taken);1108 1109 Taken = Test.drop_until([](char c) { return ::isdigit(c); });1110 EXPECT_EQ("1 Number", Taken);1111 1112 Taken = Test.drop_while([](char c) { return true; });1113 EXPECT_EQ("", Taken);1114 1115 Taken = Test.drop_until([](char c) { return true; });1116 EXPECT_EQ(Test, Taken);1117 1118 StringRef EmptyString = "";1119 Taken = EmptyString.drop_while([](char c) { return true; });1120 EXPECT_EQ("", Taken);1121}1122 1123TEST(StringRefTest, StringLiteral) {1124 constexpr StringRef StringRefs[] = {"Foo", "Bar"};1125 EXPECT_EQ(StringRef("Foo"), StringRefs[0]);1126 EXPECT_EQ(3u, (std::integral_constant<size_t, StringRefs[0].size()>::value));1127 EXPECT_EQ(false, (std::bool_constant<StringRefs[0].empty()>::value));1128 EXPECT_EQ(StringRef("Bar"), StringRefs[1]);1129 1130 constexpr StringLiteral Strings[] = {"Foo", "Bar"};1131 EXPECT_EQ(StringRef("Foo"), Strings[0]);1132 EXPECT_EQ(3u, (std::integral_constant<size_t, Strings[0].size()>::value));1133 EXPECT_EQ(false, (std::bool_constant<Strings[0].empty()>::value));1134 EXPECT_EQ(StringRef("Bar"), Strings[1]);1135}1136 1137// Check gtest prints StringRef as a string instead of a container of chars.1138// The code is in utils/unittest/googletest/internal/custom/gtest-printers.h1139TEST(StringRefTest, GTestPrinter) {1140 EXPECT_EQ(R"("foo")", ::testing::PrintToString(StringRef("foo")));1141}1142 1143TEST(StringRefTest, LFLineEnding) {1144 constexpr StringRef Cases[] = {"\nDoggo\nPupper", "Floofer\n", "Woofer"};1145 EXPECT_EQ(StringRef("\n"), Cases[0].detectEOL());1146 EXPECT_EQ(StringRef("\n"), Cases[1].detectEOL());1147 EXPECT_EQ(StringRef("\n"), Cases[2].detectEOL());1148}1149 1150TEST(StringRefTest, CRLineEnding) {1151 constexpr StringRef Cases[] = {"\rDoggo\rPupper", "Floofer\r", "Woo\rfer\n"};1152 EXPECT_EQ(StringRef("\r"), Cases[0].detectEOL());1153 EXPECT_EQ(StringRef("\r"), Cases[1].detectEOL());1154 EXPECT_EQ(StringRef("\r"), Cases[2].detectEOL());1155}1156 1157TEST(StringRefTest, CRLFLineEnding) {1158 constexpr StringRef Cases[] = {"\r\nDoggo\r\nPupper", "Floofer\r\n",1159 "Woofer\r\nSubWoofer\n"};1160 EXPECT_EQ(StringRef("\r\n"), Cases[0].detectEOL());1161 EXPECT_EQ(StringRef("\r\n"), Cases[1].detectEOL());1162 EXPECT_EQ(StringRef("\r\n"), Cases[2].detectEOL());1163}1164 1165TEST(StringRefTest, LFCRLineEnding) {1166 constexpr StringRef Cases[] = {"\n\rDoggo\n\rPupper", "Floofer\n\r",1167 "Woofer\n\rSubWoofer\n"};1168 EXPECT_EQ(StringRef("\n\r"), Cases[0].detectEOL());1169 EXPECT_EQ(StringRef("\n\r"), Cases[1].detectEOL());1170 EXPECT_EQ(StringRef("\n\r"), Cases[2].detectEOL());1171}1172 1173static_assert(std::is_trivially_copyable_v<StringRef>, "trivially copyable");1174 1175} // end anonymous namespace1176