123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-09-09
- * Changed: 2022-05-20
- *
- * */
- #include <gtest/gtest.h>
- #include <ls_std/ls_std_core.hpp>
- #include <ls_std/ls_std_logic.hpp>
- #include <ls_std_logic_test.hpp>
- namespace
- {
- class StateMachineTest : public ::testing::Test
- {
- protected:
- StateMachineTest() = default;
- ~StateMachineTest() override = default;
- void SetUp() override
- {}
- void TearDown() override
- {}
- };
- TEST_F(StateMachineTest, getClassName)
- {
- ls::std::logic::StateMachine stateMachine {"machine"};
- ASSERT_STREQ("StateMachine", stateMachine.getClassName().c_str());
- }
- TEST_F(StateMachineTest, constructor_empty_name)
- {
- EXPECT_THROW({
- try
- {
- ls::std::logic::StateMachine stateMachine {""};
- }
- catch (const ls::std::core::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls::std::core::IllegalArgumentException);
- }
- TEST_F(StateMachineTest, addState)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
- }
- TEST_F(StateMachineTest, addState_state_already_exists)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
- ASSERT_FALSE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
- }
- TEST_F(StateMachineTest, addState_no_reference)
- {
- EXPECT_THROW({
- try
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- stateMachine.addState(nullptr);
- }
- catch (const ls::std::core::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls::std::core::IllegalArgumentException);
- }
- TEST_F(StateMachineTest, getCurrentState)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
- ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
- }
- TEST_F(StateMachineTest, getMemory)
- {
- ls::std::logic::StateMachine stateMachine {"machine"};
- ASSERT_TRUE(stateMachine.getMemory().empty());
- }
- TEST_F(StateMachineTest, getName)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
- }
- TEST_F(StateMachineTest, getStates)
- {
- ls::std::logic::StateMachine stateMachine {"machine"};
- ASSERT_TRUE(stateMachine.getStates().empty());
- }
- TEST_F(StateMachineTest, hasState)
- {
- ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
- ASSERT_TRUE(stateMachine.hasState("A"));
- ASSERT_TRUE(stateMachine.hasState("B"));
- ASSERT_TRUE(stateMachine.hasState("C"));
- ASSERT_TRUE(stateMachine.hasState("D"));
- ASSERT_TRUE(stateMachine.hasState("E"));
- }
- TEST_F(StateMachineTest, hasState_no_state_available)
- {
- ls::std::logic::StateMachine stateMachine {"machine"};
- ASSERT_FALSE(stateMachine.hasState("F"));
- }
- TEST_F(StateMachineTest, proceed)
- {
- ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
- ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
- ASSERT_TRUE(stateMachine.setStartState("A"));
- ASSERT_FALSE(stateMachine.proceed());
- // activate AB
- stateMachine.getCurrentState()->getConnectedStates().at("AB")->updatePassCondition(true);
- ASSERT_TRUE(stateMachine.proceed());
- ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
- // activate BC and BD
- stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(true);
- stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
- ASSERT_FALSE(stateMachine.proceed());
- // BC only!
- stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(false);
- ASSERT_TRUE(stateMachine.proceed());
- ASSERT_STREQ("C", stateMachine.getCurrentState()->getId().c_str());
- // activate CB
- stateMachine.getCurrentState()->getConnectedStates().at("CB")->updatePassCondition(true);
- ASSERT_TRUE(stateMachine.proceed());
- ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
- // BD only!
- stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
- stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(false);
- ASSERT_TRUE(stateMachine.proceed());
- ASSERT_STREQ("D", stateMachine.getCurrentState()->getId().c_str());
- // activate DE
- stateMachine.getCurrentState()->getConnectedStates().at("DE")->updatePassCondition(true);
- ASSERT_TRUE(stateMachine.proceed());
- ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
- // end reached
- ASSERT_FALSE(stateMachine.proceed());
- ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
- }
- TEST_F(StateMachineTest, setMemory_no_memory)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ::std::vector<ls::std::core::type::state_id> memory{};
- EXPECT_THROW({
- try
- {
- stateMachine.setMemory(memory);
- }
- catch (const ls::std::core::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls::std::core::IllegalArgumentException);
- }
- TEST_F(StateMachineTest, setName)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
- stateMachine.setName("bla");
- ASSERT_STREQ("bla", stateMachine.getName().c_str());
- }
- TEST_F(StateMachineTest, setName_empty_name)
- {
- EXPECT_THROW({
- try
- {
- ls::std::logic::StateMachine stateMachine {"machine"};
- stateMachine.setName("");
- }
- catch (const ls::std::core::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls::std::core::IllegalArgumentException);
- }
- TEST_F(StateMachineTest, setStartState)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
- stateMachine.addState(::std::make_shared<ls::std::logic::State>("A"));
- ASSERT_TRUE(stateMachine.setStartState("A"));
- ASSERT_FALSE(stateMachine.getCurrentState() == nullptr);
- }
- TEST_F(StateMachineTest, setStartState_state_does_not_exist)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- ASSERT_FALSE(stateMachine.setStartState("A"));
- }
- TEST_F(StateMachineTest, setStartState_empty_id)
- {
- ls::std::logic::StateMachine stateMachine{"test_machine"};
- EXPECT_THROW({
- try
- {
- stateMachine.setStartState("");
- }
- catch (const ls::std::core::IllegalArgumentException &_exception)
- {
- throw;
- }
- }, ls::std::core::IllegalArgumentException);
- }
- }
|