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
122 lines (100 loc) · 3.03 KB
/
Copy pathtest_starmap.cpp
File metadata and controls
122 lines (100 loc) · 3.03 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
#include <imap.hpp>
#include "helpers.hpp"
#include <vector>
#include <list>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::starmap;
namespace {
long f(long d, int i) {
return d * i;
}
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()(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<double, 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: list of tuples", "[starmap]") {
using Vec = const std::vector<std::string>;
using T = std::tuple<std::string, int, double>;
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([](long 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);
}