A C++ toolbox repo until the pair uTL/dTL arives
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sequencer.cpp 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. * \file sequencer.cpp
  3. *
  4. * \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
  5. *
  6. * <dl class=\"section copyright\"><dt>License</dt><dd>
  7. * The MIT License (MIT)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. * </dd></dl>
  27. *
  28. */
  29. #include <utils/sequencer.h>
  30. #include <gtest/gtest.h>
  31. namespace test_sequencer {
  32. using namespace tbx;
  33. // Sequencer implementer mock
  34. class Seq : public sequencer_t<Seq, char, 128> {
  35. const char *msg_[10] = {
  36. "", "", "", "\r\nOK\r\n",
  37. "", "", "+CCLK = \"21/08/26-12:16:30+12\"\r\nOK\r\n"
  38. "", "", "\r\nERROR\r\n"
  39. };
  40. public:
  41. size_t get(char* data) {
  42. static int msg =0;
  43. size_t len = strlen(msg_[msg]);
  44. strcpy(data, msg_[msg++]);
  45. if (msg >= 10) msg =0;
  46. return len;
  47. }
  48. size_t put (const char* data, size_t n) {
  49. (void)*data;
  50. return n;
  51. }
  52. clock_t clock() { static clock_t t=0; return ++t; }
  53. static status_t my_handler (const char* data, size_t size) {
  54. (void)*data;
  55. (void)size;
  56. return Seq::status_t::OK;
  57. }
  58. };
  59. /*
  60. * Test sequencer.run()
  61. */
  62. TEST(Tsequencer, run_delay) {
  63. Seq s;
  64. const std::array<Seq::record_t, 1> script = {{
  65. /* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
  66. }};
  67. EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script));
  68. }
  69. TEST(Tsequencer, run_dummy_output) {
  70. Seq s;
  71. const std::array<Seq::record_t, 2> script = {{
  72. /* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
  73. /* 1 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
  74. }};
  75. EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script));
  76. }
  77. TEST(Tsequencer, run_exits) {
  78. Seq s;
  79. const std::array<Seq::record_t, 1> script1 = {{
  80. /* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000},
  81. }};
  82. EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script1));
  83. const std::array<Seq::record_t, 1> script2 = {{
  84. /* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_ERROR, 0}, 1000},
  85. }};
  86. EXPECT_EQ ((int)Seq::status_t::ERROR, (int)s.run(script2));
  87. }
  88. TEST(Tsequencer, run_sequence) {
  89. Seq s;
  90. const std::array<Seq::record_t, 8> script = {{
  91. /* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::GOTO, 1}, 1000},
  92. /* 1 */{Seq::control_t::SEND, {"ATE0\r\n", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
  93. /* 2 */{Seq::control_t::EXPECT, {{
  94. {"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
  95. {"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
  96. 1000
  97. },
  98. /* 3 */{Seq::control_t::SEND, {"AT+CCLK?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
  99. /* 4 */{Seq::control_t::EXPECT, {{
  100. {"OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, Seq::action_t::NEXT, 0},
  101. {"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
  102. 1000
  103. },
  104. /* 5 */{Seq::control_t::SEND, {"AT+CT?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
  105. /* 6 */{Seq::control_t::EXPECT, {{
  106. {"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
  107. {"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
  108. 1000
  109. },
  110. /* 7 */{Seq::control_t::SEND, {"AT+POWD=0", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
  111. }};
  112. EXPECT_EQ ((int)Seq::status_t::ERROR, (int)s.run(script));
  113. }
  114. }