StateMachineTest.cpp 7.2 KB

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