This repository was archived by the owner on Nov 15, 2022. It is now read-only.
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
174 lines (142 loc) · 4.41 KB
/
Copy pathtest_groupby.cpp
File metadata and controls
174 lines (142 loc) · 4.41 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
#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") {
for (auto&& gb : groupby(vec, 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;
for (auto&& gb : groupby(ivec)) {
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("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 );
}