forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_starmap.cpp
More file actions
230 lines (196 loc) · 6.56 KB
/
Copy pathtest_starmap.cpp
File metadata and controls
230 lines (196 loc) · 6.56 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
223
224
225
226
227
228
229
230
#include <cppitertools/starmap.hpp>
#include "helpers.hpp"
#include <iterator>
#include <list>
#include <string>
#include <type_traits>
#include <vector>
#include "catch.hpp"
using iter::starmap;
namespace {
long f(long d, int i) {
return d * i;
}
int& larger_ref(int& a, int& b) {
return a > b ? a : b;
}
const int& larger_const_ref(const int& a, const int& b) {
return a > b ? a : b;
}
int larger(int a, int b) {
return a > b ? a : b;
}
std::string g(const std::string& s, int i, char c) {
std::stringstream ss;
ss << s << ' ' << i << ' ' << c;
return ss.str();
}
struct Callable {
int operator()(int a, int b, int c) {
return a + b + c;
}
int operator()(double a, int b) {
return int(a + b);
}
int operator()(int a) {
return a;
}
};
}
TEST_CASE("starmap: works with function pointer and lambda", "[starmap]") {
using Vec = const std::vector<int>;
const std::vector<std::pair<long, int>> v1 = {{1l, 2}, {3l, 11}, {6l, 7}};
Vec vc = {2l, 33l, 42l};
std::vector<int> v;
SECTION("with function") {
SECTION("Normal call") {
auto sm = starmap(f, v1);
v.assign(std::begin(sm), std::end(sm));
}
SECTION("pipe") {
auto sm = v1 | starmap(f);
v.assign(std::begin(sm), std::end(sm));
}
}
SECTION("with lambda") {
auto sm = starmap([](long a, int b) { return a * b; }, v1);
v.assign(std::begin(sm), std::end(sm));
}
REQUIRE(v == vc);
}
TEST_CASE("starmap: works with pointer to member function", "[starmap]") {
using itertest::Point;
std::vector<std::pair<Point, std::string>> tup = {
{{10, 20}, "a"}, {{6, 8}, "point"}, {{3, 15}, "pos"}};
auto sm = starmap(&Point::prefix, tup);
std::vector<std::string> v(std::begin(sm), std::end(sm));
const std::vector<std::string> vc = {
"a(10, 20)", "point(6, 8)", "pos(3, 15)"};
REQUIRE(v == vc);
}
TEST_CASE("starmap: vector of pairs const iteration", "[starmap][const]") {
using Vec = const std::vector<int>;
const std::vector<std::pair<double, int>> v1 = {
{1.0, 2}, {3.0, 11}, {6.0, 7}};
const auto sm = starmap(Callable{}, v1);
std::vector<int> v(std::begin(sm), std::end(sm));
Vec vc = {3, 14, 13};
REQUIRE(v == vc);
}
TEST_CASE(
"starmap: vector of pairs const iterators can be compared to non-const "
"iterators",
"[starmap][const]") {
const std::vector<std::pair<double, int>> v1;
auto sm = starmap(Callable{}, v1);
const auto& csm = sm;
(void)(std::begin(sm) == std::end(csm));
}
TEST_CASE("starmap: Works with different begin and end types", "[starmap]") {
IntCharPairRange icr{{3, 'd'}};
using Vec = std::vector<std::string>;
auto sm = starmap([](int i, char c) { return std::to_string(i) + c; }, icr);
Vec v(sm.begin(), sm.end());
Vec vc{"0a", "1b", "2c"};
REQUIRE(v == vc);
}
TEST_CASE("starmap: tuple of tuples const iteration", "[starmap][const]") {
using Vec = const std::vector<int>;
auto tup = std::make_tuple(std::make_tuple(10, 19, 60), std::make_tuple(7));
const auto sm = starmap(Callable{}, tup);
Vec v(std::begin(sm), std::end(sm));
}
TEST_CASE(
"starmap: tuple of tuples const iterators can be compared to non-const "
"iterator",
"[starmap][const]") {
auto tup = std::make_tuple(std::make_tuple(10, 19, 60), std::make_tuple(7));
auto sm = starmap(Callable{}, tup);
const auto& csm = sm;
(void)(std::begin(sm) == std::end(csm));
(void)(std::begin(csm) == std::end(sm));
}
TEST_CASE("starmap: list of tuples", "[starmap]") {
using Vec = const std::vector<std::string>;
using T = std::tuple<std::string, int, char>;
std::list<T> li = {T{"hey", 42, 'a'}, T{"there", 3, 'b'}, T{"yall", 5, 'c'}};
auto sm = starmap(g, li);
Vec v(std::begin(sm), std::end(sm));
Vec vc = {"hey 42 a", "there 3 b", "yall 5 c"};
REQUIRE(v == vc);
}
TEST_CASE("starmap: tuple of tuples", "[starmap]") {
using Vec = const std::vector<int>;
auto tup = std::make_tuple(std::make_tuple(10, 19, 60), std::make_tuple(7));
std::vector<int> v;
SECTION("Normal call") {
auto sm = starmap(Callable{}, tup);
v.assign(std::begin(sm), std::end(sm));
}
SECTION("Pipe") {
auto sm = tup | starmap(Callable{});
v.assign(std::begin(sm), std::end(sm));
}
Vec vc = {89, 7};
REQUIRE(v == vc);
}
TEST_CASE("starmap: tuple of pairs", "[starmap]") {
using Vec = const std::vector<int>;
auto p =
std::make_pair(std::array<int, 3>{{15, 100, 2000}}, std::make_tuple(16));
Callable c;
auto sm = starmap(c, p);
Vec v(std::begin(sm), std::end(sm));
Vec vc = {2115, 16};
REQUIRE(v == vc);
}
TEST_CASE("starmap: moves rvalues, binds to lvalues", "[starmap]") {
itertest::BasicIterable<std::tuple<int>> bi{};
starmap(Callable{}, bi);
REQUIRE_FALSE(bi.was_moved_from());
starmap(Callable{}, std::move(bi));
REQUIRE(bi.was_moved_from());
}
TEST_CASE("starmap: iterator meets requirements", "[starmap]") {
std::string s{};
const std::vector<std::pair<double, int>> v1;
auto sm = starmap([](double a, int b) { return a * b; }, v1);
REQUIRE(itertest::IsIterator<decltype(std::begin(sm))>::value);
}
TEST_CASE(
"starmap: tuple of tuples iterator meets requirements", "[starmap]") {
auto tup = std::make_tuple(std::make_tuple(10, 19, 60), std::make_tuple(7));
auto sm = starmap(Callable{}, tup);
REQUIRE(itertest::IsIterator<decltype(std::begin(sm))>::value);
}
TEST_CASE("starmap: iterator dereference type matches 'reference' type alias",
"[starmap]") {
std::vector<std::pair<int, int>> input;
SECTION("with reference return type") {
auto sm = iter::starmap(larger_ref, input);
REQUIRE(itertest::ReferenceMatchesDeref<decltype(std::begin(sm))>::value);
}
SECTION("with const reference return type") {
auto sm = iter::starmap(larger_const_ref, input);
REQUIRE(itertest::ReferenceMatchesDeref<decltype(std::begin(sm))>::value);
}
SECTION("with value return type") {
auto sm = iter::starmap(larger, input);
REQUIRE(itertest::ReferenceMatchesDeref<decltype(std::begin(sm))>::value);
}
}
TEST_CASE("starmap: iterator has correct 'value' type alias", "[starmap]") {
std::vector<std::pair<int, int>> input;
SECTION("with reference return type") {
auto sm = iter::starmap(larger_ref, input);
REQUIRE(std::is_same_v<int, decltype(sm.begin())::value_type>);
}
SECTION("with const reference return type") {
auto sm = iter::starmap(larger_const_ref, input);
REQUIRE(std::is_same_v<int, decltype(sm.begin())::value_type>);
}
SECTION("with value return type") {
auto sm = iter::starmap(larger, input);
REQUIRE(std::is_same_v<int, decltype(sm.begin())::value_type>);
}
}