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, EarlyIncrementTest) { 461 std::list<int> L = {1, 2, 3, 4}; 462 463 auto EIR = make_early_inc_range(L); 464 465 auto I = EIR.begin(); 466 auto EI = EIR.end(); 467 EXPECT_NE(I, EI); 468 469 EXPECT_EQ(1, *I); 470 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 471 #ifndef NDEBUG 472 // Repeated dereferences are not allowed. 473 EXPECT_DEATH(*I, "Cannot dereference"); 474 // Comparison after dereference is not allowed. 475 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 476 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 477 #endif 478 #endif 479 480 ++I; 481 EXPECT_NE(I, EI); 482 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 483 #ifndef NDEBUG 484 // You cannot increment prior to dereference. 485 EXPECT_DEATH(++I, "Cannot increment"); 486 #endif 487 #endif 488 EXPECT_EQ(2, *I); 489 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 490 #ifndef NDEBUG 491 // Repeated dereferences are not allowed. 492 EXPECT_DEATH(*I, "Cannot dereference"); 493 #endif 494 #endif 495 496 // Inserting shouldn't break anything. We should be able to keep dereferencing 497 // the currrent iterator and increment. The increment to go to the "next" 498 // iterator from before we inserted. 499 L.insert(std::next(L.begin(), 2), -1); 500 ++I; 501 EXPECT_EQ(3, *I); 502 503 // Erasing the front including the current doesn't break incrementing. 504 L.erase(L.begin(), std::prev(L.end())); 505 ++I; 506 EXPECT_EQ(4, *I); 507 ++I; 508 EXPECT_EQ(EIR.end(), I); 509 } 510 511 // A custom iterator that returns a pointer when dereferenced. This is used to 512 // test make_early_inc_range with iterators that do not return a reference on 513 // dereferencing. 514 struct CustomPointerIterator 515 : public iterator_adaptor_base<CustomPointerIterator, 516 std::list<int>::iterator, 517 std::forward_iterator_tag> { 518 using base_type = 519 iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator, 520 std::forward_iterator_tag>; 521 522 explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {} 523 524 // Retrieve a pointer to the current int. 525 int *operator*() const { return &*base_type::wrapped(); } 526 }; 527 528 // Make sure make_early_inc_range works with iterators that do not return a 529 // reference on dereferencing. The test is similar to EarlyIncrementTest, but 530 // uses CustomPointerIterator. 531 TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) { 532 std::list<int> L = {1, 2, 3, 4}; 533 534 auto CustomRange = make_range(CustomPointerIterator(L.begin()), 535 CustomPointerIterator(L.end())); 536 auto EIR = make_early_inc_range(CustomRange); 537 538 auto I = EIR.begin(); 539 auto EI = EIR.end(); 540 EXPECT_NE(I, EI); 541 542 EXPECT_EQ(&*L.begin(), *I); 543 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 544 #ifndef NDEBUG 545 // Repeated dereferences are not allowed. 546 EXPECT_DEATH(*I, "Cannot dereference"); 547 // Comparison after dereference is not allowed. 548 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 549 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 550 #endif 551 #endif 552 553 ++I; 554 EXPECT_NE(I, EI); 555 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 556 #ifndef NDEBUG 557 // You cannot increment prior to dereference. 558 EXPECT_DEATH(++I, "Cannot increment"); 559 #endif 560 #endif 561 EXPECT_EQ(&*std::next(L.begin()), *I); 562 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 563 #ifndef NDEBUG 564 // Repeated dereferences are not allowed. 565 EXPECT_DEATH(*I, "Cannot dereference"); 566 #endif 567 #endif 568 569 // Inserting shouldn't break anything. We should be able to keep dereferencing 570 // the currrent iterator and increment. The increment to go to the "next" 571 // iterator from before we inserted. 572 L.insert(std::next(L.begin(), 2), -1); 573 ++I; 574 EXPECT_EQ(&*std::next(L.begin(), 3), *I); 575 576 // Erasing the front including the current doesn't break incrementing. 577 L.erase(L.begin(), std::prev(L.end())); 578 ++I; 579 EXPECT_EQ(&*L.begin(), *I); 580 ++I; 581 EXPECT_EQ(EIR.end(), I); 582 } 583 584 TEST(STLExtrasTest, splat) { 585 std::vector<int> V; 586 EXPECT_FALSE(is_splat(V)); 587 588 V.push_back(1); 589 EXPECT_TRUE(is_splat(V)); 590 591 V.push_back(1); 592 V.push_back(1); 593 EXPECT_TRUE(is_splat(V)); 594 595 V.push_back(2); 596 EXPECT_FALSE(is_splat(V)); 597 } 598 599 TEST(STLExtrasTest, to_address) { 600 int *V1 = new int; 601 EXPECT_EQ(V1, to_address(V1)); 602 603 // Check fancy pointer overload for unique_ptr 604 std::unique_ptr<int> V2 = std::make_unique<int>(0); 605 EXPECT_EQ(V2.get(), llvm::to_address(V2)); 606 607 V2.reset(V1); 608 EXPECT_EQ(V1, llvm::to_address(V2)); 609 V2.release(); 610 611 // Check fancy pointer overload for shared_ptr 612 std::shared_ptr<int> V3 = std::make_shared<int>(0); 613 std::shared_ptr<int> V4 = V3; 614 EXPECT_EQ(V3.get(), V4.get()); 615 EXPECT_EQ(V3.get(), llvm::to_address(V3)); 616 EXPECT_EQ(V4.get(), llvm::to_address(V4)); 617 618 V3.reset(V1); 619 EXPECT_EQ(V1, llvm::to_address(V3)); 620 } 621 622 TEST(STLExtrasTest, partition_point) { 623 std::vector<int> V = {1, 3, 5, 7, 9}; 624 625 // Range version. 626 EXPECT_EQ(V.begin() + 3, 627 partition_point(V, [](unsigned X) { return X < 7; })); 628 EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); 629 EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); 630 } 631 632 TEST(STLExtrasTest, hasSingleElement) { 633 const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 634 const std::vector<int> V10(10); 635 636 EXPECT_EQ(hasSingleElement(V0), false); 637 EXPECT_EQ(hasSingleElement(V1), true); 638 EXPECT_EQ(hasSingleElement(V2), false); 639 EXPECT_EQ(hasSingleElement(V10), false); 640 } 641 642 TEST(STLExtrasTest, hasNItems) { 643 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 644 const std::list<int> V3 = {1, 3, 5}; 645 646 EXPECT_TRUE(hasNItems(V0, 0)); 647 EXPECT_FALSE(hasNItems(V0, 2)); 648 EXPECT_TRUE(hasNItems(V1, 1)); 649 EXPECT_FALSE(hasNItems(V1, 2)); 650 651 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 652 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; })); 653 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 654 } 655 656 TEST(STLExtras, hasNItemsOrMore) { 657 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 658 const std::list<int> V3 = {1, 3, 5}; 659 660 EXPECT_TRUE(hasNItemsOrMore(V1, 1)); 661 EXPECT_FALSE(hasNItemsOrMore(V1, 2)); 662 663 EXPECT_TRUE(hasNItemsOrMore(V2, 1)); 664 EXPECT_TRUE(hasNItemsOrMore(V2, 2)); 665 EXPECT_FALSE(hasNItemsOrMore(V2, 3)); 666 667 EXPECT_TRUE(hasNItemsOrMore(V3, 3)); 668 EXPECT_FALSE(hasNItemsOrMore(V3, 4)); 669 670 EXPECT_TRUE( 671 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 672 EXPECT_FALSE( 673 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; })); 674 EXPECT_TRUE( 675 hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 676 } 677 678 TEST(STLExtras, hasNItemsOrLess) { 679 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 680 const std::list<int> V3 = {1, 3, 5}; 681 682 EXPECT_TRUE(hasNItemsOrLess(V0, 0)); 683 EXPECT_TRUE(hasNItemsOrLess(V0, 1)); 684 EXPECT_TRUE(hasNItemsOrLess(V0, 2)); 685 686 EXPECT_FALSE(hasNItemsOrLess(V1, 0)); 687 EXPECT_TRUE(hasNItemsOrLess(V1, 1)); 688 EXPECT_TRUE(hasNItemsOrLess(V1, 2)); 689 690 EXPECT_FALSE(hasNItemsOrLess(V2, 0)); 691 EXPECT_FALSE(hasNItemsOrLess(V2, 1)); 692 EXPECT_TRUE(hasNItemsOrLess(V2, 2)); 693 EXPECT_TRUE(hasNItemsOrLess(V2, 3)); 694 695 EXPECT_FALSE(hasNItemsOrLess(V3, 0)); 696 EXPECT_FALSE(hasNItemsOrLess(V3, 1)); 697 EXPECT_FALSE(hasNItemsOrLess(V3, 2)); 698 EXPECT_TRUE(hasNItemsOrLess(V3, 3)); 699 EXPECT_TRUE(hasNItemsOrLess(V3, 4)); 700 701 EXPECT_TRUE( 702 hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; })); 703 EXPECT_TRUE( 704 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 705 EXPECT_TRUE( 706 hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; })); 707 EXPECT_FALSE( 708 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; })); 709 } 710 711 TEST(STLExtras, MoveRange) { 712 class Foo { 713 bool A; 714 715 public: 716 Foo() : A(true) {} 717 Foo(const Foo &) = delete; 718 Foo(Foo &&Other) : A(Other.A) { Other.A = false; } 719 Foo &operator=(const Foo &) = delete; 720 Foo &operator=(Foo &&Other) { 721 if (this != &Other) { 722 A = Other.A; 723 Other.A = false; 724 } 725 return *this; 726 } 727 operator bool() const { return A; } 728 }; 729 SmallVector<Foo, 4U> V1, V2, V3, V4; 730 auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); }; 731 auto Build = [&] { 732 SmallVector<Foo, 4U> Foos; 733 Foos.resize(4U); 734 return Foos; 735 }; 736 737 V1.resize(4U); 738 EXPECT_TRUE(llvm::all_of(V1, HasVal)); 739 740 llvm::move(V1, std::back_inserter(V2)); 741 742 // Ensure input container is same size, but its contents were moved out. 743 EXPECT_EQ(V1.size(), 4U); 744 EXPECT_TRUE(llvm::none_of(V1, HasVal)); 745 746 // Ensure output container has the contents of the input container. 747 EXPECT_EQ(V2.size(), 4U); 748 EXPECT_TRUE(llvm::all_of(V2, HasVal)); 749 750 llvm::move(std::move(V2), std::back_inserter(V3)); 751 752 EXPECT_TRUE(llvm::none_of(V2, HasVal)); 753 EXPECT_EQ(V3.size(), 4U); 754 EXPECT_TRUE(llvm::all_of(V3, HasVal)); 755 756 llvm::move(Build(), std::back_inserter(V4)); 757 EXPECT_EQ(V4.size(), 4U); 758 EXPECT_TRUE(llvm::all_of(V4, HasVal)); 759 } 760 761 TEST(STLExtras, Unique) { 762 std::vector<int> V = {1, 5, 5, 4, 3, 3, 3}; 763 764 auto I = llvm::unique(V, [](int a, int b) { return a == b; }); 765 766 EXPECT_EQ(I, V.begin() + 4); 767 768 EXPECT_EQ(1, V[0]); 769 EXPECT_EQ(5, V[1]); 770 EXPECT_EQ(4, V[2]); 771 EXPECT_EQ(3, V[3]); 772 } 773 774 TEST(STLExtrasTest, MakeVisitorOneCallable) { 775 auto IdentityLambda = [](auto X) { return X; }; 776 auto IdentityVisitor = makeVisitor(IdentityLambda); 777 EXPECT_EQ(IdentityLambda(1), IdentityVisitor(1)); 778 EXPECT_EQ(IdentityLambda(2.0f), IdentityVisitor(2.0f)); 779 EXPECT_TRUE((std::is_same<decltype(IdentityLambda(IdentityLambda)), 780 decltype(IdentityLambda)>::value)); 781 EXPECT_TRUE((std::is_same<decltype(IdentityVisitor(IdentityVisitor)), 782 decltype(IdentityVisitor)>::value)); 783 } 784 785 TEST(STLExtrasTest, MakeVisitorTwoCallables) { 786 auto Visitor = 787 makeVisitor([](int) { return 0; }, [](std::string) { return 1; }); 788 EXPECT_EQ(Visitor(42), 0); 789 EXPECT_EQ(Visitor("foo"), 1); 790 } 791 792 TEST(STLExtrasTest, MakeVisitorCallableMultipleOperands) { 793 auto Second = makeVisitor([](int I, float F) { return F; }, 794 [](float F, int I) { return I; }); 795 EXPECT_EQ(Second(1.f, 1), 1); 796 EXPECT_EQ(Second(1, 1.f), 1.f); 797 } 798 799 TEST(STLExtrasTest, MakeVisitorDefaultCase) { 800 { 801 auto Visitor = makeVisitor([](int I) { return I + 100; }, 802 [](float F) { return F * 2; }, 803 [](auto) { return -1; }); 804 EXPECT_EQ(Visitor(24), 124); 805 EXPECT_EQ(Visitor(2.f), 4.f); 806 EXPECT_EQ(Visitor(2.), -1); 807 EXPECT_EQ(Visitor(Visitor), -1); 808 } 809 { 810 auto Visitor = makeVisitor([](auto) { return -1; }, 811 [](int I) { return I + 100; }, 812 [](float F) { return F * 2; }); 813 EXPECT_EQ(Visitor(24), 124); 814 EXPECT_EQ(Visitor(2.f), 4.f); 815 EXPECT_EQ(Visitor(2.), -1); 816 EXPECT_EQ(Visitor(Visitor), -1); 817 } 818 } 819 820 template <bool Moveable, bool Copyable> 821 struct Functor : Counted<Moveable, Copyable> { 822 using Counted<Moveable, Copyable>::Counted; 823 void operator()() {} 824 }; 825 826 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsPRValue) { 827 int Copies = 0; 828 int Moves = 0; 829 int Destructors = 0; 830 { 831 auto V = makeVisitor(Functor<true, false>(Copies, Moves, Destructors)); 832 (void)V; 833 EXPECT_EQ(0, Copies); 834 EXPECT_EQ(1, Moves); 835 EXPECT_EQ(1, Destructors); 836 } 837 EXPECT_EQ(0, Copies); 838 EXPECT_EQ(1, Moves); 839 EXPECT_EQ(2, Destructors); 840 } 841 842 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsRValue) { 843 int Copies = 0; 844 int Moves = 0; 845 int Destructors = 0; 846 { 847 Functor<true, false> F(Copies, Moves, Destructors); 848 { 849 auto V = makeVisitor(std::move(F)); 850 (void)V; 851 EXPECT_EQ(0, Copies); 852 EXPECT_EQ(1, Moves); 853 EXPECT_EQ(0, Destructors); 854 } 855 EXPECT_EQ(0, Copies); 856 EXPECT_EQ(1, Moves); 857 EXPECT_EQ(1, Destructors); 858 } 859 EXPECT_EQ(0, Copies); 860 EXPECT_EQ(1, Moves); 861 EXPECT_EQ(2, Destructors); 862 } 863 864 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsLValue) { 865 int Copies = 0; 866 int Moves = 0; 867 int Destructors = 0; 868 { 869 Functor<true, true> F(Copies, Moves, Destructors); 870 { 871 auto V = makeVisitor(F); 872 (void)V; 873 EXPECT_EQ(1, Copies); 874 EXPECT_EQ(0, Moves); 875 EXPECT_EQ(0, Destructors); 876 } 877 EXPECT_EQ(1, Copies); 878 EXPECT_EQ(0, Moves); 879 EXPECT_EQ(1, Destructors); 880 } 881 EXPECT_EQ(1, Copies); 882 EXPECT_EQ(0, Moves); 883 EXPECT_EQ(2, Destructors); 884 } 885 886 TEST(STLExtrasTest, AllOfZip) { 887 std::vector<int> v1 = {0, 4, 2, 1}; 888 std::vector<int> v2 = {1, 4, 3, 6}; 889 EXPECT_TRUE(all_of_zip(v1, v2, [](int v1, int v2) { return v1 <= v2; })); 890 EXPECT_FALSE(all_of_zip(v1, v2, [](int L, int R) { return L < R; })); 891 892 // Triple vectors 893 std::vector<int> v3 = {1, 6, 5, 7}; 894 EXPECT_EQ(true, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 895 return a <= b && b <= c; 896 })); 897 EXPECT_EQ(false, all_of_zip(v1, v2, v3, [](int a, int b, int c) { 898 return a < b && b < c; 899 })); 900 901 // Shorter vector should fail even with an always-true predicate. 902 std::vector<int> v_short = {1, 4}; 903 EXPECT_EQ(false, all_of_zip(v1, v_short, [](int, int) { return true; })); 904 EXPECT_EQ(false, 905 all_of_zip(v1, v2, v_short, [](int, int, int) { return true; })); 906 } 907 908 TEST(STLExtrasTest, TypesAreDistinct) { 909 EXPECT_TRUE((llvm::TypesAreDistinct<>::value)); 910 EXPECT_TRUE((llvm::TypesAreDistinct<int>::value)); 911 EXPECT_FALSE((llvm::TypesAreDistinct<int, int>::value)); 912 EXPECT_TRUE((llvm::TypesAreDistinct<int, float>::value)); 913 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, int>::value)); 914 EXPECT_TRUE((llvm::TypesAreDistinct<int, float, double>::value)); 915 EXPECT_FALSE((llvm::TypesAreDistinct<int, float, double, float>::value)); 916 EXPECT_TRUE((llvm::TypesAreDistinct<int, int *>::value)); 917 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &>::value)); 918 EXPECT_TRUE((llvm::TypesAreDistinct<int, int &&>::value)); 919 EXPECT_TRUE((llvm::TypesAreDistinct<int, const int>::value)); 920 } 921 922 TEST(STLExtrasTest, FirstIndexOfType) { 923 EXPECT_EQ((llvm::FirstIndexOfType<int, int>::value), 0u); 924 EXPECT_EQ((llvm::FirstIndexOfType<int, int, int>::value), 0u); 925 EXPECT_EQ((llvm::FirstIndexOfType<int, float, int>::value), 1u); 926 EXPECT_EQ((llvm::FirstIndexOfType<int const *, float, int, int const *, 927 const int>::value), 928 2u); 929 } 930 931 TEST(STLExtrasTest, TypeAtIndex) { 932 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int>>::value)); 933 EXPECT_TRUE((std::is_same<int, llvm::TypeAtIndex<0, int, float>>::value)); 934 EXPECT_TRUE((std::is_same<float, llvm::TypeAtIndex<1, int, float>>::value)); 935 EXPECT_TRUE( 936 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 937 EXPECT_TRUE( 938 (std::is_same<float, llvm::TypeAtIndex<1, int, float, double>>::value)); 939 EXPECT_TRUE( 940 (std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value)); 941 } 942 943 } // namespace 944