@@ -40,6 +40,43 @@ namespace {
40
40
41
41
const std::vector<std::string> vec = {
42
42
" hi" , " ab" , " ho" , " abc" , " def" , " abcde" , " efghi" };
43
+
44
+ struct Person {
45
+ std::string name;
46
+ int id;
47
+ bool operator ==(const Person& other) const {
48
+ return id == other.id ;
49
+ }
50
+ };
51
+
52
+ std::string& get_name (Person& p) {
53
+ return p.name ;
54
+ }
55
+
56
+ template <typename G>
57
+ std::vector<Person> extract_person_group (G g) {
58
+ return {std::begin (g), std::end (g)};
59
+ }
60
+ }
61
+
62
+ TEST_CASE (" groupby: handle key function that returns reference" , " [groupby]" ) {
63
+ std::vector<Person> people = {{" first" , 1 }, {" first" , 2 }, {" first" , 3 }};
64
+ std::vector<std::string> keys;
65
+ std::vector<std::vector<Person>> groups;
66
+
67
+ for (auto && gb : groupby (people, get_name)) {
68
+ groups.push_back (extract_person_group (std::move (gb.second )));
69
+ keys.push_back (gb.first );
70
+ }
71
+
72
+ const std::vector<std::string> kc = {" first" };
73
+ const std::vector<std::vector<Person>> gc = {
74
+ {{" first" , 1 }, {" first" , 2 }, {" first" , 3 }}};
75
+
76
+ REQUIRE (people[0 ].name == " first" );
77
+ REQUIRE (gc[0 ][0 ].name == " first" );
78
+ REQUIRE (keys == kc);
79
+ REQUIRE (groups == gc);
43
80
}
44
81
45
82
TEST_CASE (" groupby: handles different callable types" , " [groupby]" ) {
0 commit comments