blob: e5ac1800d2f5b79a9ba701595b19c59c02d9df1b [file] [log] [blame]
Chandler Carruthe8529c22016-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
Zachary Turner4f20a0a2016-09-30 15:43:59 +000013#include <vector>
14
Chandler Carruthe8529c22016-08-19 02:07:51 +000015using namespace llvm;
16
17namespace {
18
19int f(rank<0>) { return 0; }
20int f(rank<1>) { return 1; }
21int f(rank<2>) { return 2; }
22int f(rank<4>) { return 4; }
23
24TEST(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
Zachary Turneraa0a5622016-10-05 16:54:09 +000042TEST(STLExtrasTest, EnumerateLValue) {
43 // Test that a simple LValue can be enumerated and gives correct results with
44 // multiple types, including the empty container.
Zachary Turner4f20a0a2016-09-30 15:43:59 +000045 std::vector<char> foo = {'a', 'b', 'c'};
Zachary Turneraa0a5622016-10-05 16:54:09 +000046 std::vector<std::pair<std::size_t, char>> CharResults;
Zachary Turner4f20a0a2016-09-30 15:43:59 +000047
48 for (auto X : llvm::enumerate(foo)) {
Zachary Turneraa0a5622016-10-05 16:54:09 +000049 CharResults.emplace_back(X.Index, X.Value);
Zachary Turner4f20a0a2016-09-30 15:43:59 +000050 }
Zachary Turneraa0a5622016-10-05 16:54:09 +000051 ASSERT_EQ(3u, CharResults.size());
52 EXPECT_EQ(std::make_pair(0u, 'a'), CharResults[0]);
53 EXPECT_EQ(std::make_pair(1u, 'b'), CharResults[1]);
54 EXPECT_EQ(std::make_pair(2u, 'c'), CharResults[2]);
Zachary Turner4f20a0a2016-09-30 15:43:59 +000055
Zachary Turneraa0a5622016-10-05 16:54:09 +000056 // Test a const range of a different type.
57 std::vector<std::pair<std::size_t, int>> IntResults;
58 const std::vector<int> bar = {1, 2, 3};
Zachary Turner4f20a0a2016-09-30 15:43:59 +000059 for (auto X : llvm::enumerate(bar)) {
Zachary Turneraa0a5622016-10-05 16:54:09 +000060 IntResults.emplace_back(X.Index, X.Value);
Zachary Turner4f20a0a2016-09-30 15:43:59 +000061 }
Zachary Turneraa0a5622016-10-05 16:54:09 +000062 ASSERT_EQ(3u, IntResults.size());
63 EXPECT_EQ(std::make_pair(0u, 1), IntResults[0]);
64 EXPECT_EQ(std::make_pair(1u, 2), IntResults[1]);
65 EXPECT_EQ(std::make_pair(2u, 3), IntResults[2]);
Zachary Turner4f20a0a2016-09-30 15:43:59 +000066
Zachary Turneraa0a5622016-10-05 16:54:09 +000067 // Test an empty range.
68 IntResults.clear();
Zachary Turner4f20a0a2016-09-30 15:43:59 +000069 const std::vector<int> baz;
70 for (auto X : llvm::enumerate(baz)) {
Zachary Turneraa0a5622016-10-05 16:54:09 +000071 IntResults.emplace_back(X.Index, X.Value);
Zachary Turner4f20a0a2016-09-30 15:43:59 +000072 }
Zachary Turneraa0a5622016-10-05 16:54:09 +000073 EXPECT_TRUE(IntResults.empty());
Zachary Turner4f20a0a2016-09-30 15:43:59 +000074}
75
Zachary Turneraa0a5622016-10-05 16:54:09 +000076TEST(STLExtrasTest, EnumerateModifyLValue) {
77 // Test that you can modify the underlying entries of an lvalue range through
78 // the enumeration iterator.
Zachary Turner4f20a0a2016-09-30 15:43:59 +000079 std::vector<char> foo = {'a', 'b', 'c'};
80
81 for (auto X : llvm::enumerate(foo)) {
82 ++X.Value;
83 }
Zachary Turner4f20a0a2016-09-30 15:43:59 +000084 EXPECT_EQ('b', foo[0]);
85 EXPECT_EQ('c', foo[1]);
86 EXPECT_EQ('d', foo[2]);
87}
Zachary Turneraa0a5622016-10-05 16:54:09 +000088
89TEST(STLExtrasTest, EnumerateRValueRef) {
90 // Test that an rvalue can be enumerated.
91 std::vector<std::pair<std::size_t, int>> Results;
92
93 auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3});
94
95 for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) {
96 Results.emplace_back(X.Index, X.Value);
97 }
98
99 ASSERT_EQ(3u, Results.size());
100 EXPECT_EQ(std::make_pair(0u, 1), Results[0]);
101 EXPECT_EQ(std::make_pair(1u, 2), Results[1]);
102 EXPECT_EQ(std::make_pair(2u, 3), Results[2]);
103}
104
105TEST(STLExtrasTest, EnumerateModifyRValue) {
106 // Test that when enumerating an rvalue, modification still works (even if
107 // this isn't terribly useful, it at least shows that we haven't snuck an
108 // extra const in there somewhere.
109 std::vector<std::pair<std::size_t, char>> Results;
110
111 for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) {
112 ++X.Value;
113 Results.emplace_back(X.Index, X.Value);
114 }
115
116 ASSERT_EQ(3u, Results.size());
117 EXPECT_EQ(std::make_pair(0u, '2'), Results[0]);
118 EXPECT_EQ(std::make_pair(1u, '3'), Results[1]);
119 EXPECT_EQ(std::make_pair(2u, '4'), Results[2]);
120}
121
122template <bool B> struct CanMove {};
123template <> struct CanMove<false> {
124 CanMove(CanMove &&) = delete;
125
126 CanMove() = default;
127 CanMove(const CanMove &) = default;
128};
129
130template <bool B> struct CanCopy {};
131template <> struct CanCopy<false> {
132 CanCopy(const CanCopy &) = delete;
133
134 CanCopy() = default;
135 CanCopy(CanCopy &&) = default;
136};
137
138template <bool Moveable, bool Copyable>
139struct Range : CanMove<Moveable>, CanCopy<Copyable> {
140 explicit Range(int &C, int &M, int &D) : C(C), M(M), D(D) {}
141 Range(const Range &R) : CanCopy<Copyable>(R), C(R.C), M(R.M), D(R.D) { ++C; }
142 Range(Range &&R) : CanMove<Moveable>(std::move(R)), C(R.C), M(R.M), D(R.D) {
143 ++M;
144 }
145 ~Range() { ++D; }
146
147 int &C;
148 int &M;
149 int &D;
150
151 int *begin() { return nullptr; }
152 int *end() { return nullptr; }
153};
154
155TEST(STLExtrasTest, EnumerateLifetimeSemantics) {
156 // Test that when enumerating lvalues and rvalues, there are no surprise
157 // copies or moves.
158
159 // With an rvalue, it should not be destroyed until the end of the scope.
160 int Copies = 0;
161 int Moves = 0;
162 int Destructors = 0;
163 {
164 auto E1 = enumerate(Range<true, false>(Copies, Moves, Destructors));
165 // Doesn't compile. rvalue ranges must be moveable.
166 // auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors));
167 EXPECT_EQ(0, Copies);
168 EXPECT_EQ(1, Moves);
169 EXPECT_EQ(1, Destructors);
170 }
171 EXPECT_EQ(0, Copies);
172 EXPECT_EQ(1, Moves);
173 EXPECT_EQ(2, Destructors);
174
175 Copies = Moves = Destructors = 0;
176 // With an lvalue, it should not be destroyed even after the end of the scope.
177 // lvalue ranges need be neither copyable nor moveable.
178 Range<false, false> R(Copies, Moves, Destructors);
179 {
180 auto Enumerator = enumerate(R);
181 (void)Enumerator;
182 EXPECT_EQ(0, Copies);
183 EXPECT_EQ(0, Moves);
184 EXPECT_EQ(0, Destructors);
185 }
186 EXPECT_EQ(0, Copies);
187 EXPECT_EQ(0, Moves);
188 EXPECT_EQ(0, Destructors);
189}
Chandler Carruthe8529c22016-08-19 02:07:51 +0000190}