Compare commits

...

9 Commits

Author SHA1 Message Date
66cc5c87d4 clean: rename *.asm -> *.s 2025-05-12 14:44:16 +02:00
0b4d562ef9 clean 2024-09-16 16:21:20 +02:00
42b7aab1ae remove bonus 2024-09-16 16:19:38 +02:00
e3b2eb0852 fix: strdup: add 1 to \0 2024-09-11 20:40:52 +02:00
ebf0b0e442 fix: test leak 2024-09-11 20:20:49 +02:00
3effb7caf5 rename: include to headers 2024-09-11 20:15:32 +02:00
27f25d2cea clean 2024-09-05 18:41:46 +02:00
fdc1923211 save register 2024-09-05 18:39:51 +02:00
cbed6c468f add: strdup test 2024-09-05 18:39:38 +02:00
12 changed files with 37 additions and 157 deletions

View File

@ -24,7 +24,7 @@ all : $(NAME)
test : $(NAME)
$(CC) $(CFLAGS) test/test.c $(BUILDDIR)/$(NAME) -o $(BUILDDIR)/test
$(BUILDDIR)/test
valgrind --leak-check=full $(BUILDDIR)/test
clean :
rm -rf $(OBJDIR)

View File

@ -3,14 +3,9 @@
#include <stddef.h>
#include <aio.h>
#include "./list.h"
char *ft_strcpy(char *dest, const char *src);
size_t ft_strlen(const char *str);
int ft_strcmp( const char *first, const char *second);
ssize_t ft_write(int fd, const void *buf, size_t count);
char *ft_strdup(const char *s);
ssize_t ft_read(int fildes, void *buf, size_t nbyte);
int ft_list_size(t_list *begin_list);
void ft_list_push_front(t_list **begin_list, void *data);

View File

@ -1,7 +0,0 @@
#pragma once
typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;

View File

@ -1,27 +0,0 @@
extern malloc
section .text
global ft_list_push_front
ft_list_push_front:
push rdi
push rsi
mov rdi, 16
call malloc wrt ..plt
cmp rax, 0
je out
pop rsi
pop rdi
mov rdx, [rdi]
mov [rax + 0], rsi ; .data = arg#1
mov [rax + 8], rdx ; .next = *arg#0
mov [rdi], rax
out:
ret

View File

@ -1,19 +0,0 @@
section .text
global ft_list_size
ft_list_size:
xor rcx, rcx
loop:
cmp QWORD [rdi + 8], 0
je out
mov rdi, [rdi + 8]
inc rcx
jmp loop
out:
mov rax, rcx
ret

View File

@ -15,6 +15,8 @@ section .text
syscall_failed:
push rbx
neg rax
mov rbx, rax
@ -22,4 +24,7 @@ section .text
mov [rax], rbx
mov rax, -1
pop rbx
ret

View File

@ -2,6 +2,8 @@ section .text
global ft_strcmp
ft_strcmp:
push rbx
xor rcx, rcx
loop:
@ -20,4 +22,7 @@ section .text
out:
sub bl, dl
movsx rax, bl
pop rbx
ret

View File

@ -5,10 +5,13 @@ extern ft_strcpy
section .text
global ft_strdup
ft_strdup:
push rbx
call ft_strlen
mov rbx, rdi
mov rdi, rax
add rdi, 1
call malloc wrt ..plt
@ -20,8 +23,13 @@ section .text
call ft_strcpy
pop rbx
ret
error:
xor rax, rax
pop rbx
ret

View File

@ -6,7 +6,9 @@ section .text
loop:
cmp BYTE [rdi + rcx], 0
je out
inc rcx
jmp loop
out:
mov rax, rcx

View File

@ -14,10 +14,15 @@ section .text
syscall_failed:
push rbx
neg rax
mov rbx, rax
call __errno_location wrt ..plt
mov [rax], rbx
mov rax, -1
pop rbx
ret

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include "../include/libasm.h"
#include "../headers/libasm.h"
#include "string.h"
#include "../include/list.h"
#include <time.h>
#include <stdlib.h>
@ -75,90 +74,15 @@ void multiple_test_atoi_base(char *(*own_func)(char *, const char *), const char
}
}
void destroy_list(t_list *root, void *(destroy_data)(void *))
void multiple_test_strdup(const char * const *values)
{
t_list *current = root;
t_list *prev;
char *tmp;
while (current != NULL)
for (size_t i = 0; values[i] != NULL; i++)
{
prev = current;
if (current->data)
destroy_data(current->data);
current = current->next;
free(prev);
}
}
t_list *create_list(size_t len)
{
t_list *root;
t_list *current = root;
root = malloc(sizeof(t_list));
if (root == NULL)
return NULL;
current = root;
for (size_t i = 0; i < len; i++)
{
current->next = malloc(sizeof(t_list));
current->data = (void *) i;
if (current->next == NULL)
{
destroy_list(current, NULL);
return NULL;
}
current = current->next;
}
current->next = NULL;
return root;
}
#define nb_random_test 10
#define nb_specific_test 1
void multiple_test_list_size()
{
size_t len;
t_list *root;
size_t specific_size[nb_specific_test] = {0};
srand(time(NULL));
for (size_t i = 0; i < nb_random_test + nb_specific_test; i++)
{
len = i < nb_specific_test ? specific_size[i] : rand() % 100;
root = create_list(len);
if (root == NULL)
return;
test_int(len, ft_list_size(root));
printf("\n");
}
}
void multiple_test_push_front()
{
size_t len;
t_list *root;
srand(time(NULL));
for (size_t i = 0; i < nb_random_test; i++)
{
len = rand() % 100;
root = create_list(len);
if (root == NULL)
return;
ft_list_push_front(&root, (void *) 101);
printf("data: ");
test_size_t((size_t) root->data, 101);
printf(" data-next: ");
test_size_t((size_t) root->next->data, 0);
printf(" list-size: ");
test_int(len + 1, ft_list_size(root));
tmp = ft_strdup(values[i]);
test_str(tmp, values[i]);
free(tmp);
printf("\n");
}
}
@ -180,19 +104,8 @@ int main()
multiple_test_strcpy(ft_strcpy, strcpy_tests);
printf("\n");
ft_write(1, "bozo\n", 5);
printf("%s\n", ft_strdup("sdsfsd"));
/*
printf("ATOI_BASE\n");
const char *atoi_base_tests[][2] = {{"0123", ""}, {"0123", "0"}, {"0123", "0123456789"}, {"bonjour", "bonjour"}, {"", ""}, {"bonjour", "salut"}, {"co\0fgf", "co\0fgf"}, NULL};
multiple_test_int_2(strcmp, ft_strcmp, strcmp_tests);
printf("\n");*/
printf("FT_LIST_SIZE\n");
multiple_test_list_size();
printf("\n");
printf("FT_LIST_PUSH_FRONT\n");
multiple_test_push_front();
printf("STRDUP\n");
const char *strdup_tests[] = {"yo", "", "bonjour", "co\0fgf", NULL};
multiple_test_strdup(strdup_tests);
printf("\n");
}