From af1af49ae177e60d04abda494589bdefc0ef04d8 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Thu, 8 Jan 2026 17:03:52 +0100 Subject: [PATCH] feat(utils): string utils + corresponding tests --- src/utils/string_utils/string_utils.c | 16 +++++ src/utils/string_utils/string_utils.h | 10 +++ tests/unit/lexer/lexer_tests.c | 39 +++++------ tests/unit/utils/utils_tests.c | 97 +++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 src/utils/string_utils/string_utils.c create mode 100644 src/utils/string_utils/string_utils.h create mode 100644 tests/unit/utils/utils_tests.c diff --git a/src/utils/string_utils/string_utils.c b/src/utils/string_utils/string_utils.c new file mode 100644 index 0000000..6e07f7e --- /dev/null +++ b/src/utils/string_utils/string_utils.c @@ -0,0 +1,16 @@ +#include "utils/string_utils/string_utils.h" + +ssize_t skip_blanks(char **str) +{ + if (str == NULL || *str == NULL) + { + return 0; + } + ssize_t skipped = 0; + while (str[skipped] != '\0' && !isblank(str[skipped])) + { + skipped++; + } + *str += skipped; + return skipped; +} diff --git a/src/utils/string_utils/string_utils.h b/src/utils/string_utils/string_utils.h new file mode 100644 index 0000000..3fee923 --- /dev/null +++ b/src/utils/string_utils/string_utils.h @@ -0,0 +1,10 @@ +#ifndef STRING_UTILS_H +#define STRING_UTILS_H + +/* + * @brief: skips blank characters at the beginning of [str]. + * @return: number of characters skipped. + */ +ssize_t skip_blanks(char **str); + +#endif /* STRING_UTILS_H */ diff --git a/tests/unit/lexer/lexer_tests.c b/tests/unit/lexer/lexer_tests.c index 0a3ccf7..d9046d4 100644 --- a/tests/unit/lexer/lexer_tests.c +++ b/tests/unit/lexer/lexer_tests.c @@ -1,8 +1,9 @@ #include #include -#include +#include #include #include +#include #include "lexer/lexer.h" @@ -10,50 +11,44 @@ TestSuite(token_creation); Test(token_creation, basic) { - char *input = "Hello World"; + char input[] = "Hello World"; - char *actual = new_token(input, 5); - char *expected = "Hello"; + char actual[] = new_token(input, 5); + char expected[] = "Hello"; cr_expect(eq(str, actual, expected)); free(actual); } Test(token_creation, nul) { - char *input = NULL; + char input[] = NULL; - char *actual = new_token(input, 5); - char *expected = NULL; - cr_expect(eq(str, actual, expected)); - free(actual); + char actual[] = new_token(input, 5); + cr_expect(actual == NULL); } Test(token_creation, too_large) { - char *input = "Hel"; + char input[] = "Hel"; - char *actual = new_token(input, 5); - char *expected = NULL; - cr_expect(eq(str, actual, expected)); - free(actual); + char actual[] = new_token(input, 5); + cr_expect(actual == NULL); } Test(token_creation, empty) { - char *input = ""; + char input[] = ""; - char *actual = new_token(input, 5); - char *expected = NULL; - cr_expect(eq(str, actual, expected)); - free(actual); + char actual[] = new_token(input, 5); + cr_expect(actual == NULL); } Test(token_creation, basic_long) { - char *input = "Hello World! This project is a mini shell, I love BIG G."; + char input[] = "Hello World! This project is a mini shell, I love BIG G."; - char *actual = new_token(input, 42); - char *expected = calloc(42 + 1, sizeof(char)); + char actual[] = new_token(input, 42); + char expected[] = calloc(42 + 1, sizeof(char)); strncpy(input, expected, 42); cr_expect(eq(str, actual, expected)); free(actual); diff --git a/tests/unit/utils/utils_tests.c b/tests/unit/utils/utils_tests.c new file mode 100644 index 0000000..1076aab --- /dev/null +++ b/tests/unit/utils/utils_tests.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +#include "utils/string_utils/string_utils.h" + +TestSuite(string_utils); + +Test(string_utils, skipblank_basic) +{ + char input[] = " Hello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 2; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_noblank) +{ + char input[] = "Hello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 0; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_tab) +{ + char input[] = "\tHello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 1; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_space_tab) +{ + char input[] = " \tHello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 2; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_2tab_1space) +{ + char input[] = "\t \tHello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 3; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_a_lot) +{ + char input[] = "\t \t \tHello World"; + char expected_str[] = "Hello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 8; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_newline) +{ + char input[] = "\nHello World"; + char expected_str[] = "\nHello World"; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 0; + cr_expect(eq(str, input, expected_str)); + cr_expect(actual == expected); +} + +Test(string_utils, skipblank_nul) +{ + char *input = NULL; + + ssize_t actual = skip_blanks(input); + ssize_t expected = 0; + cr_expect(input == NULL); + cr_expect(actual == expected); +}