Skip to content

Commit 5d6497e

Browse files
committed
Tests groupby with pointer to member
1 parent 56525bc commit 5d6497e

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

test/test_groupby.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ TEST_CASE("groupby: works with lambda, callable, and function pointer") {
5959
}
6060
}
6161

62+
SECTION("pointer to member") {
63+
for (auto&& gb : groupby(vec, &std::string::size)) {
64+
keys.push_back(gb.first);
65+
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
66+
}
67+
}
68+
6269
const std::vector<int> kc = {2, 3, 5};
6370
REQUIRE(keys == kc);
6471

@@ -69,6 +76,39 @@ TEST_CASE("groupby: works with lambda, callable, and function pointer") {
6976
REQUIRE(groups == gc);
7077
}
7178

79+
TEST_CASE("groupby: handles pointer to member", "[groupby]") {
80+
using itertest::Point;
81+
const std::vector<Point> ps = {
82+
{0, 2}, {0, 4}, {1, 3}, {1, 7}, {1, 10}, {1, 12}, {3, 5}};
83+
84+
std::vector<std::vector<Point>> groups;
85+
std::vector<int> keys;
86+
87+
SECTION("with pointer to data member") {
88+
auto g = groupby(ps, &Point::x);
89+
for (auto&& gb : g) {
90+
keys.push_back(gb.first);
91+
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
92+
}
93+
}
94+
95+
SECTION("with pointer member function") {
96+
auto g = groupby(ps, &Point::get_x);
97+
for (auto&& gb : g) {
98+
keys.push_back(gb.first);
99+
groups.emplace_back(std::begin(gb.second), std::end(gb.second));
100+
}
101+
}
102+
103+
const std::vector<int> kc = {0, 1, 3};
104+
REQUIRE(keys == kc);
105+
106+
const std::vector<std::vector<Point>> gc = {
107+
{{0, 2}, {0, 4}}, {{1, 3}, {1, 7}, {1, 10}, {1, 12}}, {{3, 5}}};
108+
109+
REQUIRE(groups == gc);
110+
}
111+
72112
TEST_CASE("groupby: const iteration", "[groupby][const]") {
73113
std::vector<int> keys;
74114
std::vector<std::vector<std::string>> groups;

0 commit comments

Comments
 (0)