Reworked, no version bump
This commit is contained in:
+54
-93
@@ -1,49 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.h"
|
||||
|
||||
#define assert_equals(condition) \
|
||||
do { \
|
||||
if ((condition)) break; \
|
||||
const malunal_cstr_t format = \
|
||||
"[libc_allocator(%s:%d)]: " \
|
||||
MALUNAL_TOSTRING(condition) \
|
||||
" failed\n"; \
|
||||
fprintf(stderr, format, __FILE__, __LINE__); \
|
||||
return 0; \
|
||||
} while (0)
|
||||
|
||||
|
||||
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();
|
||||
allocation_result_t result;
|
||||
|
||||
result = allocator_acquire((allocator_acquire_req_t) {
|
||||
.allocator = &allocator,
|
||||
.size = init_bytes
|
||||
});
|
||||
allocator_t libc_alloc = libc_allocator();
|
||||
allocator_mptr_t allocator = &libc_alloc;
|
||||
result = allocator_acquire(allocator, init_bytes);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
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;
|
||||
}
|
||||
malunal_size_t count;
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
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;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == init_bytes);
|
||||
|
||||
if (allocator.acquires != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 1);
|
||||
|
||||
if (allocator.releases != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
malunal_int32_t index;
|
||||
malunal_int32_t* vector;
|
||||
@@ -52,83 +46,50 @@ int test_libc_allocator() {
|
||||
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
|
||||
});
|
||||
result = allocator_reacquire(
|
||||
allocator,
|
||||
result.address,
|
||||
init_bytes,
|
||||
final_bytes
|
||||
);
|
||||
|
||||
if (result.threw)
|
||||
return result.exception;
|
||||
return result.error;
|
||||
|
||||
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;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
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;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == final_bytes);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 1) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 1, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
|
||||
allocator_exception_t relexc =
|
||||
allocator_release((allocator_release_req_t) {
|
||||
.allocator = &allocator,
|
||||
.address = result.address,
|
||||
.size = final_bytes
|
||||
});
|
||||
allocation_error_t relexc = allocator_release(
|
||||
allocator,
|
||||
result.address,
|
||||
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;
|
||||
}
|
||||
assert_equals(allocator_reserved_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.allocated != 0) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.allocator != 0, actual == %d";
|
||||
fprintf(stderr, format, allocator.reserved);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_allocated_bytes(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 0);
|
||||
|
||||
if (allocator.acquires != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.acquires != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.acquires);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_acquires_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
if (allocator.releases != 2) {
|
||||
const malunal_cstr_t format =
|
||||
"[libc_allocator]: allocator.releases != 2, actual == %d";
|
||||
fprintf(stderr, format, allocator.releases);
|
||||
return 1;
|
||||
}
|
||||
assert_equals(allocator_releases_count(allocator, &count) == ALLOCATION_ERROR_SUCCESS);
|
||||
assert_equals(count == 2);
|
||||
|
||||
return relexc == ALLOCATOR_EXCEPTION_SUCCESS;
|
||||
return relexc == ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user