blob: e65e71fe4855dc83f509dbea2d7d4f17e60d2bb5 [file] [log] [blame]
Chandler Carruthd3197162016-08-19 02:07:51 +00001//===- STLExtrasTest.cpp - Unit tests for STL extras ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/STLExtras.h"
11#include "gtest/gtest.h"
12
Chandler Carruth1e976302016-12-25 23:41:14 +000013#include <list>
Zachary Turnerf216a862016-09-30 15:43:59 +000014#include <vector>
15
Chandler Carruthd3197162016-08-19 02:07:51 +000016using namespace llvm;
17
18namespace {
19
20int f(rank<0>) { return 0; }
21int f(rank<1>) { return 1; }
22int f(rank<2>) { return 2; }
23int f(rank<4>) { return 4; }
24
25TEST(STLExtrasTest, Rank) {
26 // We shouldn't get ambiguities and should select the overload of the same
27 // rank as the argument.
28 EXPECT_EQ(0, f(rank<0>()));
29 EXPECT_EQ(1, f(rank<1>()));
30 EXPECT_EQ(2, f(rank<2>()));
31
32 // This overload is missing so we end up back at 2.
33 EXPECT_EQ(2, f(rank<3>()));
34
35 // But going past 3 should work fine.
36 EXPECT_EQ(4, f(rank<4>()));
37
38 // And we can even go higher and just fall back to the last overload.
39 EXPECT_EQ(4, f(rank<5>()));
40 EXPECT_EQ(4, f(rank<6>()));
41}
42
Zachary Turner5844b062016-10-05 16:54:09 +000043TEST(STLExtrasTest, EnumerateLValue) {
44 // Test that a simple LValue can be enumerated and gives correct results with
45 // multiple types, including the empty container.
Zachary Turnerf216a862016-09-30 15:43:59 +000046 std::vector<char> foo = {'a', 'b', 'c'};
Zachary Turnere53904e2016-10-05 17:04:36 +000047 typedef std::pair<std::size_t, char> CharPairType;
48 std::vector<CharPairType> CharResults;
Zachary Turnerf216a862016-09-30 15:43:59 +000049
50 for (auto X : llvm::enumerate(foo)) {
Zachary Turner39e53ee2017-03-13 16:24:10 +000051 CharResults.emplace_back(X.index(), X.value());
Zachary Turnerf216a862016-09-30 15:43:59 +000052 }
Zachary Turner5844b062016-10-05 16:54:09 +000053 ASSERT_EQ(3u, CharResults.size());
Zachary Turnere53904e2016-10-05 17:04:36 +000054 EXPECT_EQ(CharPairType(0u, 'a'), CharResults[0]);
55 EXPECT_EQ(CharPairType(1u, 'b'), CharResults[1]);
56 EXPECT_EQ(CharPairType(2u, 'c'), CharResults[2]);
Zachary Turnerf216a862016-09-30 15:43:59 +000057
Zachary Turner5844b062016-10-05 16:54:09 +000058 // Test a const range of a different type.
Zachary Turnere53904e2016-10-05 17:04:36 +000059 typedef std::pair<std::size_t, int> IntPairType;
60 std::vector<IntPairType> IntResults;
Zachary Turner5844b062016-10-05 16:54:09 +000061 const std::vector<int> bar = {1, 2, 3};
Zachary Turnerf216a862016-09-30 15:43:59 +000062 for (auto X : llvm::enumerate(bar)) {
Zachary Turner39e53ee2017-03-13 16:24:10 +000063 IntResults.emplace_back(X.index(), X.value());
Zachary Turnerf216a862016-09-30 15:43:59 +000064 }
Zachary Turner5844b062016-10-05 16:54:09 +000065 ASSERT_EQ(3u, IntResults.size());
Zachary Turnere53904e2016-10-05 17:04:36 +000066 EXPECT_EQ(IntPairType(0u, 1), IntResults[0]);
67 EXPECT_EQ(IntPairType(1u, 2), IntResults[1]);
68 EXPECT_EQ(IntPairType(2u, 3), IntResults[2]);
Zachary Turnerf216a862016-09-30 15:43:59 +000069
Zachary Turner5844b062016-10-05 16:54:09 +000070 // Test an empty range.
71 IntResults.clear();
Davide Italiano96acf922017-03-09 23:48:58 +000072 const std::vector<int> baz{};
Zachary Turnerf216a862016-09-30 15:43:59 +000073 for (auto X : llvm::enumerate(baz)) {
Zachary Turner39e53ee2017-03-13 16:24:10 +000074 IntResults.emplace_back(X.index(), X.value());
Zachary Turnerf216a862016-09-30 15:43:59 +000075 }
Zachary Turner5844b062016-10-05 16:54:09 +000076 EXPECT_TRUE(IntResults.empty());
Zachary Turnerf216a862016-09-30 15:43:59 +000077}
78
Zachary Turner5844b062016-10-05 16:54:09 +000079TEST(STLExtrasTest, EnumerateModifyLValue) {
80 // Test that you can modify the underlying entries of an lvalue range through
81 // the enumeration iterator.
Zachary Turnerf216a862016-09-30 15:43:59 +000082 std::vector<char> foo = {'a', 'b', 'c'};
83
84 for (auto X : llvm::enumerate(foo)) {
Zachary Turner39e53ee2017-03-13 16:24:10 +000085 ++X.value();
Zachary Turnerf216a862016-09-30 15:43:59 +000086 }
Zachary Turnerf216a862016-09-30 15:43:59 +000087 EXPECT_EQ('b', foo[0]);
88 EXPECT_EQ('c', foo[1]);
89 EXPECT_EQ('d', foo[2]);
90}
Zachary Turner5844b062016-10-05 16:54:09 +000091
92TEST(STLExtrasTest, EnumerateRValueRef) {
93 // Test that an rvalue can be enumerated.
Zachary Turnere53904e2016-10-05 17:04:36 +000094 typedef std::pair<std::size_t, int> PairType;
95 std::vector<PairType> Results;
Zachary Turner5844b062016-10-05 16:54:09 +000096
97 auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3});
98
99 for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) {
Zachary Turner39e53ee2017-03-13 16:24:10 +0000100 Results.emplace_back(X.index(), X.value());
Zachary Turner5844b062016-10-05 16:54:09 +0000101 }
102
103 ASSERT_EQ(3u, Results.size());
Zachary Turnere53904e2016-10-05 17:04:36 +0000104 EXPECT_EQ(PairType(0u, 1), Results[0]);
105 EXPECT_EQ(PairType(1u, 2), Results[1]);
106 EXPECT_EQ(PairType(2u, 3), Results[2]);
Zachary Turner5844b062016-10-05 16:54:09 +0000107}
108
109TEST(STLExtrasTest, EnumerateModifyRValue) {
110 // Test that when enumerating an rvalue, modification still works (even if
111 // this isn't terribly useful, it at least shows that we haven't snuck an
112 // extra const in there somewhere.
Zachary Turnere53904e2016-10-05 17:04:36 +0000113 typedef std::pair<std::size_t, char> PairType;
114 std::vector<PairType> Results;
Zachary Turner5844b062016-10-05 16:54:09 +0000115
116 for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) {
Zachary Turner39e53ee2017-03-13 16:24:10 +0000117 ++X.value();
118 Results.emplace_back(X.index(), X.value());
Zachary Turner5844b062016-10-05 16:54:09 +0000119 }
120
121 ASSERT_EQ(3u, Results.size());
Zachary Turnere53904e2016-10-05 17:04:36 +0000122 EXPECT_EQ(PairType(0u, '2'), Results[0]);
123 EXPECT_EQ(PairType(1u, '3'), Results[1]);
124 EXPECT_EQ(PairType(2u, '4'), Results[2]);
Zachary Turner5844b062016-10-05 16:54:09 +0000125}
126
127template <bool B> struct CanMove {};
128template <> struct CanMove<false> {
129 CanMove(CanMove &&) = delete;
130
131 CanMove() = default;
132 CanMove(const CanMove &) = default;
133};
134
135template <bool B> struct CanCopy {};
136template <> struct CanCopy<false> {
137 CanCopy(const CanCopy &) = delete;
138
139 CanCopy() = default;
Vedant Kumar59b22132016-10-25 18:11:17 +0000140 CanCopy(CanCopy &&) = default;
Zachary Turner5844b062016-10-05 16:54:09 +0000141};
142
143template <bool Moveable, bool Copyable>
144struct Range : CanMove<Moveable>, CanCopy<Copyable> {
145 explicit Range(int &C, int &M, int &D) : C(C), M(M), D(D) {}
146 Range(const Range &R) : CanCopy<Copyable>(R), C(R.C), M(R.M), D(R.D) { ++C; }
147 Range(Range &&R) : CanMove<Moveable>(std::move(R)), C(R.C), M(R.M), D(R.D) {
148 ++M;
149 }
150 ~Range() { ++D; }
151
152 int &C;
153 int &M;
154 int &D;
155
156 int *begin() { return nullptr; }
157 int *end() { return nullptr; }
158};
159
160TEST(STLExtrasTest, EnumerateLifetimeSemantics) {
161 // Test that when enumerating lvalues and rvalues, there are no surprise
162 // copies or moves.
163
164 // With an rvalue, it should not be destroyed until the end of the scope.
165 int Copies = 0;
166 int Moves = 0;
167 int Destructors = 0;
168 {
169 auto E1 = enumerate(Range<true, false>(Copies, Moves, Destructors));
170 // Doesn't compile. rvalue ranges must be moveable.
171 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors));
172 EXPECT_EQ(0, Copies);
173 EXPECT_EQ(1, Moves);
174 EXPECT_EQ(1, Destructors);
175 }
176 EXPECT_EQ(0, Copies);
177 EXPECT_EQ(1, Moves);
178 EXPECT_EQ(2, Destructors);
179
180 Copies = Moves = Destructors = 0;
181 // With an lvalue, it should not be destroyed even after the end of the scope.
182 // lvalue ranges need be neither copyable nor moveable.
183 Range<false, false> R(Copies, Moves, Destructors);
184 {
185 auto Enumerator = enumerate(R);
186 (void)Enumerator;
187 EXPECT_EQ(0, Copies);
188 EXPECT_EQ(0, Moves);
189 EXPECT_EQ(0, Destructors);
190 }
191 EXPECT_EQ(0, Copies);
192 EXPECT_EQ(0, Moves);
193 EXPECT_EQ(0, Destructors);
194}
Zachary Turnerbd90bcc2016-10-10 16:44:09 +0000195
196TEST(STLExtrasTest, ApplyTuple) {
197 auto T = std::make_tuple(1, 3, 7);
Zachary Turner1793ab42016-10-10 21:24:34 +0000198 auto U = llvm::apply_tuple(
Zachary Turnerbd90bcc2016-10-10 16:44:09 +0000199 [](int A, int B, int C) { return std::make_tuple(A - B, B - C, C - A); },
200 T);
201
202 EXPECT_EQ(-2, std::get<0>(U));
203 EXPECT_EQ(-4, std::get<1>(U));
204 EXPECT_EQ(6, std::get<2>(U));
205
Zachary Turner1793ab42016-10-10 21:24:34 +0000206 auto V = llvm::apply_tuple(
Zachary Turnerbd90bcc2016-10-10 16:44:09 +0000207 [](int A, int B, int C) {
208 return std::make_tuple(std::make_pair(A, char('A' + A)),
209 std::make_pair(B, char('A' + B)),
210 std::make_pair(C, char('A' + C)));
211 },
212 T);
213
214 EXPECT_EQ(std::make_pair(1, 'B'), std::get<0>(V));
215 EXPECT_EQ(std::make_pair(3, 'D'), std::get<1>(V));
216 EXPECT_EQ(std::make_pair(7, 'H'), std::get<2>(V));
217}
218
219class apply_variadic {
220 static int apply_one(int X) { return X + 1; }
221 static char apply_one(char C) { return C + 1; }
222 static StringRef apply_one(StringRef S) { return S.drop_back(); }
223
224public:
225 template <typename... Ts>
226 auto operator()(Ts &&... Items)
227 -> decltype(std::make_tuple(apply_one(Items)...)) {
228 return std::make_tuple(apply_one(Items)...);
229 }
230};
231
232TEST(STLExtrasTest, ApplyTupleVariadic) {
233 auto Items = std::make_tuple(1, llvm::StringRef("Test"), 'X');
Zachary Turner1793ab42016-10-10 21:24:34 +0000234 auto Values = apply_tuple(apply_variadic(), Items);
Zachary Turnerbd90bcc2016-10-10 16:44:09 +0000235
236 EXPECT_EQ(2, std::get<0>(Values));
237 EXPECT_EQ("Tes", std::get<1>(Values));
238 EXPECT_EQ('Y', std::get<2>(Values));
239}
Michael Gottesman6a2af3e2016-12-04 10:26:53 +0000240
241TEST(STLExtrasTest, CountAdaptor) {
242 std::vector<int> v;
243
244 v.push_back(1);
245 v.push_back(2);
246 v.push_back(1);
247 v.push_back(4);
248 v.push_back(3);
249 v.push_back(2);
250 v.push_back(1);
251
252 EXPECT_EQ(3, count(v, 1));
253 EXPECT_EQ(2, count(v, 2));
254 EXPECT_EQ(1, count(v, 3));
David Blaikie3f395b62017-11-20 22:12:55 +0000255 EXPECT_EQ(1, count(v, 4));
256}
257
258TEST(STLExtrasTest, for_each) {
259 std::vector<int> v{0, 1, 2, 3, 4};
260 int count = 0;
261
262 llvm::for_each(v, [&count](int) { ++count; });
263 EXPECT_EQ(5, count);
264}
265
266TEST(STLExtrasTest, ToVector) {
267 std::vector<char> v = {'a', 'b', 'c'};
268 auto Enumerated = to_vector<4>(enumerate(v));
David Blaikie9812b902017-03-13 21:46:12 +0000269 ASSERT_EQ(3u, Enumerated.size());
Zachary Turner39e53ee2017-03-13 16:24:10 +0000270 for (size_t I = 0; I < v.size(); ++I) {
271 EXPECT_EQ(I, Enumerated[I].index());
272 EXPECT_EQ(v[I], Enumerated[I].value());
273 }
274}
275
Chandler Carruth1e976302016-12-25 23:41:14 +0000276TEST(STLExtrasTest, ConcatRange) {
277 std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8};
278 std::vector<int> Test;
279
280 std::vector<int> V1234 = {1, 2, 3, 4};
281 std::list<int> L56 = {5, 6};
282 SmallVector<int, 2> SV78 = {7, 8};
283
284 // Use concat across different sized ranges of different types with different
285 // iterators.
286 for (int &i : concat<int>(V1234, L56, SV78))
287 Test.push_back(i);
288 EXPECT_EQ(Expected, Test);
289
290 // Use concat between a temporary, an L-value, and an R-value to make sure
291 // complex lifetimes work well.
292 Test.clear();
293 for (int &i : concat<int>(std::vector<int>(V1234), L56, std::move(SV78)))
294 Test.push_back(i);
295 EXPECT_EQ(Expected, Test);
296}
Chandler Carruth85419112016-12-26 23:10:40 +0000297
298TEST(STLExtrasTest, PartitionAdaptor) {
299 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
300
301 auto I = partition(V, [](int i) { return i % 2 == 0; });
302 ASSERT_EQ(V.begin() + 4, I);
303
304 // Sort the two halves as partition may have messed with the order.
Mandeep Singh Grang77a1dcd2018-04-07 01:29:45 +0000305 llvm::sort(V.begin(), I);
306 llvm::sort(I, V.end());
Chandler Carruth85419112016-12-26 23:10:40 +0000307
308 EXPECT_EQ(2, V[0]);
309 EXPECT_EQ(4, V[1]);
310 EXPECT_EQ(6, V[2]);
311 EXPECT_EQ(8, V[3]);
312 EXPECT_EQ(1, V[4]);
313 EXPECT_EQ(3, V[5]);
314 EXPECT_EQ(5, V[6]);
315 EXPECT_EQ(7, V[7]);
316}
317
Chandler Carruth5c40baf2016-12-26 23:30:44 +0000318TEST(STLExtrasTest, EraseIf) {
319 std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
320
321 erase_if(V, [](int i) { return i % 2 == 0; });
322 EXPECT_EQ(4u, V.size());
323 EXPECT_EQ(1, V[0]);
324 EXPECT_EQ(3, V[1]);
325 EXPECT_EQ(5, V[2]);
326 EXPECT_EQ(7, V[3]);
327}
328
David Blaikie3f395b62017-11-20 22:12:55 +0000329namespace some_namespace {
330struct some_struct {
331 std::vector<int> data;
332 std::string swap_val;
333};
334
335std::vector<int>::const_iterator begin(const some_struct &s) {
336 return s.data.begin();
Chandler Carruthd3197162016-08-19 02:07:51 +0000337}
David Blaikie3f395b62017-11-20 22:12:55 +0000338
339std::vector<int>::const_iterator end(const some_struct &s) {
340 return s.data.end();
341}
342
343void swap(some_struct &lhs, some_struct &rhs) {
344 // make swap visible as non-adl swap would even seem to
345 // work with std::swap which defaults to moving
346 lhs.swap_val = "lhs";
347 rhs.swap_val = "rhs";
348}
349} // namespace some_namespace
350
351TEST(STLExtrasTest, ADLTest) {
352 some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""};
353 some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""};
354
355 EXPECT_EQ(*adl_begin(s), 1);
356 EXPECT_EQ(*(adl_end(s) - 1), 5);
357
358 adl_swap(s, s2);
359 EXPECT_EQ(s.swap_val, "lhs");
360 EXPECT_EQ(s2.swap_val, "rhs");
361
362 int count = 0;
363 llvm::for_each(s, [&count](int) { ++count; });
364 EXPECT_EQ(5, count);
365}
366
Matthias Braunb3bc9582018-10-31 00:23:23 +0000367TEST(STLExtrasTest, EmptyTest) {
368 std::vector<void*> V;
Matthias Braun4f277562018-10-31 01:58:00 +0000369 EXPECT_TRUE(llvm::empty(V));
Matthias Braunb3bc9582018-10-31 00:23:23 +0000370 V.push_back(nullptr);
Matthias Braun4f277562018-10-31 01:58:00 +0000371 EXPECT_FALSE(llvm::empty(V));
Matthias Braunb3bc9582018-10-31 00:23:23 +0000372
373 std::initializer_list<int> E = {};
374 std::initializer_list<int> NotE = {7, 13, 42};
Matthias Braun4f277562018-10-31 01:58:00 +0000375 EXPECT_TRUE(llvm::empty(E));
376 EXPECT_FALSE(llvm::empty(NotE));
Matthias Braunb3bc9582018-10-31 00:23:23 +0000377
378 auto R0 = make_range(V.begin(), V.begin());
Matthias Braun4f277562018-10-31 01:58:00 +0000379 EXPECT_TRUE(llvm::empty(R0));
Matthias Braunb3bc9582018-10-31 00:23:23 +0000380 auto R1 = make_range(V.begin(), V.end());
Matthias Braun4f277562018-10-31 01:58:00 +0000381 EXPECT_FALSE(llvm::empty(R1));
Matthias Braunb3bc9582018-10-31 00:23:23 +0000382}
383
Chandler Carruthb2e68022018-08-04 08:17:26 +0000384TEST(STLExtrasTest, EarlyIncrementTest) {
385 std::list<int> L = {1, 2, 3, 4};
386
387 auto EIR = make_early_inc_range(L);
388
389 auto I = EIR.begin();
390 auto EI = EIR.end();
391 EXPECT_NE(I, EI);
392
393 EXPECT_EQ(1, *I);
394#if LLVM_ENABLE_ABI_BREAKING_CHECKS
395#ifndef NDEBUG
396 // Repeated dereferences are not allowed.
397 EXPECT_DEATH(*I, "Cannot dereference");
398 // Comparison after dereference is not allowed.
399 EXPECT_DEATH((void)(I == EI), "Cannot compare");
400 EXPECT_DEATH((void)(I != EI), "Cannot compare");
401#endif
402#endif
403
404 ++I;
405 EXPECT_NE(I, EI);
406#if LLVM_ENABLE_ABI_BREAKING_CHECKS
407#ifndef NDEBUG
408 // You cannot increment prior to dereference.
409 EXPECT_DEATH(++I, "Cannot increment");
410#endif
411#endif
412 EXPECT_EQ(2, *I);
413#if LLVM_ENABLE_ABI_BREAKING_CHECKS
414#ifndef NDEBUG
415 // Repeated dereferences are not allowed.
416 EXPECT_DEATH(*I, "Cannot dereference");
417#endif
418#endif
419
420 // Inserting shouldn't break anything. We should be able to keep dereferencing
421 // the currrent iterator and increment. The increment to go to the "next"
422 // iterator from before we inserted.
423 L.insert(std::next(L.begin(), 2), -1);
424 ++I;
425 EXPECT_EQ(3, *I);
426
427 // Erasing the front including the current doesn't break incrementing.
428 L.erase(L.begin(), std::prev(L.end()));
429 ++I;
430 EXPECT_EQ(4, *I);
431 ++I;
432 EXPECT_EQ(EIR.end(), I);
433}
434
Chen Zheng12061ae2018-08-17 07:51:01 +0000435TEST(STLExtrasTest, splat) {
436 std::vector<int> V;
437 EXPECT_FALSE(is_splat(V));
438
439 V.push_back(1);
440 EXPECT_TRUE(is_splat(V));
441
442 V.push_back(1);
443 V.push_back(1);
444 EXPECT_TRUE(is_splat(V));
445
446 V.push_back(2);
447 EXPECT_FALSE(is_splat(V));
448}
449
David Blaikie3f395b62017-11-20 22:12:55 +0000450} // namespace