-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
history_pseudo_state.cpp
85 lines (73 loc) · 2.13 KB
/
history_pseudo_state.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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <boost/hana/experimental/printable.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
// States
struct S1 {
};
struct S2 {
};
struct S3 {
};
// Events
struct e1 {
};
struct e2 {
};
struct e3 {
};
using namespace ::testing;
using namespace boost::hana;
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 SubState {
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 MainState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> / log = hsm::state<SubState>
, hsm::state<S1> + hsm::event<e2> / log = hsm::history<SubState>
, hsm::state<SubState> + hsm::event<e3> / log = hsm::state<S2>
, hsm::state<SubState> + hsm::event<e2> / log = hsm::history<SubState>
);
// clang-format on
}
};
}
class HistoryPseudoStateTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(HistoryPseudoStateTests, should_reentry_substate_in_history_state)
{
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
sm.process_event(e1 {});
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(e2 {});
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S2>));
}
TEST_F(HistoryPseudoStateTests, should_enter_history_substate_first_time_on_initial_state)
{
ASSERT_TRUE(sm.is(hsm::state<MainState>, hsm::state<S1>));
sm.process_event(e2 {});
ASSERT_FALSE(sm.is(hsm::state<MainState>, hsm::state<S1>));
ASSERT_TRUE(sm.is(hsm::state<SubState>, hsm::state<S1>));
}