forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_enumerate.cpp
More file actions
224 lines (188 loc) · 5.47 KB
/
Copy pathtest_enumerate.cpp
File metadata and controls
224 lines (188 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <enumerate.hpp>
#include "helpers.hpp"
#include <iterator>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
namespace Catch {
template <typename A, typename B>
std::string toString(const std::pair<A, B>& p) {
std::ostringstream oss;
oss << '{' << p.first << ", " << p.second << '}';
return oss.str();
}
}
#include "catch.hpp"
using Vec = std::vector<std::pair<std::size_t, char>>;
using iter::enumerate;
using itertest::BasicIterable;
using itertest::SolidInt;
TEST_CASE("Basic Functioning enumerate", "[enumerate]") {
std::string str = "abc";
auto e = enumerate(str);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'b'}, {2, 'c'}};
REQUIRE(v == vc);
}
TEST_CASE("enumerate: has .index, .element, .first, and .second") {
std::string s = "abc";
auto e = enumerate(s);
auto it = std::begin(e);
REQUIRE(it->index == it->first);
REQUIRE(&it->element == &it->second);
}
TEST_CASE("Empty enumerate", "[enumerate]") {
std::string emp{};
auto e = enumerate(emp);
REQUIRE(std::begin(e) == std::end(e));
}
TEST_CASE("Postfix ++ enumerate", "[enumerate]") {
std::string s{"amz"};
auto e = enumerate(s);
auto it = std::begin(e);
it++;
REQUIRE((*it).first == 1);
}
TEST_CASE("enumerate: structured bindings", "[enumerate]") {
{
std::string s{"amz"};
auto e = enumerate(s);
auto it = std::begin(e);
REQUIRE(std::tuple_size<std::decay_t<decltype(*it)>>{} == 2);
REQUIRE(std::get<0>(*it) == it->first);
}
Vec v;
for (auto && [ i, c ] : enumerate(std::string{"xyz"})) {
v.emplace_back(i, c);
}
const Vec vc{{0, 'x'}, {1, 'y'}, {2, 'z'}};
REQUIRE(v == vc);
}
TEST_CASE("enumerate: with starting value", "[enumerate]") {
std::string str = "hey";
auto e = enumerate(str, 5u);
Vec v(std::begin(e), std::end(e));
Vec vc{{5, 'h'}, {6, 'e'}, {7, 'y'}};
REQUIRE(v == vc);
}
TEST_CASE("Modifications through enumerate affect container", "[enumerate]") {
std::vector<int> v{1, 2, 3, 4};
std::vector<int> vc(v.size(), -1);
for (auto&& p : enumerate(v)) {
p.second = -1;
}
REQUIRE(v == vc);
}
TEST_CASE("enumerate with static array works", "[enumerate]") {
char arr[] = {'w', 'x', 'y'};
SECTION("Conversion to vector") {
auto e = enumerate(arr);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'w'}, {1, 'x'}, {2, 'y'}};
REQUIRE(v == vc);
}
SECTION("Modification through enumerate") {
for (auto&& p : enumerate(arr)) {
p.second = 'z';
}
std::vector<char> v(std::begin(arr), std::end(arr));
decltype(v) vc(v.size(), 'z');
REQUIRE(v == vc);
}
}
TEST_CASE("binds reference when it should", "[enumerate]") {
BasicIterable<char> bi{'x', 'y', 'z'};
auto e = enumerate(bi);
(void)e;
REQUIRE_FALSE(bi.was_moved_from());
}
TEST_CASE("moves rvalues into enumerable object", "[enumerate]") {
BasicIterable<char> bi{'x', 'y', 'z'};
auto e = enumerate(std::move(bi));
REQUIRE(bi.was_moved_from());
(void)e;
}
TEST_CASE("enumerate: operator->", "[enumerate]") {
std::vector<int> ns = {50, 60, 70};
auto e = enumerate(ns);
auto it = std::begin(e);
REQUIRE(it->first == 0);
REQUIRE(it->second == 50);
}
TEST_CASE("enumerate: index and element", "[enumerate]") {
std::string s{"ace"};
auto e = enumerate(s);
auto it = std::begin(e);
REQUIRE((*it).index == 0);
REQUIRE((*it).element == 'a');
Vec v;
for (auto&& p : enumerate(s)) {
v.emplace_back(p.index, p.element);
}
Vec vc{{0, 'a'}, {1, 'c'}, {2, 'e'}};
REQUIRE(v == vc);
}
TEST_CASE("enumerate: index and element through arrow", "[enumerate]") {
std::string s{"ace"};
auto e = enumerate(s);
SECTION("One inspection") {
auto it = std::begin(e);
REQUIRE(it->index == 0);
REQUIRE(it->element == 'a');
}
SECTION("full loop") {
Vec v;
for (auto it = std::begin(e), end_it = std::end(e); it != end_it; ++it) {
v.emplace_back(it->index, it->element);
}
Vec vc{{0, 'a'}, {1, 'c'}, {2, 'e'}};
REQUIRE(v == vc);
}
}
TEST_CASE("Works with const iterable", "[enumerate]") {
const std::string s{"ace"};
auto e = enumerate(s);
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'c'}, {2, 'e'}};
REQUIRE(v == vc);
}
TEST_CASE("Doesn't move or copy elements of iterable", "[enumerate]") {
constexpr SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i : enumerate(arr)) {
(void)i;
}
}
TEST_CASE("enumerate: iterator meets requirements", "[enumerate]") {
std::string s{};
auto c = enumerate(s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
TEST_CASE("enumerate: works with pipe", "[enumerate]") {
constexpr char str[] = {'a', 'b', 'c'};
auto e = str | enumerate;
Vec v(std::begin(e), std::end(e));
Vec vc{{0, 'a'}, {1, 'b'}, {2, 'c'}};
REQUIRE(v == vc);
}
TEST_CASE("enumerate: works index and pipe", "[enumerate]") {
constexpr char str[] = {'a', 'b', 'c'};
auto e = str | enumerate(2);
Vec v(std::begin(e), std::end(e));
Vec vc{{2, 'a'}, {3, 'b'}, {4, 'c'}};
REQUIRE(v == vc);
}
TEST_CASE(
"enumerate: Works with different begin and end types", "[enumerate]") {
CharRange cr{'d'};
auto e = enumerate(cr);
Vec v(e.begin(), e.end());
Vec vc{{0, 'a'}, {1, 'b'}, {2, 'c'}};
REQUIRE(v == vc);
}
template <typename T>
using ImpT = decltype(enumerate(std::declval<T>()));
TEST_CASE("enumerate: has correct ctor and assign ops", "[enumerate]") {
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string&>>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<ImpT<std::string>>::value);
}