-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_state.cpp
More file actions
202 lines (168 loc) · 5.32 KB
/
Copy pathtest_state.cpp
File metadata and controls
202 lines (168 loc) · 5.32 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
#include "state.h"
#include "catch.hpp"
namespace stde = std::experimental;
struct random_state {
int next_value;
friend bool operator==(random_state lhs, random_state rhs) {
return lhs.next_value == rhs.next_value;
}
};
template <typename R, typename... A>
struct OneShotFunction {
struct IInvoker {
virtual ~IInvoker() = default;
virtual R invoke(A...) && = 0;
};
template <typename F>
struct Invoker : IInvoker {
F f;
template <typename G>
Invoker(G&& f) : f(std::forward<G>(f)) {}
R invoke(A... args) && override { return std::move(f)(args...); }
};
std::shared_ptr<IInvoker> invoker;
template <typename F>
OneShotFunction(F&& f)
: invoker(std::make_shared<Invoker<std::remove_cvref_t<F>>>(
std::forward<F>(f))) {}
auto operator()(A... args) && { return std::move(*invoker).invoke(args...); }
};
struct OneShotFunctionTC {
template <typename R, typename... A>
using invoke = OneShotFunction<R, A...>;
};
struct StdFunctionTC {
template <typename R, typename... A>
using invoke = std::function<R(A...)>;
};
namespace std::experimental::type_constructible {
template <>
struct traits<StdFunctionTC> {
template <typename R, typename... A, typename F>
static auto make(F&& f) {
return std::function<R(A...)>(std::forward<F>(f));
}
};
} // namespace std::experimental::type_constructible
using MyState = toby::state::StateTC<StdFunctionTC, random_state>;
using MyStateOneShot = toby::state::StateTC<OneShotFunctionTC, random_state>;
auto random(random_state s) -> std::pair<double, random_state> {
return {static_cast<double>(s.next_value), {s.next_value + 1}};
}
MyState::t<double> const next_random = MyState::get >>= [](auto&& rs) {
auto _ = random(rs);
auto&& v = _.first;
auto&& rs2 = _.second;
return MyState::put(rs2) >>= [=](auto&&) { return MyState::pure(v); };
};
auto const next_random_co = []() -> MyStateOneShot::t<double> {
auto rs = co_await MyState::get;
auto [v, rs2] = random(rs);
auto _ = co_await MyState::put(rs2);
co_return v;
};
TEST_CASE("pure raw") {
auto r = toby::state::pure(2.3).run(random_state{7});
CHECK(r.data == 2.3);
CHECK(r.state == random_state{7});
}
TEST_CASE("pure type-erased") {
auto r = MyState::pure(2.3).run({7});
CHECK(r.data == 2.3);
CHECK(r.state == random_state{7});
}
TEST_CASE("get raw") {
CHECK(std::is_const_v<decltype(toby::state::get)>);
auto r = toby::state::get.run(random_state{7});
CHECK(r.data == random_state{7});
CHECK(r.state == random_state{7});
}
TEST_CASE("get type-erased") {
auto r = MyState::get.run({7});
CHECK(r.data == random_state{7});
CHECK(r.state == random_state{7});
}
TEST_CASE("put raw") {
auto r = toby::state::put(random_state{42}).run(random_state{7});
CHECK(r.data == toby::state::unit{});
CHECK(r.state == random_state{42});
}
TEST_CASE("put type-erased") {
auto r = MyState::put(random_state{42}).run({7});
CHECK(r.data == toby::state::unit{});
CHECK(r.state == random_state{42});
}
TEST_CASE("fmap raw") {
auto st = toby::state::pure(4.2);
auto st2 =
toby::state::transform(st, [](auto x) { return std::to_string(x); });
auto r = st2.run(7);
CHECK(r.data == std::to_string(4.2));
CHECK(r.state == 7);
}
TEST_CASE("fmap type-erased") {
auto st = toby::state::StateTC<StdFunctionTC, int>::pure(4.2);
auto st2 =
stde::functor::transform(st, [](auto x) { return std::to_string(x); });
auto r = st2.run(7);
CHECK(r.data == std::to_string(4.2));
CHECK(r.state == 7);
}
TEST_CASE("next_random") {
auto r = next_random.run({7});
CHECK(r.data == 7.0);
CHECK(r.state == random_state{8});
}
TEST_CASE("next_random transform") {
auto st =
stde::functor::transform(next_random, [](auto&& arg) { return arg * 2; });
auto r = st.run({7});
CHECK(r.data == 14.0);
CHECK(r.state == random_state{8});
}
TEST_CASE("next_random_co") {
auto st = next_random_co();
auto r = std::move(st).run({7});
CHECK(r.data == 7.0);
CHECK(r.state == random_state{8});
// check that creating it again is independent
auto st2 = next_random_co();
r = std::move(st2).run({8});
CHECK(r.data == 8.0);
CHECK(r.state == random_state{9});
}
TEST_CASE("next_random_co transform") {
auto st = stde::functor::transform(next_random_co(),
[](auto&& arg) { return arg * 2; });
auto r = std::move(st).run({7});
CHECK(r.data == 14.0);
CHECK(r.state == random_state{8});
}
TEST_CASE("running a coroutine-based state twice throws an exception") {
auto st = next_random_co();
auto r = std::move(st).run({7});
CHECK_THROWS(std::move(st).run({7}));
}
TEST_CASE("next_random_thrice") {
auto st = next_random >>= [](auto x) {
return next_random >>= [=](auto y) {
return next_random >>= [=](auto z) {
return MyState::pure(std::tuple{x, y, z});
};
};
};
auto r = st.run({7});
CHECK(r.data == std::make_tuple(7.0, 8.0, 9.0));
CHECK(r.state == random_state{10});
}
TEST_CASE("next_random_co_thrice") {
auto st = []() -> MyStateOneShot::t<std::tuple<double, double, double>> {
auto x = co_await next_random_co();
auto y = co_await next_random_co();
auto z = co_await next_random_co();
co_return std::make_tuple(x, y, z);
}();
auto r = std::move(st).run({7});
CHECK(r.data == std::make_tuple(7.0, 8.0, 9.0));
CHECK(r.state == random_state{10});
}