From 3d696c8f0677f51ec3da5214060fb8d73bcf3948 Mon Sep 17 00:00:00 2001 From: revsic Date: Sun, 7 Apr 2019 02:27:06 +0900 Subject: [PATCH] obfs, test: Add tests for fsm --- test/impl/fsm.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 test/impl/fsm.cpp 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); +}