StateMachineTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-09
  6. * Changed: 2022-05-20
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_logic.hpp>
  12. #include <ls_std_logic_test.hpp>
  13. namespace
  14. {
  15. class StateMachineTest : public ::testing::Test
  16. {
  17. protected:
  18. StateMachineTest() = default;
  19. ~StateMachineTest() override = default;
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. TEST_F(StateMachineTest, getClassName)
  26. {
  27. ls::std::logic::StateMachine stateMachine {"machine"};
  28. ASSERT_STREQ("StateMachine", stateMachine.getClassName().c_str());
  29. }
  30. TEST_F(StateMachineTest, constructor_empty_name)
  31. {
  32. EXPECT_THROW({
  33. try
  34. {
  35. ls::std::logic::StateMachine stateMachine {""};
  36. }
  37. catch (const ls::std::core::IllegalArgumentException &_exception)
  38. {
  39. throw;
  40. }
  41. }, ls::std::core::IllegalArgumentException);
  42. }
  43. TEST_F(StateMachineTest, addState)
  44. {
  45. ls::std::logic::StateMachine stateMachine{"test_machine"};
  46. ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
  47. }
  48. TEST_F(StateMachineTest, addState_state_already_exists)
  49. {
  50. ls::std::logic::StateMachine stateMachine{"test_machine"};
  51. ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
  52. ASSERT_FALSE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
  53. }
  54. TEST_F(StateMachineTest, addState_no_reference)
  55. {
  56. EXPECT_THROW({
  57. try
  58. {
  59. ls::std::logic::StateMachine stateMachine{"test_machine"};
  60. stateMachine.addState(nullptr);
  61. }
  62. catch (const ls::std::core::IllegalArgumentException &_exception)
  63. {
  64. throw;
  65. }
  66. }, ls::std::core::IllegalArgumentException);
  67. }
  68. TEST_F(StateMachineTest, getCurrentState)
  69. {
  70. ls::std::logic::StateMachine stateMachine{"test_machine"};
  71. ASSERT_TRUE(stateMachine.addState(::std::make_shared<ls::std::logic::State>("A")));
  72. ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
  73. }
  74. TEST_F(StateMachineTest, getMemory)
  75. {
  76. ls::std::logic::StateMachine stateMachine {"machine"};
  77. ASSERT_TRUE(stateMachine.getMemory().empty());
  78. }
  79. TEST_F(StateMachineTest, getName)
  80. {
  81. ls::std::logic::StateMachine stateMachine{"test_machine"};
  82. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  83. }
  84. TEST_F(StateMachineTest, getStates)
  85. {
  86. ls::std::logic::StateMachine stateMachine {"machine"};
  87. ASSERT_TRUE(stateMachine.getStates().empty());
  88. }
  89. TEST_F(StateMachineTest, hasState)
  90. {
  91. ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
  92. ASSERT_TRUE(stateMachine.hasState("A"));
  93. ASSERT_TRUE(stateMachine.hasState("B"));
  94. ASSERT_TRUE(stateMachine.hasState("C"));
  95. ASSERT_TRUE(stateMachine.hasState("D"));
  96. ASSERT_TRUE(stateMachine.hasState("E"));
  97. }
  98. TEST_F(StateMachineTest, hasState_no_state_available)
  99. {
  100. ls::std::logic::StateMachine stateMachine {"machine"};
  101. ASSERT_FALSE(stateMachine.hasState("F"));
  102. }
  103. TEST_F(StateMachineTest, proceed)
  104. {
  105. ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
  106. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  107. ASSERT_TRUE(stateMachine.setStartState("A"));
  108. ASSERT_FALSE(stateMachine.proceed());
  109. // activate AB
  110. stateMachine.getCurrentState()->getConnectedStates().at("AB")->updatePassCondition(true);
  111. ASSERT_TRUE(stateMachine.proceed());
  112. ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
  113. // activate BC and BD
  114. stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(true);
  115. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
  116. ASSERT_FALSE(stateMachine.proceed());
  117. // BC only!
  118. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(false);
  119. ASSERT_TRUE(stateMachine.proceed());
  120. ASSERT_STREQ("C", stateMachine.getCurrentState()->getId().c_str());
  121. // activate CB
  122. stateMachine.getCurrentState()->getConnectedStates().at("CB")->updatePassCondition(true);
  123. ASSERT_TRUE(stateMachine.proceed());
  124. ASSERT_STREQ("B", stateMachine.getCurrentState()->getId().c_str());
  125. // BD only!
  126. stateMachine.getCurrentState()->getConnectedStates().at("BD")->updatePassCondition(true);
  127. stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(false);
  128. ASSERT_TRUE(stateMachine.proceed());
  129. ASSERT_STREQ("D", stateMachine.getCurrentState()->getId().c_str());
  130. // activate DE
  131. stateMachine.getCurrentState()->getConnectedStates().at("DE")->updatePassCondition(true);
  132. ASSERT_TRUE(stateMachine.proceed());
  133. ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
  134. // end reached
  135. ASSERT_FALSE(stateMachine.proceed());
  136. ASSERT_STREQ("E", stateMachine.getCurrentState()->getId().c_str());
  137. }
  138. TEST_F(StateMachineTest, setMemory_no_memory)
  139. {
  140. ls::std::logic::StateMachine stateMachine{"test_machine"};
  141. ::std::vector<ls::std::core::type::state_id> memory{};
  142. EXPECT_THROW({
  143. try
  144. {
  145. stateMachine.setMemory(memory);
  146. }
  147. catch (const ls::std::core::IllegalArgumentException &_exception)
  148. {
  149. throw;
  150. }
  151. }, ls::std::core::IllegalArgumentException);
  152. }
  153. TEST_F(StateMachineTest, setName)
  154. {
  155. ls::std::logic::StateMachine stateMachine{"test_machine"};
  156. ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
  157. stateMachine.setName("bla");
  158. ASSERT_STREQ("bla", stateMachine.getName().c_str());
  159. }
  160. TEST_F(StateMachineTest, setName_empty_name)
  161. {
  162. EXPECT_THROW({
  163. try
  164. {
  165. ls::std::logic::StateMachine stateMachine {"machine"};
  166. stateMachine.setName("");
  167. }
  168. catch (const ls::std::core::IllegalArgumentException &_exception)
  169. {
  170. throw;
  171. }
  172. }, ls::std::core::IllegalArgumentException);
  173. }
  174. TEST_F(StateMachineTest, setStartState)
  175. {
  176. ls::std::logic::StateMachine stateMachine{"test_machine"};
  177. ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
  178. stateMachine.addState(::std::make_shared<ls::std::logic::State>("A"));
  179. ASSERT_TRUE(stateMachine.setStartState("A"));
  180. ASSERT_FALSE(stateMachine.getCurrentState() == nullptr);
  181. }
  182. TEST_F(StateMachineTest, setStartState_state_does_not_exist)
  183. {
  184. ls::std::logic::StateMachine stateMachine{"test_machine"};
  185. ASSERT_FALSE(stateMachine.setStartState("A"));
  186. }
  187. TEST_F(StateMachineTest, setStartState_empty_id)
  188. {
  189. ls::std::logic::StateMachine stateMachine{"test_machine"};
  190. EXPECT_THROW({
  191. try
  192. {
  193. stateMachine.setStartState("");
  194. }
  195. catch (const ls::std::core::IllegalArgumentException &_exception)
  196. {
  197. throw;
  198. }
  199. }, ls::std::core::IllegalArgumentException);
  200. }
  201. }