-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
entry_exit_pseudo_states.cpp
164 lines (142 loc) · 4.76 KB
/
entry_exit_pseudo_states.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
#include "hsm/hsm.h"
#include <boost/hana/experimental/printable.hpp>
#include <gtest/gtest.h>
using namespace ::testing;
using namespace boost::hana;
namespace {
// States
struct S1 {
};
struct S2 {
};
struct S3 {
};
struct S4 {
};
struct S5 {
};
// Events
struct e1 {
};
struct e2 {
};
struct e3 {
};
struct e4 {
};
struct e5 {
};
struct e6 {
};
// Guards
const auto fail = [](auto /*event*/, auto /*source*/, auto /*target*/) { return false; };
const auto success = [](auto /*event*/, auto /*source*/, auto /*target*/) { return true; };
// Actions
const auto action = [](auto /*event*/, auto /*source*/, auto /*target*/) {};
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<e2> = hsm::state<S3>,
hsm::state<S1> + hsm::event<e4> = hsm::state<S4>,
hsm::state<S1> + hsm::event<e5> = hsm::state<S5>
);
// 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::entry<SubState, S3>,
hsm::state<S1> + hsm::event<e2> = hsm::state<SubState>,
hsm::state<S1> + hsm::event<e3> [fail] = hsm::entry<SubState, S3>,
hsm::state<S1> + hsm::event<e4> [fail] / action = hsm::entry<SubState, S3>,
hsm::state<S1> + hsm::event<e5> / action = hsm::entry<SubState, S3>,
hsm::state<S1> + hsm::event<e6> = hsm::state<S2>,
hsm::state<S2> = hsm::entry<SubState, S3>,
hsm::exit<SubState, S4> + hsm::event<e1> = hsm::state<S1>,
hsm::exit<SubState, S4> + hsm::event<e2> [success] = hsm::state<S1>,
hsm::exit<SubState, S4> + hsm::event<e3> [success] / action = hsm::state<S1>,
hsm::exit<SubState, S4> + hsm::event<e4> / action = hsm::state<S1>,
hsm::exit<SubState, S5> = hsm::state<S1>
);
// clang-format on
}
};
}
class EntryPseudoStatesTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(EntryPseudoStatesTests, should_entry_substate_on_pseudo_entry)
{
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S3>));
}
TEST_F(EntryPseudoStatesTests, should_entry_substate_on_pseudo_entry_with_anonymous_transition)
{
sm.process_event(e6 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S3>));
}
TEST_F(EntryPseudoStatesTests, should_fail_to_entry_substate_on_pseudo_entry)
{
sm.process_event(e3 {});
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
}
TEST_F(EntryPseudoStatesTests, should_fail_to_entry_substate_on_pseudo_entry_with_action)
{
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
}
TEST_F(EntryPseudoStatesTests, should_entry_substate_on_pseudo_entry_with_action)
{
sm.process_event(e5 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S3>));
}
class ExitPseudoStatesTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(ExitPseudoStatesTests, should_exit_subsubstate_from_pseudo_exit)
{
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e4 {});
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(ExitPseudoStatesTests, should_exit_subsubstate_from_pseudo_exit_with_action)
{
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e4 {});
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(ExitPseudoStatesTests, should_exit_subsubstate_from_pseudo_exit_with_guard)
{
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e4 {});
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(ExitPseudoStatesTests, should_exit_subsubstate_from_pseudo_exit_with_action_and_guard)
{
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e4 {});
sm.process_event(e3 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(ExitPseudoStatesTests, should_exit_subsubstate_from_pseudo_exit_with_anonymous_transition)
{
sm.process_event(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
sm.process_event(e5 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}