forked from erikzenker/hsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_transition.cpp
148 lines (123 loc) · 3.57 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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
// States
struct S1 {
};
struct S2 {
};
// Events
struct e1 {
};
struct e2 {
e2(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e3 {
};
struct e4 {
e4(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e5 {
};
// 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(); };
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> {}
);
// clang-format on
}
constexpr auto make_internal_transition_table()
{
// clang-format off
return hsm::transition_table(
+ (hsm::event<e1> {})
);
// 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<e2> {} = hsm::state<S2> {},
hsm::state<S1> {} + hsm::event<e3> {} = hsm::state<SubState> {}
);
// clang-format on
}
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::event<e4>{} [fail] / action)
);
// clang-format on
}
};
}
class InternalTransitionTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(InternalTransitionTests, should_overwrite_external_by_internal_transition)
{
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
sm.process_event(e1 {});
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
}
TEST_F(InternalTransitionTests, should_overwrite_external_by_internal_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<S1> {}));
}
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_EQ(
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_EQ(
std::future_status::timeout,
actionCalled->get_future().wait_for(std::chrono::milliseconds(1)));
}