awtk/tests/allocator_test.c

207 lines
6.4 KiB
C
Raw Normal View History

#include "tkc/mem.h"
#include "tkc/mem_allocator_pool.h"
2020-06-17 17:59:38 +08:00
#include "tkc/mem_allocator_simple.h"
#include "tkc/mem_allocator_composite.h"
2021-04-24 11:18:47 +08:00
static void allocator_test_simple(void) {
char buff[64];
void* addr1 = NULL;
void* addr2 = NULL;
mem_allocator_simple_t simple;
mem_allocator_t* allocator = mem_allocator_simple_init(&simple, buff, sizeof(buff));
assert(simple.info.buffer == buff);
assert(simple.info.size == sizeof(buff));
addr1 = mem_allocator_alloc(allocator, 10, __FUNCTION__, __LINE__);
assert(addr1 != NULL);
assert(simple.info.used_block_nr == 1);
2021-04-28 09:03:35 +08:00
2021-04-24 11:18:47 +08:00
addr2 = mem_allocator_alloc(allocator, 10, __FUNCTION__, __LINE__);
assert(addr2 != NULL);
assert(simple.info.used_block_nr == 2);
2021-04-28 09:03:35 +08:00
2021-04-24 11:18:47 +08:00
addr1 = mem_allocator_realloc(allocator, addr1, 12, __FUNCTION__, __LINE__);
assert(simple.info.used_block_nr == 2);
2021-04-28 09:03:35 +08:00
2021-04-24 11:18:47 +08:00
addr2 = mem_allocator_realloc(allocator, addr2, 12, __FUNCTION__, __LINE__);
assert(simple.info.used_block_nr == 2);
mem_allocator_free(allocator, addr1);
assert(simple.info.used_block_nr == 1);
2021-04-28 09:03:35 +08:00
2021-04-24 11:18:47 +08:00
mem_allocator_free(allocator, addr2);
assert(simple.info.used_block_nr == 0);
assert(simple.info.used_bytes == 0);
assert(simple.info.used_max_block_nr == 2);
assert(simple.info.free_list->size == sizeof(buff));
mem_allocator_destroy(allocator);
}
void allocator_test_basic_ex(mem_allocator_t* allocator) {
2020-06-17 17:59:38 +08:00
uint32_t i = 0;
void* addr = NULL;
void* addrs[100];
for (i = 0; i < ARRAY_SIZE(addrs); i++) {
addr = mem_allocator_alloc(allocator, i + 1, __FUNCTION__, __LINE__);
assert(addr != NULL);
addr = mem_allocator_realloc(allocator, addr, i + 1, __FUNCTION__, __LINE__);
assert(addr != NULL);
addr = mem_allocator_realloc(allocator, addr, i + 5, __FUNCTION__, __LINE__);
assert(addr != NULL);
addrs[i] = addr;
}
for (i = 0; i < ARRAY_SIZE(addrs); i++) {
mem_allocator_free(allocator, addrs[i]);
}
}
void allocator_test_basic() {
uint32_t i = 0;
char buff[102400];
mem_allocator_simple_t simple;
mem_allocator_t* allocator = mem_allocator_simple_init(&simple, buff, sizeof(buff));
mem_allocator_pool_t pool;
allocator = mem_allocator_pool_init(&pool, allocator, 10, 10, 10, 10, 10);
allocator_test_basic_ex(allocator);
2020-06-19 17:54:43 +08:00
for (i = 0; i < TK_MEM_POOLS_NR; i++) {
assert(pool.pools[i]->used == 0);
}
mem_allocator_destroy(allocator);
return;
2020-06-17 17:59:38 +08:00
}
void allocator_test_rand_ex(mem_allocator_t* allocator) {
2020-06-17 17:59:38 +08:00
uint32_t k = 0;
uint32_t i = 0;
void* addr = NULL;
void* addrs[1000];
for (k = 0; k < 1000; k++) {
2020-06-17 17:59:38 +08:00
for (i = 0; i < ARRAY_SIZE(addrs); i++) {
uint32_t size = random() % 100;
addr = mem_allocator_alloc(allocator, size, __FUNCTION__, __LINE__);
assert(addr != NULL);
addr = mem_allocator_realloc(allocator, addr, size, __FUNCTION__, __LINE__);
assert(addr != NULL);
addr = mem_allocator_realloc(allocator, addr, size + 10, __FUNCTION__, __LINE__);
assert(addr != NULL);
addrs[i] = addr;
}
for (i = 0; i < ARRAY_SIZE(addrs); i++) {
mem_allocator_free(allocator, addrs[i]);
}
}
}
void allocator_test_rand() {
uint32_t i = 0;
char buff[1000 * 1000];
mem_allocator_simple_t simple;
mem_allocator_t* allocator = mem_allocator_simple_init(&simple, buff, sizeof(buff));
mem_allocator_pool_t pool;
allocator = mem_allocator_pool_init(&pool, allocator, 10, 10, 10, 10, 10);
allocator_test_rand_ex(allocator);
2020-06-19 17:54:43 +08:00
for (i = 0; i < TK_MEM_POOLS_NR; i++) {
assert(pool.pools[i]->used == 0);
}
mem_allocator_destroy(allocator);
}
void allocator_test_composite0() {
2021-04-28 09:03:35 +08:00
char mem1[100 * 1000];
char mem2[1000 * 1000];
mem_allocator_composite_t composite;
2021-04-28 09:03:35 +08:00
mem_allocator_t* allocator =
mem_allocator_composite_init(&composite, mem1, sizeof(mem1), mem2, sizeof(mem2), NULL);
allocator_test_basic_ex(allocator);
allocator_test_rand_ex(allocator);
mem_allocator_destroy(allocator);
}
void allocator_test_composite1() {
char mem1[36];
char mem2[64];
char mem3[1280];
2021-04-28 09:03:35 +08:00
char* addr = NULL;
mem_allocator_composite_t composite;
2021-04-28 09:03:35 +08:00
mem_allocator_t* allocator = mem_allocator_composite_init(&composite, mem1, sizeof(mem1), mem2,
sizeof(mem2), mem3, sizeof(mem3), NULL);
/*mem1*/
addr = mem_allocator_alloc(allocator, 16, __FUNCTION__, __LINE__);
assert(addr >= mem1);
assert(addr < (mem1 + sizeof(mem1)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
mem_allocator_free(allocator, addr);
addr = mem_allocator_alloc(allocator, 16, __FUNCTION__, __LINE__);
assert(addr >= mem1);
assert(addr < (mem1 + sizeof(mem1)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
2021-04-24 09:46:04 +08:00
addr = mem_allocator_realloc(allocator, addr, 16, __FUNCTION__, __LINE__);
assert(addr >= mem1);
assert(addr < (mem1 + sizeof(mem1)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
addr = mem_allocator_realloc(allocator, addr, 16 + 2, __FUNCTION__, __LINE__);
assert(addr >= mem1);
assert(addr < (mem1 + sizeof(mem1)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
/*mem2*/
addr = mem_allocator_alloc(allocator, 36, __FUNCTION__, __LINE__);
assert(addr >= mem2);
assert(addr < (mem2 + sizeof(mem2)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
mem_allocator_free(allocator, addr);
addr = mem_allocator_alloc(allocator, 36, __FUNCTION__, __LINE__);
assert(addr >= mem2);
assert(addr < (mem2 + sizeof(mem2)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
addr = mem_allocator_realloc(allocator, addr, 30, __FUNCTION__, __LINE__);
assert(addr >= mem2);
assert(addr < (mem2 + sizeof(mem2)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
/*mem3*/
addr = mem_allocator_alloc(allocator, 66, __FUNCTION__, __LINE__);
assert(addr >= mem3);
assert(addr < (mem3 + sizeof(mem3)));
2021-04-28 09:03:35 +08:00
mem_allocator_free(allocator, addr);
addr = mem_allocator_alloc(allocator, 66, __FUNCTION__, __LINE__);
assert(addr >= mem3);
assert(addr < (mem3 + sizeof(mem3)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
2021-04-28 09:03:35 +08:00
addr = mem_allocator_realloc(allocator, addr, 66 + 2, __FUNCTION__, __LINE__);
assert(addr >= mem3);
assert(addr < (mem3 + sizeof(mem3)));
assert(mem_allocator_composite_is_valid_addr(allocator, addr));
mem_allocator_destroy(allocator);
return;
2020-06-17 17:59:38 +08:00
}
void allocator_test() {
allocator_test_composite1();
2020-06-17 17:59:38 +08:00
allocator_test_basic();
allocator_test_rand();
2021-04-24 11:18:47 +08:00
allocator_test_simple();
2020-06-17 17:59:38 +08:00
}