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 struct Range : CanMove<Moveable>, CanCopy<Copyable> { 144 explicit Range(int &C, int &M, int &D) : C(C), M(M), D(D) {} 145 Range(const Range &R) : CanCopy<Copyable>(R), C(R.C), M(R.M), D(R.D) { ++C; } 146 Range(Range &&R) : CanMove<Moveable>(std::move(R)), C(R.C), M(R.M), D(R.D) { 147 ++M; 148 } 149 ~Range() { ++D; } 150 151 int &C; 152 int &M; 153 int &D; 154 155 int *begin() { return nullptr; } 156 int *end() { return nullptr; } 157 }; 158 159 TEST(STLExtrasTest, EnumerateLifetimeSemantics) { 160 // Test that when enumerating lvalues and rvalues, there are no surprise 161 // copies or moves. 162 163 // With an rvalue, it should not be destroyed until the end of the scope. 164 int Copies = 0; 165 int Moves = 0; 166 int Destructors = 0; 167 { 168 auto E1 = enumerate(Range<true, false>(Copies, Moves, Destructors)); 169 // Doesn't compile. rvalue ranges must be moveable. 170 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors)); 171 EXPECT_EQ(0, Copies); 172 EXPECT_EQ(1, Moves); 173 EXPECT_EQ(1, Destructors); 174 } 175 EXPECT_EQ(0, Copies); 176 EXPECT_EQ(1, Moves); 177 EXPECT_EQ(2, Destructors); 178 179 Copies = Moves = Destructors = 0; 180 // With an lvalue, it should not be destroyed even after the end of the scope. 181 // lvalue ranges need be neither copyable nor moveable. 182 Range<false, false> R(Copies, Moves, Destructors); 183 { 184 auto Enumerator = enumerate(R); 185 (void)Enumerator; 186 EXPECT_EQ(0, Copies); 187 EXPECT_EQ(0, Moves); 188 EXPECT_EQ(0, Destructors); 189 } 190 EXPECT_EQ(0, Copies); 191 EXPECT_EQ(0, Moves); 192 EXPECT_EQ(0, Destructors); 193 } 194 195 TEST(STLExtrasTest, ApplyTuple) { 196 auto T = std::make_tuple(1, 3, 7); 197 auto U = llvm::apply_tuple( 198 [](int A, int B, int C) { return std::make_tuple(A - B, B - C, C - A); }, 199 T); 200 201 EXPECT_EQ(-2, std::get<0>(U)); 202 EXPECT_EQ(-4, std::get<1>(U)); 203 EXPECT_EQ(6, std::get<2>(U)); 204 205 auto V = llvm::apply_tuple( 206 [](int A, int B, int C) { 207 return std::make_tuple(std::make_pair(A, char('A' + A)), 208 std::make_pair(B, char('A' + B)), 209 std::make_pair(C, char('A' + C))); 210 }, 211 T); 212 213 EXPECT_EQ(std::make_pair(1, 'B'), std::get<0>(V)); 214 EXPECT_EQ(std::make_pair(3, 'D'), std::get<1>(V)); 215 EXPECT_EQ(std::make_pair(7, 'H'), std::get<2>(V)); 216 } 217 218 class apply_variadic { 219 static int apply_one(int X) { return X + 1; } 220 static char apply_one(char C) { return C + 1; } 221 static StringRef apply_one(StringRef S) { return S.drop_back(); } 222 223 public: 224 template <typename... Ts> auto operator()(Ts &&... Items) { 225 return std::make_tuple(apply_one(Items)...); 226 } 227 }; 228 229 TEST(STLExtrasTest, ApplyTupleVariadic) { 230 auto Items = std::make_tuple(1, llvm::StringRef("Test"), 'X'); 231 auto Values = apply_tuple(apply_variadic(), Items); 232 233 EXPECT_EQ(2, std::get<0>(Values)); 234 EXPECT_EQ("Tes", std::get<1>(Values)); 235 EXPECT_EQ('Y', std::get<2>(Values)); 236 } 237 238 TEST(STLExtrasTest, CountAdaptor) { 239 std::vector<int> v; 240 241 v.push_back(1); 242 v.push_back(2); 243 v.push_back(1); 244 v.push_back(4); 245 v.push_back(3); 246 v.push_back(2); 247 v.push_back(1); 248 249 EXPECT_EQ(3, count(v, 1)); 250 EXPECT_EQ(2, count(v, 2)); 251 EXPECT_EQ(1, count(v, 3)); 252 EXPECT_EQ(1, count(v, 4)); 253 } 254 255 TEST(STLExtrasTest, for_each) { 256 std::vector<int> v{0, 1, 2, 3, 4}; 257 int count = 0; 258 259 llvm::for_each(v, [&count](int) { ++count; }); 260 EXPECT_EQ(5, count); 261 } 262 263 TEST(STLExtrasTest, ToVector) { 264 std::vector<char> v = {'a', 'b', 'c'}; 265 auto Enumerated = to_vector<4>(enumerate(v)); 266 ASSERT_EQ(3u, Enumerated.size()); 267 for (size_t I = 0; I < v.size(); ++I) { 268 EXPECT_EQ(I, Enumerated[I].index()); 269 EXPECT_EQ(v[I], Enumerated[I].value()); 270 } 271 } 272 273 TEST(STLExtrasTest, ConcatRange) { 274 std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8}; 275 std::vector<int> Test; 276 277 std::vector<int> V1234 = {1, 2, 3, 4}; 278 std::list<int> L56 = {5, 6}; 279 SmallVector<int, 2> SV78 = {7, 8}; 280 281 // Use concat across different sized ranges of different types with different 282 // iterators. 283 for (int &i : concat<int>(V1234, L56, SV78)) 284 Test.push_back(i); 285 EXPECT_EQ(Expected, Test); 286 287 // Use concat between a temporary, an L-value, and an R-value to make sure 288 // complex lifetimes work well. 289 Test.clear(); 290 for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78))) 291 Test.push_back(i); 292 EXPECT_EQ(Expected, Test); 293 } 294 295 TEST(STLExtrasTest, PartitionAdaptor) { 296 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 297 298 auto I = partition(V, [](int i) { return i % 2 == 0; }); 299 ASSERT_EQ(V.begin() + 4, I); 300 301 // Sort the two halves as partition may have messed with the order. 302 llvm::sort(V.begin(), I); 303 llvm::sort(I, V.end()); 304 305 EXPECT_EQ(2, V[0]); 306 EXPECT_EQ(4, V[1]); 307 EXPECT_EQ(6, V[2]); 308 EXPECT_EQ(8, V[3]); 309 EXPECT_EQ(1, V[4]); 310 EXPECT_EQ(3, V[5]); 311 EXPECT_EQ(5, V[6]); 312 EXPECT_EQ(7, V[7]); 313 } 314 315 TEST(STLExtrasTest, EraseIf) { 316 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8}; 317 318 erase_if(V, [](int i) { return i % 2 == 0; }); 319 EXPECT_EQ(4u, V.size()); 320 EXPECT_EQ(1, V[0]); 321 EXPECT_EQ(3, V[1]); 322 EXPECT_EQ(5, V[2]); 323 EXPECT_EQ(7, V[3]); 324 } 325 326 TEST(STLExtrasTest, AppendRange) { 327 auto AppendVals = {3}; 328 std::vector<int> V = {1, 2}; 329 append_range(V, AppendVals); 330 EXPECT_EQ(1, V[0]); 331 EXPECT_EQ(2, V[1]); 332 EXPECT_EQ(3, V[2]); 333 } 334 335 namespace some_namespace { 336 struct some_struct { 337 std::vector<int> data; 338 std::string swap_val; 339 }; 340 341 std::vector<int>::const_iterator begin(const some_struct &s) { 342 return s.data.begin(); 343 } 344 345 std::vector<int>::const_iterator end(const some_struct &s) { 346 return s.data.end(); 347 } 348 349 void swap(some_struct &lhs, some_struct &rhs) { 350 // make swap visible as non-adl swap would even seem to 351 // work with std::swap which defaults to moving 352 lhs.swap_val = "lhs"; 353 rhs.swap_val = "rhs"; 354 } 355 } // namespace some_namespace 356 357 TEST(STLExtrasTest, ADLTest) { 358 some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""}; 359 some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""}; 360 361 EXPECT_EQ(*adl_begin(s), 1); 362 EXPECT_EQ(*(adl_end(s) - 1), 5); 363 364 adl_swap(s, s2); 365 EXPECT_EQ(s.swap_val, "lhs"); 366 EXPECT_EQ(s2.swap_val, "rhs"); 367 368 int count = 0; 369 llvm::for_each(s, [&count](int) { ++count; }); 370 EXPECT_EQ(5, count); 371 } 372 373 TEST(STLExtrasTest, EmptyTest) { 374 std::vector<void*> V; 375 EXPECT_TRUE(llvm::empty(V)); 376 V.push_back(nullptr); 377 EXPECT_FALSE(llvm::empty(V)); 378 379 std::initializer_list<int> E = {}; 380 std::initializer_list<int> NotE = {7, 13, 42}; 381 EXPECT_TRUE(llvm::empty(E)); 382 EXPECT_FALSE(llvm::empty(NotE)); 383 384 auto R0 = make_range(V.begin(), V.begin()); 385 EXPECT_TRUE(llvm::empty(R0)); 386 auto R1 = make_range(V.begin(), V.end()); 387 EXPECT_FALSE(llvm::empty(R1)); 388 } 389 390 TEST(STLExtrasTest, DropBeginTest) { 391 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 392 393 for (int n = 0; n < 5; ++n) { 394 int i = n; 395 for (auto &v : drop_begin(vec, n)) { 396 EXPECT_EQ(v, i); 397 i += 1; 398 } 399 EXPECT_EQ(i, 5); 400 } 401 } 402 403 TEST(STLExtrasTest, DropBeginDefaultTest) { 404 SmallVector<int, 5> vec{0, 1, 2, 3, 4}; 405 406 int i = 1; 407 for (auto &v : drop_begin(vec)) { 408 EXPECT_EQ(v, i); 409 i += 1; 410 } 411 EXPECT_EQ(i, 5); 412 } 413 414 TEST(STLExtrasTest, EarlyIncrementTest) { 415 std::list<int> L = {1, 2, 3, 4}; 416 417 auto EIR = make_early_inc_range(L); 418 419 auto I = EIR.begin(); 420 auto EI = EIR.end(); 421 EXPECT_NE(I, EI); 422 423 EXPECT_EQ(1, *I); 424 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 425 #ifndef NDEBUG 426 // Repeated dereferences are not allowed. 427 EXPECT_DEATH(*I, "Cannot dereference"); 428 // Comparison after dereference is not allowed. 429 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 430 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 431 #endif 432 #endif 433 434 ++I; 435 EXPECT_NE(I, EI); 436 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 437 #ifndef NDEBUG 438 // You cannot increment prior to dereference. 439 EXPECT_DEATH(++I, "Cannot increment"); 440 #endif 441 #endif 442 EXPECT_EQ(2, *I); 443 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 444 #ifndef NDEBUG 445 // Repeated dereferences are not allowed. 446 EXPECT_DEATH(*I, "Cannot dereference"); 447 #endif 448 #endif 449 450 // Inserting shouldn't break anything. We should be able to keep dereferencing 451 // the currrent iterator and increment. The increment to go to the "next" 452 // iterator from before we inserted. 453 L.insert(std::next(L.begin(), 2), -1); 454 ++I; 455 EXPECT_EQ(3, *I); 456 457 // Erasing the front including the current doesn't break incrementing. 458 L.erase(L.begin(), std::prev(L.end())); 459 ++I; 460 EXPECT_EQ(4, *I); 461 ++I; 462 EXPECT_EQ(EIR.end(), I); 463 } 464 465 // A custom iterator that returns a pointer when dereferenced. This is used to 466 // test make_early_inc_range with iterators that do not return a reference on 467 // dereferencing. 468 struct CustomPointerIterator 469 : public iterator_adaptor_base<CustomPointerIterator, 470 std::list<int>::iterator, 471 std::forward_iterator_tag> { 472 using base_type = 473 iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator, 474 std::forward_iterator_tag>; 475 476 explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {} 477 478 // Retrieve a pointer to the current int. 479 int *operator*() const { return &*base_type::wrapped(); } 480 }; 481 482 // Make sure make_early_inc_range works with iterators that do not return a 483 // reference on dereferencing. The test is similar to EarlyIncrementTest, but 484 // uses CustomPointerIterator. 485 TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) { 486 std::list<int> L = {1, 2, 3, 4}; 487 488 auto CustomRange = make_range(CustomPointerIterator(L.begin()), 489 CustomPointerIterator(L.end())); 490 auto EIR = make_early_inc_range(CustomRange); 491 492 auto I = EIR.begin(); 493 auto EI = EIR.end(); 494 EXPECT_NE(I, EI); 495 496 EXPECT_EQ(&*L.begin(), *I); 497 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 498 #ifndef NDEBUG 499 // Repeated dereferences are not allowed. 500 EXPECT_DEATH(*I, "Cannot dereference"); 501 // Comparison after dereference is not allowed. 502 EXPECT_DEATH((void)(I == EI), "Cannot compare"); 503 EXPECT_DEATH((void)(I != EI), "Cannot compare"); 504 #endif 505 #endif 506 507 ++I; 508 EXPECT_NE(I, EI); 509 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 510 #ifndef NDEBUG 511 // You cannot increment prior to dereference. 512 EXPECT_DEATH(++I, "Cannot increment"); 513 #endif 514 #endif 515 EXPECT_EQ(&*std::next(L.begin()), *I); 516 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 517 #ifndef NDEBUG 518 // Repeated dereferences are not allowed. 519 EXPECT_DEATH(*I, "Cannot dereference"); 520 #endif 521 #endif 522 523 // Inserting shouldn't break anything. We should be able to keep dereferencing 524 // the currrent iterator and increment. The increment to go to the "next" 525 // iterator from before we inserted. 526 L.insert(std::next(L.begin(), 2), -1); 527 ++I; 528 EXPECT_EQ(&*std::next(L.begin(), 3), *I); 529 530 // Erasing the front including the current doesn't break incrementing. 531 L.erase(L.begin(), std::prev(L.end())); 532 ++I; 533 EXPECT_EQ(&*L.begin(), *I); 534 ++I; 535 EXPECT_EQ(EIR.end(), I); 536 } 537 538 TEST(STLExtrasTest, splat) { 539 std::vector<int> V; 540 EXPECT_FALSE(is_splat(V)); 541 542 V.push_back(1); 543 EXPECT_TRUE(is_splat(V)); 544 545 V.push_back(1); 546 V.push_back(1); 547 EXPECT_TRUE(is_splat(V)); 548 549 V.push_back(2); 550 EXPECT_FALSE(is_splat(V)); 551 } 552 553 TEST(STLExtrasTest, to_address) { 554 int *V1 = new int; 555 EXPECT_EQ(V1, to_address(V1)); 556 557 // Check fancy pointer overload for unique_ptr 558 std::unique_ptr<int> V2 = std::make_unique<int>(0); 559 EXPECT_EQ(V2.get(), llvm::to_address(V2)); 560 561 V2.reset(V1); 562 EXPECT_EQ(V1, llvm::to_address(V2)); 563 V2.release(); 564 565 // Check fancy pointer overload for shared_ptr 566 std::shared_ptr<int> V3 = std::make_shared<int>(0); 567 std::shared_ptr<int> V4 = V3; 568 EXPECT_EQ(V3.get(), V4.get()); 569 EXPECT_EQ(V3.get(), llvm::to_address(V3)); 570 EXPECT_EQ(V4.get(), llvm::to_address(V4)); 571 572 V3.reset(V1); 573 EXPECT_EQ(V1, llvm::to_address(V3)); 574 } 575 576 TEST(STLExtrasTest, partition_point) { 577 std::vector<int> V = {1, 3, 5, 7, 9}; 578 579 // Range version. 580 EXPECT_EQ(V.begin() + 3, 581 partition_point(V, [](unsigned X) { return X < 7; })); 582 EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); 583 EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); 584 } 585 586 TEST(STLExtrasTest, hasSingleElement) { 587 const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 588 const std::vector<int> V10(10); 589 590 EXPECT_EQ(hasSingleElement(V0), false); 591 EXPECT_EQ(hasSingleElement(V1), true); 592 EXPECT_EQ(hasSingleElement(V2), false); 593 EXPECT_EQ(hasSingleElement(V10), false); 594 } 595 596 TEST(STLExtrasTest, hasNItems) { 597 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 598 const std::list<int> V3 = {1, 3, 5}; 599 600 EXPECT_TRUE(hasNItems(V0, 0)); 601 EXPECT_FALSE(hasNItems(V0, 2)); 602 EXPECT_TRUE(hasNItems(V1, 1)); 603 EXPECT_FALSE(hasNItems(V1, 2)); 604 605 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 606 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; })); 607 EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 608 } 609 610 TEST(STLExtras, hasNItemsOrMore) { 611 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 612 const std::list<int> V3 = {1, 3, 5}; 613 614 EXPECT_TRUE(hasNItemsOrMore(V1, 1)); 615 EXPECT_FALSE(hasNItemsOrMore(V1, 2)); 616 617 EXPECT_TRUE(hasNItemsOrMore(V2, 1)); 618 EXPECT_TRUE(hasNItemsOrMore(V2, 2)); 619 EXPECT_FALSE(hasNItemsOrMore(V2, 3)); 620 621 EXPECT_TRUE(hasNItemsOrMore(V3, 3)); 622 EXPECT_FALSE(hasNItemsOrMore(V3, 4)); 623 624 EXPECT_TRUE( 625 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; })); 626 EXPECT_FALSE( 627 hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; })); 628 EXPECT_TRUE( 629 hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 630 } 631 632 TEST(STLExtras, hasNItemsOrLess) { 633 const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2}; 634 const std::list<int> V3 = {1, 3, 5}; 635 636 EXPECT_TRUE(hasNItemsOrLess(V0, 0)); 637 EXPECT_TRUE(hasNItemsOrLess(V0, 1)); 638 EXPECT_TRUE(hasNItemsOrLess(V0, 2)); 639 640 EXPECT_FALSE(hasNItemsOrLess(V1, 0)); 641 EXPECT_TRUE(hasNItemsOrLess(V1, 1)); 642 EXPECT_TRUE(hasNItemsOrLess(V1, 2)); 643 644 EXPECT_FALSE(hasNItemsOrLess(V2, 0)); 645 EXPECT_FALSE(hasNItemsOrLess(V2, 1)); 646 EXPECT_TRUE(hasNItemsOrLess(V2, 2)); 647 EXPECT_TRUE(hasNItemsOrLess(V2, 3)); 648 649 EXPECT_FALSE(hasNItemsOrLess(V3, 0)); 650 EXPECT_FALSE(hasNItemsOrLess(V3, 1)); 651 EXPECT_FALSE(hasNItemsOrLess(V3, 2)); 652 EXPECT_TRUE(hasNItemsOrLess(V3, 3)); 653 EXPECT_TRUE(hasNItemsOrLess(V3, 4)); 654 655 EXPECT_TRUE( 656 hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; })); 657 EXPECT_TRUE( 658 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; })); 659 EXPECT_TRUE( 660 hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; })); 661 EXPECT_FALSE( 662 hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; })); 663 } 664 665 TEST(STLExtras, MoveRange) { 666 class Foo { 667 bool A; 668 669 public: 670 Foo() : A(true) {} 671 Foo(const Foo &) = delete; 672 Foo(Foo &&Other) : A(Other.A) { Other.A = false; } 673 Foo &operator=(const Foo &) = delete; 674 Foo &operator=(Foo &&Other) { 675 if (this != &Other) { 676 A = Other.A; 677 Other.A = false; 678 } 679 return *this; 680 } 681 operator bool() const { return A; } 682 }; 683 SmallVector<Foo, 4U> V1, V2, V3, V4; 684 auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); }; 685 auto Build = [&] { 686 SmallVector<Foo, 4U> Foos; 687 Foos.resize(4U); 688 return Foos; 689 }; 690 691 V1.resize(4U); 692 EXPECT_TRUE(llvm::all_of(V1, HasVal)); 693 694 llvm::move(V1, std::back_inserter(V2)); 695 696 // Ensure input container is same size, but its contents were moved out. 697 EXPECT_EQ(V1.size(), 4U); 698 EXPECT_TRUE(llvm::none_of(V1, HasVal)); 699 700 // Ensure output container has the contents of the input container. 701 EXPECT_EQ(V2.size(), 4U); 702 EXPECT_TRUE(llvm::all_of(V2, HasVal)); 703 704 llvm::move(std::move(V2), std::back_inserter(V3)); 705 706 EXPECT_TRUE(llvm::none_of(V2, HasVal)); 707 EXPECT_EQ(V3.size(), 4U); 708 EXPECT_TRUE(llvm::all_of(V3, HasVal)); 709 710 llvm::move(Build(), std::back_inserter(V4)); 711 EXPECT_EQ(V4.size(), 4U); 712 EXPECT_TRUE(llvm::all_of(V4, HasVal)); 713 } 714 } // namespace 715