-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathrecursive_generator_tests.cpp
More file actions
426 lines (371 loc) · 7.11 KB
/
recursive_generator_tests.cpp
File metadata and controls
426 lines (371 loc) · 7.11 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/recursive_generator.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <cppcoro/fmap.hpp>
#include <chrono>
#include <algorithm>
#include <ostream>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("recursive_generator");
using cppcoro::recursive_generator;
TEST_CASE("default constructed recursive_generator is empty")
{
recursive_generator<std::uint32_t> ints;
CHECK(ints.begin() == ints.end());
}
TEST_CASE("non-recursive use of recursive_generator")
{
auto f = []() -> recursive_generator<float>
{
co_yield 1.0f;
co_yield 2.0f;
};
auto gen = f();
auto iter = gen.begin();
CHECK(*iter == 1.0f);
++iter;
CHECK(*iter == 2.0f);
++iter;
CHECK(iter == gen.end());
}
TEST_CASE("throw before first yield")
{
class MyException : public std::exception {};
auto f = []() -> recursive_generator<std::uint32_t>
{
throw MyException{};
co_return;
};
auto gen = f();
try
{
auto iter = gen.begin();
(void) iter;
CHECK(false);
}
catch (MyException)
{
CHECK(true);
}
}
TEST_CASE("throw after first yield")
{
class MyException : public std::exception {};
auto f = []() -> recursive_generator<std::uint32_t>
{
co_yield 1;
throw MyException{};
};
auto gen = f();
auto iter = gen.begin();
CHECK(*iter == 1u);
try
{
++iter;
CHECK(false);
}
catch (MyException)
{
CHECK(true);
}
}
TEST_CASE("generator doesn't start executing until begin is called")
{
bool reachedA = false;
bool reachedB = false;
bool reachedC = false;
auto f = [&]() -> recursive_generator<std::uint32_t>
{
reachedA = true;
co_yield 1;
reachedB = true;
co_yield 2;
reachedC = true;
};
auto gen = f();
CHECK(!reachedA);
auto iter = gen.begin();
CHECK(reachedA);
CHECK(!reachedB);
CHECK(*iter == 1u);
++iter;
CHECK(reachedB);
CHECK(!reachedC);
CHECK(*iter == 2u);
++iter;
CHECK(reachedC);
CHECK(iter == gen.end());
}
TEST_CASE("destroying generator before completion destructs objects on stack")
{
bool destructed = false;
bool completed = false;
auto f = [&]() -> recursive_generator<std::uint32_t>
{
auto onExit = cppcoro::on_scope_exit([&]
{
destructed = true;
});
co_yield 1;
co_yield 2;
completed = true;
};
{
auto g = f();
auto it = g.begin();
auto itEnd = g.end();
(void) itEnd;
CHECK(*it == 1u);
CHECK(!destructed);
}
CHECK(!completed);
CHECK(destructed);
}
TEST_CASE("simple recursive yield")
{
auto f = [](int n, auto& f) -> recursive_generator<const std::uint32_t>
{
co_yield n;
if (n > 0)
{
co_yield f(n - 1, f);
co_yield n;
}
};
auto f2 = [&f](int n)
{
return f(n, f);
};
{
auto gen = f2(1);
auto iter = gen.begin();
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 0u);
++iter;
CHECK(*iter == 1u);
++iter;
CHECK(iter == gen.end());
}
{
auto gen = f2(2);
auto iter = gen.begin();
CHECK(*iter == 2u);
++iter;
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 0u);
++iter;
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 2u);
++iter;
CHECK(iter == gen.end());
}
}
TEST_CASE("nested yield that yields nothing")
{
auto f = []() -> recursive_generator<std::uint32_t>
{
co_return;
};
auto g = [&f]() -> recursive_generator<std::uint32_t>
{
co_yield 1;
co_yield f();
co_yield 2;
};
auto gen = g();
auto iter = gen.begin();
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 2u);
++iter;
CHECK(iter == gen.end());
}
TEST_CASE("exception thrown from recursive call can be caught by caller")
{
class SomeException : public std::exception {};
auto f = [](std::uint32_t depth, auto&& f) -> recursive_generator<std::uint32_t>
{
if (depth == 1u)
{
throw SomeException{};
}
co_yield 1;
try
{
co_yield f(1, f);
}
catch (SomeException)
{
}
co_yield 2;
};
auto gen = f(0, f);
auto iter = gen.begin();
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 2u);
++iter;
CHECK(iter == gen.end());
}
TEST_CASE("exceptions thrown from nested call can be caught by caller")
{
class SomeException : public std::exception {};
auto f = [](std::uint32_t depth, auto&& f) -> recursive_generator<std::uint32_t>
{
if (depth == 4u)
{
throw SomeException{};
}
else if (depth == 3u)
{
co_yield 3;
try
{
co_yield f(4, f);
}
catch (SomeException)
{
}
co_yield 33;
throw SomeException{};
}
else if (depth == 2u)
{
bool caught = false;
try
{
co_yield f(3, f);
}
catch (SomeException)
{
caught = true;
}
if (caught)
{
co_yield 2;
}
}
else
{
co_yield 1;
co_yield f(2, f);
co_yield f(3, f);
}
};
auto gen = f(1, f);
auto iter = gen.begin();
CHECK(*iter == 1u);
++iter;
CHECK(*iter == 3u);
++iter;
CHECK(*iter == 33u);
++iter;
CHECK(*iter == 2u);
++iter;
CHECK(*iter == 3u);
++iter;
CHECK(*iter == 33u);
try
{
++iter;
CHECK(false);
}
catch (SomeException)
{
}
CHECK(iter == gen.end());
}
namespace
{
recursive_generator<std::uint32_t> iterate_range(std::uint32_t begin, std::uint32_t end)
{
if ((end - begin) <= 10u)
{
for (std::uint32_t i = begin; i < end; ++i)
{
co_yield i;
}
}
else
{
std::uint32_t mid = begin + (end - begin) / 2;
co_yield iterate_range(begin, mid);
co_yield iterate_range(mid, end);
}
}
}
TEST_CASE("recursive iteration performance")
{
const std::uint32_t count = 100000;
auto start = std::chrono::high_resolution_clock::now();
std::uint64_t sum = 0;
for (auto i : iterate_range(0, count))
{
sum += i;
}
auto end = std::chrono::high_resolution_clock::now();
CHECK(sum == (std::uint64_t(count) * (count - 1)) / 2);
const auto timeTakenUs = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
MESSAGE("Range iteration of " << count << "elements took " << timeTakenUs << "us");
}
TEST_CASE("usage in standard algorithms")
{
{
auto a = iterate_range(5, 30);
auto b = iterate_range(5, 30);
CHECK(std::equal(a.begin(), a.end(), b.begin(), b.end()));
}
{
auto a = iterate_range(5, 30);
auto b = iterate_range(5, 300);
CHECK(!std::equal(a.begin(), a.end(), b.begin(), b.end()));
}
}
namespace
{
recursive_generator<int> range(int start, int end)
{
while (start < end)
{
co_yield start++;
}
}
recursive_generator<int> range_chunks(int start, int end, int runLength, int stride)
{
while (start < end)
{
co_yield range(start, std::min(end, start + runLength));
start += stride;
}
}
}
TEST_CASE("fmap operator")
{
// 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24
cppcoro::generator<int> gen = range_chunks(0, 30, 5, 10)
| cppcoro::fmap([](int x) { return x * 3; });
auto it = gen.begin();
CHECK(*it == 0);
CHECK(*++it == 3);
CHECK(*++it == 6);
CHECK(*++it == 9);
CHECK(*++it == 12);
CHECK(*++it == 30);
CHECK(*++it == 33);
CHECK(*++it == 36);
CHECK(*++it == 39);
CHECK(*++it == 42);
CHECK(*++it == 60);
CHECK(*++it == 63);
CHECK(*++it == 66);
CHECK(*++it == 69);
CHECK(*++it == 72);
CHECK(++it == gen.end());
}
TEST_SUITE_END();