1- #include < cppitertools/takewhile.hpp>
2-
31#include < array>
2+ #include < cppitertools/takewhile.hpp>
43#include < string>
54#include < utility>
65#include < vector>
1110using iter::takewhile;
1211using Vec = const std::vector<int >;
1312
14- namespace {
15- bool under_ten (int i) {
16- return i < 10 ;
17- }
18-
19- struct UnderTen {
20- bool operator ()(int i) {
21- return i < 10 ;
22- }
23- };
24- }
25-
2613TEST_CASE (" takewhile: works with lambda, callable, and function pointer" ,
2714 " [takewhile]" ) {
28- Vec ns = {1 , 3 , 5 , 20 , 2 , 4 , 6 , 8 };
15+ Vec ns = {1 , 3 , 4 , 20 , 2 , 4 , 6 , 8 };
16+ const Vec vc = {1 , 3 , 4 };
2917 SECTION (" function pointer" ) {
30- auto tw = takewhile (under_ten , ns);
18+ auto tw = takewhile (less_than_five , ns);
3119 Vec v (std::begin (tw), std::end (tw));
32- Vec vc = {1 , 3 , 5 };
3320 REQUIRE (v == vc);
3421 }
3522
3623 SECTION (" callable object" ) {
3724 std::vector<int > v;
3825 SECTION (" Normal call" ) {
39- auto tw = takewhile (UnderTen{ }, ns);
26+ auto tw = takewhile (LessThanValue{ 10 }, ns);
4027 v.assign (std::begin (tw), std::end (tw));
4128 }
4229
4330 SECTION (" Pipe" ) {
44- auto tw = ns | takewhile (UnderTen{ });
31+ auto tw = ns | takewhile (LessThanValue{ 10 });
4532 v.assign (std::begin (tw), std::end (tw));
4633 }
4734
48- Vec vc = {1 , 3 , 5 };
4935 REQUIRE (v == vc);
5036 }
5137
5238 SECTION (" lambda" ) {
5339 auto tw = takewhile ([](int i) { return i < 10 ; }, ns);
5440 Vec v (std::begin (tw), std::end (tw));
55- Vec vc = {1 , 3 , 5 };
5641 REQUIRE (v == vc);
5742 }
5843}
@@ -78,15 +63,15 @@ TEST_CASE("takewhile: handles pointer to member", "[takewhile]") {
7863
7964TEST_CASE (" takewhile: supports const iteration" , " [takewhile][const]" ) {
8065 Vec ns = {1 , 3 , 5 , 20 , 2 , 4 , 6 , 8 };
81- const auto tw = takewhile (UnderTen{ }, ns);
66+ const auto tw = takewhile (LessThanValue{ 10 }, ns);
8267 Vec v (std::begin (tw), std::end (tw));
8368 Vec vc = {1 , 3 , 5 };
8469 REQUIRE (v == vc);
8570}
8671
8772TEST_CASE (" takewhile: const iterator and non-const iterator are comparable" ,
8873 " [takewhile][const]" ) {
89- auto tw = takewhile (UnderTen{ }, Vec{});
74+ auto tw = takewhile (LessThanValue{ 10 }, Vec{});
9075 const auto & ctw = tw;
9176 (void )(std::begin (tw) == std::end (ctw));
9277}
@@ -117,14 +102,14 @@ TEST_CASE("takewhile: identity", "[takewhile]") {
117102
118103TEST_CASE (" takewhile: everything passes predicate" , " [takewhile]" ) {
119104 Vec ns{1 , 2 , 3 };
120- auto tw = takewhile (under_ten , ns);
105+ auto tw = takewhile (less_than_five , ns);
121106 Vec v (std::begin (tw), std::end (tw));
122107 Vec vc = {1 , 2 , 3 };
123108}
124109
125110TEST_CASE (" takewhile: empty iterable is empty" , " [takewhile]" ) {
126111 Vec ns{};
127- auto tw = takewhile (under_ten , ns);
112+ auto tw = takewhile (less_than_five , ns);
128113 SECTION (" normal compare" ) {
129114 REQUIRE (std::begin (tw) == std::end (tw));
130115 }
@@ -138,7 +123,7 @@ TEST_CASE(
138123 " [takewhile]" ) {
139124 SECTION (" First element is only element" ) {
140125 Vec ns = {20 };
141- auto tw = takewhile (under_ten , ns);
126+ auto tw = takewhile (less_than_five , ns);
142127 SECTION (" normal compare" ) {
143128 REQUIRE (std::begin (tw) == std::end (tw));
144129 }
@@ -149,7 +134,7 @@ TEST_CASE(
149134
150135 SECTION (" First element followed by elements that pass" ) {
151136 Vec ns = {20 , 1 , 1 };
152- auto tw = takewhile (under_ten , ns);
137+ auto tw = takewhile (less_than_five , ns);
153138 SECTION (" normal compare" ) {
154139 REQUIRE (std::begin (tw) == std::end (tw));
155140 }
@@ -161,10 +146,10 @@ TEST_CASE(
161146
162147TEST_CASE (" takewhile: moves rvalues, binds to lvalues" , " [takewhile]" ) {
163148 itertest::BasicIterable<int > bi{1 , 2 };
164- takewhile (under_ten , bi);
149+ takewhile (less_than_five , bi);
165150 REQUIRE_FALSE (bi.was_moved_from ());
166151
167- takewhile (under_ten , std::move (bi));
152+ takewhile (less_than_five , std::move (bi));
168153 REQUIRE (bi.was_moved_from ());
169154}
170155
0 commit comments