15 copp::coroutine_context_container<copp::allocator::stack_allocator_memory>;
17#if defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && LIBCOPP_MACRO_ENABLE_WIN_FIBER
18using test_context_channel_coroutine_context_fiber_test_type =
19 copp::coroutine_context_fiber_container<copp::allocator::stack_allocator_memory>;
29 int operator()(copp::copp_error_code err_code) {
return static_cast<int>(err_code) - 10000; }
43 return copp::this_coroutine::get<test_context_channel_coroutine_context_test_type>();
47#if defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && LIBCOPP_MACRO_ENABLE_WIN_FIBER
50 static test_context_channel_coroutine_context_fiber_test_type *
get() {
51 return copp::this_fiber::get<test_context_channel_coroutine_context_fiber_test_type>();
56template <
class TCTX,
class T>
63 copp::memory::strong_rc_ptr<copp::stackful_channel_sender<T>>
sender;
67 auto ret = copp::make_stackful_channel<T>();
68 sender = copp::memory::make_strong_rc<copp::stackful_channel_sender<T>>(ret.second);
69 return std::move(ret.first);
89 unsigned char *stack_buff =
new unsigned char[128 * 1024];
96 copp::stack_context test_move_alloc;
98 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
99 copp::allocator::stack_allocator_memory alloc(alloc_created);
101 alloc_created.allocate(test_move_alloc, 64 * 1024);
104 test_context_channel_coroutine_context_test_type::ptr_t co =
105 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
114 runner.
sender->set_value(137);
119 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
120 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
127 copp::stack_context test_move_alloc;
129 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
130 copp::allocator::stack_allocator_memory alloc(alloc_created);
132 alloc_created.allocate(test_move_alloc, 64 * 1024);
135 test_context_channel_coroutine_context_test_type::ptr_t co =
136 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
150 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
151 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
158 copp::stack_context test_move_alloc;
160 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
161 copp::allocator::stack_allocator_memory alloc(alloc_created);
163 alloc_created.allocate(test_move_alloc, 64 * 1024);
166 test_context_channel_coroutine_context_test_type::ptr_t co =
167 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
182 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
183 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
190 unsigned char *stack_buff =
new unsigned char[128 * 1024];
197 copp::stack_context test_move_alloc;
199 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
200 copp::allocator::stack_allocator_memory alloc(alloc_created);
202 alloc_created.allocate(test_move_alloc, 64 * 1024);
205 test_context_channel_coroutine_context_test_type::ptr_t co =
206 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
215 runner.
sender->set_value(
reinterpret_cast<int *
>(stack_buff));
220 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
221 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
228 copp::stack_context test_move_alloc;
230 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
231 copp::allocator::stack_allocator_memory alloc(alloc_created);
233 alloc_created.allocate(test_move_alloc, 64 * 1024);
236 test_context_channel_coroutine_context_test_type::ptr_t co =
237 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
251 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
252 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
259 copp::stack_context test_move_alloc;
261 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
262 copp::allocator::stack_allocator_memory alloc(alloc_created);
264 alloc_created.allocate(test_move_alloc, 64 * 1024);
267 test_context_channel_coroutine_context_test_type::ptr_t co =
268 test_context_channel_coroutine_context_test_type::create(&runner, alloc);
283 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
284 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
290#if defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && LIBCOPP_MACRO_ENABLE_WIN_FIBER
292CASE_TEST(coroutine_channel, fiber_channel_int) {
293 unsigned char *stack_buff =
new unsigned char[128 * 1024];
300 copp::stack_context test_move_alloc;
302 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
303 copp::allocator::stack_allocator_memory alloc(alloc_created);
305 alloc_created.allocate(test_move_alloc, 64 * 1024);
308 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
309 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
318 runner.
sender->set_value(157);
323 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
324 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
331 copp::stack_context test_move_alloc;
333 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
334 copp::allocator::stack_allocator_memory alloc(alloc_created);
336 alloc_created.allocate(test_move_alloc, 64 * 1024);
339 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
340 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
354 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
355 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
362 copp::stack_context test_move_alloc;
364 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
365 copp::allocator::stack_allocator_memory alloc(alloc_created);
367 alloc_created.allocate(test_move_alloc, 64 * 1024);
370 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
371 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
386 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
387 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
393CASE_TEST(coroutine_channel, fiber_channel_pointer) {
394 unsigned char *stack_buff =
new unsigned char[128 * 1024];
401 copp::stack_context test_move_alloc;
403 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
404 copp::allocator::stack_allocator_memory alloc(alloc_created);
406 alloc_created.allocate(test_move_alloc, 64 * 1024);
409 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
410 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
419 runner.
sender->set_value(
reinterpret_cast<int *
>(stack_buff));
424 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
425 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
432 copp::stack_context test_move_alloc;
434 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
435 copp::allocator::stack_allocator_memory alloc(alloc_created);
437 alloc_created.allocate(test_move_alloc, 64 * 1024);
440 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
441 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
455 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
456 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
463 copp::stack_context test_move_alloc;
465 copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
466 copp::allocator::stack_allocator_memory alloc(alloc_created);
468 alloc_created.allocate(test_move_alloc, 64 * 1024);
471 test_context_channel_coroutine_context_fiber_test_type::ptr_t co =
472 test_context_channel_coroutine_context_fiber_test_type::create(&runner, alloc);
487 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_NOT_READY, co->resume());
488 CASE_EXPECT_EQ(LIBCOPP_COPP_NAMESPACE_ID::COPP_EC_ALREADY_EXIST, co->yield());
496struct test_context_channel_fifo_context :
public copp::stackful_channel_context_base {};
498std::vector<int> g_test_coroutine_channel_fifo_order;
500int test_context_channel_fifo_resume(
void *handle_data, copp::stackful_channel_context_base *) {
501 g_test_coroutine_channel_fifo_order.push_back(*
reinterpret_cast<int *
>(handle_data));
506CASE_TEST(coroutine_channel, multiple_callers_resume_fifo) {
507 constexpr int k_caller_count = 8;
508 int caller_indexes[k_caller_count];
509 copp::stackful_channel_handle_delegate callers[k_caller_count];
510 test_context_channel_fifo_context context;
512 g_test_coroutine_channel_fifo_order.clear();
513 for (
int index = 0; index < k_caller_count; ++index) {
514 caller_indexes[index] = index;
515 callers[index].handle_data = &caller_indexes[index];
516 callers[index].resume_handle = &test_context_channel_fifo_resume;
517 context.add_caller(callers[index]);
520 context.add_caller(callers[3]);
523 CASE_EXPECT_EQ(k_caller_count,
static_cast<int>(context.resume_callers()));
524 CASE_EXPECT_EQ(k_caller_count,
static_cast<int>(g_test_coroutine_channel_fifo_order.size()));
525 for (
int index = 0; index < k_caller_count; ++index) {
526 CASE_EXPECT_EQ(index, g_test_coroutine_channel_fifo_order[
static_cast<size_t>(index)]);
530 g_test_coroutine_channel_fifo_order.clear();
531 for (
int index = 0; index < k_caller_count; ++index) {
532 context.add_caller(callers[index]);
535 context.add_caller(callers[2]);
537 const int expected_order[k_caller_count] = {0, 1, 3, 4, 5, 6, 7, 2};
538 CASE_EXPECT_EQ(k_caller_count,
static_cast<int>(context.resume_callers()));
539 CASE_EXPECT_EQ(k_caller_count,
static_cast<int>(g_test_coroutine_channel_fifo_order.size()));
540 if (k_caller_count ==
static_cast<int>(g_test_coroutine_channel_fifo_order.size())) {
541 for (
int index = 0; index < k_caller_count; ++index) {
542 CASE_EXPECT_EQ(expected_order[index], g_test_coroutine_channel_fifo_order[
static_cast<size_t>(index)]);
547CASE_TEST(coroutine_channel, multiple_callers_resume_fifo_after_first_removed) {
548 int caller_indexes[3] = {0, 1, 2};
549 copp::stackful_channel_handle_delegate callers[3];
550 test_context_channel_fifo_context context;
551 for (
int index = 0; index < 3; ++index) {
552 callers[index].handle_data = &caller_indexes[index];
553 callers[index].resume_handle = &test_context_channel_fifo_resume;
556 context.add_caller(callers[0]);
557 context.add_caller(callers[1]);
562 context.add_caller(callers[2]);
563 context.add_caller(callers[0]);
564 context.add_caller(callers[1]);
567 g_test_coroutine_channel_fifo_order.clear();
569 CASE_EXPECT_EQ(3,
static_cast<int>(g_test_coroutine_channel_fifo_order.size()));
570 if (g_test_coroutine_channel_fifo_order.size() == 3) {
579CASE_TEST(coroutine_channel, multiple_callers_add_remove_edge_cases) {
580 int caller_indexes[3] = {0, 1, 2};
581 copp::stackful_channel_handle_delegate first_caller;
582 copp::stackful_channel_handle_delegate second_caller;
583 copp::stackful_channel_handle_delegate unregistered_caller;
584 first_caller.handle_data = &caller_indexes[0];
585 first_caller.resume_handle = &test_context_channel_fifo_resume;
586 second_caller.handle_data = &caller_indexes[1];
587 second_caller.resume_handle = &test_context_channel_fifo_resume;
588 unregistered_caller.handle_data = &caller_indexes[2];
589 unregistered_caller.resume_handle = &test_context_channel_fifo_resume;
591 test_context_channel_fifo_context context;
594 context.add_caller(first_caller);
595 context.add_caller(first_caller);
602 context.add_caller(second_caller);
603 context.add_caller(first_caller);
612 context.add_caller(second_caller);
617 g_test_coroutine_channel_fifo_order.clear();
621 context.add_caller(first_caller);
624 CASE_EXPECT_EQ(1,
static_cast<int>(g_test_coroutine_channel_fifo_order.size()));
copp::memory::strong_rc_ptr< copp::stackful_channel_sender< T > > sender
test_context_channel_foo_runner()
copp::stackful_channel_receiver< T > create_channel()
static int g_test_coroutine_channel_status
copp::coroutine_context_container< copp::allocator::stack_allocator_memory > test_context_channel_coroutine_context_test_type
LIBCOPP_COPP_API_HEAD_ONLY Tc * get()
get current coroutine and try to convert type
int operator()(copp::copp_error_code err_code)
int * operator()(copp::copp_error_code)
static test_context_channel_coroutine_context_test_type * get()
#define CASE_EXPECT_FALSE(c)
#define CASE_EXPECT_EQ(l, r)
#define CASE_TEST(test_name, case_name)
#define CASE_EXPECT_TRUE(c)