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

View File

@ -14,7 +14,8 @@ SRCS_NAMES = ft_strlen.s\
SRCS_BONUS_NAMES = ft_atoi_base.s\
ft_list_push_front.s\
ft_list_size.s\
ft_list_sort.s
ft_list_sort.s\
ft_list_remove_if.s\
SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES))
SRCS_BONUS = $(addprefix $(SRCS_BONUS_DIR)/, $(SRCS_NAMES))

View File

@ -19,5 +19,6 @@ int ft_atoi_base(char *nbr, char *base);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *list_start);
void ft_list_sort(t_list **begin_list, int (*cmp)());
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *));
#endif

View File

@ -9,7 +9,7 @@ ft_list_push_front:
push rdi
push rsi
mov rdi, 10
mov rdi, 16
call malloc wrt ..plt
pop rsi
pop rdi

View File

@ -0,0 +1,95 @@
global ft_list_remove_if
section .text
remove_element:
call call_free_elem
push rax
mov rax, [rax + 8]
test r8, r8
jz element_is_first
mov [r8 + 8], rax
pop rax
ret
element_is_first:
mov [rdi], rax
pop rax
ret
call_free_elem:
push rdi
push rsi
push rdx
push rcx
push rax
push r8
mov rdi, [rax]
call rcx
pop r8
pop rax
pop rcx
pop rdx
pop rsi
pop rdi
ret
ft_list_remove_if:
test rdi, rdi
jz end
test rdx, rdx
jz end
test rcx, rcx
jz end
xor r8, r8
mov rax, [rdi]
loop:
test rax, rax
jz end
push rax
push rdi
push rsi
push rdx
push rcx
push r8
mov rdi, [rax]
call rdx
test eax, eax
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
jz loop_end
mov rax, [rsp]
call remove_element
loop_end:
pop rax
mov r8, rax
mov rax, [rax + 8]
jmp loop
end:
ret

View File

@ -7,18 +7,16 @@ ft_strcpy:
test rsi, rsi
jz err
push rdi
jmp loop_start
condition_check:
mov al, [rdi]
test al, al
jz end
loop_start:
mov rax, [rsi]
mov [rdi], rax
mov al, [rsi]
mov [rdi],al
lea rdi, [rdi + 1]
lea rsi, [rsi + 1]
jmp condition_check
test al, al
jne loop_start
end:
pop rax

View File

@ -11,6 +11,7 @@ ft_strdup:
push rdi ;push to use it for the strcpy
call ft_strlen
inc rax
mov rdi, rax
call malloc wrt ..plt

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);
}