blob: 9177161afcf7800de600daf67e43d402d674b604 (
plain) (
blame)
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
|
//
// experimental/coro/stack_test.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2021-2023 Klemens D. Morgenstern
// (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/experimental/coro.hpp"
#include "asio/detached.hpp"
#include "asio/io_context.hpp"
#include <iostream>
#include "../../unit_test.hpp"
using namespace asio::experimental;
namespace coro {
asio::experimental::coro<int()>
stack_generator(asio::any_io_executor, int i = 1)
{
for (;;)
{
co_yield i;
i *= 2;
}
}
asio::experimental::coro<int(int)>
stack_accumulate(asio::any_io_executor exec)
{
auto gen = stack_generator(exec);
int offset = 0;
while (auto next = co_await gen) // 1, 2, 4, 8, ...
offset = co_yield *next + offset; // offset is delayed by one cycle
}
asio::experimental::coro<int>
main_stack_coro(asio::io_context&, bool & done)
{
auto g = stack_accumulate(co_await asio::this_coro::executor);
ASIO_CHECK(g.is_open());
ASIO_CHECK(1 == (co_await g(1000)).value_or(-1));
ASIO_CHECK(2002 == (co_await g(2000)).value_or(-1));
ASIO_CHECK(3004 == (co_await g(3000)).value_or(-1));
ASIO_CHECK(4008 == (co_await g(4000)).value_or(-1));
ASIO_CHECK(5016 == (co_await g(5000)).value_or(-1));
ASIO_CHECK(6032 == (co_await g(6000)).value_or(-1));
ASIO_CHECK(7064 == (co_await g(7000)).value_or(-1));
ASIO_CHECK(8128 == (co_await g(8000)).value_or(-1));
ASIO_CHECK(9256 == (co_await g(9000)).value_or(-1));
ASIO_CHECK(511 == (co_await g(-1)).value_or(-1));
done = true;
}
void stack_test()
{
bool done = false;
asio::io_context ctx;
auto k = main_stack_coro(ctx, done);
k.async_resume(asio::detached);
ctx.run();
ASIO_CHECK(done);
}
} // namespace coro
ASIO_TEST_SUITE
(
"coro/stack_test",
ASIO_TEST_CASE(::coro::stack_test)
)
|