From dfc44fce071a29ce1de05eaecd76e795b09ea8fc Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 8 Sep 2021 23:33:10 +0200 Subject: [PATCH] added DLE encoder test files --- .../unit/globalfunctions/CMakeLists.txt | 1 + .../unit/globalfunctions/testDleEncoder.cpp | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp diff --git a/tests/src/fsfw_tests/unit/globalfunctions/CMakeLists.txt b/tests/src/fsfw_tests/unit/globalfunctions/CMakeLists.txt index 8e57e01b..617c7f5a 100644 --- a/tests/src/fsfw_tests/unit/globalfunctions/CMakeLists.txt +++ b/tests/src/fsfw_tests/unit/globalfunctions/CMakeLists.txt @@ -1,2 +1,3 @@ target_sources(${TARGET_NAME} PRIVATE + testDleEncoder.cpp ) diff --git a/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp b/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp new file mode 100644 index 00000000..01ac3568 --- /dev/null +++ b/tests/src/fsfw_tests/unit/globalfunctions/testDleEncoder.cpp @@ -0,0 +1,69 @@ +#include "fsfw/globalfunctions/DleEncoder.h" +#include "fsfw_tests/unit/CatchDefinitions.h" +#include "catch2/catch_test_macros.hpp" + +#include + +const std::array TEST_ARRAY_0 = { 0 }; +const std::array TEST_ARRAY_1 = { 0, DleEncoder::DLE_CHAR, 5}; +const std::array TEST_ARRAY_2 = { 0, DleEncoder::STX_CHAR, 5}; +const std::array TEST_ARRAY_3 = { 0, DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR}; + +TEST_CASE("DleEncoder" , "[DleEncoder]") { + + DleEncoder dleEncoder; + std::array buffer; + SECTION("Encoding") { + size_t encodedLen = 0; + ReturnValue_t result = dleEncoder.encode(TEST_ARRAY_0.data(), TEST_ARRAY_0.size(), + buffer.data(), buffer.size(), &encodedLen); + REQUIRE(result == retval::CATCH_OK); + std::vector expected = {DleEncoder::STX_CHAR, 0, 0, 0, 0, 0, + DleEncoder::ETX_CHAR}; + for(size_t idx = 0; idx < expected.size(); idx++) { + REQUIRE(buffer[idx] == expected[idx]); + } + REQUIRE(encodedLen == 7); + + result = dleEncoder.encode(TEST_ARRAY_1.data(), TEST_ARRAY_1.size(), + buffer.data(), buffer.size(), &encodedLen); + REQUIRE(result == retval::CATCH_OK); + expected = std::vector{DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, + DleEncoder::DLE_CHAR, 5, DleEncoder::ETX_CHAR}; + for(size_t idx = 0; idx < expected.size(); idx++) { + REQUIRE(buffer[idx] == expected[idx]); + } + REQUIRE(encodedLen == expected.size()); + + result = dleEncoder.encode(TEST_ARRAY_2.data(), TEST_ARRAY_2.size(), + buffer.data(), buffer.size(), &encodedLen); + REQUIRE(result == retval::CATCH_OK); + expected = std::vector{DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, + DleEncoder::STX_CHAR + 0x40, 5, DleEncoder::ETX_CHAR}; + for(size_t idx = 0; idx < expected.size(); idx++) { + REQUIRE(buffer[idx] == expected[idx]); + } + REQUIRE(encodedLen == expected.size()); + + result = dleEncoder.encode(TEST_ARRAY_3.data(), TEST_ARRAY_3.size(), + buffer.data(), buffer.size(), &encodedLen); + REQUIRE(result == retval::CATCH_OK); + expected = std::vector{DleEncoder::STX_CHAR, 0, DleEncoder::CARRIAGE_RETURN, + DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR}; + for(size_t idx = 0; idx < expected.size(); idx++) { + REQUIRE(buffer[idx] == expected[idx]); + } + REQUIRE(encodedLen == expected.size()); + + result = dleEncoder.encode(TEST_ARRAY_3.data(), TEST_ARRAY_3.size(), + buffer.data(), 0, &encodedLen); + REQUIRE(result == DleEncoder::STREAM_TOO_SHORT); + result = dleEncoder.encode(TEST_ARRAY_1.data(), TEST_ARRAY_1.size(), + buffer.data(), 4, &encodedLen); + REQUIRE(result == DleEncoder::STREAM_TOO_SHORT); + } + + SECTION("Decoding") { + + } +}