-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
internal_transition.cpp
239 lines (206 loc) · 6.1 KB
/
internal_transition.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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
// States
struct S1 {
};
struct S2 {
static constexpr auto on_entry()
{
return [](auto event, auto...) { (void)event.name; };
}
static constexpr auto on_exit()
{
return [](auto event, auto...) { (void)event.name; };
}
};
struct S3 {
};
// Events
struct e1 {
std::string name = "e1";
};
struct e2 {
e2(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
std::string name = "e2";
};
struct e3 {
};
struct e4 {
e4(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e5 {
};
struct e6 {
std::string name = "e2";
};
struct e7 {
std::string name = "e7";
};
struct e8 {
};
struct e9 {
};
struct e10 { };
struct e11 { };
// Guards
const auto fail = [](auto /*event*/, auto /*source*/, auto /*target*/) { return false; };
// Actions
const auto action = [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
const auto error = [](auto /*event*/, auto /*source*/, auto /*target*/) { EXPECT_TRUE(false); };
using namespace ::testing;
using namespace boost::hana;
struct SubState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> = hsm::state<S2>
, hsm::state<S1> + hsm::event<e5> = hsm::state<S3>
);
// clang-format on
}
static constexpr auto make_internal_transition_table()
{
// clang-format off
return hsm::transition_table(
+ (hsm::event<e1>),
+ (hsm::event<e8> / hsm::log)
);
// clang-format on
}
};
struct InternalOnlySubState {
static constexpr auto make_internal_transition_table()
{
// clang-format off
return hsm::transition_table(
+ (hsm::event<e11>)
);
// clang-format on
}
};
struct MainState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> = hsm::state<S2>
, hsm::state<S1> + hsm::event<e7> = hsm::state<S2>
, hsm::state<S1> + hsm::event<e3> = hsm::state<SubState>
, hsm::state<S2> + hsm::event<e6> = hsm::state<S1>
, hsm::state<S1> + hsm::event<e10> = hsm::state<InternalOnlySubState>
);
// clang-format on
}
static constexpr auto make_internal_transition_table()
{
// clang-format off
return hsm::transition_table(
+ (hsm::event<e1> ),
+ (hsm::event<e2> / action),
+ (hsm::event<e5> [fail] / hsm::log),
+ (hsm::event<e4> [fail] / action),
+ (hsm::event<e8> / hsm::chain(hsm::log, error)),
+ (hsm::event<e9>)
);
// clang-format on
}
};
}
class InternalTransitionTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(InternalTransitionTests, should_overwrite_internal_by_inner_external_transition)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
TEST_F(InternalTransitionTests, should_overwrite_internal_by_inner_transition_in_substate)
{
sm.process_event(e3 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
}
TEST_F(InternalTransitionTests, should_call_action_on_internal_transition)
{
auto actionCalled = std::make_shared<std::promise<void>>();
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e2 { actionCalled });
ASSERT_TRUE(sm.is(hsm::state<S1>));
ASSERT_TRUE(
std::future_status::ready == actionCalled->get_future().wait_for(std::chrono::seconds(1)));
}
TEST_F(InternalTransitionTests, should_guard_internal_transition)
{
auto actionCalled = std::make_shared<std::promise<void>>();
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e5 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(InternalTransitionTests, should_guard_internal_transition_with_action)
{
auto actionCalled = std::make_shared<std::promise<void>>();
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e4 { actionCalled });
ASSERT_TRUE(sm.is(hsm::state<S1>));
ASSERT_TRUE(
std::future_status::timeout
== actionCalled->get_future().wait_for(std::chrono::milliseconds(1)));
}
TEST_F(
InternalTransitionTests, should_not_overwrite_substate_transition_by_parent_internal_transition)
{
sm.process_event(e3 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e5 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S3>));
}
TEST_F(
InternalTransitionTests,
should_not_overwrite_substate_internal_transition_by_parent_internal_transition)
{
sm.process_event(e3 {});
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
sm.process_event(e8 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
}
TEST_F(InternalTransitionTests, should_not_transit_when_internal_transition_on_parent_state)
{
sm.process_event(e3 {});
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
sm.process_event(e9 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
}
TEST_F(InternalTransitionTests, should_not_transit_to_initial_state)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e7 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
TEST_F(InternalTransitionTests, should_recognize_substates_with_only_internal_transition_table)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e10 {});
ASSERT_TRUE(sm.is(hsm::state<InternalOnlySubState>));
sm.process_event(e11 {});
ASSERT_TRUE(sm.is(hsm::state<InternalOnlySubState>));
}