Skip to content

Commit f9f7de4

Browse files
author
Ryan Haining
committed
Adds support for move-only callables in dropwhile
Issue #89
1 parent 020f99d commit f9f7de4

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

cppitertools/dropwhile.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class iter::impl::Dropper {
2929

3030
Dropper(FilterFunc filter_func, Container&& container)
3131
: container_(std::forward<Container>(container)),
32-
filter_func_(filter_func) {}
32+
filter_func_(std::move(filter_func)) {}
3333

3434
public:
3535
Dropper(Dropper&&) = default;

test/test_dropwhile.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,51 @@ using iter::dropwhile;
1010

1111
using Vec = const std::vector<int>;
1212

13+
TEST_CASE("dropwhile: handles different callable types", "[dropwhile]") {
14+
Vec ns = {1, 3, 4, 20, 2, 4, 6, 8};
15+
Vec vc = {20, 2, 4, 6, 8};
16+
std::vector<int> v;
17+
SECTION("with function pointer") {
18+
auto d = dropwhile(less_than_five, ns);
19+
v = Vec(std::begin(d), std::end(d));
20+
}
21+
22+
SECTION("with callable object") {
23+
auto d = dropwhile(LessThanValue{5}, ns);
24+
v = Vec(std::begin(d), std::end(d));
25+
}
26+
27+
SECTION("with lvalue callable object") {
28+
auto lt = LessThanValue{5};
29+
SECTION("normal call") {
30+
auto d = dropwhile(lt, ns);
31+
v = Vec(std::begin(d), std::end(d));
32+
}
33+
SECTION("pipe") {
34+
auto d = ns | dropwhile(lt);
35+
v = Vec(std::begin(d), std::end(d));
36+
}
37+
}
38+
39+
SECTION("with move-only callable object") {
40+
SECTION("normal call") {
41+
auto d = dropwhile(MoveOnlyLessThanValue{5}, ns);
42+
v = Vec(std::begin(d), std::end(d));
43+
}
44+
SECTION("pipe") {
45+
auto d = ns | dropwhile(MoveOnlyLessThanValue{5});
46+
v = Vec(std::begin(d), std::end(d));
47+
}
48+
}
49+
50+
SECTION("with lambda") {
51+
auto ltf = [](int i) { return i < 5; };
52+
auto d = dropwhile(ltf, ns);
53+
v = Vec(std::begin(d), std::end(d));
54+
}
55+
REQUIRE(v == vc);
56+
}
57+
1358
TEST_CASE("dropwhile: skips initial elements", "[dropwhile]") {
1459
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
1560
std::vector<int> v;

0 commit comments

Comments
 (0)