From 3043d38bf04bea7bed62caac9ab0766dc72dbdcb Mon Sep 17 00:00:00 2001 From: lalgarra Date: Tue, 7 Oct 2025 18:09:08 +0200 Subject: [PATCH] ft_strnstr() --- Makefile | 10 ++- ft_strnstr_expected.output | 7 ++ ft_strnstr_test.c | 131 +++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 ft_strnstr_expected.output create mode 100644 ft_strnstr_test.c diff --git a/Makefile b/Makefile index e6a1419..b29157b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: lalgarra +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/10/03 18:03:12 by lalgarra #+# #+# # -# Updated: 2025/10/06 20:06:11 by lalgarra ### ########.fr # +# Updated: 2025/10/07 18:08:12 by lalgarra ### ########.fr # # # # **************************************************************************** # @@ -15,13 +15,14 @@ LIBDIR = ../repo_libft_algarra #LIBDIR = ../vogsphere_repo CFLAGS += -Wall -Wextra -Werror -g3 -iquote $(LIBDIR) \ - -fsanitize=address -Wanalyzer-malloc-leak +# -fsanitize=address -Wanalyzer-malloc-leak TESTS := ft_isalpha_test ft_isdigit_test ft_isalnum_test ft_isascii_test \ ft_isprint_test ft_toupper_test ft_tolower_test ft_strlen_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_strrchr_test ft_strncmp_test ft_memcmp_test ft_strnstr_test \ + %_test : %_test.c $(CC) $(CFLAGS) $*_test.c -L$(LIBDIR) -lft -o $*_test @@ -29,6 +30,9 @@ TESTS := ft_isalpha_test ft_isdigit_test ft_isalnum_test ft_isascii_test \ %_actual.output : %_test @./$*_test 2>&1 > ./$*_actual.output ; +#alternative command +# @script -qc './$*_test &> ./$*_actual.output' 2> ./$*_actual.output 1> /dev/null + %_report : %_test %_actual.output -(diff -a $*_expected.output $*_actual.output \ && echo "Test OK") > $*_report diff --git a/ft_strnstr_expected.output b/ft_strnstr_expected.output new file mode 100644 index 0000000..f9d7570 --- /dev/null +++ b/ft_strnstr_expected.output @@ -0,0 +1,7 @@ +test 1: len:[40], big:[Lorem ipsum dolor sit amet consectetur adipiscing elit.], litl:[ipsum dolor], result:[ipsum dolor sit amet consectetur adipiscing elit.] +test 2: len:[15], big:[Lorem ipsum], litl:[z], result:[-NULL-] +test 3: len:[5], big:[], litl:[], result:[] +test 4: len:[0], big:[], litl:[Lorem ip], result:[-NULL-] +test 5: len:[4], big:[ÿªÞX], litl:[ÿªÞ], result:[ÿªÞX] +test 6: len:[20], big:[is there], litl:[e], result:[ere] +test 7: len:[3], big:[abcdefgh], litl:[abc], result:[abcdefgh] diff --git a/ft_strnstr_test.c b/ft_strnstr_test.c new file mode 100644 index 0000000..b1f9184 --- /dev/null +++ b/ft_strnstr_test.c @@ -0,0 +1,131 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr_test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lalgarra +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/10/06 20:46:15 by lalgarra #+# #+# */ +/* Updated: 2025/10/07 18:00:51 by lalgarra ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "libft.h" +#include +#include + +static void test(char *prefix, char big[], char litl[], size_t len) +{ + char *res; + int fd; + + fd = STDOUT_FILENO; + ft_putstr_fd(prefix, fd); + ft_putstr_fd("len:[", fd); + ft_putnbr_fd((int) len, fd); + ft_putstr_fd("], big:[", fd); + ft_putstr_fd(big, fd); + ft_putstr_fd("], litl:[", fd); + ft_putstr_fd(litl, fd); + res = ft_strnstr(big, litl, len); + ft_putstr_fd("], result:[", fd); + if (res == NULL) + ft_putstr_fd("-NULL-", fd); + else + ft_putstr_fd(res, fd); + ft_putendl_fd("]", fd); +} + +static void zero(char big[], char littl[]) +{ + bzero(big, 128); + bzero(littl, 128); +} + +static void do_test1(char *prefix, char big[], char littl[]) +{ + size_t size; + + size = 40; + zero(big, littl); + memcpy(big, "Lorem ipsum dolor sit amet consectetur adipiscing elit.", 56); + memcpy(littl, "ipsum dolor", 11); + test(prefix, big, littl, size); +} + +static void do_test2(char *prefix, char big[], char littl[]) +{ + size_t size; + + zero(big, littl); + size = 15; + memcpy(big, "Lorem ipsum", 11); + memcpy(littl, "z", 1); + test(prefix, big, littl, size); +} + +static void do_test3(char *prefix, char big[], char littl[]) +{ + size_t size; + + zero(big, littl); + size = 5; + test(prefix, big, littl, size); +} + +static void do_test4(char *prefix, char big[], char littl[]) +{ + size_t size; + + zero(big, littl); + size = 0; + memcpy(littl, "Lorem ipsum dolor sit amet consectetur adipiscing elit.", + 8); + test(prefix, big, littl, size); +} + +static void do_test5(char *prefix, char big[], char littl[]) +{ + size_t size; + + zero(big, littl); + size = 4; + memcpy(littl, "\xff\xaa\xde\x12", 5); + memcpy(big, "\xff\xaa\xde\x12XABC", 5); + test(prefix, big, littl, size); +} + +static void do_test6(char *prefix, char big[], char littl[]) +{ + size_t size; + + zero(big, littl); + size = 20; + memcpy(littl, "e\x00g", 3); + memcpy(big, "is there\x00 a nyacat ?",20); + test(prefix, big, littl, size); +} + +static void do_test7(char *prefix) +{ + size_t size; + + size = 3; + test(prefix, "abcdefgh", "abc", size); +} + +int main(void) +{ + char big[128]; + char littl[128]; + + do_test1("test 1: ", big, littl); + do_test2("test 2: ", big, littl); + do_test3("test 3: ", big, littl); + do_test4("test 4: ", big, littl); + do_test5("test 5: ", big, littl); + do_test6("test 6: ", big, littl); + do_test7("test 7: "); + return (0); +}