139 lines
5.7 KiB
C++
139 lines
5.7 KiB
C++
/*!
|
|
* \file sequencer.cpp
|
|
*
|
|
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
|
|
*
|
|
* <dl class=\"section copyright\"><dt>License</dt><dd>
|
|
* The MIT License (MIT)
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
* </dd></dl>
|
|
*
|
|
*/
|
|
#include <utils/sequencer.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
namespace test_sequencer {
|
|
using namespace tbx;
|
|
|
|
struct seq_cont_t {
|
|
const char *msg_[10] = {
|
|
"", "", "", "\r\nOK\r\n",
|
|
"", "", "+CCLK = \"21/08/26-12:16:30+12\"\r\nOK\r\n"
|
|
"", "", "\r\nERROR\r\n"
|
|
};
|
|
using value_type = char;
|
|
using range_t = range<const char*>;
|
|
};
|
|
seq_cont_t seq_cont;
|
|
|
|
// Sequencer implementer mock
|
|
class Seq : public sequencer<Seq, seq_cont_t, char, 128> {
|
|
using range_t = typename seq_cont_t::range_t;
|
|
|
|
public:
|
|
size_t get(char* data) {
|
|
static int msg =0;
|
|
size_t len = strlen(seq_cont.msg_[msg]);
|
|
strcpy(data, seq_cont.msg_[msg++]);
|
|
if (msg >= 10) msg =0;
|
|
return len;
|
|
}
|
|
size_t put (const char* data, size_t n) {
|
|
(void)*data;
|
|
return n;
|
|
}
|
|
const range_t contents () const {
|
|
return range_t {&seq_cont.msg_[6][0], &seq_cont.msg_[6][5]};
|
|
}
|
|
clock_t clock() { static clock_t t=0; return ++t; }
|
|
|
|
static void my_handler (const char* data, size_t size) {
|
|
(void)*data;
|
|
(void)size;
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Test sequencer.run()
|
|
*/
|
|
TEST(Tsequencer, run_delay) {
|
|
Seq s;
|
|
const std::array<Seq::record_t<1>, 1> script = {{
|
|
/* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
|
|
}};
|
|
EXPECT_EQ (s.run(script), true);
|
|
}
|
|
|
|
TEST(Tsequencer, run_dummy_output) {
|
|
Seq s;
|
|
const std::array<Seq::record_t<1>, 2> script = {{
|
|
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
|
|
/* 1 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
|
|
}};
|
|
EXPECT_EQ (s.run(script), true);
|
|
}
|
|
|
|
TEST(Tsequencer, run_exits) {
|
|
Seq s;
|
|
const std::array<Seq::record_t<1>, 1> script1 = {{
|
|
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000},
|
|
}};
|
|
EXPECT_EQ (s.run(script1), true);
|
|
|
|
const std::array<Seq::record_t<1>, 1> script2 = {{
|
|
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_ERROR, 0}, 1000},
|
|
}};
|
|
EXPECT_EQ (s.run(script2), false);
|
|
}
|
|
|
|
TEST(Tsequencer, run_sequence) {
|
|
Seq s;
|
|
const std::array<Seq::record_t<2>, 9> script = {{
|
|
/* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::GOTO, 1}, 1000},
|
|
/* 1 */{Seq::control_t::SEND, {"ATE0\r\n", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
|
|
/* 2 */{Seq::control_t::EXPECT, {{
|
|
{"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
|
|
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
|
|
1000
|
|
},
|
|
/* 3 */{Seq::control_t::DETECT, {{
|
|
{"+CCLK", Seq::match_t::CONTAINS, nullptr, Seq::action_t::NEXT, 0},
|
|
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
|
|
1000
|
|
},
|
|
/* 4 */{Seq::control_t::SEND, {"AT+CCLK?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
|
|
/* 5 */{Seq::control_t::EXPECT, {{
|
|
{"OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, Seq::action_t::NEXT, 0},
|
|
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
|
|
1000
|
|
},
|
|
/* 6 */{Seq::control_t::SEND, {"AT+CT?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
|
|
/* 7 */{Seq::control_t::EXPECT, {{
|
|
{"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
|
|
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
|
|
1000
|
|
},
|
|
/* 8 */{Seq::control_t::SEND, {"AT+POWD=0", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
|
|
}};
|
|
EXPECT_EQ (s.run(script), false);
|
|
}
|
|
}
|