From 55ea5b31e1102f027c752aac52abea344b5e1b67 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 2 Dec 2023 10:40:56 +0100 Subject: [PATCH] tcp_cmd add functions that need to be tested to header file --- project/Core/Inc/tcp_cmd.h | 3 +++ project/Core/Src/tcp_cmd.c | 8 +++---- tests/tcp_cmd.cpp | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/project/Core/Inc/tcp_cmd.h b/project/Core/Inc/tcp_cmd.h index f1ee0fd..17ebdd0 100644 --- a/project/Core/Inc/tcp_cmd.h +++ b/project/Core/Inc/tcp_cmd.h @@ -21,6 +21,9 @@ #include #include +void tcp_cmd_remove_newline(char* str, size_t len); +char* tcp_cmd_get_filename_ext(char* filename); +char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next); void tcp_cmd_init(void); err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err); diff --git a/project/Core/Src/tcp_cmd.c b/project/Core/Src/tcp_cmd.c index 474afb2..0e9913b 100644 --- a/project/Core/Src/tcp_cmd.c +++ b/project/Core/Src/tcp_cmd.c @@ -61,7 +61,7 @@ static void tcp_cmd_print_help(struct tcp_pcb* pcb) { * @brief This function removes the newline from a string the string can contain multiple lines * @param str The string to remove the newline from */ -void remove_newline(char* str, size_t len) { +void tcp_cmd_remove_newline(char* str, size_t len) { size_t i = 0; size_t j = 0; while (str[i] != '\0' && j < len) { @@ -78,7 +78,7 @@ void remove_newline(char* str, size_t len) { * @brief This function converts a string to lowercase * @param str The string to convert */ -void str_tolower(char* str) { +void tcp_cmd_str_tolower(char* str) { int i = 0; while (str[i] != '\0') { str[i] = (char)tolower((int)str[i]); @@ -91,7 +91,7 @@ void str_tolower(char* str) { * @param filename The filename to get the extension from * @return char* The extension of the file */ -char* get_filename_ext(char* filename) { +char* tcp_cmd_get_filename_ext(char* filename) { char* dot = strrchr(filename, '.'); if (!dot || dot == filename) return NULL; @@ -106,7 +106,7 @@ char* get_filename_ext(char* filename) { * @param next The next token * @return char* The next token */ -char* get_next_token(char* input, const char* delimiters, char** next) { +char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next) { if (input == NULL) { input = *next; } diff --git a/tests/tcp_cmd.cpp b/tests/tcp_cmd.cpp index 5f95645..434cc8a 100644 --- a/tests/tcp_cmd.cpp +++ b/tests/tcp_cmd.cpp @@ -9,6 +9,51 @@ extern "C" { #include "tcp_cmd.h" } +TEST(TCP_CMD, tcp_cmd_remove_newline) { + char* cmd = (char*)calloc(50, 1); + strcpy(cmd, "help\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "help"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "help\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\nhelp\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + strcpy(cmd, "\n\nhel\np\n"); + tcp_cmd_remove_newline(cmd, strlen(cmd)); + EXPECT_STREQ(cmd, "help"); + + free(cmd); +} + TEST(TCP_CMD, tcp_data_cb) { char* cmd = (char*)calloc(50, 1); std::string output;