forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_groupby.cpp
More file actions
371 lines (315 loc) · 9.98 KB
/
Copy pathtest_groupby.cpp
File metadata and controls
371 lines (315 loc) · 9.98 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <cppitertools/groupby.hpp>
#include "helpers.hpp"
#include <iterator>
#include <string>
#include <vector>
#include "catch.hpp"
using iter::groupby;
namespace {
int length(const std::string& s) {
return s.size();
}
struct Sizer {
int operator()(const std::string& s) {
return s.size();
}
};
const std::vector<std::string> vec = {
"hi", "ab", "ho", "abc", "def", "abcde", "efghi"};
}
TEST_CASE("groupby: works with lambda, callable, and function pointer") {
std::vector<int> keys;
std::vector<std::vector<std::string>> groups;
SECTION("Function pointer") {
SECTION("Normal call") {
for (auto&& gb : groupby(vec, length)) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("Pipe") {
for (auto&& gb : vec | groupby(length)) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
}
SECTION("Callable object") {
for (auto&& gb : groupby(vec, Sizer{})) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("lambda function") {
for (auto&& gb :
groupby(vec, [](const std::string& s) { return s.size(); })) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
const std::vector<int> kc = {2, 3, 5};
REQUIRE(keys == kc);
const std::vector<std::vector<std::string>> gc = {
{"hi", "ab", "ho"},
{"abc", "def"},
{"abcde", "efghi"},
};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: handles pointer to member function", "[groupby]") {
std::vector<itertest::Integer> nums = {
10, 20, 30, -5, 40, -6, -7, 50, 60, -8, -9, -10, -11, 70};
std::vector<std::vector<itertest::Integer>> groups;
std::vector<bool> keys;
for (auto&& gb : groupby(nums, &itertest::Integer::is_positive)) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
const std::vector<bool> kc = {true, false, true, false, true, false, true};
const std::vector<std::vector<itertest::Integer>> gc = {
{10, 20, 30}, {-5}, {40}, {-6, -7}, {50, 60}, {-8, -9, -10, -11}, {70}};
REQUIRE(keys == kc);
REQUIRE(groups == gc);
}
TEST_CASE("groupby: handles pointer to data member", "[groupby]") {
using itertest::Point;
const std::vector<Point> ps = {
{0, 2}, {0, 4}, {1, 3}, {1, 7}, {1, 10}, {1, 12}, {3, 5}};
std::vector<std::vector<Point>> groups;
std::vector<int> keys;
SECTION("with pointer to data member") {
auto g = groupby(ps, &Point::x);
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("with pointer member function") {
auto g = groupby(ps, &Point::get_x);
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
const std::vector<int> kc = {0, 1, 3};
REQUIRE(keys == kc);
const std::vector<std::vector<Point>> gc = {
{{0, 2}, {0, 4}}, {{1, 3}, {1, 7}, {1, 10}, {1, 12}}, {{3, 5}}};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: const iteration", "[groupby][const]") {
std::vector<int> keys;
std::vector<std::vector<std::string>> groups;
SECTION("Function pointer") {
SECTION("lvalue") {
std::vector<std::string> local_vec(vec);
const auto g = groupby(local_vec, length);
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("rvalue") {
const auto g = groupby(std::vector<std::string>(vec), length);
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
}
SECTION("Callable object") {
const auto g = groupby(vec, Sizer{});
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("lambda function") {
const auto g = groupby(vec, [](const std::string& s) { return s.size(); });
for (auto&& gb : g) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
const std::vector<int> kc = {2, 3, 5};
REQUIRE(keys == kc);
const std::vector<std::vector<std::string>> gc = {
{"hi", "ab", "ho"},
{"abc", "def"},
{"abcde", "efghi"},
};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: iterators compare equal to non-const iterators",
"[groupby][const]") {
auto gb = groupby(std::vector<std::string>{"hi"}, length);
const auto& cgb = gb;
auto gb_it = std::begin(gb);
(void)(gb_it == std::end(cgb));
// TODO figure out how to make GroupIterator<Container> and
// GroupIterator<AsConst<Container>> comparable
#if 0
auto group = std::begin(gb_it->second);
const auto& cgroup = group;
(void)(std::begin(group) == std::begin(cgroup));
#endif
}
TEST_CASE("groupby: Works with different begin and end types", "[groupby]") {
CharRange cr{'f'};
std::vector<bool> keys;
std::vector<std::vector<char>> groups;
for (auto&& gb : groupby(cr, [](char c) { return c == 'c'; })) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
const std::vector<bool> kc = {false, true, false};
const std::vector<std::vector<char>> gc = {{'a', 'b'}, {'c'}, {'d', 'e'}};
REQUIRE(keys == kc);
REQUIRE(groups == gc);
}
TEST_CASE("groupby: groups can be skipped completely", "[groupby]") {
std::vector<int> keys;
std::vector<std::vector<std::string>> groups;
for (auto&& gb : groupby(vec, &length)) {
if (gb.first == 3) {
continue;
}
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
const std::vector<int> kc = {2, 5};
REQUIRE(keys == kc);
const std::vector<std::vector<std::string>> gc = {
{"hi", "ab", "ho"},
{"abcde", "efghi"},
};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: groups can be skipped partially", "[groupby]") {
std::vector<int> keys;
std::vector<std::vector<std::string>> groups;
for (auto&& gb : groupby(vec, &length)) {
keys.push_back(gb.first);
if (gb.first == 3) {
std::vector<std::string> cut_short = {*std::begin(gb.second)};
groups.push_back(cut_short);
} else {
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
const std::vector<int> kc = {2, 3, 5};
REQUIRE(keys == kc);
const std::vector<std::vector<std::string>> gc = {
{"hi", "ab", "ho"},
{"abc"},
{"abcde", "efghi"},
};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: single argument uses elements as keys", "[groupby]") {
std::vector<int> ivec = {5, 5, 6, 6, 19, 19, 19, 19, 69, 0, 10, 10};
std::vector<int> keys;
std::vector<std::vector<int>> groups;
SECTION("Normal call") {
for (auto&& gb : groupby(ivec)) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
SECTION("Pipe") {
for (auto&& gb : ivec | groupby) {
keys.push_back(gb.first);
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
}
}
const std::vector<int> kc = {5, 6, 19, 69, 0, 10};
REQUIRE(keys == kc);
std::vector<std::vector<int>> gc = {
{5, 5},
{6, 6},
{19, 19, 19, 19},
{69},
{0},
{10, 10},
};
REQUIRE(groups == gc);
}
TEST_CASE("groupby: empty iterable yields nothing", "[groupby]") {
std::vector<int> ivec{};
auto g = groupby(ivec);
REQUIRE(std::begin(g) == std::end(g));
}
TEST_CASE("groupby: inner iterator (group) not used", "[groupby]") {
std::vector<int> keys;
for (auto&& gb : groupby(vec, length)) {
keys.push_back(gb.first);
}
std::vector<int> kc = {2, 3, 5};
REQUIRE(keys == kc);
}
TEST_CASE("groupby: doesn't double dereference", "[groupby]") {
itertest::InputIterable seq;
for (auto&& kg : groupby(seq, [](int i) { return i < 3; })) {
for (auto&& e : kg.second) {
(void)e;
}
}
}
TEST_CASE("grouby: iterator doesn't need to be dereferenced before advanced",
"[groupby]") {
std::vector<int> ns = {2, 4, 7};
auto g = groupby(ns);
auto it = std::begin(g);
++it;
REQUIRE((*it).first == 4);
}
TEST_CASE("groupby: iterator can be dereferenced multiple times", "[groupby]") {
std::vector<int> ns = {2, 4, 7};
auto g = groupby(ns);
auto it = std::begin(g);
auto k1 = (*it).first;
auto k2 = (*it).first;
REQUIRE(k1 == k2);
}
TEST_CASE(
"groupby: copy constructed iterators behave as expected", "[groupby]") {
std::vector<int> ns = {2, 3, 4, 5};
auto g = groupby(ns);
auto it = std::begin(g);
REQUIRE(it->first == 2);
{
auto it2 = it;
REQUIRE(it2->first == 2);
++it;
REQUIRE(it->first == 3);
REQUIRE(*std::begin(it->second) == 3);
}
REQUIRE(it->first == 3);
REQUIRE(*std::begin(it->second) == 3);
}
TEST_CASE("groupby: operator-> on both iterator types", "[groupby]") {
std::vector<std::string> ns = {"a", "abc"};
auto g = groupby(ns, [](const std::string& str) { return str.size(); });
auto it = std::begin(g);
REQUIRE(it->first == 1);
auto it2 = std::begin(it->second);
REQUIRE(it2->size() == 1);
}
TEST_CASE("groupby: iterator and groupiterator are correct", "[groupby]") {
std::string s{"abc"};
auto c = groupby(s);
auto it = std::begin(c);
REQUIRE(itertest::IsIterator<decltype(it)>::value);
auto&& gp = (*it).second;
auto it2 = std::begin(gp);
REQUIRE(itertest::IsIterator<decltype(it2)>::value);
}
template <typename T, typename U>
using ImpT = decltype(groupby(std::declval<T>(), std::declval<U>()));
TEST_CASE("groupby: has correct ctor and assign ops", "[groupby]") {
using T1 = ImpT<std::string&, bool (*)(char c)>;
auto lam = [](char) { return false; };
using T2 = ImpT<std::string, decltype(lam)>;
REQUIRE(itertest::IsMoveConstructibleOnly<T1>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<T2>::value);
}