forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_filter.cpp
More file actions
125 lines (98 loc) · 3.01 KB
/
Copy pathtest_filter.cpp
File metadata and controls
125 lines (98 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <filter.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::filter;
using Vec = const std::vector<int>;
namespace {
bool less_than_five(int i) {
return i < 5;
}
class LessThanValue {
private:
int compare_val;
public:
LessThanValue(int v) : compare_val(v) { }
bool operator() (int i) {
return i < this->compare_val;
}
};
}
TEST_CASE("filter: handles different functor types", "[filter]") {
Vec ns = {1,2, 5,6, 3,1, 7, -1, 5};
Vec vc = {1,2,3,1,-1};
SECTION("with function pointer") {
auto f = filter(less_than_five, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );
}
SECTION("with callable object") {
auto f = filter(LessThanValue{5}, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );
}
SECTION("with lambda") {
auto ltf = [](int i) {return i < 5;};
auto f = filter(ltf, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );
}
}
TEST_CASE("filter: iterator with lambda can be assigned", "[filter]") {
Vec ns{};
auto ltf = [](int i) {return i < 5;};
auto f = filter(ltf, ns);
auto it = std::begin(f);
it = std::begin(f);
}
TEST_CASE("filter: using identity", "[filter]") {
Vec ns{0, 1, 2, 0, 3, 0, 0, 0, 4, 5, 0};
auto f = filter(ns);
Vec v(std::begin(f), std::end(f));
Vec vc = {1,2,3,4,5};
REQUIRE( v == vc );
}
TEST_CASE("filter: binds to lvalues, moves rvales", "[filter]") {
itertest::BasicIterable<int> bi{1,2,3,4};
SECTION("one-arg binds to lvalues") {
filter(bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("two-arg binds to lvalues") {
filter(less_than_five, bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("one-arg moves rvalues") {
filter(std::move(bi));
REQUIRE(bi.was_moved_from());
}
SECTION("two-arg moves rvalues") {
filter(less_than_five, std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE("filter: operator->", "[filter]") {
std::vector<std::string> vs = {"ab", "abc", "abcdef"};
auto f = filter([](const std::string& str) {return str.size() > 4;}, vs);
auto it = std::begin(f);
REQUIRE( it->size() == 6 );
}
TEST_CASE("filter: all elements fail predicate", "[filter]") {
Vec ns{10,20,30,40,50};
auto f = filter(less_than_five, ns);
REQUIRE( std::begin(f) == std::end(f) );
}
TEST_CASE("filter: doesn't move or copy elements of iterable", "[filter]") {
constexpr itertest::SolidInt arr[] = {{1}, {0}, {2}};
for (auto&& i : filter(
[](const itertest::SolidInt& si) {return si.getint();},arr)) {
(void)i;
}
}
TEST_CASE("filter: iterator meets requirements", "[filter]") {
std::string s{};
auto c = filter([]{return true;}, s);
REQUIRE( itertest::IsIterator<decltype(std::begin(c))>::value );
}