libcopp  2.2.0
coroutine_context_fiber_test.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
4 
5 #include <cstdio>
6 #include <cstring>
7 #include <iostream>
8 
9 #include "frame/test_macros.h"
10 
11 #if defined(LIBCOPP_MACRO_ENABLE_WIN_FIBER) && LIBCOPP_MACRO_ENABLE_WIN_FIBER
12 
13 typedef copp::coroutine_fiber_context_default test_coroutine_context_fiber_test_type;
14 
15 static int g_test_coroutine_fiber_status = 0;
16 
17 class test_context_fiber_foo_runner {
18  public:
19  test_context_fiber_foo_runner() : call_times(0) {}
20  int call_times;
21  int operator()(void *) {
22  ++call_times;
23  ++g_test_coroutine_fiber_status;
24  copp::this_fiber::get<test_coroutine_context_fiber_test_type>()->yield();
25 
26  ++g_test_coroutine_fiber_status;
27  return 0;
28  }
29 };
30 
31 CASE_TEST(coroutine_fiber, context_base) {
32  g_test_coroutine_fiber_status = 0;
33  ++g_test_coroutine_fiber_status;
34  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 1);
35 
36  test_context_fiber_foo_runner runner;
37  {
38  copp::stack_context test_move_alloc;
39 
40  test_coroutine_context_fiber_test_type::ptr_t co = test_coroutine_context_fiber_test_type::create(&runner);
41  runner.call_times = 0;
42 
43  CASE_EXPECT_TRUE(!!co);
44 
46 
47  co->start();
48 
49  ++g_test_coroutine_fiber_status;
50  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 3);
51  co->resume();
52 
53  ++g_test_coroutine_fiber_status;
54  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 5);
55 
58  }
59 
60  {
61  g_test_coroutine_fiber_status = 1;
62  copp::stack_context test_move_alloc;
63 
64  test_coroutine_context_fiber_test_type::ptr_t co = test_coroutine_context_fiber_test_type::create(&runner);
65  runner.call_times = 0;
66 
67  CASE_EXPECT_TRUE(!!co);
68 
70 
71  co->start();
72 
73  ++g_test_coroutine_fiber_status;
74  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 3);
75  co->resume();
76 
77  ++g_test_coroutine_fiber_status;
78  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 5);
79 
82  }
83 }
84 
85 CASE_TEST(coroutine_fiber, shared_runner) {
86  g_test_coroutine_fiber_status = 0;
87  ++g_test_coroutine_fiber_status;
88  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 1);
89 
90  test_context_fiber_foo_runner runner;
91  runner.call_times = 0;
92 
93  {
94  test_coroutine_context_fiber_test_type::ptr_t co[4];
95  for (int i = 0; i < 4; ++i) {
96  co[i] = test_coroutine_context_fiber_test_type::create(&runner);
97  co[i]->start();
98  }
99 
100  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 5);
101 
102  for (int i = 0; i < 4; ++i) {
103  co[i]->resume();
104  }
105 
106  CASE_EXPECT_EQ(g_test_coroutine_fiber_status, 9);
107 
108  CASE_EXPECT_EQ(runner.call_times, 4);
109  }
110 }
111 
112 # if defined(LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR) && LIBCOPP_MACRO_ENABLE_STD_EXCEPTION_PTR
113 static int test_context_fiber_foo_runner_throw_exception(void *) { return static_cast<int>(std::string().at(1)); }
114 
115 CASE_TEST(coroutine_fiber, coroutine_context_throw_exception) {
116  try {
117  copp::stack_context test_move_alloc;
118 
119  test_coroutine_context_fiber_test_type::ptr_t co =
120  test_coroutine_context_fiber_test_type::create(test_context_fiber_foo_runner_throw_exception);
121 
122  CASE_EXPECT_TRUE(!!co);
123 
125 
126  co->start();
127  } catch (const std::exception &e) {
128  CASE_MSG_INFO() << "Caught exception \"" << e.what() << "\"" << std::endl;
129  }
130 }
131 
132 # endif
133 
134 #endif
@ COPP_EC_NOT_READY
COPP_EC_NOT_READY.
Definition: errno.h:24
@ COPP_EC_ALREADY_EXIST
COPP_EC_ALREADY_EXIST.
Definition: errno.h:29
@ COPP_EC_NOT_RUNNING
COPP_EC_NOT_RUNNING.
Definition: errno.h:25
#define CASE_MSG_INFO()
Definition: test_macros.h:114
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:96
#define CASE_TEST(test_name, case_name)
Definition: test_macros.h:46
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:94