ft_strnstr()

This commit is contained in:
lalgarra 2025-10-07 18:09:08 +02:00
parent 25cee93b16
commit 3043d38bf0
3 changed files with 145 additions and 3 deletions

View file

@ -6,7 +6,7 @@
# By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ # # By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/10/03 18:03:12 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 #LIBDIR = ../vogsphere_repo
CFLAGS += -Wall -Wextra -Werror -g3 -iquote $(LIBDIR) \ 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 \ 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_isprint_test ft_toupper_test ft_tolower_test ft_strlen_test \
ft_memset_test ft_bzero_test ft_memcpy_test ft_calloc_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_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 %_test : %_test.c
$(CC) $(CFLAGS) $*_test.c -L$(LIBDIR) -lft -o $*_test $(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 %_actual.output : %_test
@./$*_test 2>&1 > ./$*_actual.output ; @./$*_test 2>&1 > ./$*_actual.output ;
#alternative command
# @script -qc './$*_test &> ./$*_actual.output' 2> ./$*_actual.output 1> /dev/null
%_report : %_test %_actual.output %_report : %_test %_actual.output
-(diff -a $*_expected.output $*_actual.output \ -(diff -a $*_expected.output $*_actual.output \
&& echo "Test OK") > $*_report && echo "Test OK") > $*_report

View file

@ -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]

131
ft_strnstr_test.c Normal file
View file

@ -0,0 +1,131 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr_test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/06 20:46:15 by lalgarra #+# #+# */
/* Updated: 2025/10/07 18:00:51 by lalgarra ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <string.h>
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);
}