libcopp  1.1.0
coroutine_context_base_test.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 
5 #include "frame/test_macros.h"
7 
8 
10 
12 
14 public:
16  int operator()(void *) {
17  ++call_times;
19  copp::this_coroutine::get<test_context_base_coroutine_context_test_type>()->yield();
20 
22  return 0;
23  }
24 };
25 
26 CASE_TEST(coroutine, context_base) {
27  char *stack_buff = new char[128 * 1024];
31 
33  {
34  copp::stack_context test_move_alloc;
35 
36  copp::allocator::stack_allocator_memory alloc_created(stack_buff, 128 * 1024);
37  copp::allocator::stack_allocator_memory alloc(alloc_created);
38 
39  alloc_created.allocate(test_move_alloc, 64 * 1024);
40  CASE_EXPECT_EQ(NULL, test_move_alloc.sp);
41 
43  runner.call_times = 0;
44 
45  CASE_EXPECT_TRUE(!!co);
46 
48 
49  co->start();
50 
53  co->resume();
54 
57 
58  CASE_EXPECT_EQ(::copp::COPP_EC_NOT_READY, co->resume());
60  }
61 
62  {
64  copp::stack_context test_move_alloc;
65 
67  alloc_created.attach(stack_buff, 128 * 1024);
69  alloc = alloc_created;
70 
71  alloc_created.allocate(test_move_alloc, 64 * 1024);
72  CASE_EXPECT_EQ(NULL, test_move_alloc.sp);
73 
74 
76  runner.call_times = 0;
77 
78  CASE_EXPECT_TRUE(!!co);
79 
81 
82  co->start();
83 
86  co->resume();
87 
90 
91  CASE_EXPECT_EQ(::copp::COPP_EC_NOT_READY, co->resume());
93  }
94 
95  delete[] stack_buff;
96 }
97 
98 
99 CASE_TEST(coroutine, shared_runner) {
100  const int stack_len = 128 * 1024;
101  char *stack_buff = new char[4 * stack_len];
105 
107  runner.call_times = 0;
108 
109  {
111  for (int i = 0; i < 4; ++i) {
112  copp::allocator::stack_allocator_memory alloc(stack_buff + i * stack_len, stack_len);
114  co[i]->start();
115  }
116 
118 
119  for (int i = 0; i < 4; ++i) {
120  co[i]->resume();
121  }
122 
124 
125  CASE_EXPECT_EQ(runner.call_times, 4);
126  }
127 
128  delete[] stack_buff;
129 }
COPP_EC_ALREADY_EXIST.
Definition: errno.h:29
memory allocator this allocator will return address of specified memory section
void allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT
COPP_EC_NOT_RUNNING.
Definition: errno.h:25
static ptr_t create(const callback_t &runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
create and init coroutine with specify runner and specify stack size
COPP_EC_NOT_READY.
Definition: errno.h:24
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:62
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:60
void * sp
stack size
Definition: stack_context.h:14
CASE_TEST(coroutine, context_base)
copp::coroutine_context_container< copp::allocator::stack_allocator_memory > test_context_base_coroutine_context_test_type
static int g_test_coroutine_base_status
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield current coroutine
coroutine container contain stack context, stack allocator and runtime fcontext
void attach(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT