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
230 lines (194 loc) · 6 KB
/
Copy pathtest_groupby.cpp
File metadata and controls
230 lines (194 loc) · 6 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
#include <groupby.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#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: 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);
}