From e5cd17c1b883667d22759741c9b7ea65602e0dec Mon Sep 17 00:00:00 2001 From: lalgarra Date: Thu, 9 Oct 2025 17:37:10 +0200 Subject: [PATCH] Working on ft_split() --- Makefile | 5 ++-- ft_split_test.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 ft_split_test.c diff --git a/Makefile b/Makefile index c6bc71a..905db79 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: lalgarra +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/10/03 18:03:12 by lalgarra #+# #+# # -# Updated: 2025/10/08 20:53:52 by lalgarra ### ########.fr # +# Updated: 2025/10/09 17:21:34 by lalgarra ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,8 @@ TESTS := ft_isalpha_test ft_isdigit_test ft_isalnum_test ft_isascii_test \ ft_memset_test ft_bzero_test ft_memcpy_test ft_calloc_test \ ft_memmove_test ft_strlcpy_test ft_strlcat_test ft_strchr_test \ ft_strrchr_test ft_strncmp_test ft_memcmp_test ft_strnstr_test \ - ft_atoi_test ft_substr_test ft_strjoin_test ft_strtrim_test + ft_atoi_test ft_substr_test ft_strjoin_test ft_strtrim_test \ + ft_split_test %_test : %_test.c $(CC) $(CFLAGS) $*_test.c -L$(LIBDIR) -lft -o $*_test diff --git a/ft_split_test.c b/ft_split_test.c new file mode 100644 index 0000000..a981705 --- /dev/null +++ b/ft_split_test.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lalgarra +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/10/09 17:20:58 by lalgarra #+# #+# */ +/* Updated: 2025/10/09 17:35:12 by lalgarra ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +static void ft_putstr_fd1(char *res, int fd) +{ + if (res == NULL) + ft_putstr_fd("-NULL-", fd); + else + ft_putstr_fd(res, fd); +} + +static void printres(char **res, int fd) +{ + size_t idx; + + idx = 0; + while (res[idx] != NULL) + { + ft_putstr_fd1(res[idx], fd); + ft_putstr_fd("], [", fd); + } + ft_putstr_fd1(res[idx], fd); +} + +static void freeres(char **res) +{ + size_t idx; + + idx = 0; + while (res[idx] != NULL) + free(res[idx]); + free(res); +} + +static void test(char *prefix, char *str1, char c) +{ + char **res; + int fd; + + fd = STDOUT_FILENO; + ft_putstr_fd(prefix, fd); + ft_putstr_fd("str1:[", fd); + ft_putstr_fd1(str1, fd); + ft_putstr_fd("], c:[", fd); + ft_putchar_fd(c, fd); + res = ft_split(str1, c); + ft_putstr_fd("], result:{[", fd); + printres(res, fd); + freeres(res); + ft_putendl_fd("]}", fd); +} + +int main(void) +{ + test("test 1: ", "Lorem", ' '); + test("test 2: ", " Lorem ", ' '); + test("test 3: ", " Lorem ", 'r'); + test("test 4: ", (void *)0, ' '); + test("test 6: ", "Lorem", '\0'); + return (0); +}