From 536643a462ea99e27bb4110b9470e22f06736377 Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Sun, 17 May 2020 20:09:58 +0200 Subject: [PATCH 1/6] 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 From 0ebbcc602cbb75e8baa72eae6c25584fec119fc1 Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Sun, 17 May 2020 20:15:40 +0200 Subject: [PATCH 2/6] Fix identation --- makefile | 116 +++++++++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/makefile b/makefile index 00d3aed..d6686c4 100644 --- a/makefile +++ b/makefile @@ -32,8 +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_removeChars.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 \ @@ -73,87 +73,87 @@ DEPS := $(OBJ:%.o=%.d) export help: - @echo Shellmatta help - @echo ----------------------------------------------- - @echo test - run all tests - @echo cppcheck - run static code analysis (cppcheck) - @echo example - build example - @echo ----------------------------------------------- + @echo Shellmatta help + @echo ----------------------------------------------- + @echo test - run all tests + @echo cppcheck - run static code analysis (cppcheck) + @echo example - build example + @echo ----------------------------------------------- test: unittest integrationtest cppcheck: - - @mkdir -p output/cppcheck/html - cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) - cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) --xml 2>output/cppcheck/cppcheck.xml - cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html + - @mkdir -p output/cppcheck/html + cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) + cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) --xml 2>output/cppcheck/cppcheck.xml + cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html unittest: $(UNITTEST_TARGET) - - @mkdir -p output/test/unittest/report - @echo running test: - @# remove coverage from former run - @-find . -name "*.gcda" -type f -delete - -$(UNITTEST_TARGET) - @#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src + - @mkdir -p output/test/unittest/report + @echo running test: + @# remove coverage from former run + @-find . -name "*.gcda" -type f -delete + -$(UNITTEST_TARGET) + @#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src - @# remove report from former run - -rm -rf $(OBJ_DIR)test/unittest/report/* - gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api - @#-rm *.gcov - + @# remove report from former run + -rm -rf $(OBJ_DIR)test/unittest/report/* + gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api + @#-rm *.gcov + integrationtest: $(INTEGRATIONTEST_TARGET) - - @mkdir -p output/test/integrationtest/report - @echo running test: - -$(INTEGRATIONTEST_TARGET) - #gcov -o output/test $(TEST_CPPOBJ) -r - gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api - #-rm *.gcov - + - @mkdir -p output/test/integrationtest/report + @echo running test: + -$(INTEGRATIONTEST_TARGET) + #gcov -o output/test $(TEST_CPPOBJ) -r + gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api + #-rm *.gcov + example: $(EXAMPLE_TARGET) - @echo building example + @echo building example doc: - - @mkdir -p output/doc/html - - @mkdir -p output/doc/latex - doxygen cfg/doxygen/doxyfile - + - @mkdir -p output/doc/html + - @mkdir -p output/doc/latex + doxygen cfg/doxygen/doxyfile + clean: - - rm -rf $(OBJ_DIR) + - rm -rf $(OBJ_DIR) $(EXAMPLE_TARGET): $(COBJ) $(EXAMPLE_COBJ) - - @mkdir -p $(@D) - $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + - @mkdir -p $(@D) + $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) $(UNITTEST_TARGET): $(UNITTEST_CPPOBJ) - - @mkdir -p $(@D) - $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) - + - @mkdir -p $(@D) + $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + $(INTEGRATIONTEST_TARGET): $(INTEGRATIONTEST_CPPOBJ) $(COBJ) - - @mkdir -p $(@D) - $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + - @mkdir -p $(@D) + $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) $(TARGET): $(OBJ) - - @mkdir -p $(@D) - $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) - + - @mkdir -p $(@D) + $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + $(COBJ): - - @mkdir -p $(@D) - @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + - @mkdir -p $(@D) + @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) $(EXAMPLE_COBJ): - - @mkdir -p $(@D) - @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + - @mkdir -p $(@D) + @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) $(UNITTEST_CPPOBJ) $(INTEGRATIONTEST_CPPOBJ): - - @mkdir -p $(@D) - @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) - $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) + - @mkdir -p $(@D) + @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) + $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) %.o: %.cpp - - @mkdir -p $(@D) - @$(CPP) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CPP) -c $(CFLAGS) -o $@ $< + - @mkdir -p $(@D) + @$(CPP) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CPP) -c $(CFLAGS) -o $@ $< -include $(DEPS) From 0ae7e244707e4cb1f18ceee99cbf41c97bc49752 Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Mon, 18 May 2020 21:10:26 +0200 Subject: [PATCH 3/6] Revert "Fix identation" This reverts commit 0ebbcc602cbb75e8baa72eae6c25584fec119fc1. --- makefile | 116 +++++++++++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/makefile b/makefile index d6686c4..00d3aed 100644 --- a/makefile +++ b/makefile @@ -32,8 +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_removeChars.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 \ @@ -73,87 +73,87 @@ DEPS := $(OBJ:%.o=%.d) export help: - @echo Shellmatta help - @echo ----------------------------------------------- - @echo test - run all tests - @echo cppcheck - run static code analysis (cppcheck) - @echo example - build example - @echo ----------------------------------------------- + @echo Shellmatta help + @echo ----------------------------------------------- + @echo test - run all tests + @echo cppcheck - run static code analysis (cppcheck) + @echo example - build example + @echo ----------------------------------------------- test: unittest integrationtest cppcheck: - - @mkdir -p output/cppcheck/html - cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) - cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) --xml 2>output/cppcheck/cppcheck.xml - cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html + - @mkdir -p output/cppcheck/html + cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) + cppcheck --enable=all --template=gcc --cppcheck-build-dir=output/cppcheck $(SOURCES) --xml 2>output/cppcheck/cppcheck.xml + cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html unittest: $(UNITTEST_TARGET) - - @mkdir -p output/test/unittest/report - @echo running test: - @# remove coverage from former run - @-find . -name "*.gcda" -type f -delete - -$(UNITTEST_TARGET) - @#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src + - @mkdir -p output/test/unittest/report + @echo running test: + @# remove coverage from former run + @-find . -name "*.gcda" -type f -delete + -$(UNITTEST_TARGET) + @#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src - @# remove report from former run - -rm -rf $(OBJ_DIR)test/unittest/report/* - gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api - @#-rm *.gcov - + @# remove report from former run + -rm -rf $(OBJ_DIR)test/unittest/report/* + gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api + @#-rm *.gcov + integrationtest: $(INTEGRATIONTEST_TARGET) - - @mkdir -p output/test/integrationtest/report - @echo running test: - -$(INTEGRATIONTEST_TARGET) - #gcov -o output/test $(TEST_CPPOBJ) -r - gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api - #-rm *.gcov - + - @mkdir -p output/test/integrationtest/report + @echo running test: + -$(INTEGRATIONTEST_TARGET) + #gcov -o output/test $(TEST_CPPOBJ) -r + gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api + #-rm *.gcov + example: $(EXAMPLE_TARGET) - @echo building example + @echo building example doc: - - @mkdir -p output/doc/html - - @mkdir -p output/doc/latex - doxygen cfg/doxygen/doxyfile - + - @mkdir -p output/doc/html + - @mkdir -p output/doc/latex + doxygen cfg/doxygen/doxyfile + clean: - - rm -rf $(OBJ_DIR) + - rm -rf $(OBJ_DIR) $(EXAMPLE_TARGET): $(COBJ) $(EXAMPLE_COBJ) - - @mkdir -p $(@D) - $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + - @mkdir -p $(@D) + $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) $(UNITTEST_TARGET): $(UNITTEST_CPPOBJ) - - @mkdir -p $(@D) - $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) - + - @mkdir -p $(@D) + $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + $(INTEGRATIONTEST_TARGET): $(INTEGRATIONTEST_CPPOBJ) $(COBJ) - - @mkdir -p $(@D) - $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + - @mkdir -p $(@D) + $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) $(TARGET): $(OBJ) - - @mkdir -p $(@D) - $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) - + - @mkdir -p $(@D) + $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS) + $(COBJ): - - @mkdir -p $(@D) - @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + - @mkdir -p $(@D) + @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) $(EXAMPLE_COBJ): - - @mkdir -p $(@D) - @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + - @mkdir -p $(@D) + @$(CC) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) $(UNITTEST_CPPOBJ) $(INTEGRATIONTEST_CPPOBJ): - - @mkdir -p $(@D) - @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) - $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) + - @mkdir -p $(@D) + @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) + $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.cpp)) %.o: %.cpp - - @mkdir -p $(@D) - @$(CPP) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) - $(CPP) -c $(CFLAGS) -o $@ $< + - @mkdir -p $(@D) + @$(CPP) -c $(CFLAGS) $(DEPEND) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c)) + $(CPP) -c $(CFLAGS) -o $@ $< -include $(DEPS) From 15c846a9f1ae87c0acac1ec18c4d945a5463b442 Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Mon, 18 May 2020 21:14:47 +0200 Subject: [PATCH 4/6] Fix identation --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 00d3aed..d96c36f 100644 --- a/makefile +++ b/makefile @@ -32,8 +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_removeChars.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 \ From 2060cd61ebfa8ff824cf70598d3335b65cc6e88f Mon Sep 17 00:00:00 2001 From: "S.Hentges" Date: Thu, 28 May 2020 22:39:53 +0200 Subject: [PATCH 5/6] Expand unittest for utils_removeChars --- .../test_utils_removeChars.cpp | 99 +++++++++++++++++-- 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/test/unittest/shellmatta_utils/test_utils_removeChars.cpp b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp index 4eb9b15..f4d9e02 100644 --- a/test/unittest/shellmatta_utils/test_utils_removeChars.cpp +++ b/test/unittest/shellmatta_utils/test_utils_removeChars.cpp @@ -28,7 +28,7 @@ TEST_CASE("shellmatta_utils_removeChars_nothing_removed"){ inst.cursor = 20u; inst.inputCount = 20u; - char buffer[20] = "abcdefghijklmnopqr\0"; + char buffer[20] = "abcdefghijklmnopqr"; inst.buffer = buffer; inst.bufferSize = 20u; @@ -36,12 +36,12 @@ TEST_CASE("shellmatta_utils_removeChars_nothing_removed"){ CHECK( inst.cursor == 20u); CHECK( inst.inputCount == 20); - REQUIRE(strncmp("abcdefghijklmnopqr\0", buffer, sizeof(buffer)) == 0); + REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); } TEST_CASE("shellmatta_utils_removeChars_backspace_false"){ shellmatta_instance_t inst; - char buffer[20] = "abcdefghijklmnopqr\0"; + char buffer[20] = "abcdefghijklmnopqr"; memset(&inst, 0, sizeof(inst)); uint32_t length = 5u; bool backspace = false; @@ -61,12 +61,12 @@ TEST_CASE("shellmatta_utils_removeChars_backspace_false"){ CHECK( inst.cursor == 20u); CHECK( inst.inputCount == 20); - REQUIRE(strncmp("abcdefghijklmnopqr\0", buffer, sizeof(buffer)) == 0); + REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); } TEST_CASE("shellmatta_utils_removeChars_remove_five"){ shellmatta_instance_t inst; - char buffer[20] = "abcdefghijklmnopqr\0"; + char buffer[20] = "abcdefghijklmnopqr"; memset(&inst, 0, sizeof(inst)); @@ -88,12 +88,12 @@ TEST_CASE("shellmatta_utils_removeChars_remove_five"){ CHECK( inst.cursor == 5u); CHECK( inst.inputCount == 15u); - REQUIRE(strncmp("abcdeklmnopqr\0", buffer, sizeof(buffer)) == 0); + REQUIRE(strncmp("abcdeklmnopqr", buffer, sizeof(buffer)) == 0); } TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){ shellmatta_instance_t inst; - char buffer[20] = "abcdefghijklmnopqr\0"; + char buffer[20] = "abcdefghijklmnopqr"; memset(&inst, 0, sizeof(inst)); @@ -115,5 +115,86 @@ TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){ CHECK( inst.cursor == 0u); CHECK( inst.inputCount == 10u); - REQUIRE(strncmp("klmnopqr\0", buffer, sizeof(buffer)) == 0); -} \ No newline at end of file + REQUIRE(strncmp("klmnopqr", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_remove_chars_in_the_middle_of_the_buffer_backspace_false"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr"; + + 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 = false; + + inst.cursor = 10u; + inst.inputCount = 20u; + + inst.bufferSize = 20u; + inst.buffer = buffer; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 10u); + CHECK( inst.inputCount == 20u); + REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_remove_more_chars_in_middle_of_buffer_than_are_present_backspace_false"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr"; + + 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 = false; + + inst.cursor = 10u; + inst.inputCount = 20u; + + inst.bufferSize = 20u; + inst.buffer = buffer; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 10u); + CHECK( inst.inputCount == 20u); + REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); +} + +TEST_CASE("shellmatta_utils_removeChars_curser_outside_buffer"){ + shellmatta_instance_t inst; + char buffer[20] = "abcdefghijklmnopqr"; + + 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 = false; + + inst.cursor = 21u; + inst.inputCount = 20u; + + inst.bufferSize = 20u; + inst.buffer = buffer; + + utils_removeChars(&inst, length, backspace); + + CHECK( inst.cursor == 21u); + CHECK( inst.inputCount == 20u); + REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0); +} From f5f9c624937d1b6b5ad4900d75b6018d01b3fc2c Mon Sep 17 00:00:00 2001 From: prozessorkern Date: Tue, 2 Jun 2020 18:40:24 +0200 Subject: [PATCH 6/6] 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"){