From e8fbe50093f97c6cbd5bba0c8e31e155d93658ea Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 20 Mar 2025 17:58:36 +0100 Subject: [PATCH] add tests --- Makefile | 2 +- tests.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests.c diff --git a/Makefile b/Makefile index 7a15d90..1c00869 100755 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ all: libft_malloc.so exec: $(OBJS_DIR) $(NAME) $(LFT) # $(CC) -o a.out $(FLAGS) main.c $(NAME) $(LFT) - $(CC) -o a.out $(FLAGS) main.c -L. -lft_malloc $(LFT) -Wl,-rpath=. + $(CC) -o a.out $(FLAGS) tests.c -L. -lft_malloc $(LFT) -Wl,-rpath=. libft_malloc.so: $(NAME) ln -sf "$(NAME)" libft_malloc.so diff --git a/tests.c b/tests.c new file mode 100644 index 0000000..9796d29 --- /dev/null +++ b/tests.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 17:58:16 by tomoron #+# #+# */ +/* Updated: 2025/03/20 17:58:17 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "srcs/includes/malloc.h" + +void malloc_free_test() +{ + void *ptr[3]; + + printf("MALLOC TEST (pointers shouldn't be null):\n"); + ptr[0] = malloc(42); + printf("tiny malloc : %p\n", ptr); + ptr[1] = malloc(1024); + printf("small malloc : %p\n", ptr); + ptr[2] = malloc(16384 * 1024); + printf("large malloc : %p\n", ptr); + + printf("\n\n\nFREE TEST (1024 bloc is from printf):\n"); + printf("before : \n"); + show_alloc_mem(); + printf("\n"); + free(ptr[0]); + free(ptr[1]); + free(ptr[2]); + printf("after : \n"); + show_alloc_mem(); +} + + +void realloc_test() +{ + void *ptr[3]; + + printf("\n\n\nREALLOC TEST :\n"); + printf("test 1 : the data in the first print should be the same in the second print and the size should be 20 in the show alloc\n"); + ptr[0] = malloc(10); + strcpy(ptr[0], "potato"); + printf("before realloc : %s\nptr : %p\n", (char *)ptr[0], ptr[0]); + + ptr[0] = realloc(ptr[0], 20); + printf("after realloc :%s\nptr : %p\n", (char *)ptr[0], ptr[0]); + show_alloc_mem(); + + printf("test 2 : changing chunk size (form 20 to 500), the pointer used is the same as test1\n"); + ptr[0] = realloc(ptr[0], 500); + printf("after realloc :%s\nptr : %p\n", (char *)ptr[0], ptr[0]); + show_alloc_mem(); + + printf("test 3 : changing chunk size (form 500 to 8192), the pointer used is the same as test2\n"); + ptr[0] = realloc(ptr[0], 8192); + printf("after realloc :%s\nptr : %p\n", (char *)ptr[0], ptr[0]); + show_alloc_mem(); + free(ptr[0]); +} + +void show_alloc_ex_test() +{ + char *data; + + data = malloc(1020); + memset(data, 0, 1020); + strcpy(data, "0123456789abcdef"); + strcpy(data + 16, "potato"); + data[584] = 'a'; + show_alloc_mem_ex(); +} + +int main(void) +{ + malloc_free_test(); + realloc_test(); + show_alloc_ex_test(); + + #ifdef LEAK_TEST + printf("leak_test set, allocating memory before end of program"); + malloc(10); + malloc(50); + malloc(8000); + malloc(1024); + #endif +}