From 49283f4227c1cdbba991b94597e0e54ac4bca952 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 5 Sep 2024 13:15:14 +0200 Subject: [PATCH] add: ft_list_size --- include/libasm.h | 6 +++- include/list.h | 7 ++++ src/ft_list_size.asm | 19 ++++++++++ test/test.c | 86 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 include/list.h create mode 100644 src/ft_list_size.asm diff --git a/include/libasm.h b/include/libasm.h index 1bef4a0..6e16d40 100644 --- a/include/libasm.h +++ b/include/libasm.h @@ -3,9 +3,13 @@ #include #include +#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); \ No newline at end of file +ssize_t ft_read(int fildes, void *buf, size_t nbyte); + +size_t ft_list_size(const t_list *root); \ No newline at end of file diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..684d375 --- /dev/null +++ b/include/list.h @@ -0,0 +1,7 @@ +#pragma once + +typedef struct s_list +{ + void *data; + struct s_list *next; +} t_list; \ No newline at end of file diff --git a/src/ft_list_size.asm b/src/ft_list_size.asm new file mode 100644 index 0000000..5b6f70e --- /dev/null +++ b/src/ft_list_size.asm @@ -0,0 +1,19 @@ +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 diff --git a/test/test.c b/test/test.c index 1b69f0a..f5b1227 100644 --- a/test/test.c +++ b/test/test.c @@ -1,6 +1,10 @@ #include #include "../include/libasm.h" #include "string.h" +#include "../include/list.h" +#include +#include + void test_size_t(size_t expected_value, size_t value) { @@ -60,6 +64,79 @@ void multiple_test_strcpy(char *(*own_func)(char *, const char *), const char * } +void multiple_test_atoi_base(char *(*own_func)(char *, const char *), const char * const *values[2]) +{ + char tmp[4096]; + + for (size_t i = 0; values[i] != NULL; i++) + { + printf("test: %s", values[i]); + printf("\n"); + } +} + +void destroy_list(t_list *root, void *(destroy_data)(void *)) +{ + t_list *current = root; + t_list *prev; + + while (current != NULL) + { + 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)); + 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"); + } +} + int main() { printf("STRLEN\n"); @@ -79,4 +156,13 @@ int main() 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"); } \ No newline at end of file