42_malloc/test/main.c

73 lines
3.0 KiB
C

#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>
#include <stdint.h>
void test(const void* expected_value, const void* value, const char* test_name, const char* description)
{
printf("%s ", test_name);
if (expected_value == value)
printf("[OK]");
else
printf("[FAILED] %s {%p != %p}", description, expected_value, value);
printf("\n");
}
int main(int ac, char **av)
{
void *ptr, *ptr1, *ptr2, *ptr3;
(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");
ptr = ft_malloc(10000000);
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);
ptr1 = ft_malloc(10000000);
ptr2 = ft_malloc(10000000);
ptr3 = ft_malloc(10000000);
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr3) == NULL), "alloc third", "");
ft_free(ptr1);
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", "");
ft_free(ptr3);
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", "");
ft_free(ptr2);
test(NULL, allocs_tree[LARGE], "free disorder3", "");
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_by_root(allocs_tree[TINY], ptr1) == NULL), "alloc first", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "alloc second", "");
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr3) == NULL), "alloc third", "");
ft_free(ptr1);
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder1", "");
ft_free(ptr3);
test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder2", "");
ft_free(ptr2);
test(NULL, allocs_tree[TINY], "free disorder3", "");
show_alloc_mem();
return 0;
}