-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_functional.cpp
215 lines (197 loc) · 6 KB
/
test_functional.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <functional>
#include <math.h>
#include "assertion.h"
#include "task.h"
#include "functional.h"
using namespace std;
using namespace kaiu;
using namespace kaiu::functional_chain;
Assertions assert({
{ nullptr, "Basic currying" },
{ "BR", "R-value curried" },
{ "BL", "L-value curried" },
{ nullptr, "Cross-thread currying via apply" },
{ "ACR", "R-value curried by value" },
{ "ACL", "L-value curried by reference" },
{ nullptr, "Cross-thread currying via operator()" },
{ "AOCR", "R-value curried by value" },
{ "AOCL", "L-value curried by reference" },
{ nullptr, "Cross-thread currying via operator <<" },
{ "SHCR", "R-value curried by value" },
{ "SHCL", "L-value curried by value" },
{ "SHCWL", "Reference-wrapped L-value curried by reference" },
{ nullptr, "Modads" },
{ "MONADS", "Synchronous monad chain (horrible, never use in production)" },
{ "MONADA", "Asynchronous cross-thread monad chain" },
});
int sqr__(int x)
{
return x * x;
}
/*
* ╕╥╠▒╬▓▄
* ¬≈╫██████Mù,▄▄ .;░╥úúë;▒▒▒▒╦╥ ²
* ╩ ╙╫▒╨╢██╥/-╠█╩▒╩▒▒▒▒▓╗▄µ░ú▒▒╥╦▒▓▒▒▒▒▒▒╫▓▄
* ╙╬ ╨╫██▒▒▒╫█═▓▒▒▒▓▓▓▓▓▒▒▒▒╫╫▒▒▒▒▒▒▒▒▒▒▓█
* ▌ ▒╫█▓▒╫╫▒▒▒╫████████▓▓▒▒▒╫▒╫▒╬▓▓▒▒▓██
* ╫ █▒▓▓▓▒▓██╫╬▓███████▓╣▓▓▓▓▓╫▓▓███████▌
* ╚ ╫▓█▓▓█▓╫▒▓╬███████▓▓▓██╫▓████████████
* a² + b² = ║ .╬▓▓███▒▒╫╣▓███████████▓█████████████
* ,╙ ⌂║█▒╬▓█Ü▒▒╬█▓██▓█████████████████████
* "Θ╗—-j, ²▒▒▓█▒▓▓█▓▓████████████████████████▌
* - ╔ `╖╬██ "▀╬▒╬▓████████████████████████
* `ⁿ¥═≥▀^ `██████████████████████████
* ╙███▓███████████████▀█████
* ╙██████"███"▀████▌ ████▌
* ╙████ ███ █████ ████▌
*/
int hippopotenuse(int x, int y)
{
return sqrt(sqr__(x) + sqr__(y));
}
void *logOutput(const string line, const int value)
{
cout << line << ": " << value << endl;
return nullptr;
}
void *test__(int a, const string& code, int b)
{
assert.expect(a, b, code);
return nullptr;
}
ParallelEventLoop loop{ {
{ EventLoopPool::reactor, 1 },
{ EventLoopPool::calculation, 1 }
} };
const auto hippo = curry_wrap(hippopotenuse);
const auto sqr = curry_wrap(sqr__);
const auto test = curry_wrap(test__);
const auto Hippo = promise::dispatchable(hippopotenuse,
EventLoopPool::calculation,
EventLoopPool::reactor) << ref(loop);
const auto Sqr = promise::dispatchable(sqr__,
EventLoopPool::calculation,
EventLoopPool::reactor) << ref(loop);
const auto Test = promise::dispatchable(test__,
EventLoopPool::reactor,
EventLoopPool::reactor) << ref(loop);
const auto LogOutput = promise::dispatchable(logOutput,
EventLoopPool::reactor) << ref(loop);
void basic_tests()
{
const auto hippo = curry_wrap(hippopotenuse);
assert.expect(hippo.apply(3, 4)(), 5, "BR");
const int x = 3, y = 4;
assert.expect(hippo.apply(x, y)(), 5, "BL");
}
void cross_thread_currying()
{
/* AC */
{
Hippo.apply(3, 4).invoke()
->then(
[] (int z) {
assert.expect(z, 5, "ACR");
},
[] (auto e) {
assert.fail("ACR");
});
int x = 5, y = 12;
const auto fxy = Hippo.apply(x, y);
x = 3, y = 4;
fxy.invoke()
->then(
[] (int z) {
assert.expect(z, 5, "ACL");
},
[] (auto e) {
assert.fail("ACL");
});
}
/* AOC */
{
Hippo(3, 4)->then(
[] (int z) {
assert.expect(z, 5, "AOCR");
},
[] (auto e) {
assert.fail("AOCR");
});
int x = 5, y = 4;
const auto fx = Hippo.apply(x);
x = 3;
fx(y)->then(
[] (int z) {
assert.expect(z, 5, "AOCL");
},
[] (auto e) {
assert.fail("AOCL");
});
}
/* SHC */
{
(Hippo << 3 << 4).invoke()->then(
[] (int z) {
assert.expect(z, 5, "SHCR");
},
[] (auto e) {
assert.fail("SHCR");
});
int x = 5, y = 12;
const auto fxy = Hippo << x << y;
x = 3, y = 4;
fxy()->then(
[] (int z) {
assert.expect(z, 13, "SHCL");
},
[] (auto e) {
assert.fail("SHCL");
});
}
{
int x = 5, y = 12;
const auto fxy = Hippo << ref(x) << cref(y);
x = 35;
fxy()->then(
[] (int z) {
assert.expect(z, 37, "SHCWL");
},
[] (auto e) {
assert.fail("SHCWL");
});
}
loop.join();
}
void monad_tests()
{
using namespace kaiu::functional_chain;
using namespace kaiu::task_monad;
/*** Do not use synchronous monads, they're here for fun only ***/
/* Synchronous monad, operator in functional.h */
4, sqr, hippo << 63, test << 65 << "MONADS";
/* Asynchronous monad, operator in task.h */
promise::resolved(3) | Sqr | Hippo << 40 | Test << 41 << "MONADA";
}
int main(int argc, char **argv)
try {
basic_tests();
cross_thread_currying();
monad_tests();
// /*
// * Play with Pythagorean triples:
// * 3:4:5
// * 5:12:13
// * 8:13:15
// */
// auto h3 = Hippo.apply(3);
//
// h3(4)
// ->then(Hippo << 12)
// ->then(Hippo << 8)
// ->then(LogOutput << "Should be 15");
loop.join();
return assert.print(argc, argv);
} catch (...) {
assert.print_error();
}