42_malloc/test/main.c

73 lines
2.9 KiB
C
Raw Normal View History

2024-07-30 03:28:43 -04:00
#include "../include/malloc.h"
#include "../src/align.h"
#include "../src/malloc.h"
#include "../src/chunk.h"
#include "../src/raw_chunk_manager.h"
#include <stdio.h>
2024-07-30 07:05:18 -04:00
#include <stdint.h>
2024-07-30 03:28:43 -04:00
void test(const void* expected_value, const void* value, const char* test_name, const char* description)
{
printf("%s ", test_name);
2024-07-30 07:05:18 -04:00
if (expected_value == value)
2024-07-30 03:28:43 -04:00
printf("[OK]");
else
printf("[FAILED] %s {%p != %p}", description, expected_value, value);
printf("\n");
}
int main(int ac, char **av)
{
2024-07-30 13:16:15 -04:00
void *ptr, *ptr1, *ptr2, *ptr3;
2024-07-30 03:28:43 -04:00
(void) ac;
(void) av;
printf("-----------ALIGN-----------\n");
test(0, (void*) get_align_increment(0), "align test 1", "");
test((void*) 15, (void*) get_align_increment((void*) 1), "align test 2", "");
printf("-----------ALLOC (LARGE)-----------\n");
2024-07-30 13:16:15 -04:00
ptr = ft_malloc(10000000);
2024-07-30 03:28:43 -04:00
ft_free(ptr);
test(NULL, allocs_tree[LARGE], "alloc free", "simple alloc, simple free");
ptr = ft_malloc(10000000);
test(ptr, ((void **) allocs_tree[LARGE])[CHUNK_DATA_START_POS], "free alloc", "alloc after a free on the first block");
ft_free(ptr);
2024-07-30 13:16:15 -04:00
ptr1 = ft_malloc(10000000);
ptr2 = ft_malloc(10000000);
ptr3 = ft_malloc(10000000);
2024-07-30 07:05:18 -04:00
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr3) == NULL), "alloc third", "");
2024-07-30 03:28:43 -04:00
ft_free(ptr1);
2024-07-30 07:05:18 -04:00
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", "");
2024-07-30 03:28:43 -04:00
ft_free(ptr3);
2024-07-30 07:05:18 -04:00
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", "");
2024-07-30 03:28:43 -04:00
ft_free(ptr2);
test(NULL, allocs_tree[LARGE], "free disorder3", "");
2024-07-30 03:28:43 -04:00
2024-07-30 13:16:15 -04:00
printf("-----------ALLOC (TINY)-----------\n");
ptr = ft_malloc(4);
ft_free(ptr);
test(NULL, allocs_tree[TINY], "alloc free", "simple alloc, simple free");
ptr = ft_malloc(4);
test(ptr, ((void **) allocs_tree[TINY])[CHUNK_DATA_START_POS], "free alloc", "alloc after a free on the first block");
ft_free(ptr);
ptr1 = ft_malloc(4);
ptr2 = ft_malloc(4);
ptr3 = ft_malloc(4);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr3) == NULL), "alloc third", "");
ft_free(ptr1);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder1", "");
ft_free(ptr3);
test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder2", "");
ft_free(ptr2);
test(NULL, allocs_tree[TINY], "free disorder3", "");
2024-07-30 13:19:55 -04:00
show_alloc_mem();
2024-07-30 13:16:15 -04:00
2024-07-30 03:28:43 -04:00
return 0;
}