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