Files
allocators/tests/libc_allocator.c
T
2026-08-07 18:33:50 -05:00

135 lines
3.6 KiB
C

#include <stdio.h>
#include "malunal/allocators.h"
int test_libc_allocator() {
const malunal_size_t init_bytes = sizeof(malunal_int32_t) * 4;
const malunal_size_t final_bytes = sizeof(malunal_int32_t) * 8;
allocator_acquire_res_t result;
allocator_t allocator = libc_allocator();
result = allocator_acquire((allocator_acquire_req_t) {
.allocator = &allocator,
.size = init_bytes
});
if (result.threw)
return result.exception;
if (allocator.reserved != init_bytes) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.reserved != %d, actual == %d";
fprintf(stderr, format, init_bytes, allocator.reserved);
return 1;
}
if (allocator.allocated != init_bytes) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.allocated != %d, actual == %d";
fprintf(stderr, format, init_bytes, allocator.allocated);
return 1;
}
if (allocator.acquires != 1) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.acquires != 1, actual == %d";
fprintf(stderr, format, allocator.acquires);
return 1;
}
if (allocator.releases != 0) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.releases != 0, actual == %d";
fprintf(stderr, format, allocator.releases);
return 1;
}
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_reacquire_req_t) {
.allocator = &allocator,
.prev = result.address,
.oldsz = init_bytes,
.newsz = final_bytes
});
if (result.threw)
return result.exception;
if (allocator.reserved != final_bytes) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.reserved != %d, actual == %d";
fprintf(stderr, format, final_bytes, allocator.reserved);
return 1;
}
if (allocator.allocated != final_bytes) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.allocator != %d, actual == %d";
fprintf(stderr, format, final_bytes, allocator.reserved);
return 1;
}
if (allocator.acquires != 2) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.acquires != 2, actual == %d";
fprintf(stderr, format, allocator.acquires);
return 1;
}
if (allocator.releases != 1) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.releases != 1, actual == %d";
fprintf(stderr, format, allocator.releases);
return 1;
}
vector = result.address;
for (index = 0; index < 4; index++)
if (vector[index] != index + 1)
return 1;
allocator_exception_t relexc =
allocator_release((allocator_release_req_t) {
.allocator = &allocator,
.address = result.address,
.size = final_bytes
});
if (allocator.reserved != 0) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.reserved != 0, actual == %d";
fprintf(stderr, format, allocator.reserved);
return 1;
}
if (allocator.allocated != 0) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.allocator != 0, actual == %d";
fprintf(stderr, format, allocator.reserved);
return 1;
}
if (allocator.acquires != 2) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.acquires != 2, actual == %d";
fprintf(stderr, format, allocator.acquires);
return 1;
}
if (allocator.releases != 2) {
const malunal_cstr_t format =
"[libc_allocator]: allocator.releases != 2, actual == %d";
fprintf(stderr, format, allocator.releases);
return 1;
}
return relexc == ALLOCATOR_EXCEPTION_SUCCESS;
}