Files
allocators/tests/platform_allocator.c
T

96 lines
2.8 KiB
C
Raw Normal View History

2026-08-07 18:33:50 -05:00
#include <stdio.h>
#include "malunal/allocators.h"
2026-08-12 19:47:59 -05:00
#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)
2026-08-07 18:33:50 -05:00
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;
2026-08-12 19:47:59 -05:00
allocation_result_t result;
2026-08-07 18:33:50 -05:00
2026-08-12 19:47:59 -05:00
allocator_t plat_alloc = platform_allocator();
allocator_mptr_t allocator = &plat_alloc;
result = allocator_acquire(allocator, init_bytes);
2026-08-07 18:33:50 -05:00
if (result.threw)
2026-08-12 19:47:59 -05:00
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);
2026-08-07 18:33:50 -05:00
malunal_int32_t index;
malunal_int32_t* vector;
vector = result.address;
for (index = 0; index < 4; index++)
vector[index] = index + 1;
2026-08-12 19:47:59 -05:00
result = allocator_reacquire(
allocator,
result.address,
init_bytes,
final_bytes
);
2026-08-07 18:33:50 -05:00
if (result.threw)
2026-08-12 19:47:59 -05:00
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);
2026-08-07 18:33:50 -05:00
vector = result.address;
for (index = 0; index < 4; index++)
if (vector[index] != index + 1)
return 1;
2026-08-12 19:47:59 -05:00
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;
2026-08-07 18:33:50 -05:00
}