#include #include "malunal/allocators.h" #define assert_equals(condition) \ do { \ if ((condition)) break; \ const malunal_cstr_t format = \ "[platform_allocator(%s:%d)]: " \ MALUNAL_TOSTRING(condition) \ " failed\n"; \ fprintf(stderr, format, __FILE__, __LINE__); \ return 0; \ } while (0) int test_platform_allocator() { const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4; const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8; allocation_result_t result; allocator_t plat_alloc = platform_allocator(); allocator_mptr_t allocator = &plat_alloc; result = allocator_acquire(allocator, init_bytes); if (result.threw) return result.error; malunal_size_t count; assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == init_bytes); assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == init_bytes); assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 1); assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 0); malunal_int32_t index; malunal_int32_t* vector; vector = result.address; for (index = 0; index < 4; index++) vector[index] = index + 1; result = allocator_reacquire( allocator, result.address, init_bytes, final_bytes ); if (result.threw) return result.error; assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == final_bytes); assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == final_bytes); assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 2); assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 1); vector = result.address; for (index = 0; index < 4; index++) if (vector[index] != index + 1) return 1; allocation_error_t relexc = allocator_release( allocator, result.address, final_bytes ); assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 0); assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 0); assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 2); assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS); assert_equals(count == 2); return relexc == ALLOCATION_ERROR_SUCCESS; }