forked from facebookexperimental/libunifex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_stream_test.cpp
178 lines (141 loc) · 4.61 KB
/
filter_stream_test.cpp
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
#include <unifex/filter_stream.hpp>
#include <unifex/for_each.hpp>
#include <unifex/just_void_or_done.hpp>
#include <unifex/range_stream.hpp>
#include <unifex/reduce_stream.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/then.hpp>
#include <unifex/trampoline_scheduler.hpp>
#include <unifex/transform_stream.hpp>
#include <unifex/via_stream.hpp>
#include <gtest/gtest.h>
#include <array>
#include <memory>
#include <vector>
using namespace unifex;
TEST(filter_stream, StepByStep) {
auto ints = range_stream{1, 11};
auto evens =
filter_stream(std::move(ints), [](int val) { return val % 2 == 0; });
auto sum = reduce_stream(
std::move(evens), 0, [](int state, int val) { return state + val; });
auto res = (sync_wait(std::move(sum)));
ASSERT_TRUE(res);
EXPECT_EQ(30, *res);
}
TEST(filter_stream, Composition) {
auto res = sync_wait(reduce_stream(
filter_stream(range_stream{1, 11}, [](int val) { return val % 2 == 0; }),
0,
[](int state, int val) { return state + val; }));
ASSERT_TRUE(res);
EXPECT_EQ(30, *res);
}
TEST(filter_stream, Pipeable) {
auto res = range_stream{1, 11} |
filter_stream([](int val) { return val % 2 == 0; }) |
reduce_stream(0, [](int state, int val) { return state + val; }) |
sync_wait();
ASSERT_TRUE(res);
EXPECT_EQ(30, *res);
}
TEST(filter_stream, FilterFuncThrows) {
auto st = range_stream{1, 11} | filter_stream([](int) -> bool { throw 42; });
EXPECT_THROW(sync_wait(next(st)), int);
}
struct ThrowingStream {
auto next() {
// Throw in the 2nd iteration
if (++i == 2) {
throw 42;
}
return unifex::next(underlyingStream_);
}
auto cleanup() { return unifex::cleanup(underlyingStream_); }
range_stream underlyingStream_{1, 10};
size_t i = 0;
};
TEST(filter_stream, StreamNextSenderThrows) {
auto st = ThrowingStream{} | filter_stream([](auto&&) { return true; });
// first iteration doesn't throw
EXPECT_EQ(1, sync_wait(next(st)));
// second iteration throws
EXPECT_THROW(sync_wait(next(st)), int);
}
// I tried to use "mock_receiver.hpp", but it seems gmock/gmock.h
// isn't available in my set up. Below is a simple way to verify
// set_error() is being called
struct ThrowingReceiver {
template <typename V>
void set_value(V&&) {
throw 42;
}
void set_done() noexcept {}
template <typename E>
void set_error(E&&) noexcept {
errorCalled_ = true;
}
bool& errorCalled_;
};
TEST(filter_stream, ConnectedReceiverThrowsOnSetValue) {
auto st = range_stream{1, 11} |
filter_stream([](int val) -> bool { return val % 2 == 0; });
auto nextSender = next(st);
bool errorCalled = false;
auto rec = ThrowingReceiver{errorCalled};
auto op = unifex::connect(nextSender, rec);
unifex::start(op);
EXPECT_TRUE(errorCalled);
}
struct StreamOfMoveOnlyObjects {
StreamOfMoveOnlyObjects() {
pointers_.emplace_back(std::make_unique<int>(1));
pointers_.emplace_back(nullptr);
pointers_.emplace_back(nullptr);
pointers_.emplace_back(std::make_unique<int>(2));
}
auto next() {
return just_void_or_done(curr_ < pointers_.size()) |
then([this]() mutable noexcept {
return std::move(pointers_[curr_++]);
});
}
auto cleanup() { return just_done(); }
std::vector<std::unique_ptr<int>> pointers_{};
size_t curr_ = 0;
};
TEST(filter_stream, MoveOnlyObjects) {
auto sumOfNonNulls = StreamOfMoveOnlyObjects{} |
filter_stream([](auto&& ptr) { return ptr != nullptr; }) |
reduce_stream(0, [](int state, auto&& ptr) { return state + *ptr; }) |
sync_wait();
ASSERT_TRUE(sumOfNonNulls);
EXPECT_EQ(3, *sumOfNonNulls);
}
TEST(filter_stream, StreamOfReferences) {
std::array<int, 5> ints{1, 2, 3, 4, 5};
auto res = range_stream{0, 4} |
transform_stream([&](int idx) -> int& { return ints[idx]; }) |
filter_stream([](int val) { return val % 2 == 0; }) |
transform_stream([&](int& val) {
// ensuring we're propagating the referenceness correctly
if (val == 2) {
EXPECT_TRUE(&val == &ints[1]);
} else if (val == 4) {
EXPECT_TRUE(&val == &ints[3]);
}
return val;
}) |
reduce_stream(0, [](int state, int val) { return state + val; }) |
sync_wait();
ASSERT_TRUE(res);
EXPECT_EQ(6, *res);
}
TEST(filter_stream, StackExhaustion) {
auto res = range_stream{1, 100'000} | via_stream(trampoline_scheduler{}) |
filter_stream([](int) { return false; }) |
reduce_stream(0, [](int state, int val) { return state + val; }) |
sync_wait();
ASSERT_TRUE(res);
EXPECT_EQ(0, *res);
}