1 //===- llvm/unittest/ADT/StringRefTest.cpp - StringRef unit tests ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/StringRef.h" 11 #include "llvm/ADT/StringExtras.h" 12 #include "llvm/ADT/Hashing.h" 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "gtest/gtest.h" 16 using namespace llvm; 17 18 namespace llvm { 19 20 std::ostream &operator<<(std::ostream &OS, const StringRef &S) { 21 OS << S.str(); 22 return OS; 23 } 24 25 std::ostream &operator<<(std::ostream &OS, 26 const std::pair<StringRef, StringRef> &P) { 27 OS << "(" << P.first << ", " << P.second << ")"; 28 return OS; 29 } 30 31 } 32 33 namespace { 34 TEST(StringRefTest, Construction) { 35 EXPECT_EQ("", StringRef()); 36 EXPECT_EQ("hello", StringRef("hello")); 37 EXPECT_EQ("hello", StringRef("hello world", 5)); 38 EXPECT_EQ("hello", StringRef(std::string("hello"))); 39 } 40 41 TEST(StringRefTest, Iteration) { 42 StringRef S("hello"); 43 const char *p = "hello"; 44 for (const char *it = S.begin(), *ie = S.end(); it != ie; ++it, ++p) 45 EXPECT_EQ(*it, *p); 46 } 47 48 TEST(StringRefTest, StringOps) { 49 const char *p = "hello"; 50 EXPECT_EQ(p, StringRef(p, 0).data()); 51 EXPECT_TRUE(StringRef().empty()); 52 EXPECT_EQ((size_t) 5, StringRef("hello").size()); 53 EXPECT_EQ(-1, StringRef("aab").compare("aad")); 54 EXPECT_EQ( 0, StringRef("aab").compare("aab")); 55 EXPECT_EQ( 1, StringRef("aab").compare("aaa")); 56 EXPECT_EQ(-1, StringRef("aab").compare("aabb")); 57 EXPECT_EQ( 1, StringRef("aab").compare("aa")); 58 EXPECT_EQ( 1, StringRef("\xFF").compare("\1")); 59 60 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd")); 61 EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab")); 62 EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA")); 63 EXPECT_EQ(-1, StringRef("AaB").compare_lower("aaBb")); 64 EXPECT_EQ( 1, StringRef("AaB").compare_lower("aA")); 65 EXPECT_EQ( 1, StringRef("\xFF").compare_lower("\1")); 66 67 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aad")); 68 EXPECT_EQ( 0, StringRef("aab").compare_numeric("aab")); 69 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aaa")); 70 EXPECT_EQ(-1, StringRef("aab").compare_numeric("aabb")); 71 EXPECT_EQ( 1, StringRef("aab").compare_numeric("aa")); 72 EXPECT_EQ(-1, StringRef("1").compare_numeric("10")); 73 EXPECT_EQ( 0, StringRef("10").compare_numeric("10")); 74 EXPECT_EQ( 0, StringRef("10a").compare_numeric("10a")); 75 EXPECT_EQ( 1, StringRef("2").compare_numeric("1")); 76 EXPECT_EQ( 0, StringRef("llvm_v1i64_ty").compare_numeric("llvm_v1i64_ty")); 77 EXPECT_EQ( 1, StringRef("\xFF").compare_numeric("\1")); 78 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V1_q0")); 79 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V16")); 80 EXPECT_EQ(-1, StringRef("V8_q0").compare_numeric("V16")); 81 EXPECT_EQ( 1, StringRef("V16").compare_numeric("V8_q0")); 82 EXPECT_EQ(-1, StringRef("V1_q0").compare_numeric("V8_q0")); 83 EXPECT_EQ( 1, StringRef("V8_q0").compare_numeric("V1_q0")); 84 } 85 86 TEST(StringRefTest, Operators) { 87 EXPECT_EQ("", StringRef()); 88 EXPECT_TRUE(StringRef("aab") < StringRef("aad")); 89 EXPECT_FALSE(StringRef("aab") < StringRef("aab")); 90 EXPECT_TRUE(StringRef("aab") <= StringRef("aab")); 91 EXPECT_FALSE(StringRef("aab") <= StringRef("aaa")); 92 EXPECT_TRUE(StringRef("aad") > StringRef("aab")); 93 EXPECT_FALSE(StringRef("aab") > StringRef("aab")); 94 EXPECT_TRUE(StringRef("aab") >= StringRef("aab")); 95 EXPECT_FALSE(StringRef("aaa") >= StringRef("aab")); 96 EXPECT_EQ(StringRef("aab"), StringRef("aab")); 97 EXPECT_FALSE(StringRef("aab") == StringRef("aac")); 98 EXPECT_FALSE(StringRef("aab") != StringRef("aab")); 99 EXPECT_TRUE(StringRef("aab") != StringRef("aac")); 100 EXPECT_EQ('a', StringRef("aab")[1]); 101 } 102 103 TEST(StringRefTest, Substr) { 104 StringRef Str("hello"); 105 EXPECT_EQ("lo", Str.substr(3)); 106 EXPECT_EQ("", Str.substr(100)); 107 EXPECT_EQ("hello", Str.substr(0, 100)); 108 EXPECT_EQ("o", Str.substr(4, 10)); 109 } 110 111 TEST(StringRefTest, Slice) { 112 StringRef Str("hello"); 113 EXPECT_EQ("l", Str.slice(2, 3)); 114 EXPECT_EQ("ell", Str.slice(1, 4)); 115 EXPECT_EQ("llo", Str.slice(2, 100)); 116 EXPECT_EQ("", Str.slice(2, 1)); 117 EXPECT_EQ("", Str.slice(10, 20)); 118 } 119 120 TEST(StringRefTest, Split) { 121 StringRef Str("hello"); 122 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")), 123 Str.split('X')); 124 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")), 125 Str.split('e')); 126 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")), 127 Str.split('h')); 128 EXPECT_EQ(std::make_pair(StringRef("he"), StringRef("lo")), 129 Str.split('l')); 130 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")), 131 Str.split('o')); 132 133 EXPECT_EQ(std::make_pair(StringRef("hello"), StringRef("")), 134 Str.rsplit('X')); 135 EXPECT_EQ(std::make_pair(StringRef("h"), StringRef("llo")), 136 Str.rsplit('e')); 137 EXPECT_EQ(std::make_pair(StringRef(""), StringRef("ello")), 138 Str.rsplit('h')); 139 EXPECT_EQ(std::make_pair(StringRef("hel"), StringRef("o")), 140 Str.rsplit('l')); 141 EXPECT_EQ(std::make_pair(StringRef("hell"), StringRef("")), 142 Str.rsplit('o')); 143 } 144 145 TEST(StringRefTest, Split2) { 146 SmallVector<StringRef, 5> parts; 147 SmallVector<StringRef, 5> expected; 148 149 expected.push_back("ab"); expected.push_back("c"); 150 StringRef(",ab,,c,").split(parts, ",", -1, false); 151 EXPECT_TRUE(parts == expected); 152 153 expected.clear(); parts.clear(); 154 expected.push_back(""); expected.push_back("ab"); expected.push_back(""); 155 expected.push_back("c"); expected.push_back(""); 156 StringRef(",ab,,c,").split(parts, ",", -1, true); 157 EXPECT_TRUE(parts == expected); 158 159 expected.clear(); parts.clear(); 160 expected.push_back(""); 161 StringRef("").split(parts, ",", -1, true); 162 EXPECT_TRUE(parts == expected); 163 164 expected.clear(); parts.clear(); 165 StringRef("").split(parts, ",", -1, false); 166 EXPECT_TRUE(parts == expected); 167 168 expected.clear(); parts.clear(); 169 StringRef(",").split(parts, ",", -1, false); 170 EXPECT_TRUE(parts == expected); 171 172 expected.clear(); parts.clear(); 173 expected.push_back(""); expected.push_back(""); 174 StringRef(",").split(parts, ",", -1, true); 175 EXPECT_TRUE(parts == expected); 176 177 expected.clear(); parts.clear(); 178 expected.push_back("a"); expected.push_back("b"); 179 StringRef("a,b").split(parts, ",", -1, true); 180 EXPECT_TRUE(parts == expected); 181 182 // Test MaxSplit 183 expected.clear(); parts.clear(); 184 expected.push_back("a,,b,c"); 185 StringRef("a,,b,c").split(parts, ",", 0, true); 186 EXPECT_TRUE(parts == expected); 187 188 expected.clear(); parts.clear(); 189 expected.push_back("a,,b,c"); 190 StringRef("a,,b,c").split(parts, ",", 0, false); 191 EXPECT_TRUE(parts == expected); 192 193 expected.clear(); parts.clear(); 194 expected.push_back("a"); expected.push_back(",b,c"); 195 StringRef("a,,b,c").split(parts, ",", 1, true); 196 EXPECT_TRUE(parts == expected); 197 198 expected.clear(); parts.clear(); 199 expected.push_back("a"); expected.push_back(",b,c"); 200 StringRef("a,,b,c").split(parts, ",", 1, false); 201 EXPECT_TRUE(parts == expected); 202 203 expected.clear(); parts.clear(); 204 expected.push_back("a"); expected.push_back(""); expected.push_back("b,c"); 205 StringRef("a,,b,c").split(parts, ",", 2, true); 206 EXPECT_TRUE(parts == expected); 207 208 expected.clear(); parts.clear(); 209 expected.push_back("a"); expected.push_back("b,c"); 210 StringRef("a,,b,c").split(parts, ",", 2, false); 211 EXPECT_TRUE(parts == expected); 212 213 expected.clear(); parts.clear(); 214 expected.push_back("a"); expected.push_back(""); expected.push_back("b"); 215 expected.push_back("c"); 216 StringRef("a,,b,c").split(parts, ",", 3, true); 217 EXPECT_TRUE(parts == expected); 218 219 expected.clear(); parts.clear(); 220 expected.push_back("a"); expected.push_back("b"); expected.push_back("c"); 221 StringRef("a,,b,c").split(parts, ",", 3, false); 222 EXPECT_TRUE(parts == expected); 223 } 224 225 TEST(StringRefTest, Trim) { 226 StringRef Str0("hello"); 227 StringRef Str1(" hello "); 228 StringRef Str2(" hello "); 229 230 EXPECT_EQ(StringRef("hello"), Str0.rtrim()); 231 EXPECT_EQ(StringRef(" hello"), Str1.rtrim()); 232 EXPECT_EQ(StringRef(" hello"), Str2.rtrim()); 233 EXPECT_EQ(StringRef("hello"), Str0.ltrim()); 234 EXPECT_EQ(StringRef("hello "), Str1.ltrim()); 235 EXPECT_EQ(StringRef("hello "), Str2.ltrim()); 236 EXPECT_EQ(StringRef("hello"), Str0.trim()); 237 EXPECT_EQ(StringRef("hello"), Str1.trim()); 238 EXPECT_EQ(StringRef("hello"), Str2.trim()); 239 240 EXPECT_EQ(StringRef("ello"), Str0.trim("hhhhhhhhhhh")); 241 242 EXPECT_EQ(StringRef(""), StringRef("").trim()); 243 EXPECT_EQ(StringRef(""), StringRef(" ").trim()); 244 EXPECT_EQ(StringRef("\0", 1), StringRef(" \0 ", 3).trim()); 245 EXPECT_EQ(StringRef("\0\0", 2), StringRef("\0\0", 2).trim()); 246 EXPECT_EQ(StringRef("x"), StringRef("\0\0x\0\0", 5).trim(StringRef("\0", 1))); 247 } 248 249 TEST(StringRefTest, StartsWith) { 250 StringRef Str("hello"); 251 EXPECT_TRUE(Str.startswith("he")); 252 EXPECT_FALSE(Str.startswith("helloworld")); 253 EXPECT_FALSE(Str.startswith("hi")); 254 } 255 256 TEST(StringRefTest, EndsWith) { 257 StringRef Str("hello"); 258 EXPECT_TRUE(Str.endswith("lo")); 259 EXPECT_FALSE(Str.endswith("helloworld")); 260 EXPECT_FALSE(Str.endswith("worldhello")); 261 EXPECT_FALSE(Str.endswith("so")); 262 } 263 264 TEST(StringRefTest, Find) { 265 StringRef Str("hello"); 266 EXPECT_EQ(2U, Str.find('l')); 267 EXPECT_EQ(StringRef::npos, Str.find('z')); 268 EXPECT_EQ(StringRef::npos, Str.find("helloworld")); 269 EXPECT_EQ(0U, Str.find("hello")); 270 EXPECT_EQ(1U, Str.find("ello")); 271 EXPECT_EQ(StringRef::npos, Str.find("zz")); 272 EXPECT_EQ(2U, Str.find("ll", 2)); 273 EXPECT_EQ(StringRef::npos, Str.find("ll", 3)); 274 EXPECT_EQ(0U, Str.find("")); 275 StringRef LongStr("hellx xello hell ello world foo bar hello"); 276 EXPECT_EQ(36U, LongStr.find("hello")); 277 EXPECT_EQ(28U, LongStr.find("foo")); 278 EXPECT_EQ(12U, LongStr.find("hell", 2)); 279 EXPECT_EQ(0U, LongStr.find("")); 280 281 EXPECT_EQ(3U, Str.rfind('l')); 282 EXPECT_EQ(StringRef::npos, Str.rfind('z')); 283 EXPECT_EQ(StringRef::npos, Str.rfind("helloworld")); 284 EXPECT_EQ(0U, Str.rfind("hello")); 285 EXPECT_EQ(1U, Str.rfind("ello")); 286 EXPECT_EQ(StringRef::npos, Str.rfind("zz")); 287 288 EXPECT_EQ(2U, Str.find_first_of('l')); 289 EXPECT_EQ(1U, Str.find_first_of("el")); 290 EXPECT_EQ(StringRef::npos, Str.find_first_of("xyz")); 291 292 EXPECT_EQ(1U, Str.find_first_not_of('h')); 293 EXPECT_EQ(4U, Str.find_first_not_of("hel")); 294 EXPECT_EQ(StringRef::npos, Str.find_first_not_of("hello")); 295 296 EXPECT_EQ(3U, Str.find_last_not_of('o')); 297 EXPECT_EQ(1U, Str.find_last_not_of("lo")); 298 EXPECT_EQ(StringRef::npos, Str.find_last_not_of("helo")); 299 } 300 301 TEST(StringRefTest, Count) { 302 StringRef Str("hello"); 303 EXPECT_EQ(2U, Str.count('l')); 304 EXPECT_EQ(1U, Str.count('o')); 305 EXPECT_EQ(0U, Str.count('z')); 306 EXPECT_EQ(0U, Str.count("helloworld")); 307 EXPECT_EQ(1U, Str.count("hello")); 308 EXPECT_EQ(1U, Str.count("ello")); 309 EXPECT_EQ(0U, Str.count("zz")); 310 } 311 312 TEST(StringRefTest, EditDistance) { 313 StringRef Str("hello"); 314 EXPECT_EQ(2U, Str.edit_distance("hill")); 315 } 316 317 TEST(StringRefTest, Misc) { 318 std::string Storage; 319 raw_string_ostream OS(Storage); 320 OS << StringRef("hello"); 321 EXPECT_EQ("hello", OS.str()); 322 } 323 324 TEST(StringRefTest, Hashing) { 325 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef())); 326 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef(""))); 327 std::string S = "hello world"; 328 hash_code H = hash_value(S); 329 EXPECT_EQ(H, hash_value(StringRef("hello world"))); 330 EXPECT_EQ(H, hash_value(StringRef(S))); 331 EXPECT_NE(H, hash_value(StringRef("hello worl"))); 332 EXPECT_EQ(hash_value(std::string("hello worl")), 333 hash_value(StringRef("hello worl"))); 334 EXPECT_NE(H, hash_value(StringRef("hello world "))); 335 EXPECT_EQ(hash_value(std::string("hello world ")), 336 hash_value(StringRef("hello world "))); 337 EXPECT_EQ(H, hash_value(StringRef("hello world\0"))); 338 EXPECT_NE(hash_value(std::string("ello worl")), 339 hash_value(StringRef("hello world").slice(1, -1))); 340 } 341 342 struct UnsignedPair { 343 const char *Str; 344 uint64_t Expected; 345 } Unsigned[] = 346 { {"0", 0} 347 , {"255", 255} 348 , {"256", 256} 349 , {"65535", 65535} 350 , {"65536", 65536} 351 , {"4294967295", 4294967295ULL} 352 , {"4294967296", 4294967296ULL} 353 , {"18446744073709551615", 18446744073709551615ULL} 354 , {"042", 34} 355 , {"0x42", 66} 356 , {"0b101010", 42} 357 }; 358 359 struct SignedPair { 360 const char *Str; 361 int64_t Expected; 362 } Signed[] = 363 { {"0", 0} 364 , {"-0", 0} 365 , {"127", 127} 366 , {"128", 128} 367 , {"-128", -128} 368 , {"-129", -129} 369 , {"32767", 32767} 370 , {"32768", 32768} 371 , {"-32768", -32768} 372 , {"-32769", -32769} 373 , {"2147483647", 2147483647LL} 374 , {"2147483648", 2147483648LL} 375 , {"-2147483648", -2147483648LL} 376 , {"-2147483649", -2147483649LL} 377 , {"-9223372036854775808", -(9223372036854775807LL) - 1} 378 , {"042", 34} 379 , {"0x42", 66} 380 , {"0b101010", 42} 381 , {"-042", -34} 382 , {"-0x42", -66} 383 , {"-0b101010", -42} 384 }; 385 386 TEST(StringRefTest, getAsInteger) { 387 uint8_t U8; 388 uint16_t U16; 389 uint32_t U32; 390 uint64_t U64; 391 392 for (size_t i = 0; i < array_lengthof(Unsigned); ++i) { 393 bool U8Success = StringRef(Unsigned[i].Str).getAsInteger(0, U8); 394 if (static_cast<uint8_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 395 ASSERT_FALSE(U8Success); 396 EXPECT_EQ(U8, Unsigned[i].Expected); 397 } else { 398 ASSERT_TRUE(U8Success); 399 } 400 bool U16Success = StringRef(Unsigned[i].Str).getAsInteger(0, U16); 401 if (static_cast<uint16_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 402 ASSERT_FALSE(U16Success); 403 EXPECT_EQ(U16, Unsigned[i].Expected); 404 } else { 405 ASSERT_TRUE(U16Success); 406 } 407 bool U32Success = StringRef(Unsigned[i].Str).getAsInteger(0, U32); 408 if (static_cast<uint32_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 409 ASSERT_FALSE(U32Success); 410 EXPECT_EQ(U32, Unsigned[i].Expected); 411 } else { 412 ASSERT_TRUE(U32Success); 413 } 414 bool U64Success = StringRef(Unsigned[i].Str).getAsInteger(0, U64); 415 if (static_cast<uint64_t>(Unsigned[i].Expected) == Unsigned[i].Expected) { 416 ASSERT_FALSE(U64Success); 417 EXPECT_EQ(U64, Unsigned[i].Expected); 418 } else { 419 ASSERT_TRUE(U64Success); 420 } 421 } 422 423 int8_t S8; 424 int16_t S16; 425 int32_t S32; 426 int64_t S64; 427 428 for (size_t i = 0; i < array_lengthof(Signed); ++i) { 429 bool S8Success = StringRef(Signed[i].Str).getAsInteger(0, S8); 430 if (static_cast<int8_t>(Signed[i].Expected) == Signed[i].Expected) { 431 ASSERT_FALSE(S8Success); 432 EXPECT_EQ(S8, Signed[i].Expected); 433 } else { 434 ASSERT_TRUE(S8Success); 435 } 436 bool S16Success = StringRef(Signed[i].Str).getAsInteger(0, S16); 437 if (static_cast<int16_t>(Signed[i].Expected) == Signed[i].Expected) { 438 ASSERT_FALSE(S16Success); 439 EXPECT_EQ(S16, Signed[i].Expected); 440 } else { 441 ASSERT_TRUE(S16Success); 442 } 443 bool S32Success = StringRef(Signed[i].Str).getAsInteger(0, S32); 444 if (static_cast<int32_t>(Signed[i].Expected) == Signed[i].Expected) { 445 ASSERT_FALSE(S32Success); 446 EXPECT_EQ(S32, Signed[i].Expected); 447 } else { 448 ASSERT_TRUE(S32Success); 449 } 450 bool S64Success = StringRef(Signed[i].Str).getAsInteger(0, S64); 451 if (static_cast<int64_t>(Signed[i].Expected) == Signed[i].Expected) { 452 ASSERT_FALSE(S64Success); 453 EXPECT_EQ(S64, Signed[i].Expected); 454 } else { 455 ASSERT_TRUE(S64Success); 456 } 457 } 458 } 459 460 461 static const char* BadStrings[] = { 462 "18446744073709551617" // value just over max 463 , "123456789012345678901" // value way too large 464 , "4t23v" // illegal decimal characters 465 , "0x123W56" // illegal hex characters 466 , "0b2" // illegal bin characters 467 , "08" // illegal oct characters 468 , "0o8" // illegal oct characters 469 , "-123" // negative unsigned value 470 }; 471 472 473 TEST(StringRefTest, getAsUnsignedIntegerBadStrings) { 474 unsigned long long U64; 475 for (size_t i = 0; i < array_lengthof(BadStrings); ++i) { 476 bool IsBadNumber = StringRef(BadStrings[i]).getAsInteger(0, U64); 477 ASSERT_TRUE(IsBadNumber); 478 } 479 } 480 481 static const char *join_input[] = { "a", "b", "c" }; 482 static const char join_result1[] = "a"; 483 static const char join_result2[] = "a:b:c"; 484 static const char join_result3[] = "a::b::c"; 485 486 TEST(StringRefTest, joinStrings) { 487 std::vector<StringRef> v1; 488 std::vector<std::string> v2; 489 for (size_t i = 0; i < array_lengthof(join_input); ++i) { 490 v1.push_back(join_input[i]); 491 v2.push_back(join_input[i]); 492 } 493 494 bool v1_join1 = join(v1.begin(), v1.begin() + 1, ":") == join_result1; 495 EXPECT_TRUE(v1_join1); 496 bool v1_join2 = join(v1.begin(), v1.end(), ":") == join_result2; 497 EXPECT_TRUE(v1_join2); 498 bool v1_join3 = join(v1.begin(), v1.end(), "::") == join_result3; 499 EXPECT_TRUE(v1_join3); 500 501 bool v2_join1 = join(v2.begin(), v2.begin() + 1, ":") == join_result1; 502 EXPECT_TRUE(v2_join1); 503 bool v2_join2 = join(v2.begin(), v2.end(), ":") == join_result2; 504 EXPECT_TRUE(v2_join2); 505 bool v2_join3 = join(v2.begin(), v2.end(), "::") == join_result3; 506 EXPECT_TRUE(v2_join3); 507 } 508 509 } // end anonymous namespace 510