-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
basic_transitions.cpp
193 lines (164 loc) · 5.24 KB
/
basic_transitions.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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <boost/hana/experimental/printable.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
using namespace ::testing;
using namespace boost::hana;
// States
struct S1 {
};
struct S2 {
};
struct S3 {
};
struct S4 {
};
// Events
struct e1 {
};
struct e2 {
};
struct e3 {
};
struct e4 {
};
struct e5 {
};
struct e6 {
};
// Actions
const auto log = [](auto event, auto source, auto target) {
std::cout << experimental::print(typeid_(source)) << " + "
<< experimental::print(typeid_(event)) << " = "
<< experimental::print(typeid_(target)) << std::endl;
};
struct SubSubState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> / log = hsm::state<S2>
);
// clang-format on
}
};
struct SubState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> / log = hsm::state<S2>
, hsm::state<S1> + hsm::event<e5> / log = hsm::state<S3>
, hsm::state<S2> + hsm::event<e1> / log = hsm::state<SubSubState>
, hsm::state<SubSubState> + hsm::event<e2> / log = hsm::state<S1>
);
// clang-format on
}
};
struct MainState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
// Source , Event , Target
* hsm::state<S1> + hsm::event<e1> / log = hsm::state<S2>
, hsm::state<S1> + hsm::event<e2> / log = hsm::state<S3>
, hsm::state<S1> + hsm::event<e4> / log = hsm::state<SubState>
, hsm::state<S1> + hsm::event<e5> / log = hsm::state<S3>
, hsm::state<S1> + hsm::event<e6> / log = hsm::state<S1>
, hsm::state<S2> + hsm::event<e1> / log = hsm::state<S1>
, hsm::state<S2> + hsm::event<e2> / log = hsm::state<S1>
, hsm::state<S2> + hsm::event<e3> / log = hsm::state<S3>
, hsm::state<SubState> + hsm::event<e2> / log = hsm::state<S1>
, hsm::state<SubState> + hsm::event<e4> / log = hsm::state<SubState>
);
// clang-format on
}
static constexpr auto on_unexpected_event()
{
return [](auto event, auto /*currentState*/) {
throw std::runtime_error(
std::string("unexpected event ") + experimental::print(typeid_(event)));
};
}
};
}
class BasicTransitionTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(BasicTransitionTests, should_start_in_initial_state)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_start_in_root_state)
{
ASSERT_TRUE(sm.parent_is(hsm::state<MainState>));
}
TEST_F(BasicTransitionTests, should_process_event)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
TEST_F(BasicTransitionTests, should_transit_into_SubState)
{
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_transit_into_SubSubState)
{
sm.process_event(e4 {});
sm.process_event(e1 {});
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubSubState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_transit_in_SubState_with_unique_event)
{
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e5 {});
ASSERT_FALSE(sm.is(hsm::state<SubState>, hsm::state<S1>));
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S3>));
}
TEST_F(BasicTransitionTests, should_exit_substate_on_event_in_parentstate)
{
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_exit_subsubstate_on_event_in_parentstate)
{
sm.process_event(e4 {});
sm.process_event(e1 {});
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubSubState>, hsm::state<S1>));
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_reentry_substate_on_initial_state)
{
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
sm.process_event(e4 {});
ASSERT_TRUE(sm.parent_is(hsm::state<SubState>));
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_self_transit)
{
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
sm.process_event(e6 {});
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
sm.process_event(e6 {});
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
}
TEST_F(BasicTransitionTests, should_fail_when_access_invalid_region)
{
ASSERT_THROW(sm.is(15, hsm::state<MainState>, hsm::state<S1>), std::out_of_range);
}