| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | /* * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net> * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. *//** * @file    test_utils_removeChars.c * @brief   unittest for shellmatta utils_removeChars * @author  Stefan Strobel <stefan.strobel@shimatta.net> */#include "test/framework/catch.hpp"#include "src/shellmatta_utils.h"#include <string.h>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";    inst.buffer = buffer;    inst.bufferSize = 20u;    utils_removeChars( &inst, length, backspace);        CHECK( inst.cursor == 20u);    CHECK( inst.inputCount == 20);    REQUIRE(strncmp("abcdefghijklmnopqr", buffer, sizeof(buffer)) == 0);}TEST_CASE("shellmatta_utils_removeChars_backspace_false"){    shellmatta_instance_t inst;    char buffer[20] = "abcdefghijklmnopqr";    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", buffer, sizeof(buffer)) == 0);}TEST_CASE("shellmatta_utils_removeChars_remove_five"){    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 = 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", buffer, sizeof(buffer)) == 0);}TEST_CASE("shellmatta_utils_removeChars_length_greater_than_CursorPos"){    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 = 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", 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 == 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"){    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 == 10u);    REQUIRE(strncmp("abcdefghij", buffer, 10u) == 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);}
 |