StateMachineTest.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-09
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestDataFactory.hpp>
  12. namespace
  13. {
  14. class StateMachineTest : public ::testing::Test
  15. {
  16. protected:
  17. StateMachineTest() = default;
  18. ~StateMachineTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(StateMachineTest, addStateConnection)
  25. {
  26. ls_std::StateMachine stateMachine{"test_machine"};
  27. ASSERT_TRUE(stateMachine.addState(std::make_shared<ls_std::State>("A")));
  28. }
  29. TEST_F(StateMachineTest, addStateConnectionNegative)
  30. {
  31. ls_std::StateMachine stateMachine{"test_machine"};
  32. ASSERT_TRUE(stateMachine.addState(std::make_shared<ls_std::State>("A")));
  33. ASSERT_FALSE(stateMachine.addState(std::make_shared<ls_std::State>("A")));
  34. }
  35. TEST_F(StateMachineTest, getCurrentState)
  36. {
  37. ls_std::StateMachine stateMachine{"test_machine"};
  38. ASSERT_TRUE(stateMachine.addState(std::make_shared<ls_std::State>("A")));
  39. ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
  40. }
  41. TEST_F(StateMachineTest, getMemory)
  42. {
  43. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  44. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  45. ASSERT_EQ(0, stateMachine.getMemory().size());
  46. stateMachine.setStartState("A");
  47. ASSERT_EQ(1, stateMachine.getMemory().size());
  48. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  49. ASSERT_FALSE(stateMachine.proceed());
  50. ASSERT_EQ(1, stateMachine.getMemory().size());
  51. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  52. stateMachine.getCurrentState()->getConnectedStates().at("AB")->updatePassCondition(true);
  53. ASSERT_TRUE(stateMachine.proceed());
  54. ASSERT_EQ(2, stateMachine.getMemory().size());
  55. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  56. ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
  57. stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(true);
  58. ASSERT_TRUE(stateMachine.proceed());
  59. ASSERT_EQ(3, stateMachine.getMemory().size());
  60. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  61. ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
  62. ASSERT_STREQ("C", stateMachine.getMemory().at(2).c_str());
  63. stateMachine.getCurrentState()->getConnectedStates().at("CB")->updatePassCondition(true);
  64. ASSERT_TRUE(stateMachine.proceed());
  65. ASSERT_EQ(4, stateMachine.getMemory().size());
  66. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  67. ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
  68. ASSERT_STREQ("C", stateMachine.getMemory().at(2).c_str());
  69. ASSERT_STREQ("B", stateMachine.getMemory().at(3).c_str());
  70. }
  71. TEST_F(StateMachineTest, getName)
  72. {
  73. ls_std::StateMachine stateMachine{"test_machine"};
  74. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  75. }
  76. TEST_F(StateMachineTest, getStates)
  77. {
  78. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  79. ASSERT_TRUE(!stateMachine.getStates().empty());
  80. ASSERT_EQ(5, stateMachine.getStates().size());
  81. }
  82. TEST_F(StateMachineTest, hasState)
  83. {
  84. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  85. ASSERT_TRUE(stateMachine.hasState("A"));
  86. ASSERT_TRUE(stateMachine.hasState("B"));
  87. ASSERT_TRUE(stateMachine.hasState("C"));
  88. ASSERT_TRUE(stateMachine.hasState("D"));
  89. ASSERT_TRUE(stateMachine.hasState("E"));
  90. }
  91. TEST_F(StateMachineTest, hasStateNegative)
  92. {
  93. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  94. ASSERT_FALSE(stateMachine.hasState("F"));
  95. }
  96. TEST_F(StateMachineTest, proceed)
  97. {
  98. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  99. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  100. ASSERT_TRUE(stateMachine.setStartState("A"));
  101. ASSERT_FALSE(stateMachine.proceed());
  102. // activate AB
  103. stateMachine.getCurrentState()->getConnectedStates().at("AB")->updatePassCondition(true);
  104. ASSERT_TRUE(stateMachine.proceed());
  105. ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
  106. // activate BC and BD
  107. stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(true);
  108. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
  109. ASSERT_FALSE(stateMachine.proceed());
  110. // BC only!
  111. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(false);
  112. ASSERT_TRUE(stateMachine.proceed());
  113. ASSERT_STREQ("C", stateMachine.getCurrentState()->getId().c_str());
  114. // activate CB
  115. stateMachine.getCurrentState()->getConnectedStates().at("CB")->updatePassCondition(true);
  116. ASSERT_TRUE(stateMachine.proceed());
  117. ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
  118. // BD only!
  119. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
  120. stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(false);
  121. ASSERT_TRUE(stateMachine.proceed());
  122. ASSERT_STREQ("D", stateMachine.getCurrentState()->getId().c_str());
  123. // activate DE
  124. stateMachine.getCurrentState()->getConnectedStates().at("DE")->updatePassCondition(true);
  125. ASSERT_TRUE(stateMachine.proceed());
  126. ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
  127. // end reached
  128. ASSERT_FALSE(stateMachine.proceed());
  129. ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
  130. }
  131. TEST_F(StateMachineTest, setMemory)
  132. {
  133. ls_std::StateMachine stateMachine{"test_machine"};
  134. ASSERT_TRUE(stateMachine.getMemory().empty());
  135. std::vector<ls_std::StateId> memory{"A", "B", "C"};
  136. stateMachine.setMemory(memory);
  137. ASSERT_FALSE(stateMachine.getMemory().empty());
  138. ASSERT_EQ(3, stateMachine.getMemory().size());
  139. ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
  140. ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
  141. ASSERT_STREQ("C", stateMachine.getMemory().at(2).c_str());
  142. }
  143. TEST_F(StateMachineTest, setName)
  144. {
  145. ls_std::StateMachine stateMachine{"test_machine"};
  146. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  147. stateMachine.setName("bla");
  148. ASSERT_STREQ("bla", stateMachine.getName().c_str());
  149. }
  150. TEST_F(StateMachineTest, setStartState)
  151. {
  152. ls_std::StateMachine stateMachine{"test_machine"};
  153. ASSERT_FALSE(stateMachine.getCurrentState() != nullptr);
  154. ASSERT_TRUE(stateMachine.addState(std::make_shared<ls_std::State>("A")));
  155. ASSERT_TRUE(stateMachine.setStartState("A"));
  156. ASSERT_TRUE(stateMachine.getCurrentState() != nullptr);
  157. }
  158. }