forked from tonttu/pledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.cpp
255 lines (227 loc) · 6.34 KB
/
Tests.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <atomic>
#include <chrono>
#include <sstream>
#include <thread>
#include "ManualExecutor.hpp"
#include "Promise.hpp"
#include "ThreadPoolExecutor.hpp"
Pledge::ThreadPoolExecutor pool{ 8 };
std::string s_prev;
template <typename T>
void check(T v, const char* test, const char* file, int line)
{
std::stringstream ss;
ss << v;
s_prev = ss.str();
if (v)
return;
fprintf(stderr, "%s:%d: check failed: %s\n", file, line, test);
}
template <typename E, typename A>
void checkEqual(const E& expected,
const A& actual,
const char* expectedStr,
const char* actualStr,
const char* file,
int line)
{
std::stringstream e, a;
e << expected;
a << actual;
s_prev = e.str();
if (e.str() == a.str())
return;
fprintf(stderr,
"%s:%d: check failed, expected (%s): %s, actual (%s): %s\n",
file,
line,
expectedStr,
e.str().c_str(),
actualStr,
a.str().c_str());
}
template <typename E>
void checkPrev(const E& expected, const char* expectedStr, const char* file, int line)
{
std::stringstream e;
e << expected;
if (s_prev == e.str())
return;
fprintf(stderr,
"%s:%d: check (%s) with value %s not executed (prev: %s)\n",
file,
line,
expectedStr,
e.str().c_str(),
s_prev.c_str());
}
#define CHECK(test) check((test), #test, __FILE__, __LINE__)
#define CHECK_EQUAL(expected, actual) \
checkEqual((expected), (actual), #expected, #actual, __FILE__, __LINE__)
#define CHECK_PREV(expected) checkPrev((expected), #expected, __FILE__, __LINE__)
int main()
{
using namespace Pledge;
{
Promise<int> promise{ 42 };
CHECK_EQUAL(42, promise.future().get());
}
{
Promise<int> promise{ 43 };
promise.future().then([](int v) { CHECK_EQUAL(43, v); });
CHECK_PREV(43);
}
{
Promise<int> promise;
promise.future().then([](int v) { CHECK_EQUAL(44, v); });
promise.setValue(44);
CHECK_PREV(44);
}
{
Promise<int> promise;
promise.future().then([](int v) { return v + 1; }).then([](int v) { CHECK_EQUAL(45, v); });
promise.setValue(44);
CHECK_PREV(45);
}
{
Promise<int> promise{ 46 };
promise.future(&pool).then([](int v) { CHECK_EQUAL(46, v); }).get();
CHECK_PREV(46);
}
{
Promise<int> promise;
auto future = promise.future(&pool).then([](int v) { CHECK_EQUAL(47, v); });
promise.setValue(47);
std::move(future).get();
CHECK_PREV(47);
}
{
Promise<> promise;
auto future = promise.future(&pool).then([] { CHECK(true); });
promise.setValue();
std::move(future).get();
CHECK_PREV(true);
}
{
Promise<> promise;
auto future =
promise.future(&pool).then([] { return true; }).then([](bool) { return std::string("yay"); });
promise.setValue();
CHECK_EQUAL("yay", std::move(future).get());
}
ManualExecutor main;
{
Promise<int> promise;
std::atomic<int> a{ 0 }, b{ 0 };
promise.future(&pool)
.then([&a](int v) {
a = v;
return v + 1;
})
.via(&main)
.then([&b](int v) { b = v; });
promise.setValue(48);
while (a != 48)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
CHECK_EQUAL(0, b);
CHECK_EQUAL(1, main.run());
CHECK_EQUAL(49, b);
}
{
Promise<int> promise;
promise.future().error([](const std::exception& error) {
CHECK_EQUAL("failure", error.what());
return 0;
});
promise.setError(std::runtime_error("failure"));
CHECK_PREV("failure");
}
{
Promise<int> promise;
auto f = promise.future()
.then([](int) {
// Not called
CHECK(false);
return 123;
})
.error([](const std::runtime_error&) {
// Not called
CHECK(false);
return 12345;
})
.error([](const std::logic_error& error) {
CHECK_EQUAL("nope", error.what());
return 1234;
})
.then([](int v) { return v + 1; });
CHECK(!f.isReady());
promise.setError(std::invalid_argument("nope"));
CHECK(f.isReady());
CHECK_EQUAL(1235, std::move(f).get());
}
{
Promise<int> promise;
promise.set([]() -> int { throw "Nah"; });
promise.future().error([](const char* msg) {
CHECK_EQUAL("Nah", std::string(msg));
return 42;
});
CHECK_PREV("Nah");
}
{
Promise<int> promise;
promise.future().then([](int v) { throw v + 1; }).error([](int v) {
CHECK_EQUAL(100, v);
return 0;
});
promise.setValue(99);
CHECK_PREV(100);
}
static_assert(is_specialization_v<Future<int>, Future>);
static_assert(std::is_same_v<FutureType<int>, Future<int>>);
static_assert(std::is_same_v<FutureType<void>, Future<>>);
static_assert(std::is_same_v<FutureType<Future<int>>, Future<int>>);
static_assert(std::is_same_v<FutureType<Future<>>, Future<>>);
{
Promise<int> promise{ 100 };
int v = promise.future(&pool)
.then([](int v) {
Promise<int> promise2;
auto future2 = promise2.future(&pool).then([](int v) { return v + 1; });
promise2.setValue(v + 1);
return future2;
})
.get();
CHECK_EQUAL(102, v);
}
{
Promise<int> promise;
auto future = promise.future().error([](const char* error) {
Promise<int> promise2;
auto future2 = promise2.future(&pool).then([](int v) { return v + 1; });
promise2.setValue(std::stoi(error));
return future2;
});
promise.set([]() -> int { throw "102"; });
CHECK_EQUAL(103, std::move(future).get());
}
{
Promise<std::unique_ptr<int>> promise;
auto future = promise.future(&pool)
.then([](std::unique_ptr<int> p) {
++*p;
return p;
})
.then([](std::unique_ptr<int> p) {
++*p;
return p;
});
promise.setValue(std::make_unique<int>(1));
std::unique_ptr<int> i = std::move(future).get();
CHECK_EQUAL(3, *i);
}
{
via(&pool, [] {}).get();
}
return 0;
}