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 "gtest/gtest.h" 11 12 #include <list> 13 #include <vector> 14 15 using namespace llvm; 16 17 namespace { 18 19 int f(rank<0>) { return 0; } 20 int f(rank<1>) { return 1; } 21 int f(rank<2>) { return 2; } 22 int f(rank<4>) { return 4; } 23 24 TEST(STLExtrasTest, Rank) { 25 // We shouldn't get ambiguities and should select the overload of the same 26 // rank as the argument. 27 EXPECT_EQ(0, f(rank<0>())); 28 EXPECT_EQ(1, f(rank<1>())); 29 EXPECT_EQ(2, f(rank<2>())); 30 31 // This overload is missing so we end up back at 2. 32 EXPECT_EQ(2, f(rank<3>())); 33 34 // But going past 3 should work fine. 35 EXPECT_EQ(4, f(rank<4>())); 36 37 // And we can even go higher and just fall back to the last overload. 38 EXPECT_EQ(4, f(rank<5>())); 39 EXPECT_EQ(4, f(rank<6>())); 40 } 41 42 TEST(STLExtrasTest, EnumerateLValue) { 43 // Test that a simple LValue can be enumerated and gives correct results with 44 // multiple types, including the empty container. 45 std::vector<char> foo = {'a', 'b', 'c'}; 46 typedef std::pair<std::size_t, char> CharPairType; 47 std::vector<CharPairType> CharResults; 48 49 for (auto X : llvm::enumerate(foo)) { 50 CharResults.emplace_back(X.index(), X.value()); 51 } 52 ASSERT_EQ(3u, CharResults.size()); 53 EXPECT_EQ(CharPairType(0u, 'a'), CharResults[0]); 54 EXPECT_EQ(CharPairType(1u, 'b'), CharResults[1]); 55 EXPECT_EQ(CharPairType(2u, 'c'), CharResults[2]); 56 57 // Test a const range of a different type. 58 typedef std::pair<std::size_t, int> IntPairType; 59 std::vector<IntPairType> IntResults; 60 const std::vector<int> bar = {1, 2, 3}; 61 for (auto X : llvm::enumerate(bar)) { 62 IntResults.emplace_back(X.index(), X.value()); 63 } 64 ASSERT_EQ(3u, IntResults.size()); 65 EXPECT_EQ(IntPairType(0u, 1), IntResults[0]); 66 EXPECT_EQ(IntPairType(1u, 2), IntResults[1]); 67 EXPECT_EQ(IntPairType(2u, 3), IntResults[2]); 68 69 // Test an empty range. 70 IntResults.clear(); 71 const std::vector<int> baz{}; 72 for (auto X : llvm::enumerate(baz)) { 73 IntResults.emplace_back(X.index(), X.value()); 74 } 75 EXPECT_TRUE(IntResults.empty()); 76 } 77 78 TEST(STLExtrasTest, EnumerateModifyLValue) { 79 // Test that you can modify the underlying entries of an lvalue range through 80 // the enumeration iterator. 81 std::vector<char> foo = {'a', 'b', 'c'}; 82 83 for (auto X : llvm::enumerate(foo)) { 84 ++X.value(); 85 } 86 EXPECT_EQ('b', foo[0]); 87 EXPECT_EQ('c', foo[1]); 88 EXPECT_EQ('d', foo[2]); 89 } 90 91 TEST(STLExtrasTest, EnumerateRValueRef) { 92 // Test that an rvalue can be enumerated. 93 typedef std::pair<std::size_t, int> PairType; 94 std::vector<PairType> Results; 95 96 auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3}); 97 98 for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) { 99 Results.emplace_back(X.index(), X.value()); 100 } 101 102 ASSERT_EQ(3u, Results.size()); 103 EXPECT_EQ(PairType(0u, 1), Results[0]); 104 EXPECT_EQ(PairType(1u, 2), Results[1]); 105 EXPECT_EQ(PairType(2u, 3), Results[2]); 106 } 107 108 TEST(STLExtrasTest, EnumerateModifyRValue) { 109 // Test that when enumerating an rvalue, modification still works (even if 110 // this isn't terribly useful, it at least shows that we haven't snuck an 111 // extra const in there somewhere. 112 typedef std::pair<std::size_t, char> PairType; 113 std::vector<PairType> Results; 114 115 for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) { 116 ++X.value(); 117 Results.emplace_back(X.index(), X.value()); 118 } 119 120 ASSERT_EQ(3u, Results.size()); 121 EXPECT_EQ(PairType(0u, '2'), Results[0]); 122 EXPECT_EQ(PairType(1u, '3'), Results[1]); 123 EXPECT_EQ(PairType(2u, '4'), Results[2]); 124 } 125 126 template <bool B> struct CanMove {}; 127 template <> struct CanMove<false> { 128 CanMove(CanMove &&) = delete; 129 130 CanMove() = default; 131 CanMove(const CanMove &) = default; 132 }; 133 134 template <bool B> struct CanCopy {}; 135 template <> struct CanCopy<false> { 136 CanCopy(const CanCopy &) = delete; 137 138 CanCopy() = default; 139 CanCopy(CanCopy &&) = default; 140 }; 141 142 template <bool Moveable, bool Copyable> 143 class Counted : CanMove<Moveable>, CanCopy<Copyable> { 144 int &C; 145 int &M; 146 int &D; 147 148 public: 149 explicit Counted(int &C, int &M, int &D) : C(C), M(M), D(D) {} 150 Counted(const Counted &O) : CanCopy<Copyable>(O), C(O.C), M(O.M), D(O.D) { 151 ++C; 152 } 153 Counted(Counted &&O) 154 : CanMove<Moveable>(std::move(O)), C(O.C), M(O.M), D(O.D) { 155 ++M; 156 } 157 ~Counted() { ++D; } 158 }; 159 160 template <bool Moveable, bool Copyable> 161 struct Range : Counted<Moveable, Copyable> { 162 using Counted<Moveable, Copyable>::Counted; 163 int *begin() { return nullptr; } 164 int *end() { return nullptr; } 165 }; 166 167 TEST(STLExtrasTest, EnumerateLifetimeSemanticsPRValue) { 168 int Copies = 0; 169 int Moves = 0; 170 int Destructors = 0; 171 { 172 auto E = enumerate(Range<true, false>(Copies, Moves, Destructors)); 173 (void)E; 174 // Doesn't compile. rvalue ranges must be moveable. 175 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); 176 EXPECT_EQ(0, Copies); 177 EXPECT_EQ(1, Moves); 178 EXPECT_EQ(1, Destructors); 179 } 180 EXPECT_EQ(0, Copies); 181 EXPECT_EQ(1, Moves); 182 EXPECT_EQ(2, Destructors); 183 } 184 185 TEST(STLExtrasTest, EnumerateLifetimeSemanticsRValue) { 186 // With an rvalue, it should not be destroyed until the end of the scope. 187 int Copies = 0; 188 int Moves = 0; 189 int Destructors = 0; 190 { 191 Range<true, false> R(Copies, Moves, Destructors); 192 { 193 auto E = enumerate(std::move(R)); 194 (void)E; 195 // Doesn't compile. rvalue ranges must be moveable. 196 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); 197 EXPECT_EQ(0, Copies); 198 EXPECT_EQ(1, Moves); 199 EXPECT_EQ(0, Destructors); 200 } 201 EXPECT_EQ(0, Copies); 202 EXPECT_EQ(1, Moves); 203 EXPECT_EQ(1, Destructors); 204 } 205 EXPECT_EQ(0, Copies); 206 EXPECT_EQ(1, Moves); 207 EXPECT_EQ(2, Destructors); 208 } 209 210 TEST(STLExtrasTest, EnumerateLifetimeSemanticsLValue) { 211 // With an lvalue, it should not be destroyed even after the end of the scope. 212 // lvalue ranges need be neither copyable nor moveable. 213 int Copies = 0; 214 int Moves = 0; 215 int Destructors = 0; 216 { 217 Range<false, false> R(Copies, Moves, Destructors); 218 { 219 auto E = enumerate(R); 220 (void)E; 221 EXPECT_EQ(0, Copies); 222 EXPECT_EQ(0, Moves); 223 EXPECT_EQ(0, Destructors); 224 } 225 EXPECT_EQ(0, Copies); 226 EXPECT_EQ(0, Moves); 227 EXPECT_EQ(0, Destructors); 228 } 229 EXPECT_EQ(0, Copies); 230 EXPECT_EQ(0, Moves); 231 EXPECT_EQ(1, Destructors); 232 } 233 234 TEST(STLExtrasTest, ApplyTuple) { 235 auto T = std::make_tuple(1, 3, 7); 236 auto U = llvm::apply_tuple( 237 [](int A, int B, int C) { return std::make_tuple(A - B, B - C, C - A); }, 238 T); 239 240 EXPECT_EQ(-2, std::get<0>(U)); 241 EXPECT_EQ(-4, std::get<1>(U)); 242 EXPECT_EQ(6, std::get<2>(U)); 243 244 auto V = llvm::apply_tuple( 245 [](int A, int B, int C) { 246 return std::make_tuple(std::make_pair(A, char('A' + A)), 247 std::make_pair(B, char('A' + B)), 248 std::make_pair(C, char('A' + C))); 249 }, 250 T); 251 252 EXPECT_EQ(std::make_pair(1, 'B'), std::get<0>(V)); 253 EXPECT_EQ(std::make_pair(3, 'D'), std::get<1>(V)); 254 EXPECT_EQ(std::make_pair(7, 'H'), std::get<2>(V)); 255 } 256 257 class apply_variadic { 258 static int apply_one(int X) { return X + 1; } 259 static char apply_one(char C) { return C + 1; } 260 static StringRef apply_one(StringRef S) { return S.drop_back(); } 261 262 public: 263 template <typename... Ts> auto operator()(Ts &&... Items) { 264 return std::make_tuple(apply_one(Items)...); 265 } 266 }; 267 268 TEST(STLExtrasTest, ApplyTupleVariadic) { 269 auto Items = std::make_tuple(1, llvm::StringRef("Test"), 'X'); 270 auto Values = apply_tuple(apply_variadic(), Items); 271 272 EXPECT_EQ(2, std::get<0>(Values)); 273 EXPECT_EQ("Tes", std::get<1>(Values)); 274 EXPECT_EQ('Y', std::get<2>(Values)); 275 } 276 277 TEST(STLExtrasTest, CountAdaptor) { 278 std::vector<int> v; 279 280 v.push_back(1); 281 v.push_back(2); 282 v.push_back(1); 283 v.push_back(4); 284 v.push_back(3); 285 v.push_back(2); 286 v.push_back(1); 287 288 EXPECT_EQ(3, count(v, 1)); 289 EXPECT_EQ(2, count(v, 2)); 290 EXPECT_EQ(1, count(v, 3)); 291 EXPECT_EQ(1, count(v, 4)); 292 } 293 294 TEST(STLExtrasTest, for_each) { 295 std::vector<int> v{0, 1, 2, 3, 4}; 296 int count = 0; 297 298 llvm::for_each(v, [&count](int) { ++count; }); 299 EXPECT_EQ(5, count); 300 } 301 302 TEST(STLExtrasTest, ToVector) { 303 std::vector<char> v = {'a', 'b', 'c'}; 304 auto Enumerated = to_vector<4>(enumerate(v)); 305 ASSERT_EQ(3u, Enumerated.size()); 306 for (size_t I = 0; I < v.size(); ++I) { 307 EXPECT_EQ(I, Enumerated[I].index()); 308 EXPECT_EQ(v[I], Enumerated[I].value()); 309 } 310 311 auto EnumeratedImplicitSize = to_vector(enumerate(v)); 312 ASSERT_EQ(3u, EnumeratedImplicitSize.size()); 313 for (size_t I = 0; I < v.size(); ++I) { 314 EXPECT_EQ(I, EnumeratedImplicitSize[I].index()); 315 EXPECT_EQ(v[I], EnumeratedImplicitSize[I].value()); 316 } 317 } 318 319 TEST(STLExtrasTest, ConcatRange) { 320 std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8}; 321 std::vector<int> Test; 322 323 std::vector<int> V1234 = {1, 2, 3, 4}; 324 std::list<int> L56 = {5, 6}; 325 SmallVector<int, 2> SV78 = {7, 8}; 326 327 // Use concat across different sized ranges of different types with different 328 // iterators. 329 for (int &i : concat<int>(V1234, L56, SV78)) 330 Test.push_back(i); 331 EXPECT_EQ(Expected, Test); 332 333 // Use concat between a temporary, an L-value, and an R-value to make sure 334 // complex lifetimes work well. 335 Test.clear(); 336 for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78))) 337 Test.push_back(i); 338 EXPECT_EQ(Expected, Test); 339 } 340 341 TEST(STLExtrasTest, PartitionAdaptor) { 342 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 343 344 auto I = partition(V, [](int i) { return i % 2 == 0; }); 345 ASSERT_EQ(V.begin() + 4, I); 346 347 // Sort the two halves as partition may have messed with the order. 348 llvm::sort(V.begin(), I); 349 llvm::sort(I, V.end()); 350 351 EXPECT_EQ(2, V[0]); 352 EXPECT_EQ(4, V[1]); 353 EXPECT_EQ(6, V[2]); 354 EXPECT_EQ(8, V[3]); 355 EXPECT_EQ(1, V[4]); 356 EXPECT_EQ(3, V[5]); 357 EXPECT_EQ(5, V[6]); 358 EXPECT_EQ(7, V[7]); 359 } 360 361 TEST(STLExtrasTest, EraseIf) { 362 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 363 364 erase_if(V, [](int i) { return i % 2 == 0; }); 365 EXPECT_EQ(4u, V.size()); 366 EXPECT_EQ(1, V[0]); 367 EXPECT_EQ(3, V[1]); 368 EXPECT_EQ(5, V[2]); 369 EXPECT_EQ(7, V[3]); 370 } 371 372 TEST(STLExtrasTest, AppendRange) { 373 auto AppendVals = {3}; 374 std::vector<int> V = {1, 2}; 375 append_range(V, AppendVals); 376 EXPECT_EQ(1, V[0]); 377 EXPECT_EQ(2, V[1]); 378 EXPECT_EQ(3, V[2]); 379 } 380 381 namespace some_namespace { 382 struct some_struct { 383 std::vector<int> data; 384 std::string swap_val; 385 }; 386 387 std::vector<int>::const_iterator begin(const some_struct &s) { 388 return s.data.begin(); 389 } 390 391 std::vector<int>::const_iterator end(const some_struct &s) { 392 return s.data.end(); 393 } 394 395 void swap(some_struct &lhs, some_struct &rhs) { 396 // make swap visible as non-adl swap would even seem to 397 // work with std::swap which defaults to moving 398 lhs.swap_val = "lhs"; 399 rhs.swap_val = "rhs"; 400 } 401 } // namespace some_namespace 402 403 TEST(STLExtrasTest, ADLTest) { 404 some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""}; 405 some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""}; 406 407 EXPECT_EQ(*adl_begin(s), 1); 408 EXPECT_EQ(*(adl_end(s) - 1), 5); 409 410 adl_swap(s, s2); 411 EXPECT_EQ(s.swap_val, "lhs"); 412 EXPECT_EQ(s2.swap_val, "rhs"); 413 414 int count = 0; 415 llvm::for_each(s, [&count](int) { ++count; }); 416 EXPECT_EQ(5, count); 417 } 418 419 TEST(STLExtrasTest, EmptyTest) { 420 std::vector<void*> V; 421 EXPECT_TRUE(llvm::empty(V)); 422 V.push_back(nullptr); 423 EXPECT_FALSE(llvm::empty(V)); 424 425 std::initializer_list<int> E = {}; 426 std::initializer_list<int> NotE = {7, 13, 42}; 427 EXPECT_TRUE(llvm::empty(E)); 428 EXPECT_FALSE(llvm::empty(NotE)); 429 430 auto R0 = make_range(V.begin(), V.begin()); 431 EXPECT_TRUE(llvm::empty(R0)); 432 auto R1 = make_range(V.begin(), V.end()); 433 EXPECT_FALSE(llvm::empty(R1)); 434 } 435 436 TEST(STLExtrasTest, DropBeginTest) { 437 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 438 439 for (int n = 0; n < 5; ++n) { 440 int i = n; 441 for (auto &v : drop_begin(vec, n)) { 442 EXPECT_EQ(v, i); 443 i += 1; 444 } 445 EXPECT_EQ(i, 5); 446 } 447 } 448 449 TEST(STLExtrasTest, DropBeginDefaultTest) { 450 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 451 452 int i = 1; 453 for (auto &v : drop_begin(vec)) { 454 EXPECT_EQ(v, i); 455 i += 1; 456 } 457 EXPECT_EQ(i, 5); 458 } 459 460 TEST(STLExtrasTest, DropEndTest) { 461 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 462 463 for (int n = 0; n < 5; ++n) { 464 int i = 0; 465 for (auto &v : drop_end(vec, n)) { 466 EXPECT_EQ(v, i); 467 i += 1; 468 } 469 EXPECT_EQ(i, 5 - n); 470 } 471 } 472 473 TEST(STLExtrasTest, DropEndDefaultTest) { 474 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 475 476 int i = 0; 477 for (auto &v : drop_end(vec)) { 478 EXPECT_EQ(v, i); 479 i += 1; 480 } 481 EXPECT_EQ(i, 4); 482 } 483 484 TEST(STLExtrasTest, EarlyIncrementTest) { 485 std::list<int> L = {1, 2, 3, 4}; 486 487 auto EIR = make_early_inc_range(L); 488 489 auto I = EIR.begin(); 490 auto EI = EIR.end(); 491 EXPECT_NE(I, EI); 492 493 EXPECT_EQ(1, *I); 494 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 495 #ifndef NDEBUG 496 // Repeated dereferences are not allowed. 497 EXPECT_DEATH(*I, "Cannot dereference"); 498 // Comparison after dereference is not allowed. 499 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 500 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 501 #endif 502 #endif 503 504 ++I; 505 EXPECT_NE(I, EI); 506 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 507 #ifndef NDEBUG 508 // You cannot increment prior to dereference. 509 EXPECT_DEATH(++I, "Cannot increment"); 510 #endif 511 #endif 512 EXPECT_EQ(2, *I); 513 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 514 #ifndef NDEBUG 515 // Repeated dereferences are not allowed. 516 EXPECT_DEATH(*I, "Cannot dereference"); 517 #endif 518 #endif 519 520 // Inserting shouldn't break anything. We should be able to keep dereferencing 521 // the currrent iterator and increment. The increment to go to the "next" 522 // iterator from before we inserted. 523 L.insert(std::next(L.begin(), 2), -1); 524 ++I; 525 EXPECT_EQ(3, *I); 526 527 // Erasing the front including the current doesn't break incrementing. 528 L.erase(L.begin(), std::prev(L.end())); 529 ++I; 530 EXPECT_EQ(4, *I); 531 ++I; 532 EXPECT_EQ(EIR.end(), I); 533 } 534 535 // A custom iterator that returns a pointer when dereferenced. This is used to 536 // test make_early_inc_range with iterators that do not return a reference on 537 // dereferencing. 538 struct CustomPointerIterator 539 : public iterator_adaptor_base<CustomPointerIterator, 540 std::list<int>::iterator, 541 std::forward_iterator_tag> { 542 using base_type = 543 iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator, 544 std::forward_iterator_tag>; 545 546 explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {} 547 548 // Retrieve a pointer to the current int. 549 int *operator*() const { return &*base_type::wrapped(); } 550 }; 551 552 // Make sure make_early_inc_range works with iterators that do not return a 553 // reference on dereferencing. The test is similar to EarlyIncrementTest, but 554 // uses CustomPointerIterator. 555 TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) { 556 std::list<int> L = {1, 2, 3, 4}; 557 558 auto CustomRange = make_range(CustomPointerIterator(L.begin()), 559 CustomPointerIterator(L.end())); 560 auto EIR = make_early_inc_range(CustomRange); 561 562 auto I = EIR.begin(); 563 auto EI = EIR.end(); 564 EXPECT_NE(I, EI); 565 566 EXPECT_EQ(&*L.begin(), *I); 567 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 568 #ifndef NDEBUG 569 // Repeated dereferences are not allowed. 570 EXPECT_DEATH(*I, "Cannot dereference"); 571 // Comparison after dereference is not allowed. 572 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 573 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 574 #endif 575 #endif 576 577 ++I; 578 EXPECT_NE(I, EI); 579 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 580 #ifndef NDEBUG 581 // You cannot increment prior to dereference. 582 EXPECT_DEATH(++I, "Cannot increment"); 583 #endif 584 #endif 585 EXPECT_EQ(&*std::next(L.begin()), *I); 586 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 587 #ifndef NDEBUG 588 // Repeated dereferences are not allowed. 589 EXPECT_DEATH(*I, "Cannot dereference"); 590 #endif 591 #endif 592 593 // Inserting shouldn't break anything. We should be able to keep dereferencing 594 // the currrent iterator and increment. The increment to go to the "next" 595 // iterator from before we inserted. 596 L.insert(std::next(L.begin(), 2), -1); 597 ++I; 598 EXPECT_EQ(&*std::next(L.begin(), 3), *I); 599 600 // Erasing the front including the current doesn't break incrementing. 601 L.erase(L.begin(), std::prev(L.end())); 602 ++I; 603 EXPECT_EQ(&*L.begin(), *I); 604 ++I; 605 EXPECT_EQ(EIR.end(), I); 606 } 607 608 TEST(STLExtrasTest, splat) { 609 std::vector<int> V; 610 EXPECT_FALSE(is_splat(V)); 611 612 V.push_back(1); 613 EXPECT_TRUE(is_splat(V)); 614 615 V.push_back(1); 616 V.push_back(1); 617 EXPECT_TRUE(is_splat(V)); 618 619 V.push_back(2); 620 EXPECT_FALSE(is_splat(V)); 621 } 622 623 TEST(STLExtrasTest, to_address) { 624 int *V1 = new int; 625 EXPECT_EQ(V1, to_address(V1)); 626 627 // Check fancy pointer overload for unique_ptr 628 std::unique_ptr<int> V2 = std::make_unique<int>(0); 629 EXPECT_EQ(V2.get(), llvm::to_address(V2)); 630 631 V2.reset(V1); 632 EXPECT_EQ(V1, llvm::to_address(V2)); 633 V2.release(); 634 635 // Check fancy pointer overload for shared_ptr 636 std::shared_ptr<int> V3 = std::make_shared<int>(0); 637 std::shared_ptr<int> V4 = V3; 638 EXPECT_EQ(V3.get(), V4.get()); 639 EXPECT_EQ(V3.get(), llvm::to_address(V3)); 640 EXPECT_EQ(V4.get(), llvm::to_address(V4)); 641 642 V3.reset(V1); 643 EXPECT_EQ(V1, llvm::to_address(V3)); 644 } 645 646 TEST(STLExtrasTest, partition_point) { 647 std::vector<int> V = {1, 3, 5, 7, 9}; 648 649 // Range version. 650 EXPECT_EQ(V.begin() + 3, 651 partition_point(V, [](unsigned X) { return X < 7; })); 652 EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); 653 EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); 654 } 655 656 TEST(STLExtrasTest, hasSingleElement) { 657 const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 658 const std::vector<int> V10(10); 659 660 EXPECT_EQ(hasSingleElement(V0), false); 661 EXPECT_EQ(hasSingleElement(V1), true); 662 EXPECT_EQ(hasSingleElement(V2), false); 663 EXPECT_EQ(hasSingleElement(V10), false); 664 } 665 666 TEST(STLExtrasTest, hasNItems) { 667 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 668 const std::list<int> V3 = {1, 3, 5}; 669 670 EXPECT_TRUE(hasNItems(V0, 0)); 671 EXPECT_FALSE(hasNItems(V0, 2)); 672 EXPECT_TRUE(hasNItems(V1, 1)); 673 EXPECT_FALSE(hasNItems(V1, 2)); 674 675 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 676 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; })); 677 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 678 } 679 680 TEST(STLExtras, hasNItemsOrMore) { 681 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 682 const std::list<int> V3 = {1, 3, 5}; 683 684 EXPECT_TRUE(hasNItemsOrMore(V1, 1)); 685 EXPECT_FALSE(hasNItemsOrMore(V1, 2)); 686 687 EXPECT_TRUE(hasNItemsOrMore(V2, 1)); 688 EXPECT_TRUE(hasNItemsOrMore(V2, 2)); 689 EXPECT_FALSE(hasNItemsOrMore(V2, 3)); 690 691 EXPECT_TRUE(hasNItemsOrMore(V3, 3)); 692 EXPECT_FALSE(hasNItemsOrMore(V3, 4)); 693 694 EXPECT_TRUE( 695 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 696 EXPECT_FALSE( 697 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; })); 698 EXPECT_TRUE( 699 hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 700 } 701 702 TEST(STLExtras, hasNItemsOrLess) { 703 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 704 const std::list<int> V3 = {1, 3, 5}; 705 706 EXPECT_TRUE(hasNItemsOrLess(V0, 0)); 707 EXPECT_TRUE(hasNItemsOrLess(V0, 1)); 708 EXPECT_TRUE(hasNItemsOrLess(V0, 2)); 709 710 EXPECT_FALSE(hasNItemsOrLess(V1, 0)); 711 EXPECT_TRUE(hasNItemsOrLess(V1, 1)); 712 EXPECT_TRUE(hasNItemsOrLess(V1, 2)); 713 714 EXPECT_FALSE(hasNItemsOrLess(V2, 0)); 715 EXPECT_FALSE(hasNItemsOrLess(V2, 1)); 716 EXPECT_TRUE(hasNItemsOrLess(V2, 2)); 717 EXPECT_TRUE(hasNItemsOrLess(V2, 3)); 718 719 EXPECT_FALSE(hasNItemsOrLess(V3, 0)); 720 EXPECT_FALSE(hasNItemsOrLess(V3, 1)); 721 EXPECT_FALSE(hasNItemsOrLess(V3, 2)); 722 EXPECT_TRUE(hasNItemsOrLess(V3, 3)); 723 EXPECT_TRUE(hasNItemsOrLess(V3, 4)); 724 725 EXPECT_TRUE( 726 hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; })); 727 EXPECT_TRUE( 728 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 729 EXPECT_TRUE( 730 hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; })); 731 EXPECT_FALSE( 732 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; })); 733 } 734 735 TEST(STLExtras, MoveRange) { 736 class Foo { 737 bool A; 738 739 public: 740 Foo() : A(true) {} 741 Foo(const Foo &) = delete; 742 Foo(Foo &&Other) : A(Other.A) { Other.A = false; } 743 Foo &operator=(const Foo &) = delete; 744 Foo &operator=(Foo &&Other) { 745 if (this != &Other) { 746 A = Other.A; 747 Other.A = false; 748 } 749 return *this; 750 } 751 operator bool() const { return A; } 752 }; 753 SmallVector<Foo, 4U> V1, V2, V3, V4; 754 auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); }; 755 auto Build = [&] { 756 SmallVector<Foo, 4U> Foos; 757 Foos.resize(4U); 758 return Foos; 759 }; 760 761 V1.resize(4U); 762 EXPECT_TRUE(llvm::all_of(V1, HasVal)); 763 764 llvm::move(V1, std::back_inserter(V2)); 765 766 // Ensure input container is same size, but its contents were moved out. 767 EXPECT_EQ(V1.size(), 4U); 768 EXPECT_TRUE(llvm::none_of(V1, HasVal)); 769 770 // Ensure output container has the contents of the input container. 771 EXPECT_EQ(V2.size(), 4U); 772 EXPECT_TRUE(llvm::all_of(V2, HasVal)); 773 774 llvm::move(std::move(V2), std::back_inserter(V3)); 775 776 EXPECT_TRUE(llvm::none_of(V2, HasVal)); 777 EXPECT_EQ(V3.size(), 4U); 778 EXPECT_TRUE(llvm::all_of(V3, HasVal)); 779 780 llvm::move(Build(), std::back_inserter(V4)); 781 EXPECT_EQ(V4.size(), 4U); 782 EXPECT_TRUE(llvm::all_of(V4, HasVal)); 783 } 784 785 TEST(STLExtras, Unique) { 786 std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; 787 788 auto I = llvm::unique(V, [](int a, int b) { return a == b; }); 789 790 EXPECT_EQ(I, V.begin() + 4); 791 792 EXPECT_EQ(1, V[0]); 793 EXPECT_EQ(5, V[1]); 794 EXPECT_EQ(4, V[2]); 795 EXPECT_EQ(3, V[3]); 796 } 797 798 TEST(STLExtrasTest, MakeVisitorOneCallable) { 799 auto IdentityLambda = [](auto X) { return X; }; 800 auto IdentityVisitor = makeVisitor(IdentityLambda); 801 EXPECT_EQ(IdentityLambda(1), IdentityVisitor(1)); 802 EXPECT_EQ(IdentityLambda(2.0f), IdentityVisitor(2.0f)); 803 EXPECT_TRUE((std::is_same<decltype(IdentityLambda(IdentityLambda)), 804 decltype(IdentityLambda)>::value)); 805 EXPECT_TRUE((std::is_same<decltype(IdentityVisitor(IdentityVisitor)), 806 decltype(IdentityVisitor)>::value)); 807 } 808 809 TEST(STLExtrasTest, MakeVisitorTwoCallables) { 810 auto Visitor = 811 makeVisitor([](int) { return 0; }, [](std::string) { return 1; }); 812 EXPECT_EQ(Visitor(42), 0); 813 EXPECT_EQ(Visitor("foo"), 1); 814 } 815 816 TEST(STLExtrasTest, MakeVisitorCallableMultipleOperands) { 817 auto Second = makeVisitor([](int I, float F) { return F; }, 818 [](float F, int I) { return I; }); 819 EXPECT_EQ(Second(1.f, 1), 1); 820 EXPECT_EQ(Second(1, 1.f), 1.f); 821 } 822 823 TEST(STLExtrasTest, MakeVisitorDefaultCase) { 824 { 825 auto Visitor = makeVisitor([](int I) { return I + 100; }, 826 [](float F) { return F * 2; }, 827 [](auto) { return -1; }); 828 EXPECT_EQ(Visitor(24), 124); 829 EXPECT_EQ(Visitor(2.f), 4.f); 830 EXPECT_EQ(Visitor(2.), -1); 831 EXPECT_EQ(Visitor(Visitor), -1); 832 } 833 { 834 auto Visitor = makeVisitor([](auto) { return -1; }, 835 [](int I) { return I + 100; }, 836 [](float F) { return F * 2; }); 837 EXPECT_EQ(Visitor(24), 124); 838 EXPECT_EQ(Visitor(2.f), 4.f); 839 EXPECT_EQ(Visitor(2.), -1); 840 EXPECT_EQ(Visitor(Visitor), -1); 841 } 842 } 843 844 template <bool Moveable, bool Copyable> 845 struct Functor : Counted<Moveable, Copyable> { 846 using Counted<Moveable, Copyable>::Counted; 847 void operator()() {} 848 }; 849 850 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsPRValue) { 851 int Copies = 0; 852 int Moves = 0; 853 int Destructors = 0; 854 { 855 auto V = makeVisitor(Functor<true, false>(Copies, Moves, Destructors)); 856 (void)V; 857 EXPECT_EQ(0, Copies); 858 EXPECT_EQ(1, Moves); 859 EXPECT_EQ(1, Destructors); 860 } 861 EXPECT_EQ(0, Copies); 862 EXPECT_EQ(1, Moves); 863 EXPECT_EQ(2, Destructors); 864 } 865 866 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsRValue) { 867 int Copies = 0; 868 int Moves = 0; 869 int Destructors = 0; 870 { 871 Functor<true, false> F(Copies, Moves, Destructors); 872 { 873 auto V = makeVisitor(std::move(F)); 874 (void)V; 875 EXPECT_EQ(0, Copies); 876 EXPECT_EQ(1, Moves); 877 EXPECT_EQ(0, Destructors); 878 } 879 EXPECT_EQ(0, Copies); 880 EXPECT_EQ(1, Moves); 881 EXPECT_EQ(1, Destructors); 882 } 883 EXPECT_EQ(0, Copies); 884 EXPECT_EQ(1, Moves); 885 EXPECT_EQ(2, Destructors); 886 } 887 888 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsLValue) { 889 int Copies = 0; 890 int Moves = 0; 891 int Destructors = 0; 892 { 893 Functor<true, true> F(Copies, Moves, Destructors); 894 { 895 auto V = makeVisitor(F); 896 (void)V; 897 EXPECT_EQ(1, Copies); 898 EXPECT_EQ(0, Moves); 899 EXPECT_EQ(0, Destructors); 900 } 901 EXPECT_EQ(1, Copies); 902 EXPECT_EQ(0, Moves); 903 EXPECT_EQ(1, Destructors); 904 } 905 EXPECT_EQ(1, Copies); 906 EXPECT_EQ(0, Moves); 907 EXPECT_EQ(2, Destructors); 908 } 909 910 TEST(STLExtrasTest, AllOfZip) { 911 std::vector<int> v1 = {0, 4, 2, 1}; 912 std::vector<int> v2 = {1, 4, 3, 6}; 913 EXPECT_TRUE(all_of_zip(v1, v2, [](int v1, int v2) { return v1 <= v2; })); 914 EXPECT_FALSE(all_of_zip(v1, v2, [](int L, int R) { return L < R; })); 915 916 // Triple vectors 917 std::vector<int> v3 = {1, 6, 5, 7}; 918 EXPECT_EQ(true, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 919 return a <= b && b <= c; 920 })); 921 EXPECT_EQ(false, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 922 return a < b && b < c; 923 })); 924 925 // Shorter vector should fail even with an always-true predicate. 926 std::vector<int> v_short = {1, 4}; 927 EXPECT_EQ(false, all_of_zip(v1, v_short, [](int, int) { return true; })); 928 EXPECT_EQ(false, 929 all_of_zip(v1, v2, v_short, [](int, int, int) { return true; })); 930 } 931 932 TEST(STLExtrasTest, TypesAreDistinct) { 933 EXPECT_TRUE((llvm::TypesAreDistinct<>::value)); 934 EXPECT_TRUE((llvm::TypesAreDistinct<int>::value)); 935 EXPECT_FALSE((llvm::TypesAreDistinct<int, int>::value)); 936 EXPECT_TRUE((llvm::TypesAreDistinct<int, float>::value)); 937 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, int>::value)); 938 EXPECT_TRUE((llvm::TypesAreDistinct<int, float, double>::value)); 939 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, double, float>::value)); 940 EXPECT_TRUE((llvm::TypesAreDistinct<int, int *>::value)); 941 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &>::value)); 942 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &&>::value)); 943 EXPECT_TRUE((llvm::TypesAreDistinct<int, const int>::value)); 944 } 945 946 TEST(STLExtrasTest, FirstIndexOfType) { 947 EXPECT_EQ((llvm::FirstIndexOfType<int, int>::value), 0u); 948 EXPECT_EQ((llvm::FirstIndexOfType<int, int, int>::value), 0u); 949 EXPECT_EQ((llvm::FirstIndexOfType<int, float, int>::value), 1u); 950 EXPECT_EQ((llvm::FirstIndexOfType<int const *, float, int, int const *, 951 const int>::value), 952 2u); 953 } 954 955 TEST(STLExtrasTest, TypeAtIndex) { 956 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int>>::value)); 957 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int, float>>::value)); 958 EXPECT_TRUE((std::is_same<float, llvm::TypeAtIndex<1, int, float>>::value)); 959 EXPECT_TRUE( 960 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 961 EXPECT_TRUE( 962 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 963 EXPECT_TRUE( 964 (std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value)); 965 } 966 967 enum Doggos { 968 Floofer, 969 Woofer, 970 SubWoofer, 971 Pupper, 972 Pupperino, 973 Longboi, 974 }; 975 976 TEST(STLExtrasTest, IsContainedInitializerList) { 977 EXPECT_TRUE(is_contained({Woofer, SubWoofer}, Woofer)); 978 EXPECT_TRUE(is_contained({Woofer, SubWoofer}, SubWoofer)); 979 EXPECT_FALSE(is_contained({Woofer, SubWoofer}, Pupper)); 980 EXPECT_FALSE(is_contained({}, Longboi)); 981 982 static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!"); 983 static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!"); 984 985 EXPECT_TRUE(is_contained({1, 2, 3, 4}, 3)); 986 EXPECT_FALSE(is_contained({1, 2, 3, 4}, 5)); 987 988 static_assert(is_contained({1, 2, 3, 4}, 3), "It's there!"); 989 static_assert(!is_contained({1, 2, 3, 4}, 5), "It's not there :("); 990 } 991 992 } // namespace 993