From f5f9c624937d1b6b5ad4900d75b6018d01b3fc2c Mon Sep 17 00:00:00 2001 From: prozessorkern Date: Tue, 2 Jun 2020 18:40:24 +0200 Subject: [PATCH] tried to improve the documentation of the function utils_removeChars + made it more defensive + fixed the testcases --- src/shellmatta_utils.c | 5 +++-- test/unittest/shellmatta_utils/test_utils_removeChars.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/shellmatta_utils.c b/src/shellmatta_utils.c index da18af6..e67fda6 100644 --- a/src/shellmatta_utils.c +++ b/src/shellmatta_utils.c @@ -215,13 +215,14 @@ void utils_insertChars( shellmatta_instance_t *inst, * position * @param[in] inst pointer to a shellmatta instance * @param[in] length number of characters to remove - * @param[in] backspace remove characters left of the cursor + * @param[in] backspace true ==> remove characters left of the cursor + * false ==> remove characters right of the cursor */ void utils_removeChars( shellmatta_instance_t *inst, uint32_t length, bool backspace) { - if(0u != length) + if((0u != length) && (inst->inputCount >= inst->cursor)) { /** -# rewind the cursor in case of backspace */ if(true == backspace) diff --git a/test/unittest/shellmatta_utils/test_utils_removeChars.cpp b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp index f4d9e02..49f08a7 100644 --- a/test/unittest/shellmatta_utils/test_utils_removeChars.cpp +++ b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp @@ -141,8 +141,8 @@ TEST_CASE("shellmatta_utils_removeChars_remove_chars_in_the_middle_of_the_buffer utils_removeChars(&inst, length, backspace); CHECK( inst.cursor == 10u); - CHECK( inst.inputCount == 20u); - REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); + CHECK( inst.inputCount == 15u); + REQUIRE(strncmp("abcdefghijpqr", buffer, sizeof(buffer)) == 0); } TEST_CASE("shellmatta_utils_removeChars_remove_more_chars_in_middle_of_buffer_than_are_present_backspace_false"){ @@ -168,8 +168,8 @@ TEST_CASE("shellmatta_utils_removeChars_remove_more_chars_in_middle_of_buffer_th utils_removeChars(&inst, length, backspace); CHECK( inst.cursor == 10u); - CHECK( inst.inputCount == 20u); - REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); + CHECK( inst.inputCount == 10u); + REQUIRE(strncmp("abcdefghij", buffer, 10u) == 0); } TEST_CASE("shellmatta_utils_removeChars_curser_outside_buffer"){