From 18da369d71cd561f66c7801155612f8730bc2b9f Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 16 Jul 2024 12:01:56 +0200 Subject: [PATCH] add test for remove spaces --- CMakeLists.txt | 7 +++++-- src/rename.c | 19 ++++++++++++++++++- src/rename.h | 4 ++++ tests/CMakeLists.txt | 6 ++++-- tests/rename_tests.c | 18 +++++++++++++++--- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8767e9..8395857 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.25.1) +SET(CMAKE_C_FLAGS "-g -fsanitize=address") +MESSAGE(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}") + PROJECT(mv_none_space) -add_subdirectory(src) -add_subdirectory(tests) \ No newline at end of file +ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tests) \ No newline at end of file diff --git a/src/rename.c b/src/rename.c index 424074f..9db5fc7 100644 --- a/src/rename.c +++ b/src/rename.c @@ -1 +1,18 @@ -#include "rename.h" \ No newline at end of file +#include +#include +#include "rename.h" + +void rename_str(char* src, char* dst) +{ + int len = strlen(src); + for (int i = 0; i < len; i++) { + switch(src[i]) { + case ' ': + dst[i] = '_'; + break; + default: + dst[i] = src[i]; + break; + } + } +} \ No newline at end of file diff --git a/src/rename.h b/src/rename.h index 47ab8fb..6cc8187 100644 --- a/src/rename.h +++ b/src/rename.h @@ -1,4 +1,8 @@ #ifndef RENAME_H #define RENAME_H +#define STR_MAX_LENGTH 255 + +void rename_str(char* src, char* dst); + #endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 92eb7b3..47e1152 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,9 +4,11 @@ FIND_LIBRARY(cmocka NAMES cmocka REQUIRED) INCLUDE(CTest) -ADD_EXECUTABLE(rename_tests rename_tests.c) +ADD_EXECUTABLE(rename_tests + rename_tests.c + ../src/rename.c) TARGET_LINK_LIBRARIES(rename_tests ${cmocka}) ADD_TEST(NAME rename_tests COMMAND rename_tests) -ADD_CUSTOM_TARGET(run_tests ALL ctest) \ No newline at end of file +ADD_CUSTOM_TARGET(run_tests ALL ctest --verbose) \ No newline at end of file diff --git a/tests/rename_tests.c b/tests/rename_tests.c index 29b0ed6..da9aff1 100644 --- a/tests/rename_tests.c +++ b/tests/rename_tests.c @@ -3,16 +3,28 @@ #include #include #include +#include +#include +#include "../src/rename.h" -void sanity_check() +void rename_spaces() { - assert_true(1); + char* input = "Dies ist ein Test Satz\0"; + char* output = malloc(sizeof(char) * STR_MAX_LENGTH); + memset(output, 0, STR_MAX_LENGTH); + + rename_str(input, output); + + assert_string_equal(output, "Dies_ist_ein_Test_Satz"); + + free(output); + output = NULL; } int main() { const struct CMUnitTest tests[] = { - cmocka_unit_test(sanity_check) + cmocka_unit_test(rename_spaces) }; return cmocka_run_group_tests(tests, NULL, NULL); -- 2.39.5