forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_imap.cpp
More file actions
156 lines (132 loc) · 3.65 KB
/
Copy pathtest_imap.cpp
File metadata and controls
156 lines (132 loc) · 3.65 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
#include <imap.hpp>
#include "helpers.hpp"
#include <vector>
#include <string>
#include <iterator>
#include "catch.hpp"
using iter::imap;
using Vec = const std::vector<int>;
namespace {
int plusone(int i) {
return i + 1;
}
class PlusOner {
public:
int operator()(int i) {
return i + 1;
}
};
int power(int b, int e) {
int acc = 1;
for (int i = 0; i < e; ++i) {
acc *= b;
}
return acc;
}
}
TEST_CASE("imap: works with lambda, callable, and function", "[imap]") {
Vec ns = {10, 20, 30};
std::vector<int> v;
SECTION("with lambda") {
auto im = imap([](int i) { return i + 1; }, ns);
v.assign(std::begin(im), std::end(im));
}
SECTION("with callable") {
SECTION("Normal call") {
auto im = imap(PlusOner{}, ns);
v.assign(std::begin(im), std::end(im));
}
SECTION("Pipe") {
auto im = ns | imap(PlusOner{});
v.assign(std::begin(im), std::end(im));
}
}
SECTION("with function") {
auto im = imap(PlusOner{}, ns);
v.assign(std::begin(im), std::end(im));
}
Vec vc = {11, 21, 31};
REQUIRE(v == vc);
}
TEST_CASE("imap: works with multiple sequences", "[imap]") {
Vec bases = {0, 1, 2, 3};
Vec exps = {1, 2, 3, 4};
auto im = imap(power, bases, exps);
Vec v(std::begin(im), std::end(im));
Vec vc = {0, 1, 8, 81};
REQUIRE(v == vc);
}
TEST_CASE("imap: terminates on shortest squence", "[imap]") {
Vec ns1 = {1, 2, 3, 4};
Vec ns2 = {2, 4, 6, 8, 10};
Vec vc = {3, 6, 9, 12};
SECTION("shortest sequence first") {
auto im = imap([](int a, int b) { return a + b; }, ns1, ns2);
Vec v(std::begin(im), std::end(im));
REQUIRE(v == vc);
}
SECTION("shortest sequence second") {
auto im = imap([](int a, int b) { return a + b; }, ns2, ns1);
Vec v(std::begin(im), std::end(im));
REQUIRE(v == vc);
}
}
TEST_CASE("imap: operator->", "[imap]") {
std::vector<std::string> vs = {"ab", "abcd", "abcdefg"};
{
auto m = imap([](std::string& s) { return s; }, vs);
auto it = std::begin(m);
REQUIRE(it->size() == 2);
}
{
auto m = imap([](std::string& s) -> std::string& { return s; }, vs);
auto it = std::begin(m);
REQUIRE(it->size() == 2);
}
}
TEST_CASE("imap: empty sequence gives nothing", "[imap]") {
Vec v{};
auto im = imap(plusone, v);
REQUIRE(std::begin(im) == std::end(im));
}
TEST_CASE("imap: binds to lvalues, moves rvalues", "[imap]") {
itertest::BasicIterable<int> bi{1, 2};
SECTION("binds to lvalues") {
imap(plusone, bi);
REQUIRE_FALSE(bi.was_moved_from());
}
SECTION("moves rvalues") {
imap(plusone, std::move(bi));
REQUIRE(bi.was_moved_from());
}
}
TEST_CASE("imap: doesn't move or copy elements of iterable", "[imap]") {
constexpr itertest::SolidInt arr[] = {{1}, {0}, {2}};
for (auto&& i :
imap([](const itertest::SolidInt& si) { return si.getint(); }, arr)) {
(void)i;
}
}
TEST_CASE("imap: postfix ++", "[imap]") {
Vec ns = {10, 20};
auto im = imap(plusone, ns);
auto it = std::begin(im);
it++;
REQUIRE((*it) == 21);
it++;
REQUIRE(it == std::end(im));
}
TEST_CASE("imap: iterator meets requirements", "[imap]") {
std::string s{};
auto c = imap([](char) { return 1; }, s);
REQUIRE(itertest::IsIterator<decltype(std::begin(c))>::value);
}
template <typename T, typename U>
using ImpT = decltype(imap(std::declval<T>(), std::declval<U>()));
TEST_CASE("imap: has correct ctor and assign ops", "[imap]") {
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);
}