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 
f(rank<0>)19 int f(rank<0>) { return 0; }
f(rank<1>)20 int f(rank<1>) { return 1; }
f(rank<2>)21 int f(rank<2>) { return 2; }
f(rank<4>)22 int f(rank<4>) { return 4; }
23 
TEST(STLExtrasTest,Rank)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 
TEST(STLExtrasTest,EnumerateLValue)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 
TEST(STLExtrasTest,EnumerateModifyLValue)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 
TEST(STLExtrasTest,EnumerateRValueRef)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 
TEST(STLExtrasTest,EnumerateModifyRValue)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:
Counted(int & C,int & M,int & D)149   explicit Counted(int &C, int &M, int &D) : C(C), M(M), D(D) {}
Counted(const Counted & O)150   Counted(const Counted &O) : CanCopy<Copyable>(O), C(O.C), M(O.M), D(O.D) {
151     ++C;
152   }
Counted(Counted && O)153   Counted(Counted &&O)
154       : CanMove<Moveable>(std::move(O)), C(O.C), M(O.M), D(O.D) {
155     ++M;
156   }
~Counted()157   ~Counted() { ++D; }
158 };
159 
160 template <bool Moveable, bool Copyable>
161 struct Range : Counted<Moveable, Copyable> {
162   using Counted<Moveable, Copyable>::Counted;
begin__anon21ce8bec0111::Range163   int *begin() { return nullptr; }
end__anon21ce8bec0111::Range164   int *end() { return nullptr; }
165 };
166 
TEST(STLExtrasTest,EnumerateLifetimeSemanticsPRValue)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 
TEST(STLExtrasTest,EnumerateLifetimeSemanticsRValue)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 
TEST(STLExtrasTest,EnumerateLifetimeSemanticsLValue)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 
TEST(STLExtrasTest,ApplyTuple)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 {
apply_one(int X)258   static int apply_one(int X) { return X + 1; }
apply_one(char C)259   static char apply_one(char C) { return C + 1; }
apply_one(StringRef S)260   static StringRef apply_one(StringRef S) { return S.drop_back(); }
261 
262 public:
operator ()(Ts &&...Items)263   template <typename... Ts> auto operator()(Ts &&... Items) {
264     return std::make_tuple(apply_one(Items)...);
265   }
266 };
267 
TEST(STLExtrasTest,ApplyTupleVariadic)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 
TEST(STLExtrasTest,CountAdaptor)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 
TEST(STLExtrasTest,for_each)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 
TEST(STLExtrasTest,ToVector)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 
TEST(STLExtrasTest,ConcatRange)312 TEST(STLExtrasTest, ConcatRange) {
313   std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8};
314   std::vector<int> Test;
315 
316   std::vector<int> V1234 = {1, 2, 3, 4};
317   std::list<int> L56 = {5, 6};
318   SmallVector<int, 2> SV78 = {7, 8};
319 
320   // Use concat across different sized ranges of different types with different
321   // iterators.
322   for (int &i : concat<int>(V1234, L56, SV78))
323     Test.push_back(i);
324   EXPECT_EQ(Expected, Test);
325 
326   // Use concat between a temporary, an L-value, and an R-value to make sure
327   // complex lifetimes work well.
328   Test.clear();
329   for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78)))
330     Test.push_back(i);
331   EXPECT_EQ(Expected, Test);
332 }
333 
TEST(STLExtrasTest,PartitionAdaptor)334 TEST(STLExtrasTest, PartitionAdaptor) {
335   std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
336 
337   auto I = partition(V, [](int i) { return i % 2 == 0; });
338   ASSERT_EQ(V.begin() + 4, I);
339 
340   // Sort the two halves as partition may have messed with the order.
341   llvm::sort(V.begin(), I);
342   llvm::sort(I, V.end());
343 
344   EXPECT_EQ(2, V[0]);
345   EXPECT_EQ(4, V[1]);
346   EXPECT_EQ(6, V[2]);
347   EXPECT_EQ(8, V[3]);
348   EXPECT_EQ(1, V[4]);
349   EXPECT_EQ(3, V[5]);
350   EXPECT_EQ(5, V[6]);
351   EXPECT_EQ(7, V[7]);
352 }
353 
TEST(STLExtrasTest,EraseIf)354 TEST(STLExtrasTest, EraseIf) {
355   std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
356 
357   erase_if(V, [](int i) { return i % 2 == 0; });
358   EXPECT_EQ(4u, V.size());
359   EXPECT_EQ(1, V[0]);
360   EXPECT_EQ(3, V[1]);
361   EXPECT_EQ(5, V[2]);
362   EXPECT_EQ(7, V[3]);
363 }
364 
TEST(STLExtrasTest,AppendRange)365 TEST(STLExtrasTest, AppendRange) {
366   auto AppendVals = {3};
367   std::vector<int> V = {1, 2};
368   append_range(V, AppendVals);
369   EXPECT_EQ(1, V[0]);
370   EXPECT_EQ(2, V[1]);
371   EXPECT_EQ(3, V[2]);
372 }
373 
374 namespace some_namespace {
375 struct some_struct {
376   std::vector<int> data;
377   std::string swap_val;
378 };
379 
begin(const some_struct & s)380 std::vector<int>::const_iterator begin(const some_struct &s) {
381   return s.data.begin();
382 }
383 
end(const some_struct & s)384 std::vector<int>::const_iterator end(const some_struct &s) {
385   return s.data.end();
386 }
387 
swap(some_struct & lhs,some_struct & rhs)388 void swap(some_struct &lhs, some_struct &rhs) {
389   // make swap visible as non-adl swap would even seem to
390   // work with std::swap which defaults to moving
391   lhs.swap_val = "lhs";
392   rhs.swap_val = "rhs";
393 }
394 } // namespace some_namespace
395 
TEST(STLExtrasTest,ADLTest)396 TEST(STLExtrasTest, ADLTest) {
397   some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""};
398   some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""};
399 
400   EXPECT_EQ(*adl_begin(s), 1);
401   EXPECT_EQ(*(adl_end(s) - 1), 5);
402 
403   adl_swap(s, s2);
404   EXPECT_EQ(s.swap_val, "lhs");
405   EXPECT_EQ(s2.swap_val, "rhs");
406 
407   int count = 0;
408   llvm::for_each(s, [&count](int) { ++count; });
409   EXPECT_EQ(5, count);
410 }
411 
TEST(STLExtrasTest,EmptyTest)412 TEST(STLExtrasTest, EmptyTest) {
413   std::vector<void*> V;
414   EXPECT_TRUE(llvm::empty(V));
415   V.push_back(nullptr);
416   EXPECT_FALSE(llvm::empty(V));
417 
418   std::initializer_list<int> E = {};
419   std::initializer_list<int> NotE = {7, 13, 42};
420   EXPECT_TRUE(llvm::empty(E));
421   EXPECT_FALSE(llvm::empty(NotE));
422 
423   auto R0 = make_range(V.begin(), V.begin());
424   EXPECT_TRUE(llvm::empty(R0));
425   auto R1 = make_range(V.begin(), V.end());
426   EXPECT_FALSE(llvm::empty(R1));
427 }
428 
TEST(STLExtrasTest,DropBeginTest)429 TEST(STLExtrasTest, DropBeginTest) {
430   SmallVector<int, 5> vec{0, 1, 2, 3, 4};
431 
432   for (int n = 0; n < 5; ++n) {
433     int i = n;
434     for (auto &v : drop_begin(vec, n)) {
435       EXPECT_EQ(v, i);
436       i += 1;
437     }
438     EXPECT_EQ(i, 5);
439   }
440 }
441 
TEST(STLExtrasTest,DropBeginDefaultTest)442 TEST(STLExtrasTest, DropBeginDefaultTest) {
443   SmallVector<int, 5> vec{0, 1, 2, 3, 4};
444 
445   int i = 1;
446   for (auto &v : drop_begin(vec)) {
447     EXPECT_EQ(v, i);
448     i += 1;
449   }
450   EXPECT_EQ(i, 5);
451 }
452 
TEST(STLExtrasTest,EarlyIncrementTest)453 TEST(STLExtrasTest, EarlyIncrementTest) {
454   std::list<int> L = {1, 2, 3, 4};
455 
456   auto EIR = make_early_inc_range(L);
457 
458   auto I = EIR.begin();
459   auto EI = EIR.end();
460   EXPECT_NE(I, EI);
461 
462   EXPECT_EQ(1, *I);
463 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
464 #ifndef NDEBUG
465   // Repeated dereferences are not allowed.
466   EXPECT_DEATH(*I, "Cannot dereference");
467   // Comparison after dereference is not allowed.
468   EXPECT_DEATH((void)(I == EI), "Cannot compare");
469   EXPECT_DEATH((void)(I != EI), "Cannot compare");
470 #endif
471 #endif
472 
473   ++I;
474   EXPECT_NE(I, EI);
475 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
476 #ifndef NDEBUG
477   // You cannot increment prior to dereference.
478   EXPECT_DEATH(++I, "Cannot increment");
479 #endif
480 #endif
481   EXPECT_EQ(2, *I);
482 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
483 #ifndef NDEBUG
484   // Repeated dereferences are not allowed.
485   EXPECT_DEATH(*I, "Cannot dereference");
486 #endif
487 #endif
488 
489   // Inserting shouldn't break anything. We should be able to keep dereferencing
490   // the currrent iterator and increment. The increment to go to the "next"
491   // iterator from before we inserted.
492   L.insert(std::next(L.begin(), 2), -1);
493   ++I;
494   EXPECT_EQ(3, *I);
495 
496   // Erasing the front including the current doesn't break incrementing.
497   L.erase(L.begin(), std::prev(L.end()));
498   ++I;
499   EXPECT_EQ(4, *I);
500   ++I;
501   EXPECT_EQ(EIR.end(), I);
502 }
503 
504 // A custom iterator that returns a pointer when dereferenced. This is used to
505 // test make_early_inc_range with iterators that do not return a reference on
506 // dereferencing.
507 struct CustomPointerIterator
508     : public iterator_adaptor_base<CustomPointerIterator,
509                                    std::list<int>::iterator,
510                                    std::forward_iterator_tag> {
511   using base_type =
512       iterator_adaptor_base<CustomPointerIterator, std::list<int>::iterator,
513                             std::forward_iterator_tag>;
514 
CustomPointerIterator__anon21ce8bec0111::CustomPointerIterator515   explicit CustomPointerIterator(std::list<int>::iterator I) : base_type(I) {}
516 
517   // Retrieve a pointer to the current int.
operator *__anon21ce8bec0111::CustomPointerIterator518   int *operator*() const { return &*base_type::wrapped(); }
519 };
520 
521 // Make sure make_early_inc_range works with iterators that do not return a
522 // reference on dereferencing. The test is similar to EarlyIncrementTest, but
523 // uses CustomPointerIterator.
TEST(STLExtrasTest,EarlyIncrementTestCustomPointerIterator)524 TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) {
525   std::list<int> L = {1, 2, 3, 4};
526 
527   auto CustomRange = make_range(CustomPointerIterator(L.begin()),
528                                 CustomPointerIterator(L.end()));
529   auto EIR = make_early_inc_range(CustomRange);
530 
531   auto I = EIR.begin();
532   auto EI = EIR.end();
533   EXPECT_NE(I, EI);
534 
535   EXPECT_EQ(&*L.begin(), *I);
536 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
537 #ifndef NDEBUG
538   // Repeated dereferences are not allowed.
539   EXPECT_DEATH(*I, "Cannot dereference");
540   // Comparison after dereference is not allowed.
541   EXPECT_DEATH((void)(I == EI), "Cannot compare");
542   EXPECT_DEATH((void)(I != EI), "Cannot compare");
543 #endif
544 #endif
545 
546   ++I;
547   EXPECT_NE(I, EI);
548 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
549 #ifndef NDEBUG
550   // You cannot increment prior to dereference.
551   EXPECT_DEATH(++I, "Cannot increment");
552 #endif
553 #endif
554   EXPECT_EQ(&*std::next(L.begin()), *I);
555 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
556 #ifndef NDEBUG
557   // Repeated dereferences are not allowed.
558   EXPECT_DEATH(*I, "Cannot dereference");
559 #endif
560 #endif
561 
562   // Inserting shouldn't break anything. We should be able to keep dereferencing
563   // the currrent iterator and increment. The increment to go to the "next"
564   // iterator from before we inserted.
565   L.insert(std::next(L.begin(), 2), -1);
566   ++I;
567   EXPECT_EQ(&*std::next(L.begin(), 3), *I);
568 
569   // Erasing the front including the current doesn't break incrementing.
570   L.erase(L.begin(), std::prev(L.end()));
571   ++I;
572   EXPECT_EQ(&*L.begin(), *I);
573   ++I;
574   EXPECT_EQ(EIR.end(), I);
575 }
576 
TEST(STLExtrasTest,splat)577 TEST(STLExtrasTest, splat) {
578   std::vector<int> V;
579   EXPECT_FALSE(is_splat(V));
580 
581   V.push_back(1);
582   EXPECT_TRUE(is_splat(V));
583 
584   V.push_back(1);
585   V.push_back(1);
586   EXPECT_TRUE(is_splat(V));
587 
588   V.push_back(2);
589   EXPECT_FALSE(is_splat(V));
590 }
591 
TEST(STLExtrasTest,to_address)592 TEST(STLExtrasTest, to_address) {
593   int *V1 = new int;
594   EXPECT_EQ(V1, to_address(V1));
595 
596   // Check fancy pointer overload for unique_ptr
597   std::unique_ptr<int> V2 = std::make_unique<int>(0);
598   EXPECT_EQ(V2.get(), llvm::to_address(V2));
599 
600   V2.reset(V1);
601   EXPECT_EQ(V1, llvm::to_address(V2));
602   V2.release();
603 
604   // Check fancy pointer overload for shared_ptr
605   std::shared_ptr<int> V3 = std::make_shared<int>(0);
606   std::shared_ptr<int> V4 = V3;
607   EXPECT_EQ(V3.get(), V4.get());
608   EXPECT_EQ(V3.get(), llvm::to_address(V3));
609   EXPECT_EQ(V4.get(), llvm::to_address(V4));
610 
611   V3.reset(V1);
612   EXPECT_EQ(V1, llvm::to_address(V3));
613 }
614 
TEST(STLExtrasTest,partition_point)615 TEST(STLExtrasTest, partition_point) {
616   std::vector<int> V = {1, 3, 5, 7, 9};
617 
618   // Range version.
619   EXPECT_EQ(V.begin() + 3,
620             partition_point(V, [](unsigned X) { return X < 7; }));
621   EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; }));
622   EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; }));
623 }
624 
TEST(STLExtrasTest,hasSingleElement)625 TEST(STLExtrasTest, hasSingleElement) {
626   const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2};
627   const std::vector<int> V10(10);
628 
629   EXPECT_EQ(hasSingleElement(V0), false);
630   EXPECT_EQ(hasSingleElement(V1), true);
631   EXPECT_EQ(hasSingleElement(V2), false);
632   EXPECT_EQ(hasSingleElement(V10), false);
633 }
634 
TEST(STLExtrasTest,hasNItems)635 TEST(STLExtrasTest, hasNItems) {
636   const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2};
637   const std::list<int> V3 = {1, 3, 5};
638 
639   EXPECT_TRUE(hasNItems(V0, 0));
640   EXPECT_FALSE(hasNItems(V0, 2));
641   EXPECT_TRUE(hasNItems(V1, 1));
642   EXPECT_FALSE(hasNItems(V1, 2));
643 
644   EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 3, [](int x) { return x < 10; }));
645   EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 0, [](int x) { return x > 10; }));
646   EXPECT_TRUE(hasNItems(V3.begin(), V3.end(), 2, [](int x) { return x < 5; }));
647 }
648 
TEST(STLExtras,hasNItemsOrMore)649 TEST(STLExtras, hasNItemsOrMore) {
650   const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2};
651   const std::list<int> V3 = {1, 3, 5};
652 
653   EXPECT_TRUE(hasNItemsOrMore(V1, 1));
654   EXPECT_FALSE(hasNItemsOrMore(V1, 2));
655 
656   EXPECT_TRUE(hasNItemsOrMore(V2, 1));
657   EXPECT_TRUE(hasNItemsOrMore(V2, 2));
658   EXPECT_FALSE(hasNItemsOrMore(V2, 3));
659 
660   EXPECT_TRUE(hasNItemsOrMore(V3, 3));
661   EXPECT_FALSE(hasNItemsOrMore(V3, 4));
662 
663   EXPECT_TRUE(
664       hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x < 10; }));
665   EXPECT_FALSE(
666       hasNItemsOrMore(V3.begin(), V3.end(), 3, [](int x) { return x > 10; }));
667   EXPECT_TRUE(
668       hasNItemsOrMore(V3.begin(), V3.end(), 2, [](int x) { return x < 5; }));
669 }
670 
TEST(STLExtras,hasNItemsOrLess)671 TEST(STLExtras, hasNItemsOrLess) {
672   const std::list<int> V0 = {}, V1 = {1}, V2 = {1, 2};
673   const std::list<int> V3 = {1, 3, 5};
674 
675   EXPECT_TRUE(hasNItemsOrLess(V0, 0));
676   EXPECT_TRUE(hasNItemsOrLess(V0, 1));
677   EXPECT_TRUE(hasNItemsOrLess(V0, 2));
678 
679   EXPECT_FALSE(hasNItemsOrLess(V1, 0));
680   EXPECT_TRUE(hasNItemsOrLess(V1, 1));
681   EXPECT_TRUE(hasNItemsOrLess(V1, 2));
682 
683   EXPECT_FALSE(hasNItemsOrLess(V2, 0));
684   EXPECT_FALSE(hasNItemsOrLess(V2, 1));
685   EXPECT_TRUE(hasNItemsOrLess(V2, 2));
686   EXPECT_TRUE(hasNItemsOrLess(V2, 3));
687 
688   EXPECT_FALSE(hasNItemsOrLess(V3, 0));
689   EXPECT_FALSE(hasNItemsOrLess(V3, 1));
690   EXPECT_FALSE(hasNItemsOrLess(V3, 2));
691   EXPECT_TRUE(hasNItemsOrLess(V3, 3));
692   EXPECT_TRUE(hasNItemsOrLess(V3, 4));
693 
694   EXPECT_TRUE(
695       hasNItemsOrLess(V3.begin(), V3.end(), 1, [](int x) { return x == 1; }));
696   EXPECT_TRUE(
697       hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 5; }));
698   EXPECT_TRUE(
699       hasNItemsOrLess(V3.begin(), V3.end(), 5, [](int x) { return x < 5; }));
700   EXPECT_FALSE(
701       hasNItemsOrLess(V3.begin(), V3.end(), 2, [](int x) { return x < 10; }));
702 }
703 
TEST(STLExtras,MoveRange)704 TEST(STLExtras, MoveRange) {
705   class Foo {
706     bool A;
707 
708   public:
709     Foo() : A(true) {}
710     Foo(const Foo &) = delete;
711     Foo(Foo &&Other) : A(Other.A) { Other.A = false; }
712     Foo &operator=(const Foo &) = delete;
713     Foo &operator=(Foo &&Other) {
714       if (this != &Other) {
715         A = Other.A;
716         Other.A = false;
717       }
718       return *this;
719     }
720     operator bool() const { return A; }
721   };
722   SmallVector<Foo, 4U> V1, V2, V3, V4;
723   auto HasVal = [](const Foo &Item) { return static_cast<bool>(Item); };
724   auto Build = [&] {
725     SmallVector<Foo, 4U> Foos;
726     Foos.resize(4U);
727     return Foos;
728   };
729 
730   V1.resize(4U);
731   EXPECT_TRUE(llvm::all_of(V1, HasVal));
732 
733   llvm::move(V1, std::back_inserter(V2));
734 
735   // Ensure input container is same size, but its contents were moved out.
736   EXPECT_EQ(V1.size(), 4U);
737   EXPECT_TRUE(llvm::none_of(V1, HasVal));
738 
739   // Ensure output container has the contents of the input container.
740   EXPECT_EQ(V2.size(), 4U);
741   EXPECT_TRUE(llvm::all_of(V2, HasVal));
742 
743   llvm::move(std::move(V2), std::back_inserter(V3));
744 
745   EXPECT_TRUE(llvm::none_of(V2, HasVal));
746   EXPECT_EQ(V3.size(), 4U);
747   EXPECT_TRUE(llvm::all_of(V3, HasVal));
748 
749   llvm::move(Build(), std::back_inserter(V4));
750   EXPECT_EQ(V4.size(), 4U);
751   EXPECT_TRUE(llvm::all_of(V4, HasVal));
752 }
753 
TEST(STLExtras,Unique)754 TEST(STLExtras, Unique) {
755   std::vector<int> V = {1, 5, 5, 4, 3, 3, 3};
756 
757   auto I = llvm::unique(V, [](int a, int b) { return a == b; });
758 
759   EXPECT_EQ(I, V.begin() + 4);
760 
761   EXPECT_EQ(1, V[0]);
762   EXPECT_EQ(5, V[1]);
763   EXPECT_EQ(4, V[2]);
764   EXPECT_EQ(3, V[3]);
765 }
766 
TEST(STLExtrasTest,MakeVisitorOneCallable)767 TEST(STLExtrasTest, MakeVisitorOneCallable) {
768   auto IdentityLambda = [](auto X) { return X; };
769   auto IdentityVisitor = makeVisitor(IdentityLambda);
770   EXPECT_EQ(IdentityLambda(1), IdentityVisitor(1));
771   EXPECT_EQ(IdentityLambda(2.0f), IdentityVisitor(2.0f));
772   EXPECT_TRUE((std::is_same<decltype(IdentityLambda(IdentityLambda)),
773                             decltype(IdentityLambda)>::value));
774   EXPECT_TRUE((std::is_same<decltype(IdentityVisitor(IdentityVisitor)),
775                             decltype(IdentityVisitor)>::value));
776 }
777 
TEST(STLExtrasTest,MakeVisitorTwoCallables)778 TEST(STLExtrasTest, MakeVisitorTwoCallables) {
779   auto Visitor =
780       makeVisitor([](int) { return 0; }, [](std::string) { return 1; });
781   EXPECT_EQ(Visitor(42), 0);
782   EXPECT_EQ(Visitor("foo"), 1);
783 }
784 
TEST(STLExtrasTest,MakeVisitorCallableMultipleOperands)785 TEST(STLExtrasTest, MakeVisitorCallableMultipleOperands) {
786   auto Second = makeVisitor([](int I, float F) { return F; },
787                             [](float F, int I) { return I; });
788   EXPECT_EQ(Second(1.f, 1), 1);
789   EXPECT_EQ(Second(1, 1.f), 1.f);
790 }
791 
TEST(STLExtrasTest,MakeVisitorDefaultCase)792 TEST(STLExtrasTest, MakeVisitorDefaultCase) {
793   {
794     auto Visitor = makeVisitor([](int I) { return I + 100; },
795                                [](float F) { return F * 2; },
796                                [](auto) { return -1; });
797     EXPECT_EQ(Visitor(24), 124);
798     EXPECT_EQ(Visitor(2.f), 4.f);
799     EXPECT_EQ(Visitor(2.), -1);
800     EXPECT_EQ(Visitor(Visitor), -1);
801   }
802   {
803     auto Visitor = makeVisitor([](auto) { return -1; },
804                                [](int I) { return I + 100; },
805                                [](float F) { return F * 2; });
806     EXPECT_EQ(Visitor(24), 124);
807     EXPECT_EQ(Visitor(2.f), 4.f);
808     EXPECT_EQ(Visitor(2.), -1);
809     EXPECT_EQ(Visitor(Visitor), -1);
810   }
811 }
812 
813 template <bool Moveable, bool Copyable>
814 struct Functor : Counted<Moveable, Copyable> {
815   using Counted<Moveable, Copyable>::Counted;
operator ()__anon21ce8bec0111::Functor816   void operator()() {}
817 };
818 
TEST(STLExtrasTest,MakeVisitorLifetimeSemanticsPRValue)819 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsPRValue) {
820   int Copies = 0;
821   int Moves = 0;
822   int Destructors = 0;
823   {
824     auto V = makeVisitor(Functor<true, false>(Copies, Moves, Destructors));
825     (void)V;
826     EXPECT_EQ(0, Copies);
827     EXPECT_EQ(1, Moves);
828     EXPECT_EQ(1, Destructors);
829   }
830   EXPECT_EQ(0, Copies);
831   EXPECT_EQ(1, Moves);
832   EXPECT_EQ(2, Destructors);
833 }
834 
TEST(STLExtrasTest,MakeVisitorLifetimeSemanticsRValue)835 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsRValue) {
836   int Copies = 0;
837   int Moves = 0;
838   int Destructors = 0;
839   {
840     Functor<true, false> F(Copies, Moves, Destructors);
841     {
842       auto V = makeVisitor(std::move(F));
843       (void)V;
844       EXPECT_EQ(0, Copies);
845       EXPECT_EQ(1, Moves);
846       EXPECT_EQ(0, Destructors);
847     }
848     EXPECT_EQ(0, Copies);
849     EXPECT_EQ(1, Moves);
850     EXPECT_EQ(1, Destructors);
851   }
852   EXPECT_EQ(0, Copies);
853   EXPECT_EQ(1, Moves);
854   EXPECT_EQ(2, Destructors);
855 }
856 
TEST(STLExtrasTest,MakeVisitorLifetimeSemanticsLValue)857 TEST(STLExtrasTest, MakeVisitorLifetimeSemanticsLValue) {
858   int Copies = 0;
859   int Moves = 0;
860   int Destructors = 0;
861   {
862     Functor<true, true> F(Copies, Moves, Destructors);
863     {
864       auto V = makeVisitor(F);
865       (void)V;
866       EXPECT_EQ(1, Copies);
867       EXPECT_EQ(0, Moves);
868       EXPECT_EQ(0, Destructors);
869     }
870     EXPECT_EQ(1, Copies);
871     EXPECT_EQ(0, Moves);
872     EXPECT_EQ(1, Destructors);
873   }
874   EXPECT_EQ(1, Copies);
875   EXPECT_EQ(0, Moves);
876   EXPECT_EQ(2, Destructors);
877 }
878 
879 } // namespace
880