From 536643a462ea99e27bb4110b9470e22f06736377 Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Sun, 17 May 2020 20:09:58 +0200 Subject: [PATCH] Add Unittest for test_utils_removeChars.cpp --- makefile | 3 +- .../test_utils_removeChars.cpp | 119 ++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 test/unittest/shellmatta_utils/test_utils_removeChars.cpp diff --git a/makefile b/makefile index c4a43eb..00d3aed 100644 --- a/makefile +++ b/makefile @@ -32,7 +32,8 @@ UNITTEST_SOURCES := test/unittest/test_main.cpp test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp \ test/unittest/shellmatta_utils/test_utils_clearInput.cpp \ test/unittest/shellmatta_utils/test_utils_insertChars.cpp \ - test/unittest/shellmatta_utils/test_utils_terminateInput.cpp \ + test/unittest/shellmatta_utils/test_utils_terminateInput.cpp \ + test/unittest/shellmatta_utils/test_utils_removeChars.cpp \ test/unittest/shellmatta_autocomplete/test_autocomplete_run.cpp \ test/unittest/shellmatta_escape/test_escape_processArrowKeys.cpp \ test/unittest/shellmatta_history/test_appendHistoryByte.cpp \ diff --git a/test/unittest/shellmatta_utils/test_utils_removeChars.cpp b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp new file mode 100644 index 0000000..4eb9b15 --- /dev/null +++ b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp @@ -0,0 +1,119 @@ +#include "test/framework/catch.hpp" +#include "src/shellmatta_utils.h" +#include + +static uint32_t write_callCnt = 0u; +static char write_data[20]; +static uint32_t write_idx; + +static shellmatta_retCode_t writeFct(const char* data, uint32_t length) +{ + write_callCnt ++; + strncpy(&write_data[write_idx], data, length); + write_idx += length; + return SHELLMATTA_OK; +} + +TEST_CASE("shellmatta_utils_removeChars_nothing_removed"){ + shellmatta_instance_t inst; + memset(&inst, 0, sizeof(inst)); + uint32_t length = 0u; + bool backspace = true; + + inst.write = writeFct; + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_idx = 0u; + + inst.cursor = 20u; + inst.inputCount = 20u; + + char buffer[20] = "abcdefghijklmnopqr\0"; + inst.buffer = buffer; + inst.bufferSize = 20u; + + utils_removeChars( &inst, length, backspace); + + CHECK( inst.cursor == 20u); + CHECK( inst.inputCount == 20); + REQUIRE(strncmp("abcdefghijklmnopqr\0", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_backspace_false"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr\0"; + memset(&inst, 0, sizeof(inst)); + uint32_t length = 5u; + bool backspace = false; + + inst.write = writeFct; + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_idx = 0u; + + inst.cursor = 20; + inst.inputCount = 20u; + + inst.buffer = buffer; + inst.bufferSize = 20u; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 20u); + CHECK( inst.inputCount == 20); + REQUIRE(strncmp("abcdefghijklmnopqr\0", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_remove_five"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr\0"; + + memset(&inst, 0, sizeof(inst)); + + inst.write = writeFct; + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_idx = 0u; + + uint32_t length = 5u; + bool backspace = true; + + inst.cursor = 10u; + inst.inputCount = 20u; + + inst.bufferSize = 20u; + inst.buffer = buffer; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 5u); + CHECK( inst.inputCount == 15u); + REQUIRE(strncmp("abcdeklmnopqr\0", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr\0"; + + memset(&inst, 0, sizeof(inst)); + + inst.write = writeFct; + write_callCnt = 0u; + memset(write_data, 0, sizeof(write_data)); + write_idx = 0u; + + uint32_t length = 15u; + bool backspace = true; + + inst.cursor = 10u; + inst.inputCount = 20u; + + inst.bufferSize = 20u; + inst.buffer = buffer; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 0u); + CHECK( inst.inputCount == 10u); + REQUIRE(strncmp("klmnopqr\0", buffer, sizeof(buffer)) == 0); +} \ No newline at end of file