-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathtest_dropwhile.cpp
More file actions
222 lines (195 loc) · 6.3 KB
/
Copy pathtest_dropwhile.cpp
File metadata and controls
222 lines (195 loc) · 6.3 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <cppitertools/dropwhile.hpp>
#include <iterator>
#include <string>
#include <vector>
#include "catch.hpp"
#include "helpers.hpp"
using iter::dropwhile;
using Vec = const std::vector<int>;
TEST_CASE("dropwhile: handles different callable types", "[dropwhile]") {
Vec ns = {1, 3, 4, 20, 2, 4, 6, 8};
Vec vc = {20, 2, 4, 6, 8};
std::vector<int> v;
SECTION("with function pointer") {
auto d = dropwhile(less_than_five, ns);
v = Vec(std::begin(d), std::end(d));
}
SECTION("with callable object") {
auto d = dropwhile(LessThanValue{5}, ns);
v = Vec(std::begin(d), std::end(d));
}
SECTION("with lvalue callable object") {
auto lt = LessThanValue{5};
SECTION("normal call") {
auto d = dropwhile(lt, ns);
v = Vec(std::begin(d), std::end(d));
}
SECTION("pipe") {
auto d = ns | dropwhile(lt);
v = Vec(std::begin(d), std::end(d));
}
}
SECTION("with move-only callable object") {
SECTION("normal call") {
auto d = dropwhile(MoveOnlyLessThanValue{5}, ns);
v = Vec(std::begin(d), std::end(d));
}
SECTION("pipe") {
auto d = ns | dropwhile(MoveOnlyLessThanValue{5});
v = Vec(std::begin(d), std::end(d));
}
}
SECTION("with lambda") {
auto ltf = [](int i) { return i < 5; };
auto d = dropwhile(ltf, ns);
v = Vec(std::begin(d), std::end(d));
}
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: skips initial elements", "[dropwhile]") {
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
std::vector<int> v;
SECTION("Normal call") {
auto d = dropwhile([](int i) { return i < 5; }, ns);
v.assign(std::begin(d), std::end(d));
}
SECTION("Pipe") {
auto d = ns | dropwhile([](int i) { return i < 5; });
v.assign(std::begin(d), std::end(d));
}
Vec vc = {5, 6, 7, 8};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: handles pointer to member", "[dropwhile]") {
using itertest::Point;
const std::vector<Point> ps = {
{5, 0}, {3, 5}, {2, 1}, {0, 1}, {2, 2}, {6, 0}};
std::vector<Point> v;
SECTION("with pointer to data member") {
auto dw = dropwhile(&Point::x, ps);
v.assign(std::begin(dw), std::end(dw));
}
SECTION("with pointer to member function") {
auto dw = dropwhile(&Point::get_x, ps);
v.assign(std::begin(dw), std::end(dw));
}
const std::vector<Point> vc = {{0, 1}, {2, 2}, {6, 0}};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: drop zeros from front", "[dropwhile]") {
using itertest::Integer;
const std::vector<Integer> nums = {0, 0, 3, 4, 0, 5, 0};
auto dw = dropwhile(&Integer::is_zero, nums);
const std::vector<Integer> v(std::begin(dw), std::end(dw));
const std::vector<Integer> vc = {3, 4, 0, 5, 0};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: const iteration", "[dropwhile][const]") {
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
const auto d = dropwhile(LessThanValue{5}, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5, 6, 7, 8};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: const iterators can be compared to non-const iterators",
"[dropwhile][const]") {
auto d = dropwhile(LessThanValue{5}, Vec{});
const auto& cd = d;
(void)(std::begin(d) == std::end(cd));
}
TEST_CASE(
"dropwhile: Works with different begin and end types", "[dropwhile]") {
CharRange cr{'f'};
auto d = dropwhile([](char c) { return c < 'c'; }, cr);
Vec v(d.begin(), d.end());
Vec vc{'c', 'd', 'e'};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: doesn't skip anything if it shouldn't", "[dropwhile]") {
Vec ns{3, 4, 5, 6};
auto d = dropwhile([](int i) { return i < 3; }, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {3, 4, 5, 6};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: skips all elements when all are true under predicate",
"[dropwhile]") {
Vec ns{3, 4, 5, 6};
auto d = dropwhile([](int i) { return i != 0; }, ns);
SECTION("normal compare") {
REQUIRE(std::begin(d) == std::end(d));
}
SECTION("reversed compare") {
REQUIRE(std::end(d) == std::begin(d));
}
}
TEST_CASE("dropwhile: identity", "[dropwhile]") {
Vec ns{1, 2, 0, 3, 1, 0};
auto d = dropwhile(ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {0, 3, 1, 0};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: empty case is empty", "[dropwhile]") {
Vec ns{};
auto d = dropwhile([](int i) { return i != 0; }, ns);
SECTION("normal compare") {
REQUIRE(std::begin(d) == std::end(d));
}
SECTION("reversed compare") {
REQUIRE(std::end(d) == std::begin(d));
}
}
TEST_CASE("dropwhile: only drops from beginning", "[dropwhile]") {
Vec ns{1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
auto d = dropwhile([](int i) { return i < 5; }, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5, 6, 5, 4, 3, 2, 1};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: operator->", "[dropwhile]") {
std::vector<std::string> vs = {"a", "ab", "abcdef", "abcdefghi"};
auto d = dropwhile([](const std::string& str) { return str.size() < 3; }, vs);
auto it = std::begin(d);
REQUIRE(it->size() == 6);
}
TEST_CASE("dropwhile: works with function pointer", "[dropwhile]") {
Vec ns{1, 2, 3, 4, 5, 6, 7, 8};
auto d = dropwhile(less_than_five, ns);
Vec v(std::begin(d), std::end(d));
Vec vc = {5, 6, 7, 8};
REQUIRE(v == vc);
}
TEST_CASE("dropwhile: binds to lvalues, moves rvalues", "[dropwhile]") {
itertest::BasicIterable<int> bi{1, 2, 3, 4};
SECTION("binds to lvalues") {
dropwhile(less_than_five, bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
dropwhile(less_than_five, std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE(
"dropwhile: doesn't move or copy elements of iterable", "[dropwhile]") {
constexpr itertest::SolidInt arr[] = {{6}, {7}, {8}};
for (auto&& i :
dropwhile([](const itertest::SolidInt&) { return false; }, arr)) {
(void)i;
}
}
TEST_CASE("dropwhile: iterator meets requirements", "[dropwhile]") {
std::string s{};
auto c = dropwhile([] { return true; }, s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename T, typename U>
using ImpT = decltype(dropwhile(std::declval<T>(), std::declval<U>()));
TEST_CASE("dropwhile: has correct ctor and assign ops", "[dropwhile]") {
using T1 = ImpT<bool (*)(char c), std::string&>;
auto lam = [](char) { return false; };
using T2 = ImpT<decltype(lam), std::string>;
REQUIRE(itertest::IsMoveConstructibleOnly<T1>::value);
REQUIRE(itertest::IsMoveConstructibleOnly<T2>::value);
}