From f0b8cc989567eb28ed378baf0031e9c333ae2a31 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Wed, 11 Aug 2021 18:20:11 +0200 Subject: [PATCH] Fix encoding of Query responses for high numbers of errors + improve unit tests directly for the enumerated errors in logic/error_codes.h --- src/modules/protocol.cpp | 6 +- tests/unit/modules/protocol/test_protocol.cpp | 86 ++++++++++++++++--- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/modules/protocol.cpp b/src/modules/protocol.cpp index 3acaf34..3f2ebf6 100644 --- a/src/modules/protocol.cpp +++ b/src/modules/protocol.cpp @@ -215,13 +215,13 @@ uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseMs *dst++ = value % 10 + '0'; } else if (value < 10000) { *dst++ = value / 1000 + '0'; - *dst++ = (value / 100) % 100 + '0'; + *dst++ = (value / 100) % 10 + '0'; *dst++ = (value / 10) % 10 + '0'; *dst++ = value % 10 + '0'; } else { *dst++ = value / 10000 + '0'; - *dst++ = (value / 1000) % 1000 + '0'; - *dst++ = (value / 100) % 100 + '0'; + *dst++ = (value / 1000) % 10 + '0'; + *dst++ = (value / 100) % 10 + '0'; *dst++ = (value / 10) % 10 + '0'; *dst++ = value % 10 + '0'; } diff --git a/tests/unit/modules/protocol/test_protocol.cpp b/tests/unit/modules/protocol/test_protocol.cpp index be1b4aa..dc01ae0 100644 --- a/tests/unit/modules/protocol/test_protocol.cpp +++ b/tests/unit/modules/protocol/test_protocol.cpp @@ -1,4 +1,6 @@ #include "catch2/catch.hpp" +#include "../logic/error_codes.h" +#include "../logic/progress_codes.h" #include "protocol.h" using Catch::Matchers::Equals; @@ -162,12 +164,59 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") { auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Processing, mp::ResponseMsgParamCodes::Error, mp::ResponseMsgParamCodes::Finished); - auto value = GENERATE(0, 1, 2, 3, 10, 11, 99, 100, 101, 102, 200, 255); + auto value = GENERATE( + ProgressCode::OK, + ProgressCode::EngagingIdler, + ProgressCode::DisengagingIdler, + ProgressCode::UnloadingToFinda, + ProgressCode::UnloadingToPulley, + ProgressCode::FeedingToFinda, + ProgressCode::FeedingToBondtech, + ProgressCode::AvoidingGrind, + ProgressCode::FinishingMoves, + ProgressCode::ERRDisengagingIdler, + ProgressCode::ERREngagingIdler, + ProgressCode::ERRWaitingForUser, + ProgressCode::ERRInternal, + ProgressCode::ERRHelpingFilament, + ProgressCode::ERRTMCFailed, + ProgressCode::UnloadingFilament, + ProgressCode::LoadingFilament, + ProgressCode::SelectingFilamentSlot, + ProgressCode::PreparingBlade, + ProgressCode::PushingFilament, + ProgressCode::PerformingCut, + ProgressCode::ReturningSelector, + ProgressCode::ParkingSelector, + ProgressCode::EjectingFilament); - std::array txbuff; - uint8_t msglen = mp::Protocol::EncodeResponseQueryOperation(requestMsg, responseStatus, value, txbuff.data()); + auto error = GENERATE( + ErrorCode::RUNNING, + ErrorCode::OK, + ErrorCode::FINDA_DIDNT_SWITCH_ON, + ErrorCode::FINDA_DIDNT_SWITCH_OFF, + ErrorCode::FSENSOR_DIDNT_SWITCH_ON, + ErrorCode::FSENSOR_DIDNT_SWITCH_OFF, + ErrorCode::FILAMENT_ALREADY_LOADED, + ErrorCode::MMU_NOT_RESPONDING, + ErrorCode::INTERNAL, + ErrorCode::TMC_PULLEY_BIT, + ErrorCode::TMC_SELECTOR_BIT, + ErrorCode::TMC_IDLER_BIT, + ErrorCode::TMC_IOIN_MISMATCH, + ErrorCode::TMC_RESET, + ErrorCode::TMC_UNDERVOLTAGE_ON_CHARGE_PUMP, + ErrorCode::TMC_SHORT_TO_GROUND, + ErrorCode::TMC_OVER_TEMPERATURE_WARN, + ErrorCode::TMC_OVER_TEMPERATURE_ERROR); - CHECK(msglen <= 8); + std::array txbuff; + + uint16_t encodedParamValue = responseStatus == mp::ResponseMsgParamCodes::Error ? (uint16_t)error : (uint16_t)value; + + uint8_t msglen = mp::Protocol::EncodeResponseQueryOperation(requestMsg, responseStatus, encodedParamValue, txbuff.data()); + + CHECK(msglen <= txbuff.size()); CHECK(txbuff[0] == (uint8_t)requestMsg.code); CHECK(txbuff[1] == requestMsg.value + '0'); CHECK(txbuff[2] == ' '); @@ -177,15 +226,26 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") { CHECK(txbuff[4] == '\n'); CHECK(msglen == 5); } else { - if (value < 10) { - CHECK(txbuff[4] == value + '0'); - } else if (value < 100) { - CHECK(txbuff[4] == value / 10 + '0'); - CHECK(txbuff[5] == value % 10 + '0'); + if (encodedParamValue < 10) { + CHECK(txbuff[4] == encodedParamValue + '0'); + } else if (encodedParamValue < 100) { + CHECK(txbuff[4] == encodedParamValue / 10 + '0'); + CHECK(txbuff[5] == encodedParamValue % 10 + '0'); + } else if (encodedParamValue < 1000) { + CHECK(txbuff[4] == encodedParamValue / 100 + '0'); + CHECK(txbuff[5] == (encodedParamValue / 10) % 10 + '0'); + CHECK(txbuff[6] == encodedParamValue % 10 + '0'); + } else if (encodedParamValue < 10000) { + CHECK(txbuff[4] == encodedParamValue / 1000 + '0'); + CHECK(txbuff[5] == (encodedParamValue / 100) % 10 + '0'); + CHECK(txbuff[6] == (encodedParamValue / 10) % 10 + '0'); + CHECK(txbuff[7] == encodedParamValue % 10 + '0'); } else { - CHECK(txbuff[4] == value / 100 + '0'); - CHECK(txbuff[5] == (value / 10) % 10 + '0'); - CHECK(txbuff[6] == value % 10 + '0'); + CHECK(txbuff[4] == encodedParamValue / 10000 + '0'); + CHECK(txbuff[5] == (encodedParamValue / 1000) % 10 + '0'); + CHECK(txbuff[6] == (encodedParamValue / 100) % 10 + '0'); + CHECK(txbuff[7] == (encodedParamValue / 10) % 10 + '0'); + CHECK(txbuff[8] == encodedParamValue % 10 + '0'); } CHECK(txbuff[msglen - 1] == '\n'); @@ -262,7 +322,7 @@ TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol]") { "E0", "E1", "E2", "E3", "E4", "K0", "L0", "L1", "L2", "L3", "L4", - "T0", "T1", "T2", "T3", + "T0", "T1", "T2", "T3", "T4", "U0", "W0");