forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mixed.cpp
More file actions
123 lines (93 loc) · 3.05 KB
/
Copy pathtest_mixed.cpp
File metadata and controls
123 lines (93 loc) · 3.05 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
// mixing different itertools, there is nothing called iter::mixed()
#include "itertools.hpp"
#include "catch.hpp"
#include <iostream>
#include <vector>
class MyUnMovable {
int val;
public:
constexpr MyUnMovable(int val) : val{val} {}
MyUnMovable(const MyUnMovable&) = delete;
MyUnMovable& operator=(const MyUnMovable&) = delete;
MyUnMovable(MyUnMovable&& other) : val{other.val} {}
constexpr int get_val() const {
return val;
}
void set_val(int val) {
this->val = val;
}
bool operator==(const MyUnMovable& other) const {
return this->val == other.val;
}
bool operator!=(const MyUnMovable& other) const {
return !(*this == other);
}
};
namespace {
auto inc_ten = [](MyUnMovable& el) -> MyUnMovable& {
int va = el.get_val();
el.set_val(va + 10);
return el;
};
auto dec_ten = [](MyUnMovable& el) -> MyUnMovable& {
int va = el.get_val();
el.set_val(va - 10);
return el;
};
}
TEST_CASE("filtering doesn't dereference multiple times", "[imap][filter]") {
using iter::filter;
using iter::imap;
// source data
std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}};
auto transformed1 = imap(inc_ten, arr);
auto filtered = filter(
[](const MyUnMovable& el) { return 52 != el.get_val(); }, transformed1);
auto transformed2 = imap(dec_ten, filtered);
std::vector<int> v;
for (auto&& el : transformed2) {
// I would use imap again instead of the loop if this wasn't an imap
// test
v.push_back(el.get_val());
}
std::vector<int> vc = {41, 43};
REQUIRE(v == vc);
constexpr std::array<MyUnMovable, 3> arrc = {{{41}, {52}, {43}}};
REQUIRE(arr == arrc);
}
TEST_CASE("dropwhile doesn't dereference multiple times", "[imap][dropwhile]") {
using iter::imap;
using iter::dropwhile;
std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}};
auto transformed1 = imap(inc_ten, arr);
auto filtered = dropwhile(
[](const MyUnMovable& el) { return 52 != el.get_val(); }, transformed1);
auto transformed2 = imap(dec_ten, filtered);
std::vector<int> v;
for (auto&& el : transformed2) {
v.push_back(el.get_val());
}
std::vector<int> vc = {42, 43};
std::vector<int> vsc = {51, 42, 43};
auto get_vals = imap([](const MyUnMovable& mv) { return mv.get_val(); }, arr);
std::vector<int> vs(std::begin(get_vals), std::end(get_vals));
REQUIRE(vs == vsc);
}
TEST_CASE("takewhile doesn't dereference multiple times", "[imap][takewhile]") {
using iter::imap;
using iter::takewhile;
std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}};
auto transformed1 = imap(inc_ten, arr);
auto filtered = takewhile(
[](const MyUnMovable& el) { return 53 != el.get_val(); }, transformed1);
auto transformed2 = imap(dec_ten, filtered);
std::vector<int> v;
for (auto&& el : transformed2) {
v.push_back(el.get_val());
}
std::vector<int> vc = {41, 42};
std::vector<int> vsc = {41, 42, 53};
auto get_vals = imap([](const MyUnMovable& mv) { return mv.get_val(); }, arr);
std::vector<int> vs(std::begin(get_vals), std::end(get_vals));
REQUIRE(vs == vsc);
}