debugged and tested non-escaped encoder

This commit is contained in:
Robin Müller 2021-09-09 01:28:21 +02:00
parent 35b53e9a17
commit d05eb23ea7
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 34 additions and 9 deletions

View File

@ -19,10 +19,11 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
return STREAM_TOO_SHORT;
}
if (addStxEtx) {
size_t currentIdx = 0;
if(not escapeStxEtx) {
destStream[0] = DLE_CHAR;
destStream[currentIdx++] = DLE_CHAR;
}
destStream[1] = STX_CHAR;
destStream[currentIdx] = STX_CHAR;
}
if(escapeStxEtx) {
@ -122,8 +123,8 @@ ReturnValue_t DleEncoder::encodeStreamNonEscaped(const uint8_t *sourceStream, si
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
if (addStxEtx) {
destStream[encodedIndex] = ETX_CHAR;
++encodedIndex;
destStream[encodedIndex++] = DLE_CHAR;
destStream[encodedIndex++] = ETX_CHAR;
}
*encodedLen = encodedIndex;
return RETURN_OK;

View File

@ -22,19 +22,39 @@ const std::vector<uint8_t> TEST_ARRAY_0_ENCODED_NON_ESCAPED = {
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR, 5, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
5, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR + 0x40,
5, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0,
DleEncoder::STX_CHAR, 5, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, 0, DleEncoder::CARRIAGE_RETURN,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0,
DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_ESCAPED = {
DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::DLE_CHAR,
DleEncoder::STX_CHAR + 0x40, DleEncoder::ETX_CHAR
};
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_NON_ESCAPED = {
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
DleEncoder::ETX_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR
};
TEST_CASE("DleEncoder" , "[DleEncoder]") {
@ -75,16 +95,20 @@ TEST_CASE("DleEncoder" , "[DleEncoder]") {
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_ESCAPED);
dleEncoder.setEscapeMode(false);
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
ReturnValue_t 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);
dleEncoder.setEscapeMode(false);
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_NON_ESCAPED);
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_NON_ESCAPED);
dleEncoder.setEscapeMode(true);
}
SECTION("Decoding") {