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 | #include "gate/tests.hpp"
#include "gate/coroutines.hpp"
using namespace gate;
static gate_result_t co1(void* param);
static gate_result_t co2(void* param);
static gate_result_t co_main(void* param);
struct co_params
{
Coroutine::id_t id0;
Coroutine::id_t id1;
Coroutine::id_t id2;
int state;
};
static gate_result_t co1(void* param)
{
co_params* ptr = (co_params*)param;
GATEXX_TEST_CHECK(ptr != NULL);
GATEXX_TEST_CHECK_EQUAL(ptr->state, 0);
ptr->state = 1;
// interrupt -> switch to co2
Coroutine::switchTo(ptr->id2);
// continue from co_main
GATEXX_TEST_CHECK_EQUAL(ptr->state, 3);
ptr->state = 4;
// interrupt -> switch to co_main
Coroutine::switchTo(ptr->id0);
// exit scenario:
GATEXX_TEST_CHECK_EQUAL(ptr->state, 5);
return results::Ok;
}
static gate_result_t co2(void* param)
{
co_params* ptr = (co_params*)param;
GATEXX_TEST_CHECK(ptr != NULL);
// come from co1
GATEXX_TEST_CHECK_EQUAL(ptr->state, 1);
ptr->state = 2;
// interrupt -> switch co_main
Coroutine::switchTo(ptr->id0);
// exit scenario:
GATEXX_TEST_CHECK_EQUAL(ptr->state, 5);
return results::Ok;
}
static gate_result_t co_main(void* param)
{
co_params* ptr = (co_params*)param;
GATEXX_TEST_CHECK(Coroutine::enabled());
GATEXX_TEST_CHECK_EQUAL(ptr->state, 0);
GATEXX_TEST_CHECK_NOTHROW(ptr->id0 = Coroutine::getCurrent());
GATEXX_TEST_CHECK_NOTHROW(ptr->id1 = Coroutine::create(&co1, param));
GATEXX_TEST_CHECK_NOTHROW(ptr->id2 = Coroutine::create(&co2, param));
GATEXX_TEST_CHECK_NOT_EQUAL(ptr->id0, ptr->id1);
GATEXX_TEST_CHECK_NOT_EQUAL(ptr->id0, ptr->id2);
GATEXX_TEST_CHECK_NOT_EQUAL(ptr->id1, ptr->id2);
GATEXX_TEST_CHECK_EQUAL(ptr->state, 0);
// start co1
Coroutine::switchTo(ptr->id1);
// return from co2
GATEXX_TEST_CHECK_EQUAL(ptr->state, 2);
ptr->state = 3;
// continue co1
Coroutine::switchTo(ptr->id1);
// return from co1
GATEXX_TEST_CHECK_EQUAL(ptr->state, 4);
// exit scenario:
ptr->state = 5;
return results::Ok;
}
GATEXX_TEST_UNIT(Coroutine)
{
if (!Coroutine::supported())
{
return;
}
GATEXX_TEST_CHECK(!Coroutine::enabled());
co_params params;
params.state = 0;
GATEXX_TEST_CHECK_NOTHROW(Coroutine::run(&co_main, ¶ms));
GATEXX_TEST_CHECK_EQUAL(params.state, 5);
}
class GATE_API_LOCAL Yielder : public IRunnable
{
public:
int yield_counter;
Yielder(int counter) : yield_counter(counter)<--- Class 'Yielder' has a constructor with 1 argument that is not explicit. [+]Class 'Yielder' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
{
}
virtual void run() override
{
while (this->yield_counter-- > 0)
{
GATEXX_TEST_CHECK_NOTHROW(Coroutine::yield());
}
}
};
class GATE_API_LOCAL Waiter : public IRunnable
{
public:
virtual void run() override
{
{
Yielder yielder(3);
Coroutine::id_t yield_job = 0;
GATEXX_TEST_REQUIRE_NOTHROW(yield_job = Coroutine::create(yielder));
result_t result = Coroutine::await(yield_job);
GATEXX_TEST_CHECK_OK(result);
}
{
Ptr<IRunnable> code = new Yielder(2);
Coroutine::id_t yield_job = 0;
GATEXX_TEST_REQUIRE_NOTHROW(yield_job = Coroutine::create(code));
result_t result = Coroutine::await(yield_job);
GATEXX_TEST_CHECK_OK(result);
}
}
};
GATEXX_TEST_UNIT(CoroutineWaitYield)
{
if (!Coroutine::supported())
{
return;
}
{
Ptr<IRunnable> waiter = new Waiter();
GATEXX_TEST_CHECK_NOTHROW(Coroutine::run(waiter));
}
{
Waiter waiter;
GATEXX_TEST_CHECK_NOTHROW(Coroutine::run(waiter));
}
}
|