diff --git a/test/impl/fsm.cpp b/test/impl/fsm.cpp new file mode 100644 index 0000000..d819bff --- /dev/null +++ b/test/impl/fsm.cpp @@ -0,0 +1,102 @@ +#include +#include + +#define STATE(Name) struct Name {}; +#define EVENT(Name) struct Name {}; + +STATE(Final); +EVENT(Trigger); + +STATE(state1); STATE(state2); STATE(state3); STATE(state4); STATE(state5); +EVENT(event1); EVENT(event2); EVENT(event3); EVENT(event4); EVENT(event5); + +struct Action { + static bool trigged; + static void action() { + trigged = true; + } +}; +bool Action::trigged = true; + +struct Dummy { + static int dummy; + static void dummy1() { + dummy += 10; + } + static void dummy2() { + dummy *= 20; + } +}; +int Dummy::dummy = 0; + +TEST_CASE("Next", "[obfs::StateMachine]") { + using next = obfs::Next; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(next::action == obfs::FreeAction); + + using next2 = obfs::Next; + static_assert(next2::action == Action::action); +} + +TEST_CASE("Stage", "[obfs::StateMachine]") { + using stage = obfs::Stage< + state1, + obfs::Next, + obfs::Next>; + + static_assert(std::is_same_v< + obfs::Next, + typename stage::template next>); + + static_assert(std::is_same_v< + obfs::Next, + typename obfs::next_stage::type>); + + static_assert(std::is_same_v< + obfs::Next, + typename stage::template next>); + + static_assert(std::is_same_v< + obfs::Next, + typename obfs::next_stage::type>); + + static_assert(std::is_same_v>); + + static_assert(std::is_same_v::type>); + + static_assert(std::is_same_v::type>); +} + +TEST_CASE("StateMachine", "[obfs::StateMachine]") { + using namespace obfs; + using machine = StateMachine< + Stage, + Next>, + Stage>, + Stage>, + Stage, + Next>, + Stage>>; + + auto failure = machine::run(obfs::None{}, event5{}); + static_assert(std::is_same_v); + + auto next1 = machine::run(state1{}, event5{}); + static_assert(std::is_same_v); + REQUIRE(Dummy::dummy == 10); + + auto next2 = machine::run(next1, event2{}); + static_assert(std::is_same_v); + + auto failure2 = machine::run(next2, event1{}); + static_assert(std::is_same_v); + + auto next3 = machine::run(next2, event3{}); + static_assert(std::is_same_v); + REQUIRE(Dummy::dummy == 200); + + auto next4 = machine::run(next3, Trigger{}); + static_assert(std::is_same_v); + REQUIRE(Action::trigged); +}