1 //===- STLExtrasTest.cpp - Unit tests for STL extras ----------------------===// 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/STLExtras.h" 10 #include "llvm/ADT/StringRef.h" 11 #include "gmock/gmock.h" 12 #include "gtest/gtest.h" 13 14 #include <array> 15 #include <climits> 16 #include <cstddef> 17 #include <initializer_list> 18 #include <iterator> 19 #include <list> 20 #include <tuple> 21 #include <type_traits> 22 #include <unordered_set> 23 #include <utility> 24 #include <vector> 25 26 using namespace llvm; 27 28 using testing::ElementsAre; 29 using testing::UnorderedElementsAre; 30 31 namespace { 32 33 int f(rank<0>) { return 0; } 34 int f(rank<1>) { return 1; } 35 int f(rank<2>) { return 2; } 36 int f(rank<4>) { return 4; } 37 38 TEST(STLExtrasTest, Rank) { 39 // We shouldn't get ambiguities and should select the overload of the same 40 // rank as the argument. 41 EXPECT_EQ(0, f(rank<0>())); 42 EXPECT_EQ(1, f(rank<1>())); 43 EXPECT_EQ(2, f(rank<2>())); 44 45 // This overload is missing so we end up back at 2. 46 EXPECT_EQ(2, f(rank<3>())); 47 48 // But going past 3 should work fine. 49 EXPECT_EQ(4, f(rank<4>())); 50 51 // And we can even go higher and just fall back to the last overload. 52 EXPECT_EQ(4, f(rank<5>())); 53 EXPECT_EQ(4, f(rank<6>())); 54 } 55 56 TEST(STLExtrasTest, EnumerateLValue) { 57 // Test that a simple LValue can be enumerated and gives correct results with 58 // multiple types, including the empty container. 59 std::vector<char> foo = {'a', 'b', 'c'}; 60 typedef std::pair<std::size_t, char> CharPairType; 61 std::vector<CharPairType> CharResults; 62 63 for (auto [index, value] : llvm::enumerate(foo)) { 64 CharResults.emplace_back(index, value); 65 } 66 67 EXPECT_THAT(CharResults, 68 ElementsAre(CharPairType(0u, 'a'), CharPairType(1u, 'b'), 69 CharPairType(2u, 'c'))); 70 71 // Test a const range of a different type. 72 typedef std::pair<std::size_t, int> IntPairType; 73 std::vector<IntPairType> IntResults; 74 const std::vector<int> bar = {1, 2, 3}; 75 for (auto [index, value] : llvm::enumerate(bar)) { 76 IntResults.emplace_back(index, value); 77 } 78 EXPECT_THAT(IntResults, ElementsAre(IntPairType(0u, 1), IntPairType(1u, 2), 79 IntPairType(2u, 3))); 80 81 // Test an empty range. 82 IntResults.clear(); 83 const std::vector<int> baz{}; 84 for (auto [index, value] : llvm::enumerate(baz)) { 85 IntResults.emplace_back(index, value); 86 } 87 EXPECT_TRUE(IntResults.empty()); 88 } 89 90 TEST(STLExtrasTest, EnumerateModifyLValue) { 91 // Test that you can modify the underlying entries of an lvalue range through 92 // the enumeration iterator. 93 std::vector<char> foo = {'a', 'b', 'c'}; 94 95 for (auto X : llvm::enumerate(foo)) { 96 ++X.value(); 97 } 98 EXPECT_THAT(foo, ElementsAre('b', 'c', 'd')); 99 100 // Also test if this works with structured bindings. 101 foo = {'a', 'b', 'c'}; 102 103 for (auto [index, value] : llvm::enumerate(foo)) { 104 ++value; 105 } 106 EXPECT_THAT(foo, ElementsAre('b', 'c', 'd')); 107 } 108 109 TEST(STLExtrasTest, EnumerateRValueRef) { 110 // Test that an rvalue can be enumerated. 111 typedef std::pair<std::size_t, int> PairType; 112 std::vector<PairType> Results; 113 114 auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3}); 115 116 for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) { 117 Results.emplace_back(X.index(), X.value()); 118 } 119 120 EXPECT_THAT(Results, 121 ElementsAre(PairType(0u, 1), PairType(1u, 2), PairType(2u, 3))); 122 123 // Also test if this works with structured bindings. 124 Results.clear(); 125 126 for (auto [index, value] : llvm::enumerate(std::vector<int>{1, 2, 3})) { 127 Results.emplace_back(index, value); 128 } 129 130 EXPECT_THAT(Results, 131 ElementsAre(PairType(0u, 1), PairType(1u, 2), PairType(2u, 3))); 132 } 133 134 TEST(STLExtrasTest, EnumerateModifyRValue) { 135 // Test that when enumerating an rvalue, modification still works (even if 136 // this isn't terribly useful, it at least shows that we haven't snuck an 137 // extra const in there somewhere. 138 typedef std::pair<std::size_t, char> PairType; 139 std::vector<PairType> Results; 140 141 for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) { 142 ++X.value(); 143 Results.emplace_back(X.index(), X.value()); 144 } 145 146 EXPECT_THAT(Results, ElementsAre(PairType(0u, '2'), PairType(1u, '3'), 147 PairType(2u, '4'))); 148 149 // Also test if this works with structured bindings. 150 Results.clear(); 151 152 for (auto [index, value] : 153 llvm::enumerate(std::vector<char>{'1', '2', '3'})) { 154 ++value; 155 Results.emplace_back(index, value); 156 } 157 158 EXPECT_THAT(Results, ElementsAre(PairType(0u, '2'), PairType(1u, '3'), 159 PairType(2u, '4'))); 160 } 161 162 TEST(STLExtrasTest, EnumerateTwoRanges) { 163 using Tuple = std::tuple<size_t, int, bool>; 164 165 std::vector<int> Ints = {1, 2}; 166 std::vector<bool> Bools = {true, false}; 167 EXPECT_THAT(llvm::enumerate(Ints, Bools), 168 ElementsAre(Tuple(0, 1, true), Tuple(1, 2, false))); 169 170 // Check that we can modify the values when the temporary is a const 171 // reference. 172 for (const auto &[Idx, Int, Bool] : llvm::enumerate(Ints, Bools)) { 173 (void)Idx; 174 Bool = false; 175 Int = -1; 176 } 177 178 EXPECT_THAT(Ints, ElementsAre(-1, -1)); 179 EXPECT_THAT(Bools, ElementsAre(false, false)); 180 181 // Check that we can modify the values when the result gets copied. 182 for (auto [Idx, Bool, Int] : llvm::enumerate(Bools, Ints)) { 183 (void)Idx; 184 Int = 3; 185 Bool = true; 186 } 187 188 EXPECT_THAT(Ints, ElementsAre(3, 3)); 189 EXPECT_THAT(Bools, ElementsAre(true, true)); 190 191 // Check that we can modify the values through `.value()`. 192 size_t Iters = 0; 193 for (auto It : llvm::enumerate(Bools, Ints)) { 194 EXPECT_EQ(It.index(), Iters); 195 ++Iters; 196 197 std::get<0>(It.value()) = false; 198 std::get<1>(It.value()) = 4; 199 } 200 201 EXPECT_THAT(Ints, ElementsAre(4, 4)); 202 EXPECT_THAT(Bools, ElementsAre(false, false)); 203 } 204 205 TEST(STLExtrasTest, EnumerateThreeRanges) { 206 using Tuple = std::tuple<size_t, int, bool, char>; 207 208 std::vector<int> Ints = {1, 2}; 209 std::vector<bool> Bools = {true, false}; 210 char Chars[] = {'X', 'D'}; 211 EXPECT_THAT(llvm::enumerate(Ints, Bools, Chars), 212 ElementsAre(Tuple(0, 1, true, 'X'), Tuple(1, 2, false, 'D'))); 213 214 for (auto [Idx, Int, Bool, Char] : llvm::enumerate(Ints, Bools, Chars)) { 215 (void)Idx; 216 Int = 0; 217 Bool = true; 218 Char = '!'; 219 } 220 221 EXPECT_THAT(Ints, ElementsAre(0, 0)); 222 EXPECT_THAT(Bools, ElementsAre(true, true)); 223 EXPECT_THAT(Chars, ElementsAre('!', '!')); 224 225 // Check that we can modify the values through `.values()`. 226 size_t Iters = 0; 227 for (auto It : llvm::enumerate(Ints, Bools, Chars)) { 228 EXPECT_EQ(It.index(), Iters); 229 ++Iters; 230 auto [Int, Bool, Char] = It.value(); 231 Int = 42; 232 Bool = false; 233 Char = '$'; 234 } 235 236 EXPECT_THAT(Ints, ElementsAre(42, 42)); 237 EXPECT_THAT(Bools, ElementsAre(false, false)); 238 EXPECT_THAT(Chars, ElementsAre('$', '$')); 239 } 240 241 TEST(STLExtrasTest, EnumerateTemporaries) { 242 using Tuple = std::tuple<size_t, int, bool>; 243 244 EXPECT_THAT( 245 llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), 246 std::vector<bool>({true, false, true})), 247 ElementsAre(Tuple(0, 1, true), Tuple(1, 2, false), Tuple(2, 3, true))); 248 249 size_t Iters = 0; 250 // This is fine from the point of view of range lifetimes because `zippy` will 251 // move all temporaries into its storage. No lifetime extension is necessary. 252 for (auto [Idx, Int, Bool] : 253 llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), 254 std::vector<bool>({true, false, true}))) { 255 EXPECT_EQ(Idx, Iters); 256 ++Iters; 257 Int = 0; 258 Bool = true; 259 } 260 261 Iters = 0; 262 // The same thing but with the result as a const reference. 263 for (const auto &[Idx, Int, Bool] : 264 llvm::enumerate(llvm::SmallVector<int>({1, 2, 3}), 265 std::vector<bool>({true, false, true}))) { 266 EXPECT_EQ(Idx, Iters); 267 ++Iters; 268 Int = 0; 269 Bool = true; 270 } 271 } 272 273 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) 274 TEST(STLExtrasTest, EnumerateDifferentLengths) { 275 std::vector<int> Ints = {0, 1}; 276 bool Bools[] = {true, false, true}; 277 std::string Chars = "abc"; 278 EXPECT_DEATH(llvm::enumerate(Ints, Bools, Chars), 279 "Ranges have different length"); 280 EXPECT_DEATH(llvm::enumerate(Bools, Ints, Chars), 281 "Ranges have different length"); 282 EXPECT_DEATH(llvm::enumerate(Bools, Chars, Ints), 283 "Ranges have different length"); 284 } 285 #endif 286 287 template <bool B> struct CanMove {}; 288 template <> struct CanMove<false> { 289 CanMove(CanMove &&) = delete; 290 291 CanMove() = default; 292 CanMove(const CanMove &) = default; 293 }; 294 295 template <bool B> struct CanCopy {}; 296 template <> struct CanCopy<false> { 297 CanCopy(const CanCopy &) = delete; 298 299 CanCopy() = default; 300 CanCopy(CanCopy &&) = default; 301 }; 302 303 template <bool Moveable, bool Copyable> 304 class Counted : CanMove<Moveable>, CanCopy<Copyable> { 305 int &C; 306 int &M; 307 int &D; 308 309 public: 310 explicit Counted(int &C, int &M, int &D) : C(C), M(M), D(D) {} 311 Counted(const Counted &O) : CanCopy<Copyable>(O), C(O.C), M(O.M), D(O.D) { 312 ++C; 313 } 314 Counted(Counted &&O) 315 : CanMove<Moveable>(std::move(O)), C(O.C), M(O.M), D(O.D) { 316 ++M; 317 } 318 ~Counted() { ++D; } 319 }; 320 321 template <bool Moveable, bool Copyable> 322 struct Range : Counted<Moveable, Copyable> { 323 using Counted<Moveable, Copyable>::Counted; 324 int *begin() const { return nullptr; } 325 int *end() const { return nullptr; } 326 }; 327 328 TEST(STLExtrasTest, EnumerateLifetimeSemanticsPRValue) { 329 int Copies = 0; 330 int Moves = 0; 331 int Destructors = 0; 332 { 333 auto E = enumerate(Range<true, false>(Copies, Moves, Destructors)); 334 (void)E; 335 // Doesn't compile. rvalue ranges must be moveable. 336 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); 337 EXPECT_EQ(0, Copies); 338 EXPECT_EQ(1, Moves); 339 EXPECT_EQ(1, Destructors); 340 } 341 EXPECT_EQ(0, Copies); 342 EXPECT_EQ(1, Moves); 343 EXPECT_EQ(2, Destructors); 344 } 345 346 TEST(STLExtrasTest, EnumerateLifetimeSemanticsRValue) { 347 // With an rvalue, it should not be destroyed until the end of the scope. 348 int Copies = 0; 349 int Moves = 0; 350 int Destructors = 0; 351 { 352 Range<true, false> R(Copies, Moves, Destructors); 353 { 354 auto E = enumerate(std::move(R)); 355 (void)E; 356 // Doesn't compile. rvalue ranges must be moveable. 357 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); 358 EXPECT_EQ(0, Copies); 359 EXPECT_EQ(1, Moves); 360 EXPECT_EQ(0, Destructors); 361 } 362 EXPECT_EQ(0, Copies); 363 EXPECT_EQ(1, Moves); 364 EXPECT_EQ(1, Destructors); 365 } 366 EXPECT_EQ(0, Copies); 367 EXPECT_EQ(1, Moves); 368 EXPECT_EQ(2, Destructors); 369 } 370 371 TEST(STLExtrasTest, EnumerateLifetimeSemanticsLValue) { 372 // With an lvalue, it should not be destroyed even after the end of the scope. 373 // lvalue ranges need be neither copyable nor moveable. 374 int Copies = 0; 375 int Moves = 0; 376 int Destructors = 0; 377 { 378 Range<false, false> R(Copies, Moves, Destructors); 379 { 380 auto E = enumerate(R); 381 (void)E; 382 EXPECT_EQ(0, Copies); 383 EXPECT_EQ(0, Moves); 384 EXPECT_EQ(0, Destructors); 385 } 386 EXPECT_EQ(0, Copies); 387 EXPECT_EQ(0, Moves); 388 EXPECT_EQ(0, Destructors); 389 } 390 EXPECT_EQ(0, Copies); 391 EXPECT_EQ(0, Moves); 392 EXPECT_EQ(1, Destructors); 393 } 394 395 namespace some_namespace { 396 struct some_struct { 397 std::vector<int> data; 398 std::string swap_val; 399 }; 400 401 std::vector<int>::const_iterator begin(const some_struct &s) { 402 return s.data.begin(); 403 } 404 405 std::vector<int>::const_iterator end(const some_struct &s) { 406 return s.data.end(); 407 } 408 409 std::vector<int>::const_reverse_iterator rbegin(const some_struct &s) { 410 return s.data.rbegin(); 411 } 412 413 std::vector<int>::const_reverse_iterator rend(const some_struct &s) { 414 return s.data.rend(); 415 } 416 417 void swap(some_struct &lhs, some_struct &rhs) { 418 // make swap visible as non-adl swap would even seem to 419 // work with std::swap which defaults to moving 420 lhs.swap_val = "lhs"; 421 rhs.swap_val = "rhs"; 422 } 423 424 struct requires_move {}; 425 int *begin(requires_move &&) { return nullptr; } 426 int *end(requires_move &&) { return nullptr; } 427 } // namespace some_namespace 428 429 TEST(STLExtrasTest, EnumerateCustomBeginEnd) { 430 // Check that `enumerate` uses ADL to find `begin`/`end` iterators 431 // of the enumerated type. 432 some_namespace::some_struct X{}; 433 X.data = {1, 2, 3}; 434 435 unsigned Iters = 0; 436 for (auto [Idx, Val] : enumerate(X)) { 437 EXPECT_EQ(Val, X.data[Idx]); 438 ++Iters; 439 } 440 EXPECT_EQ(Iters, 3u); 441 } 442 443 TEST(STLExtrasTest, CountAdaptor) { 444 std::vector<int> v; 445 446 v.push_back(1); 447 v.push_back(2); 448 v.push_back(1); 449 v.push_back(4); 450 v.push_back(3); 451 v.push_back(2); 452 v.push_back(1); 453 454 EXPECT_EQ(3, count(v, 1)); 455 EXPECT_EQ(2, count(v, 2)); 456 EXPECT_EQ(1, count(v, 3)); 457 EXPECT_EQ(1, count(v, 4)); 458 } 459 460 TEST(STLExtrasTest, for_each) { 461 std::vector<int> v{0, 1, 2, 3, 4}; 462 int count = 0; 463 464 llvm::for_each(v, [&count](int) { ++count; }); 465 EXPECT_EQ(5, count); 466 } 467 468 TEST(STLExtrasTest, ToVector) { 469 std::vector<char> v = {'a', 'b', 'c'}; 470 auto Enumerated = to_vector<4>(enumerate(v)); 471 ASSERT_EQ(3u, Enumerated.size()); 472 for (size_t I = 0; I < v.size(); ++I) { 473 EXPECT_EQ(I, Enumerated[I].index()); 474 EXPECT_EQ(v[I], Enumerated[I].value()); 475 } 476 477 auto EnumeratedImplicitSize = to_vector(enumerate(v)); 478 ASSERT_EQ(3u, EnumeratedImplicitSize.size()); 479 for (size_t I = 0; I < v.size(); ++I) { 480 EXPECT_EQ(I, EnumeratedImplicitSize[I].index()); 481 EXPECT_EQ(v[I], EnumeratedImplicitSize[I].value()); 482 } 483 } 484 485 TEST(STLExtrasTest, ConcatRange) { 486 std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8}; 487 std::vector<int> Test; 488 489 std::vector<int> V1234 = {1, 2, 3, 4}; 490 std::list<int> L56 = {5, 6}; 491 SmallVector<int, 2> SV78 = {7, 8}; 492 493 // Use concat across different sized ranges of different types with different 494 // iterators. 495 for (int &i : concat<int>(V1234, L56, SV78)) 496 Test.push_back(i); 497 EXPECT_EQ(Expected, Test); 498 499 // Use concat between a temporary, an L-value, and an R-value to make sure 500 // complex lifetimes work well. 501 Test.clear(); 502 for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78))) 503 Test.push_back(i); 504 EXPECT_EQ(Expected, Test); 505 } 506 507 TEST(STLExtrasTest, PartitionAdaptor) { 508 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 509 510 auto I = partition(V, [](int i) { return i % 2 == 0; }); 511 ASSERT_EQ(V.begin() + 4, I); 512 513 // Sort the two halves as partition may have messed with the order. 514 llvm::sort(V.begin(), I); 515 llvm::sort(I, V.end()); 516 517 EXPECT_EQ(2, V[0]); 518 EXPECT_EQ(4, V[1]); 519 EXPECT_EQ(6, V[2]); 520 EXPECT_EQ(8, V[3]); 521 EXPECT_EQ(1, V[4]); 522 EXPECT_EQ(3, V[5]); 523 EXPECT_EQ(5, V[6]); 524 EXPECT_EQ(7, V[7]); 525 } 526 527 TEST(STLExtrasTest, EraseIf) { 528 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 529 530 erase_if(V, [](int i) { return i % 2 == 0; }); 531 EXPECT_EQ(4u, V.size()); 532 EXPECT_EQ(1, V[0]); 533 EXPECT_EQ(3, V[1]); 534 EXPECT_EQ(5, V[2]); 535 EXPECT_EQ(7, V[3]); 536 } 537 538 TEST(STLExtrasTest, AppendRange) { 539 std::vector<int> V = {1, 2}; 540 auto AppendVals1 = {3}; 541 append_range(V, AppendVals1); 542 EXPECT_THAT(V, ElementsAre(1, 2, 3)); 543 544 int AppendVals2[] = {4, 5}; 545 append_range(V, AppendVals2); 546 EXPECT_THAT(V, ElementsAre(1, 2, 3, 4, 5)); 547 548 std::string Str; 549 append_range(Str, "abc"); 550 EXPECT_THAT(Str, ElementsAre('a', 'b', 'c', '\0')); 551 append_range(Str, "def"); 552 EXPECT_THAT(Str, ElementsAre('a', 'b', 'c', '\0', 'd', 'e', 'f', '\0')); 553 } 554 555 TEST(STLExtrasTest, AppendValues) { 556 std::vector<int> Vals = {1, 2}; 557 append_values(Vals, 3); 558 EXPECT_THAT(Vals, ElementsAre(1, 2, 3)); 559 560 append_values(Vals, 4, 5); 561 EXPECT_THAT(Vals, ElementsAre(1, 2, 3, 4, 5)); 562 563 std::vector<StringRef> Strs; 564 std::string A = "A"; 565 std::string B = "B"; 566 std::string C = "C"; 567 append_values(Strs, A, B); 568 EXPECT_THAT(Strs, ElementsAre(A, B)); 569 append_values(Strs, C); 570 EXPECT_THAT(Strs, ElementsAre(A, B, C)); 571 572 std::unordered_set<int> Set; 573 append_values(Set, 1, 2); 574 EXPECT_THAT(Set, UnorderedElementsAre(1, 2)); 575 append_values(Set, 3, 1); 576 EXPECT_THAT(Set, UnorderedElementsAre(1, 2, 3)); 577 } 578 579 TEST(STLExtrasTest, ADLTest) { 580 some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""}; 581 some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""}; 582 583 EXPECT_EQ(*adl_begin(s), 1); 584 EXPECT_EQ(*(adl_end(s) - 1), 5); 585 EXPECT_EQ(*adl_rbegin(s), 5); 586 EXPECT_EQ(*(adl_rend(s) - 1), 1); 587 588 adl_swap(s, s2); 589 EXPECT_EQ(s.swap_val, "lhs"); 590 EXPECT_EQ(s2.swap_val, "rhs"); 591 592 int count = 0; 593 llvm::for_each(s, [&count](int) { ++count; }); 594 EXPECT_EQ(count, 5); 595 } 596 597 TEST(STLExtrasTest, ADLTestTemporaryRange) { 598 EXPECT_EQ(adl_begin(some_namespace::requires_move{}), nullptr); 599 EXPECT_EQ(adl_end(some_namespace::requires_move{}), nullptr); 600 } 601 602 TEST(STLExtrasTest, ADLTestConstexpr) { 603 // `std::begin`/`std::end` are marked as `constexpr`; check that 604 // `adl_begin`/`adl_end` also work in constant-evaluated contexts. 605 static constexpr int c_arr[] = {7, 8, 9}; 606 static_assert(adl_begin(c_arr) == c_arr); 607 static_assert(adl_end(c_arr) == c_arr + 3); 608 609 static constexpr std::array<int, 2> std_arr = {1, 2}; 610 static_assert(adl_begin(std_arr) == std_arr.begin()); 611 static_assert(adl_end(std_arr) == std_arr.end()); 612 SUCCEED(); 613 } 614 615 struct FooWithMemberSize { 616 size_t size() const { return 42; } 617 auto begin() { return Data.begin(); } 618 auto end() { return Data.end(); } 619 620 std::set<int> Data; 621 }; 622 623 namespace some_namespace { 624 struct FooWithFreeSize { 625 auto begin() { return Data.begin(); } 626 auto end() { return Data.end(); } 627 628 std::set<int> Data; 629 }; 630 631 size_t size(const FooWithFreeSize &) { return 13; } 632 } // namespace some_namespace 633 634 TEST(STLExtrasTest, ADLSizeTest) { 635 FooWithMemberSize foo1; 636 EXPECT_EQ(adl_size(foo1), 42u); 637 638 some_namespace::FooWithFreeSize foo2; 639 EXPECT_EQ(adl_size(foo2), 13u); 640 641 static constexpr int c_arr[] = {1, 2, 3}; 642 static_assert(adl_size(c_arr) == 3u); 643 644 static constexpr std::array<int, 4> cpp_arr = {}; 645 static_assert(adl_size(cpp_arr) == 4u); 646 } 647 648 TEST(STLExtrasTest, DropBeginTest) { 649 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 650 651 for (int n = 0; n < 5; ++n) { 652 int i = n; 653 for (auto &v : drop_begin(vec, n)) { 654 EXPECT_EQ(v, i); 655 i += 1; 656 } 657 EXPECT_EQ(i, 5); 658 } 659 } 660 661 TEST(STLExtrasTest, DropBeginDefaultTest) { 662 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 663 664 int i = 1; 665 for (auto &v : drop_begin(vec)) { 666 EXPECT_EQ(v, i); 667 i += 1; 668 } 669 EXPECT_EQ(i, 5); 670 } 671 672 TEST(STLExtrasTest, DropEndTest) { 673 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 674 675 for (int n = 0; n < 5; ++n) { 676 int i = 0; 677 for (auto &v : drop_end(vec, n)) { 678 EXPECT_EQ(v, i); 679 i += 1; 680 } 681 EXPECT_EQ(i, 5 - n); 682 } 683 } 684 685 TEST(STLExtrasTest, DropEndDefaultTest) { 686 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 687 688 int i = 0; 689 for (auto &v : drop_end(vec)) { 690 EXPECT_EQ(v, i); 691 i += 1; 692 } 693 EXPECT_EQ(i, 4); 694 } 695 696 TEST(STLExtrasTest, EarlyIncrementTest) { 697 std::list<int> L = {1, 2, 3, 4}; 698 699 auto EIR = make_early_inc_range(L); 700 701 auto I = EIR.begin(); 702 auto EI = EIR.end(); 703 EXPECT_NE(I, EI); 704 705 EXPECT_EQ(1, *I); 706 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 707 #ifndef NDEBUG 708 // Repeated dereferences are not allowed. 709 EXPECT_DEATH(*I, "Cannot dereference"); 710 // Comparison after dereference is not allowed. 711 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 712 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 713 #endif 714 #endif 715 716 ++I; 717 EXPECT_NE(I, EI); 718 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 719 #ifndef NDEBUG 720 // You cannot increment prior to dereference. 721 EXPECT_DEATH(++I, "Cannot increment"); 722 #endif 723 #endif 724 EXPECT_EQ(2, *I); 725 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 726 #ifndef NDEBUG 727 // Repeated dereferences are not allowed. 728 EXPECT_DEATH(*I, "Cannot dereference"); 729 #endif 730 #endif 731 732 // Inserting shouldn't break anything. We should be able to keep dereferencing 733 // the currrent iterator and increment. The increment to go to the "next" 734 // iterator from before we inserted. 735 L.insert(std::next(L.begin(), 2), -1); 736 ++I; 737 EXPECT_EQ(3, *I); 738 739 // Erasing the front including the current doesn't break incrementing. 740 L.erase(L.begin(), std::prev(L.end())); 741 ++I; 742 EXPECT_EQ(4, *I); 743 ++I; 744 EXPECT_EQ(EIR.end(), I); 745 } 746 747 // A custom iterator that returns a pointer when dereferenced. This is used to 748 // test make_early_inc_range with iterators that do not return a reference on 749 // dereferencing. 750 struct CustomPointerIterator 751 : public iterator_adaptor_base<CustomPointerIterator, 752 std::list<int>::iterator, 753 std::forward_iterator_tag> { 754 using base_type = 755 iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator, 756 std::forward_iterator_tag>; 757 758 explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {} 759 760 // Retrieve a pointer to the current int. 761 int *operator*() const { return &*base_type::wrapped(); } 762 }; 763 764 // Make sure make_early_inc_range works with iterators that do not return a 765 // reference on dereferencing. The test is similar to EarlyIncrementTest, but 766 // uses CustomPointerIterator. 767 TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) { 768 std::list<int> L = {1, 2, 3, 4}; 769 770 auto CustomRange = make_range(CustomPointerIterator(L.begin()), 771 CustomPointerIterator(L.end())); 772 auto EIR = make_early_inc_range(CustomRange); 773 774 auto I = EIR.begin(); 775 auto EI = EIR.end(); 776 EXPECT_NE(I, EI); 777 778 EXPECT_EQ(&*L.begin(), *I); 779 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 780 #ifndef NDEBUG 781 // Repeated dereferences are not allowed. 782 EXPECT_DEATH(*I, "Cannot dereference"); 783 // Comparison after dereference is not allowed. 784 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 785 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 786 #endif 787 #endif 788 789 ++I; 790 EXPECT_NE(I, EI); 791 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 792 #ifndef NDEBUG 793 // You cannot increment prior to dereference. 794 EXPECT_DEATH(++I, "Cannot increment"); 795 #endif 796 #endif 797 EXPECT_EQ(&*std::next(L.begin()), *I); 798 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 799 #ifndef NDEBUG 800 // Repeated dereferences are not allowed. 801 EXPECT_DEATH(*I, "Cannot dereference"); 802 #endif 803 #endif 804 805 // Inserting shouldn't break anything. We should be able to keep dereferencing 806 // the currrent iterator and increment. The increment to go to the "next" 807 // iterator from before we inserted. 808 L.insert(std::next(L.begin(), 2), -1); 809 ++I; 810 EXPECT_EQ(&*std::next(L.begin(), 3), *I); 811 812 // Erasing the front including the current doesn't break incrementing. 813 L.erase(L.begin(), std::prev(L.end())); 814 ++I; 815 EXPECT_EQ(&*L.begin(), *I); 816 ++I; 817 EXPECT_EQ(EIR.end(), I); 818 } 819 820 TEST(STLExtrasTest, AllEqual) { 821 std::vector<int> V; 822 EXPECT_TRUE(all_equal(V)); 823 824 V.push_back(1); 825 EXPECT_TRUE(all_equal(V)); 826 827 V.push_back(1); 828 V.push_back(1); 829 EXPECT_TRUE(all_equal(V)); 830 831 V.push_back(2); 832 EXPECT_FALSE(all_equal(V)); 833 } 834 835 // Test to verify that all_equal works with a container that does not 836 // model the random access iterator concept. 837 TEST(STLExtrasTest, AllEqualNonRandomAccess) { 838 std::list<int> V; 839 static_assert(!std::is_convertible_v< 840 std::iterator_traits<decltype(V)::iterator>::iterator_category, 841 std::random_access_iterator_tag>); 842 EXPECT_TRUE(all_equal(V)); 843 844 V.push_back(1); 845 EXPECT_TRUE(all_equal(V)); 846 847 V.push_back(1); 848 V.push_back(1); 849 EXPECT_TRUE(all_equal(V)); 850 851 V.push_back(2); 852 EXPECT_FALSE(all_equal(V)); 853 } 854 855 TEST(STLExtrasTest, AllEqualInitializerList) { 856 EXPECT_TRUE(all_equal({1})); 857 EXPECT_TRUE(all_equal({1, 1})); 858 EXPECT_FALSE(all_equal({1, 2})); 859 EXPECT_FALSE(all_equal({2, 1})); 860 EXPECT_TRUE(all_equal({1, 1, 1})); 861 } 862 863 TEST(STLExtrasTest, to_address) { 864 int *V1 = new int; 865 EXPECT_EQ(V1, to_address(V1)); 866 867 // Check fancy pointer overload for unique_ptr 868 std::unique_ptr<int> V2 = std::make_unique<int>(0); 869 EXPECT_EQ(V2.get(), llvm::to_address(V2)); 870 871 V2.reset(V1); 872 EXPECT_EQ(V1, llvm::to_address(V2)); 873 V2.release(); 874 875 // Check fancy pointer overload for shared_ptr 876 std::shared_ptr<int> V3 = std::make_shared<int>(0); 877 std::shared_ptr<int> V4 = V3; 878 EXPECT_EQ(V3.get(), V4.get()); 879 EXPECT_EQ(V3.get(), llvm::to_address(V3)); 880 EXPECT_EQ(V4.get(), llvm::to_address(V4)); 881 882 V3.reset(V1); 883 EXPECT_EQ(V1, llvm::to_address(V3)); 884 } 885 886 TEST(STLExtrasTest, partition_point) { 887 std::vector<int> V = {1, 3, 5, 7, 9}; 888 889 // Range version. 890 EXPECT_EQ(V.begin() + 3, 891 partition_point(V, [](unsigned X) { return X < 7; })); 892 EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); 893 EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); 894 } 895 896 TEST(STLExtrasTest, hasSingleElement) { 897 const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 898 const std::vector<int> V10(10); 899 900 EXPECT_EQ(hasSingleElement(V0), false); 901 EXPECT_EQ(hasSingleElement(V1), true); 902 EXPECT_EQ(hasSingleElement(V2), false); 903 EXPECT_EQ(hasSingleElement(V10), false); 904 } 905 906 TEST(STLExtrasTest, hasNItems) { 907 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 908 const std::list<int> V3 = {1, 3, 5}; 909 910 EXPECT_TRUE(hasNItems(V0, 0)); 911 EXPECT_FALSE(hasNItems(V0, 2)); 912 EXPECT_TRUE(hasNItems(V1, 1)); 913 EXPECT_FALSE(hasNItems(V1, 2)); 914 915 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 916 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; })); 917 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 918 } 919 920 TEST(STLExtras, hasNItemsOrMore) { 921 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 922 const std::list<int> V3 = {1, 3, 5}; 923 924 EXPECT_TRUE(hasNItemsOrMore(V1, 1)); 925 EXPECT_FALSE(hasNItemsOrMore(V1, 2)); 926 927 EXPECT_TRUE(hasNItemsOrMore(V2, 1)); 928 EXPECT_TRUE(hasNItemsOrMore(V2, 2)); 929 EXPECT_FALSE(hasNItemsOrMore(V2, 3)); 930 931 EXPECT_TRUE(hasNItemsOrMore(V3, 3)); 932 EXPECT_FALSE(hasNItemsOrMore(V3, 4)); 933 934 EXPECT_TRUE( 935 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 936 EXPECT_FALSE( 937 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; })); 938 EXPECT_TRUE( 939 hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 940 } 941 942 TEST(STLExtras, hasNItemsOrLess) { 943 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 944 const std::list<int> V3 = {1, 3, 5}; 945 946 EXPECT_TRUE(hasNItemsOrLess(V0, 0)); 947 EXPECT_TRUE(hasNItemsOrLess(V0, 1)); 948 EXPECT_TRUE(hasNItemsOrLess(V0, 2)); 949 950 EXPECT_FALSE(hasNItemsOrLess(V1, 0)); 951 EXPECT_TRUE(hasNItemsOrLess(V1, 1)); 952 EXPECT_TRUE(hasNItemsOrLess(V1, 2)); 953 954 EXPECT_FALSE(hasNItemsOrLess(V2, 0)); 955 EXPECT_FALSE(hasNItemsOrLess(V2, 1)); 956 EXPECT_TRUE(hasNItemsOrLess(V2, 2)); 957 EXPECT_TRUE(hasNItemsOrLess(V2, 3)); 958 959 EXPECT_FALSE(hasNItemsOrLess(V3, 0)); 960 EXPECT_FALSE(hasNItemsOrLess(V3, 1)); 961 EXPECT_FALSE(hasNItemsOrLess(V3, 2)); 962 EXPECT_TRUE(hasNItemsOrLess(V3, 3)); 963 EXPECT_TRUE(hasNItemsOrLess(V3, 4)); 964 965 EXPECT_TRUE( 966 hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; })); 967 EXPECT_TRUE( 968 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 969 EXPECT_TRUE( 970 hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; })); 971 EXPECT_FALSE( 972 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; })); 973 } 974 975 TEST(STLExtras, MoveRange) { 976 class Foo { 977 bool A; 978 979 public: 980 Foo() : A(true) {} 981 Foo(const Foo &) = delete; 982 Foo(Foo &&Other) : A(Other.A) { Other.A = false; } 983 Foo &operator=(const Foo &) = delete; 984 Foo &operator=(Foo &&Other) { 985 if (this != &Other) { 986 A = Other.A; 987 Other.A = false; 988 } 989 return *this; 990 } 991 operator bool() const { return A; } 992 }; 993 SmallVector<Foo, 4U> V1, V2, V3, V4; 994 auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); }; 995 auto Build = [&] { 996 SmallVector<Foo, 4U> Foos; 997 Foos.resize(4U); 998 return Foos; 999 }; 1000 1001 V1.resize(4U); 1002 EXPECT_TRUE(llvm::all_of(V1, HasVal)); 1003 1004 llvm::move(V1, std::back_inserter(V2)); 1005 1006 // Ensure input container is same size, but its contents were moved out. 1007 EXPECT_EQ(V1.size(), 4U); 1008 EXPECT_TRUE(llvm::none_of(V1, HasVal)); 1009 1010 // Ensure output container has the contents of the input container. 1011 EXPECT_EQ(V2.size(), 4U); 1012 EXPECT_TRUE(llvm::all_of(V2, HasVal)); 1013 1014 llvm::move(std::move(V2), std::back_inserter(V3)); 1015 1016 EXPECT_TRUE(llvm::none_of(V2, HasVal)); 1017 EXPECT_EQ(V3.size(), 4U); 1018 EXPECT_TRUE(llvm::all_of(V3, HasVal)); 1019 1020 llvm::move(Build(), std::back_inserter(V4)); 1021 EXPECT_EQ(V4.size(), 4U); 1022 EXPECT_TRUE(llvm::all_of(V4, HasVal)); 1023 } 1024 1025 TEST(STLExtras, Unique) { 1026 std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; 1027 1028 auto I = llvm::unique(V, [](int a, int b) { return a == b; }); 1029 1030 EXPECT_EQ(I, V.begin() + 4); 1031 1032 EXPECT_EQ(1, V[0]); 1033 EXPECT_EQ(5, V[1]); 1034 EXPECT_EQ(4, V[2]); 1035 EXPECT_EQ(3, V[3]); 1036 } 1037 1038 TEST(STLExtras, UniqueNoPred) { 1039 std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; 1040 1041 auto I = llvm::unique(V); 1042 1043 EXPECT_EQ(I, V.begin() + 4); 1044 1045 EXPECT_EQ(1, V[0]); 1046 EXPECT_EQ(5, V[1]); 1047 EXPECT_EQ(4, V[2]); 1048 EXPECT_EQ(3, V[3]); 1049 } 1050 1051 TEST(STLExtrasTest, MakeVisitorOneCallable) { 1052 auto IdentityLambda = [](auto X) { return X; }; 1053 auto IdentityVisitor = makeVisitor(IdentityLambda); 1054 EXPECT_EQ(IdentityLambda(1), IdentityVisitor(1)); 1055 EXPECT_EQ(IdentityLambda(2.0f), IdentityVisitor(2.0f)); 1056 EXPECT_TRUE((std::is_same<decltype(IdentityLambda(IdentityLambda)), 1057 decltype(IdentityLambda)>::value)); 1058 EXPECT_TRUE((std::is_same<decltype(IdentityVisitor(IdentityVisitor)), 1059 decltype(IdentityVisitor)>::value)); 1060 } 1061 1062 TEST(STLExtrasTest, MakeVisitorTwoCallables) { 1063 auto Visitor = 1064 makeVisitor([](int) { return 0; }, [](std::string) { return 1; }); 1065 EXPECT_EQ(Visitor(42), 0); 1066 EXPECT_EQ(Visitor("foo"), 1); 1067 } 1068 1069 TEST(STLExtrasTest, MakeVisitorCallableMultipleOperands) { 1070 auto Second = makeVisitor([](int I, float F) { return F; }, 1071 [](float F, int I) { return I; }); 1072 EXPECT_EQ(Second(1.f, 1), 1); 1073 EXPECT_EQ(Second(1, 1.f), 1.f); 1074 } 1075 1076 TEST(STLExtrasTest, MakeVisitorDefaultCase) { 1077 { 1078 auto Visitor = makeVisitor([](int I) { return I + 100; }, 1079 [](float F) { return F * 2; }, 1080 [](auto) { return -1; }); 1081 EXPECT_EQ(Visitor(24), 124); 1082 EXPECT_EQ(Visitor(2.f), 4.f); 1083 EXPECT_EQ(Visitor(2.), -1); 1084 EXPECT_EQ(Visitor(Visitor), -1); 1085 } 1086 { 1087 auto Visitor = makeVisitor([](auto) { return -1; }, 1088 [](int I) { return I + 100; }, 1089 [](float F) { return F * 2; }); 1090 EXPECT_EQ(Visitor(24), 124); 1091 EXPECT_EQ(Visitor(2.f), 4.f); 1092 EXPECT_EQ(Visitor(2.), -1); 1093 EXPECT_EQ(Visitor(Visitor), -1); 1094 } 1095 } 1096 1097 template <bool Moveable, bool Copyable> 1098 struct Functor : Counted<Moveable, Copyable> { 1099 using Counted<Moveable, Copyable>::Counted; 1100 void operator()() {} 1101 }; 1102 1103 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsPRValue) { 1104 int Copies = 0; 1105 int Moves = 0; 1106 int Destructors = 0; 1107 { 1108 auto V = makeVisitor(Functor<true, false>(Copies, Moves, Destructors)); 1109 (void)V; 1110 EXPECT_EQ(0, Copies); 1111 EXPECT_EQ(1, Moves); 1112 EXPECT_EQ(1, Destructors); 1113 } 1114 EXPECT_EQ(0, Copies); 1115 EXPECT_EQ(1, Moves); 1116 EXPECT_EQ(2, Destructors); 1117 } 1118 1119 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsRValue) { 1120 int Copies = 0; 1121 int Moves = 0; 1122 int Destructors = 0; 1123 { 1124 Functor<true, false> F(Copies, Moves, Destructors); 1125 { 1126 auto V = makeVisitor(std::move(F)); 1127 (void)V; 1128 EXPECT_EQ(0, Copies); 1129 EXPECT_EQ(1, Moves); 1130 EXPECT_EQ(0, Destructors); 1131 } 1132 EXPECT_EQ(0, Copies); 1133 EXPECT_EQ(1, Moves); 1134 EXPECT_EQ(1, Destructors); 1135 } 1136 EXPECT_EQ(0, Copies); 1137 EXPECT_EQ(1, Moves); 1138 EXPECT_EQ(2, Destructors); 1139 } 1140 1141 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsLValue) { 1142 int Copies = 0; 1143 int Moves = 0; 1144 int Destructors = 0; 1145 { 1146 Functor<true, true> F(Copies, Moves, Destructors); 1147 { 1148 auto V = makeVisitor(F); 1149 (void)V; 1150 EXPECT_EQ(1, Copies); 1151 EXPECT_EQ(0, Moves); 1152 EXPECT_EQ(0, Destructors); 1153 } 1154 EXPECT_EQ(1, Copies); 1155 EXPECT_EQ(0, Moves); 1156 EXPECT_EQ(1, Destructors); 1157 } 1158 EXPECT_EQ(1, Copies); 1159 EXPECT_EQ(0, Moves); 1160 EXPECT_EQ(2, Destructors); 1161 } 1162 1163 TEST(STLExtrasTest, AllOfZip) { 1164 std::vector<int> v1 = {0, 4, 2, 1}; 1165 std::vector<int> v2 = {1, 4, 3, 6}; 1166 EXPECT_TRUE(all_of_zip(v1, v2, [](int v1, int v2) { return v1 <= v2; })); 1167 EXPECT_FALSE(all_of_zip(v1, v2, [](int L, int R) { return L < R; })); 1168 1169 // Triple vectors 1170 std::vector<int> v3 = {1, 6, 5, 7}; 1171 EXPECT_EQ(true, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 1172 return a <= b && b <= c; 1173 })); 1174 EXPECT_EQ(false, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 1175 return a < b && b < c; 1176 })); 1177 1178 // Shorter vector should fail even with an always-true predicate. 1179 std::vector<int> v_short = {1, 4}; 1180 EXPECT_EQ(false, all_of_zip(v1, v_short, [](int, int) { return true; })); 1181 EXPECT_EQ(false, 1182 all_of_zip(v1, v2, v_short, [](int, int, int) { return true; })); 1183 } 1184 1185 TEST(STLExtrasTest, TypesAreDistinct) { 1186 EXPECT_TRUE((llvm::TypesAreDistinct<>::value)); 1187 EXPECT_TRUE((llvm::TypesAreDistinct<int>::value)); 1188 EXPECT_FALSE((llvm::TypesAreDistinct<int, int>::value)); 1189 EXPECT_TRUE((llvm::TypesAreDistinct<int, float>::value)); 1190 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, int>::value)); 1191 EXPECT_TRUE((llvm::TypesAreDistinct<int, float, double>::value)); 1192 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, double, float>::value)); 1193 EXPECT_TRUE((llvm::TypesAreDistinct<int, int *>::value)); 1194 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &>::value)); 1195 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &&>::value)); 1196 EXPECT_TRUE((llvm::TypesAreDistinct<int, const int>::value)); 1197 } 1198 1199 TEST(STLExtrasTest, FirstIndexOfType) { 1200 EXPECT_EQ((llvm::FirstIndexOfType<int, int>::value), 0u); 1201 EXPECT_EQ((llvm::FirstIndexOfType<int, int, int>::value), 0u); 1202 EXPECT_EQ((llvm::FirstIndexOfType<int, float, int>::value), 1u); 1203 EXPECT_EQ((llvm::FirstIndexOfType<int const *, float, int, int const *, 1204 const int>::value), 1205 2u); 1206 } 1207 1208 TEST(STLExtrasTest, TypeAtIndex) { 1209 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int>>::value)); 1210 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int, float>>::value)); 1211 EXPECT_TRUE((std::is_same<float, llvm::TypeAtIndex<1, int, float>>::value)); 1212 EXPECT_TRUE( 1213 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 1214 EXPECT_TRUE( 1215 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 1216 EXPECT_TRUE( 1217 (std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value)); 1218 } 1219 1220 enum Doggos { 1221 Floofer, 1222 Woofer, 1223 SubWoofer, 1224 Pupper, 1225 Pupperino, 1226 Longboi, 1227 }; 1228 1229 struct WooferCmp { 1230 // Not copyable. 1231 WooferCmp() = default; 1232 WooferCmp(const WooferCmp &) = delete; 1233 WooferCmp &operator=(const WooferCmp &) = delete; 1234 1235 friend bool operator==(const Doggos &Doggo, const WooferCmp &) { 1236 return Doggo == Doggos::Woofer; 1237 } 1238 }; 1239 1240 TEST(STLExtrasTest, IsContainedInitializerList) { 1241 EXPECT_TRUE(is_contained({Woofer, SubWoofer}, Woofer)); 1242 EXPECT_TRUE(is_contained({Woofer, SubWoofer}, SubWoofer)); 1243 EXPECT_FALSE(is_contained({Woofer, SubWoofer}, Pupper)); 1244 1245 // Check that the initializer list type and the element type do not have to 1246 // match exactly. 1247 EXPECT_TRUE(is_contained({Floofer, Woofer, SubWoofer}, WooferCmp{})); 1248 EXPECT_FALSE(is_contained({Floofer, SubWoofer}, WooferCmp{})); 1249 1250 EXPECT_TRUE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("ccc"))); 1251 EXPECT_FALSE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("x"))); 1252 1253 static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!"); 1254 static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!"); 1255 1256 EXPECT_TRUE(is_contained({1, 2, 3, 4}, 3)); 1257 EXPECT_FALSE(is_contained({1, 2, 3, 4}, 5)); 1258 1259 static_assert(is_contained({1, 2, 3, 4}, 3), "It's there!"); 1260 static_assert(!is_contained({1, 2, 3, 4}, 5), "It's not there :("); 1261 } 1262 1263 TEST(STLExtrasTest, IsContainedMemberContains) { 1264 // Check that `llvm::is_contained` uses the member `.contains()` when 1265 // available. Check that `.contains()` is preferred over `.find()`. 1266 struct Foo { 1267 bool contains(int) const { 1268 ++NumContainsCalls; 1269 return ContainsResult; 1270 } 1271 int *begin() { return nullptr; } 1272 int *end() { return nullptr; } 1273 int *find(int) { return nullptr; } 1274 1275 bool ContainsResult = false; 1276 mutable unsigned NumContainsCalls = 0; 1277 } Container; 1278 1279 EXPECT_EQ(Container.NumContainsCalls, 0u); 1280 EXPECT_FALSE(is_contained(Container, 1)); 1281 EXPECT_EQ(Container.NumContainsCalls, 1u); 1282 1283 Container.ContainsResult = true; 1284 EXPECT_TRUE(is_contained(Container, 1)); 1285 EXPECT_EQ(Container.NumContainsCalls, 2u); 1286 } 1287 1288 TEST(STLExtrasTest, IsContainedMemberFind) { 1289 // Check that `llvm::is_contained` uses the member `.find(x)` when available. 1290 struct Foo { 1291 auto begin() { return Data.begin(); } 1292 auto end() { return Data.end(); } 1293 auto find(int X) { 1294 ++NumFindCalls; 1295 return std::find(begin(), end(), X); 1296 } 1297 1298 std::vector<int> Data; 1299 mutable unsigned NumFindCalls = 0; 1300 } Container; 1301 1302 Container.Data = {1, 2, 3}; 1303 1304 EXPECT_EQ(Container.NumFindCalls, 0u); 1305 EXPECT_TRUE(is_contained(Container, 1)); 1306 EXPECT_TRUE(is_contained(Container, 3)); 1307 EXPECT_EQ(Container.NumFindCalls, 2u); 1308 1309 EXPECT_FALSE(is_contained(Container, 4)); 1310 EXPECT_EQ(Container.NumFindCalls, 3u); 1311 } 1312 1313 TEST(STLExtrasTest, addEnumValues) { 1314 enum A { Zero = 0, One = 1 }; 1315 enum B { IntMax = INT_MAX, ULongLongMax = ULLONG_MAX }; 1316 enum class C : unsigned { Two = 2 }; 1317 1318 // Non-fixed underlying types, with same underlying types 1319 static_assert(addEnumValues(Zero, One) == 1, 1320 "addEnumValues(Zero, One) failed."); 1321 static_assert(addEnumValues(IntMax, ULongLongMax) == 1322 INT_MAX + static_cast<unsigned long long>(ULLONG_MAX), 1323 "addEnumValues(IntMax, ULongLongMax) failed."); 1324 // Non-fixed underlying types, with different underlying types 1325 static_assert(addEnumValues(Zero, IntMax) == INT_MAX, 1326 "addEnumValues(Zero, IntMax) failed."); 1327 static_assert(addEnumValues(One, ULongLongMax) == 1328 1 + static_cast<unsigned long long>(ULLONG_MAX), 1329 "addEnumValues(One, ULongLongMax) failed."); 1330 // Non-fixed underlying type enum and fixed underlying type enum, with same 1331 // underlying types 1332 static_assert(addEnumValues(One, C::Two) == 3, 1333 "addEnumValues(One, C::Two) failed."); 1334 // Non-fixed underlying type enum and fixed underlying type enum, with 1335 // different underlying types 1336 static_assert(addEnumValues(ULongLongMax, C::Two) == 1337 static_cast<unsigned long long>(ULLONG_MAX) + 2, 1338 "addEnumValues(ULongLongMax, C::Two) failed."); 1339 } 1340 1341 TEST(STLExtrasTest, LessFirst) { 1342 { 1343 std::pair<int, int> A(0, 1); 1344 std::pair<int, int> B(1, 0); 1345 EXPECT_TRUE(less_first()(A, B)); 1346 EXPECT_FALSE(less_first()(B, A)); 1347 } 1348 1349 { 1350 std::tuple<int, int> A(0, 1); 1351 std::tuple<int, int> B(1, 0); 1352 EXPECT_TRUE(less_first()(A, B)); 1353 EXPECT_FALSE(less_first()(B, A)); 1354 } 1355 } 1356 1357 TEST(STLExtrasTest, LessSecond) { 1358 { 1359 std::pair<int, int> A(0, 1); 1360 std::pair<int, int> B(1, 0); 1361 EXPECT_FALSE(less_second()(A, B)); 1362 EXPECT_TRUE(less_second()(B, A)); 1363 } 1364 1365 { 1366 std::tuple<int, int> A(0, 1); 1367 std::tuple<int, int> B(1, 0); 1368 EXPECT_FALSE(less_second()(A, B)); 1369 EXPECT_TRUE(less_second()(B, A)); 1370 } 1371 } 1372 1373 TEST(STLExtrasTest, Mismatch) { 1374 { 1375 const int MMIndex = 5; 1376 StringRef First = "FooBar"; 1377 StringRef Second = "FooBaz"; 1378 auto [MMFirst, MMSecond] = mismatch(First, Second); 1379 EXPECT_EQ(MMFirst, First.begin() + MMIndex); 1380 EXPECT_EQ(MMSecond, Second.begin() + MMIndex); 1381 } 1382 1383 { 1384 SmallVector<int> First = {0, 1, 2}; 1385 SmallVector<int> Second = {0, 1, 2, 3}; 1386 auto [MMFirst, MMSecond] = mismatch(First, Second); 1387 EXPECT_EQ(MMFirst, First.end()); 1388 EXPECT_EQ(MMSecond, Second.begin() + 3); 1389 } 1390 1391 { 1392 SmallVector<int> First = {0, 1}; 1393 SmallVector<int> Empty; 1394 auto [MMFirst, MMEmpty] = mismatch(First, Empty); 1395 EXPECT_EQ(MMFirst, First.begin()); 1396 EXPECT_EQ(MMEmpty, Empty.begin()); 1397 } 1398 } 1399 1400 struct Foo; 1401 struct Bar {}; 1402 1403 static_assert(is_incomplete_v<Foo>, "Foo is incomplete"); 1404 static_assert(!is_incomplete_v<Bar>, "Bar is defined"); 1405 1406 } // namespace 1407