Skip to content

Commit fa6ee1a

Browse files
author
Ryan Haining
committed
Moves utility callables into helpers.hpp
Cleanup while doing #89
1 parent d4c7340 commit fa6ee1a

File tree

5 files changed

+33
-94
lines changed

5 files changed

+33
-94
lines changed

test/helpers.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,20 @@ class IntCharPairRange
414414
: DiffEndRange<std::pair<int, char>, IncIntCharPair>({0, 'a'}, stop) {}
415415
};
416416

417+
inline bool less_than_five(int i) {
418+
return i < 5;
419+
}
420+
421+
class LessThanValue {
422+
private:
423+
int compare_val;
424+
425+
public:
426+
LessThanValue(int v) : compare_val(v) {}
427+
428+
bool operator()(int i) {
429+
return i < this->compare_val;
430+
}
431+
};
432+
417433
#endif

test/test_dropwhile.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
11
#include <cppitertools/dropwhile.hpp>
2-
3-
#include "helpers.hpp"
4-
52
#include <iterator>
63
#include <string>
74
#include <vector>
85

96
#include "catch.hpp"
7+
#include "helpers.hpp"
108

119
using iter::dropwhile;
1210

1311
using Vec = const std::vector<int>;
1412

15-
namespace {
16-
class LessThanValue {
17-
private:
18-
int compare_val;
19-
20-
public:
21-
LessThanValue(int v) : compare_val(v) {}
22-
23-
bool operator()(int i) {
24-
return i < this->compare_val;
25-
}
26-
};
27-
}
28-
2913
TEST_CASE("dropwhile: skips initial elements", "[dropwhile]") {
3014
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
3115
std::vector<int> v;
@@ -147,12 +131,6 @@ TEST_CASE("dropwhile: operator->", "[dropwhile]") {
147131
REQUIRE(it->size() == 6);
148132
}
149133

150-
namespace {
151-
int less_than_five(int i) {
152-
return i < 5;
153-
}
154-
}
155-
156134
TEST_CASE("dropwhile: works with function pointer", "[dropwhile]") {
157135
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
158136
auto d = dropwhile(less_than_five, ns);

test/test_filter.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
#include <cppitertools/filter.hpp>
2-
3-
#include "helpers.hpp"
4-
52
#include <iterator>
63
#include <string>
74
#include <vector>
85

96
#include "catch.hpp"
7+
#include "helpers.hpp"
108

119
using iter::filter;
1210

1311
using Vec = const std::vector<int>;
1412

15-
namespace {
16-
bool less_than_five(int i) {
17-
return i < 5;
18-
}
19-
20-
class LessThanValue {
21-
private:
22-
int compare_val;
23-
24-
public:
25-
LessThanValue(int v) : compare_val(v) {}
26-
27-
bool operator()(int i) {
28-
return i < this->compare_val;
29-
}
30-
};
31-
}
32-
3313
TEST_CASE("filter: handles different callable types", "[filter]") {
3414
Vec ns = {1, 2, 5, 6, 3, 1, 7, -1, 5};
3515
Vec vc = {1, 2, 3, 1, -1};

test/test_filterfalse.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
#include <cppitertools/filterfalse.hpp>
2-
3-
#include "helpers.hpp"
4-
52
#include <iterator>
63
#include <string>
74
#include <vector>
85

96
#include "catch.hpp"
7+
#include "helpers.hpp"
108

119
using iter::filterfalse;
1210

1311
using Vec = const std::vector<int>;
1412

15-
namespace {
16-
bool less_than_five(int i) {
17-
return i < 5;
18-
}
19-
20-
class LessThanValue {
21-
private:
22-
int compare_val;
23-
24-
public:
25-
LessThanValue(int v) : compare_val(v) {}
26-
27-
bool operator()(int i) {
28-
return i < this->compare_val;
29-
}
30-
};
31-
}
32-
3313
TEST_CASE("filterfalse: handles different callable types", "[filterfalse]") {
3414
Vec ns = {1, 2, 5, 6, 3, 1, 7, -1, 5};
3515
Vec vc = {5, 6, 7, 5};

test/test_takewhile.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include <cppitertools/takewhile.hpp>
2-
31
#include <array>
2+
#include <cppitertools/takewhile.hpp>
43
#include <string>
54
#include <utility>
65
#include <vector>
@@ -11,48 +10,34 @@
1110
using iter::takewhile;
1211
using 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-
2613
TEST_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

7964
TEST_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

8772
TEST_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

118103
TEST_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

125110
TEST_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

162147
TEST_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

Comments
 (0)