forked from sPHENIX-Collaboration/acts
/
FiniteStateMachineTests.cpp
287 lines (230 loc) · 7.44 KB
/
FiniteStateMachineTests.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// This file is part of the Acts project.
//
// Copyright (C) 2019 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <boost/test/unit_test.hpp>
#include "Acts/Utilities/FiniteStateMachine.hpp"
#include <iostream>
namespace tt = boost::test_tools;
namespace Acts {
namespace Test {
namespace states {
struct Disconnected {};
struct Connecting {};
struct Pinging {};
struct Connected {};
} // namespace states
namespace events {
struct Connect {};
struct Established {};
struct Timeout {};
struct Ping {};
struct Pong {};
struct Disconnect {};
} // namespace events
struct fsm : FiniteStateMachine<fsm, states::Disconnected, states::Connecting,
states::Pinging, states::Connected> {
fsm() : fsm_base(states::Disconnected{}){};
event_return on_event(const states::Disconnected&, const events::Connect&) {
return states::Connecting{};
}
event_return on_event(const states::Connecting&, const events::Established&) {
return states::Connected{};
}
event_return on_event(const states::Connected&, const events::Ping&) {
std::cout << "ping!" << std::endl;
setState(states::Pinging{});
return process_event(events::Pong{});
}
event_return on_event(const states::Pinging&, const events::Pong&) {
std::cout << "pong!" << std::endl;
return states::Connected{};
}
event_return on_event(const states::Connected&, const events::Timeout&) {
return states::Connecting{};
}
event_return on_event(const states::Connected&, const events::Disconnect&) {
return states::Disconnected{};
}
template <typename State, typename Event>
event_return on_event(const State&, const Event&) const {
return Terminated{};
}
template <typename State, typename... Args>
void on_enter(const State&, Args&&...) {}
template <typename State, typename... Args>
void on_exit(const State&, Args&&...) {}
template <typename... Args>
void on_process(Args&&...) {}
};
BOOST_AUTO_TEST_SUITE(Utilities)
BOOST_AUTO_TEST_CASE(Transitions) {
fsm sm{};
BOOST_CHECK(sm.is(states::Disconnected{}));
sm.dispatch(events::Connect{});
BOOST_CHECK(sm.is(states::Connecting{}));
sm.dispatch(events::Established{});
BOOST_CHECK(sm.is(states::Connected{}));
sm.dispatch(events::Ping{});
sm.dispatch(events::Ping{});
sm.dispatch(events::Ping{});
sm.dispatch(events::Ping{});
BOOST_CHECK(sm.is(states::Connected{}));
sm.dispatch(events::Timeout{});
BOOST_CHECK(sm.is(states::Connecting{}));
sm.dispatch(events::Established{});
BOOST_CHECK(sm.is(states::Connected{}));
sm.dispatch(events::Disconnect{});
BOOST_CHECK(sm.is(states::Disconnected{}));
}
BOOST_AUTO_TEST_CASE(Terminted) {
fsm sm{};
BOOST_CHECK(sm.is(states::Disconnected{}));
sm.dispatch(events::Disconnect{});
BOOST_CHECK(sm.terminated());
}
struct fsm2
: FiniteStateMachine<fsm2, states::Disconnected, states::Connected> {
fsm2() : fsm_base(states::Disconnected{}){};
event_return on_event(const states::Disconnected&, const events::Connect&,
double f) {
std::cout << "f: " << f << std::endl;
return states::Connected{};
}
event_return on_event(const states::Connected&, const events::Disconnect&) {
std::cout << "disconnect!" << std::endl;
return states::Disconnected{};
}
template <typename State, typename Event, typename... Args>
event_return on_event(const State&, const Event&, Args&&...) const {
return Terminated{};
}
template <typename... Args>
void on_enter(const Terminated&, Args&&...) {
throw std::runtime_error("FSM terminated!");
}
template <typename State, typename... Args>
void on_enter(const State&, Args&&...) {}
template <typename State, typename... Args>
void on_exit(const State&, Args&&...) {}
template <typename... Args>
void on_process(Args&&...) {}
};
BOOST_AUTO_TEST_CASE(Arguments) {
fsm2 sm{};
BOOST_CHECK(sm.is(states::Disconnected{}));
sm.dispatch(events::Connect{}, 42.);
BOOST_CHECK(sm.is(states::Connected{}));
sm.dispatch(events::Disconnect{});
BOOST_CHECK(sm.is(states::Disconnected{}));
sm.dispatch(events::Connect{}, -1.);
// call disconnect, but disconnect does not accept this call signature
BOOST_REQUIRE_THROW(sm.dispatch(events::Disconnect{}, 9), std::runtime_error);
BOOST_CHECK(sm.terminated());
// cannot dispatch on terminated (in this specific configuration, in
// general terminated is just another state).
BOOST_REQUIRE_THROW(sm.dispatch(events::Connect{}), std::runtime_error);
// still in terminated
BOOST_CHECK(sm.terminated());
// we can reset the state though!
sm.setState(states::Disconnected{});
BOOST_CHECK(sm.is(states::Disconnected{}));
sm.dispatch(events::Connect{}, -1.);
BOOST_CHECK(sm.is(states::Connected{}));
}
struct S1 {};
struct S2 {};
struct S3 {};
struct E1 {};
struct E2 {};
struct E3 {};
struct fsm3 : FiniteStateMachine<fsm3, S1, S2, S3> {
bool on_exit_called = false;
bool on_enter_called = false;
bool on_process_called = false;
void reset() {
on_exit_called = false;
on_enter_called = false;
on_process_called = false;
}
// S1 + E1 = S2
event_return on_event(const S1&, const E1&) { return S2{}; }
// S2 + E1 = S2
// external transition to self
event_return on_event(const S2&, const E1&) { return S2{}; }
// S2 + E2
// internal transition
event_return on_event(const S2&, const E2&) {
return std::nullopt;
// return S2{};
}
// S2 + E3 = S3
// external transition
event_return on_event(const S2&, const E3&) { return S3{}; }
// catchers
template <typename State, typename Event, typename... Args>
event_return on_event(const State&, const Event&, Args&&...) const {
return Terminated{};
}
template <typename State, typename... Args>
void on_enter(const State&, Args&&...) {
on_enter_called = true;
}
template <typename State, typename... Args>
void on_exit(const State&, Args&&...) {
on_exit_called = true;
}
template <typename... Args>
void on_process(Args&&...) {
on_process_called = true;
}
};
BOOST_AUTO_TEST_CASE(InternalTransitions) {
fsm3 sm;
BOOST_CHECK(sm.is(S1{}));
sm.dispatch(E1{});
BOOST_CHECK(sm.is(S2{}));
BOOST_CHECK(sm.on_exit_called);
BOOST_CHECK(sm.on_enter_called);
BOOST_CHECK(sm.on_process_called);
sm.reset();
sm.dispatch(E1{});
// still in S2
BOOST_CHECK(sm.is(S2{}));
// on_enter / exit should have been called
BOOST_CHECK(sm.on_exit_called);
BOOST_CHECK(sm.on_enter_called);
BOOST_CHECK(sm.on_process_called);
sm.reset();
sm.dispatch(E2{});
// still in S2
BOOST_CHECK(sm.is(S2{}));
// on_enter / exit should NOT have been called
BOOST_CHECK(!sm.on_exit_called);
BOOST_CHECK(!sm.on_enter_called);
BOOST_CHECK(sm.on_process_called);
sm.reset();
sm.dispatch(E3{});
BOOST_CHECK(sm.is(S3{}));
// on_enter / exit should have been called
BOOST_CHECK(sm.on_exit_called);
BOOST_CHECK(sm.on_enter_called);
BOOST_CHECK(sm.on_process_called);
sm.setState(S1{});
sm.reset();
BOOST_CHECK(sm.is(S1{}));
// dispatch invalid event
sm.dispatch(E3{});
// should be terminated now
BOOST_CHECK(sm.terminated());
// hooks should have fired
BOOST_CHECK(sm.on_exit_called);
BOOST_CHECK(sm.on_enter_called);
BOOST_CHECK(sm.on_process_called);
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace Test
} // namespace Acts