add list remove if and fix valgrind error

This commit is contained in:
2025-04-12 19:07:44 +02:00
parent 919948abe9
commit 2adeac70ce
7 changed files with 144 additions and 12 deletions

40
test.c
View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
/* Updated: 2025/04/06 01:39:26 by tomoron ### ########.fr */
/* Updated: 2025/04/12 18:33:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,7 +57,7 @@ int test_strcpy(void)
memset(buf,'a', 255);
buf[255] = 0;
TEST("copy normal string", strcmp(ft_strcpy(buf, "hello"),"hello") == 0);
TEST("copy normal string", strcmp(ft_strcpy(buf, "patate douce"),"patate douce") == 0);
TEST("copy empty string", strcmp(ft_strcpy(buf, ""),"") == 0);
TEST("copy to null pointer", ft_strcpy(NULL, "") == 0);
TEST("copy from null pointer", ft_strcpy("", NULL) == 0);
@ -237,6 +237,40 @@ int test_list_sort()
return(fail);
}
int remove_if_compare(void *data, int *nb)
{
return((long)data == *nb);
}
void remove_if_free(void *data)
{
printf("%ld\n", (long int) data);
}
int test_list_remove_if()
{
int fail = 0;
int nb = 2;
t_list *tmp;
tmp = 0;
ft_list_push_front(&tmp, (void *)4);
ft_list_push_front(&tmp, (void *)3);
ft_list_push_front(&tmp, (void *)2);
ft_list_push_front(&tmp, (void *)1);
ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free);
TEST("is the number 2 gone", (long int)tmp->next->data == 3);
nb = 1;
ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free);
TEST("is the number 1 gone", (long int)tmp->data == 3);
ft_list_remove_if(&tmp, &nb, 0, remove_if_free);
ft_list_remove_if(&tmp, &nb, remove_if_compare, 0);
TEST("null pointers in functions", 1);
return(fail);
}
int main(void)
{
@ -260,5 +294,7 @@ int main(void)
printf("\n\n");
FNC_TEST("ft_list_sort", test_list_sort());
printf("\n\n");
FNC_TEST("ft_list_sort", test_list_remove_if());
printf("\n\n");
FNC_TEST("check valgrind", 1);
}