forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathgenerator_tests.cpp
More file actions
490 lines (433 loc) · 10.6 KB
/
generator_tests.cpp
File metadata and controls
490 lines (433 loc) · 10.6 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/generator.hpp>
#include <cppcoro/on_scope_exit.hpp>
#include <cppcoro/fmap.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <forward_list>
#include <cstdint>
#include <type_traits>
#include <utility>
#include "doctest/cppcoro_doctest.h"
TEST_SUITE_BEGIN("generator");
using cppcoro::generator;
namespace {
// Helper to detect presence of operator-> on iterator type
template<typename It, typename = void>
struct has_arrow : std::false_type {};
template<typename It>
struct has_arrow<It, std::void_t<decltype(std::declval<const It&>().operator->())>> : std::true_type {};
}
TEST_CASE("default-constructed generator is empty sequence")
{
generator<int> ints;
CHECK(ints.begin() == ints.end());
}
TEST_CASE("generator of arithmetic type returns by copy")
{
auto f = []() -> generator<float>
{
co_yield 1.0f;
co_yield 2.0f;
};
auto gen = f();
auto iter = gen.begin();
// TODO: Should this really be required?
//static_assert(std::is_same<decltype(*iter), float>::value, "operator* should return float by value");
CHECK(*iter == 1.0f);
++iter;
CHECK(*iter == 2.0f);
++iter;
CHECK(iter == gen.end());
}
TEST_CASE("generator of reference returns by reference")
{
auto f = [](float& value) -> generator<float&>
{
co_yield value;
};
float value = 1.0f;
for (auto& x : f(value))
{
CHECK(&x == &value);
x += 1.0f;
}
CHECK(value == 2.0f);
}
TEST_CASE("generator of const type")
{
auto fib = []() -> generator<const std::uint64_t>
{
std::uint64_t a = 0, b = 1;
while (true)
{
co_yield b;
b += std::exchange(a, b);
}
};
std::uint64_t count = 0;
for (auto i : fib())
{
if (i > 1'000'000) {
break;
}
++count;
}
// 30th fib number is 832'040
CHECK(count == 30);
}
TEST_CASE("value-category of fmap() matches reference type")
{
using cppcoro::fmap;
auto checkIsRvalue = [](auto&& x) {
static_assert(std::is_rvalue_reference_v<decltype(x)>);
static_assert(!std::is_const_v<std::remove_reference_t<decltype(x)>>);
CHECK(x == 123);
return x;
};
auto checkIsLvalue = [](auto&& x) {
static_assert(std::is_lvalue_reference_v<decltype(x)>);
static_assert(!std::is_const_v<std::remove_reference_t<decltype(x)>>);
CHECK(x == 123);
return x;
};
auto checkIsConstLvalue = [](auto&& x) {
static_assert(std::is_lvalue_reference_v<decltype(x)>);
static_assert(std::is_const_v<std::remove_reference_t<decltype(x)>>);
CHECK(x == 123);
return x;
};
auto checkIsConstRvalue = [](auto&& x) {
static_assert(std::is_rvalue_reference_v<decltype(x)>);
static_assert(std::is_const_v<std::remove_reference_t<decltype(x)>>);
CHECK(x == 123);
return x;
};
auto consume = [](auto&& range) {
for (auto&& x : range) {
(void)x;
}
};
consume([]() -> generator<int> { co_yield 123; }() | fmap(checkIsLvalue));
consume([]() -> generator<const int> { co_yield 123; }() | fmap(checkIsConstLvalue));
consume([]() -> generator<int&> { co_yield 123; }() | fmap(checkIsLvalue));
consume([]() -> generator<const int&> { co_yield 123; }() | fmap(checkIsConstLvalue));
consume([]() -> generator<int&&> { co_yield 123; }() | fmap(checkIsRvalue));
consume([]() -> generator<const int&&> { co_yield 123; }() | fmap(checkIsConstRvalue));
}
TEST_CASE("iterator arrow for lvalue yields and disabled for rvalue yields")
{
SUBCASE("arrow works for generator<int>")
{
auto g = []() -> generator<int>
{
co_yield 10;
}();
auto it = g.begin();
REQUIRE(it != g.end());
auto p = it.operator->();
CHECK(p == &*it);
CHECK(*p == 10);
}
SUBCASE("arrow works for generator<int&>")
{
int x = 42;
auto g = [&]() -> generator<int&>
{
co_yield x;
}();
auto it = g.begin();
REQUIRE(it != g.end());
CHECK(it.operator->() == &x);
CHECK(*it == 42);
}
SUBCASE("arrow is disabled for generator<int&&>")
{
using It = generator<int&&>::iterator;
static_assert(!has_arrow<It>::value, "operator-> must be disabled for rvalue reference yields");
// Still ensure rvalue-yielding generator produces correct values
auto g = []() -> generator<int&&>
{
int v = 7;
co_yield std::move(v);
}();
int count = 0;
for (auto&& v : g)
{
CHECK(v == 7);
++count;
}
CHECK(count == 1);
}
}
TEST_CASE("yielding rvalues produces stable values across suspension")
{
auto make = []() -> generator<std::string>
{
co_yield std::string{"a"};
co_yield std::string{"bc"};
};
auto gen = make();
auto it = gen.begin();
REQUIRE(it != gen.end());
CHECK(*it == std::string{"a"});
++it;
REQUIRE(it != gen.end());
CHECK(*it == std::string{"bc"});
++it;
CHECK(it == gen.end());
}
TEST_CASE("generator doesn't start until its called")
{
bool reachedA = false;
bool reachedB = false;
bool reachedC = false;
auto f = [&]() -> generator<int>
{
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 == 1);
++iter;
CHECK(reachedB);
CHECK(!reachedC);
CHECK(*iter == 2);
++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 = [&]() -> generator<int>
{
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();
CHECK(it != itEnd);
CHECK(*it == 1u);
CHECK(!destructed);
}
CHECK(!completed);
CHECK(destructed);
}
TEST_CASE("generator throwing before yielding first element rethrows out of begin()")
{
class X {};
auto g = []() -> cppcoro::generator<int>
{
throw X{};
co_return;
}();
try
{
g.begin();
FAIL("should have thrown");
}
catch (const X&)
{
}
}
TEST_CASE("generator throwing after first element rethrows out of operator++")
{
class X {};
auto g = []() -> cppcoro::generator<int>
{
co_yield 1;
throw X{};
}();
auto iter = g.begin();
REQUIRE(iter != g.end());
try
{
++iter;
FAIL("should have thrown");
}
catch (const X&)
{
}
}
namespace
{
template<typename FIRST, typename SECOND>
auto concat(FIRST&& first, SECOND&& second)
{
using value_type = std::remove_reference_t<decltype(*first.begin())>;
return [](FIRST first, SECOND second) -> cppcoro::generator<value_type>
{
for (auto&& x : first) co_yield x;
for (auto&& y : second) co_yield y;
}(std::forward<FIRST>(first), std::forward<SECOND>(second));
}
}
TEST_CASE("safe capture of r-value reference args")
{
using namespace std::string_literals;
// Check that we can capture l-values by reference and that temporary
// values are moved into the coroutine frame.
std::string byRef = "bar";
auto g = concat("foo"s, concat(byRef, std::vector<char>{ 'b', 'a', 'z' }));
byRef = "buzz";
std::string s;
for (char c : g)
{
s += c;
}
CHECK(s == "foobuzzbaz");
}
namespace
{
cppcoro::generator<int> range(int start, int end)
{
for (; start < end; ++start)
{
co_yield start;
}
}
}
TEST_CASE("fmap operator")
{
cppcoro::generator<int> gen = range(0, 5)
| 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 == gen.end());
}
namespace
{
template<std::size_t window, typename Range>
cppcoro::generator<const double> low_pass(Range rng)
{
auto it = std::begin(rng);
const auto itEnd = std::end(rng);
const double invCount = 1.0 / window;
double sum = 0;
using iter_cat =
typename std::iterator_traits<decltype(it)>::iterator_category;
if constexpr (std::is_base_of_v<std::random_access_iterator_tag, iter_cat>)
{
for (std::size_t count = 0; it != itEnd && count < window; ++it)
{
sum += *it;
++count;
co_yield sum / count;
}
for (; it != itEnd; ++it)
{
sum -= *(it - window);
sum += *it;
co_yield sum * invCount;
}
}
else if constexpr (std::is_base_of_v<std::forward_iterator_tag, iter_cat>)
{
auto windowStart = it;
for (std::size_t count = 0; it != itEnd && count < window; ++it)
{
sum += *it;
++count;
co_yield sum / count;
}
for (; it != itEnd; ++it, ++windowStart)
{
sum -= *windowStart;
sum += *it;
co_yield sum * invCount;
}
}
else
{
// Just assume an input iterator
double buffer[window];
for (std::size_t count = 0; it != itEnd && count < window; ++it)
{
buffer[count] = *it;
sum += buffer[count];
++count;
co_yield sum / count;
}
for (std::size_t pos = 0; it != itEnd; ++it, pos = (pos + 1 == window) ? 0 : (pos + 1))
{
sum -= std::exchange(buffer[pos], *it);
sum += buffer[pos];
co_yield sum * invCount;
}
}
}
}
// HACK: Disable this test as it's causing heap corruption errors under MSVC 2017 Update 5 x86 debug builds.
// Still needs investigation of root cause.
TEST_CASE("low_pass" * doctest::skip{ true })
{
// With random-access iterator
{
auto gen = low_pass<4>(std::vector<int>{ 10, 13, 10, 15, 18, 9, 11, 15 });
auto it = gen.begin();
CHECK(*it == 10.0);
CHECK(*++it == 11.5);
CHECK(*++it == 11.0);
CHECK(*++it == 12.0);
CHECK(*++it == 14.0);
CHECK(*++it == 13.0);
CHECK(*++it == 13.25);
CHECK(*++it == 13.25);
CHECK(++it == gen.end());
}
// With forward-iterator
{
auto gen = low_pass<4>(std::forward_list<int>{ 10, 13, 10, 15, 18, 9, 11, 15 });
auto it = gen.begin();
CHECK(*it == 10.0);
CHECK(*++it == 11.5);
CHECK(*++it == 11.0);
CHECK(*++it == 12.0);
CHECK(*++it == 14.0);
CHECK(*++it == 13.0);
CHECK(*++it == 13.25);
CHECK(*++it == 13.25);
CHECK(++it == gen.end());
}
// With input-iterator
{
auto gen = low_pass<3>(range(10, 20));
auto it = gen.begin();
CHECK(*it == 10.0);
CHECK(*++it == 10.5);
CHECK(*++it == 11.0);
CHECK(*++it == 12.0);
CHECK(*++it == 13.0);
CHECK(*++it == 14.0);
CHECK(*++it == 15.0);
CHECK(*++it == 16.0);
CHECK(*++it == 17.0);
CHECK(*++it == 18.0);
CHECK(++it == gen.end());
}
}
TEST_SUITE_END();