add: ft_list_size

This commit is contained in:
starnakin 2024-09-05 13:15:14 +02:00
parent 8ae1a6e876
commit 49283f4227
4 changed files with 117 additions and 1 deletions

View File

@ -3,9 +3,13 @@
#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);
size_t ft_list_size(const t_list *root);

7
include/list.h Normal file
View File

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

19
src/ft_list_size.asm Normal file
View File

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

View File

@ -1,6 +1,10 @@
#include <stdio.h>
#include "../include/libasm.h"
#include "string.h"
#include "../include/list.h"
#include <time.h>
#include <stdlib.h>
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");
}