-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasics.test.cpp
More file actions
247 lines (199 loc) · 6.25 KB
/
basics.test.cpp
File metadata and controls
247 lines (199 loc) · 6.25 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
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
#include <coroutine>
#include <boost/ut.hpp>
#include <print>
#include <variant>
import async_context;
import test_utils;
void basics()
{
using namespace boost::ut;
"sync return type void"_test = []() {
// Setup
async::inplace_context<1024> ctx;
unsigned step = 0;
auto sync_coroutine = [&step](async::context&) -> async::future<void> {
step = 1;
return {};
};
// Exercise
auto future = sync_coroutine(ctx);
// Verify
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % 1 == step);
};
"sync return type int"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr int expected_return_value = 5;
unsigned step = 0;
auto sync_coroutine = [&step](async::context&) -> async::future<int> {
step = 1;
return expected_return_value;
};
// Exercise
auto future = sync_coroutine(ctx);
// Verify
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_return_value == future.value());
expect(that % 1 == step);
};
"sync return type std::string"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr auto expected_return_value = "Hello, World\n";
unsigned step = 0;
auto sync_coroutine =
[&step](async::context&) -> async::future<std::string> {
step = 1;
return expected_return_value;
};
// Exercise
auto future = sync_coroutine(ctx);
// Verify
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_return_value == future.value());
expect(that % 1 == step);
};
"co_return"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr int expected_return_value = 5;
unsigned step = 0;
auto async_coroutine = [&step](async::context&) -> async::future<int> {
step = 1;
co_return expected_return_value;
};
// Exercise 1
auto future = async_coroutine(ctx);
// Verify 1
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 0 == step);
// Exercise 2: start and finish coroutine
future.resume();
// Verify 2
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_return_value == future.value());
expect(that % 1 == step);
};
"suspend then co_return"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr int expected_return_value = 1413;
unsigned step = 0;
auto async_coroutine = [&step](async::context&) -> async::future<int> {
step = 1;
co_await std::suspend_always{};
step = 2;
co_return expected_return_value;
};
// Exercise 1
auto future = async_coroutine(ctx);
// Verify 1
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 0 == step);
// Exercise 2: start and suspend coroutine
future.resume();
// Verify 2
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 1 == step);
// Exercise 3: resume and co_return from coroutine
future.resume();
// Verify 3
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_return_value == future.value());
expect(that % 2 == step);
};
"co_await coroutine"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr int expected_return_value = 1413;
unsigned step = 0;
auto co2 = [&step](async::context&) -> async::future<int> {
step = 2;
co_await std::suspend_always{};
// skipped as the co1 will immediately resume
step = 3;
co_return expected_return_value;
};
auto co = [&step, &co2](async::context& p_ctx) -> async::future<int> {
step = 1; // skipped as the co2 will immediately start
auto const val = co_await co2(p_ctx);
step = 4;
co_return val;
};
// Exercise 1
auto future = co(ctx);
// Verify 1
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 0 == step);
// Exercise 2: start, enter co_2, start immediately and suspend
future.resume();
// Verify 2
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 2 == step);
// Exercise 3: resume, co_2 co_returns, immediately resumes parent, return
future.resume();
// Verify 3
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_return_value == future.value());
expect(that % 4 == step);
};
"co_await coroutine sync"_test = []() {
// Setup
async::inplace_context<1024> ctx;
static constexpr int return_value1 = 1413;
static constexpr int return_value2 = 4324;
static constexpr int expected_total = return_value1 + return_value2;
unsigned step = 0;
auto co2 = [](async::context&) -> async::future<int> {
return return_value1;
};
auto co = [&step, &co2](async::context& p_ctx) -> async::future<int> {
step = 1; // skipped as the co2 will immediately start
auto val = co_await co2(p_ctx);
step = 2;
co_return val + return_value2;
};
// Exercise 1
auto future = co(ctx);
// Verify 1
expect(that % 0 < ctx.memory_used());
expect(that % not future.done());
expect(that % not future.has_value());
expect(that % 0 == step);
// Exercise 2: start, call co_2, returns value immediately and co_returns
future.resume();
// Verify 3
expect(that % 0 == ctx.memory_used());
expect(that % future.done());
expect(that % future.has_value());
expect(that % expected_total == future.value());
expect(that % 2 == step);
};
};
int main()
{
basics();
}