StateMachineTest.cpp 6.8 KB

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