New allocators, testing framework, overall improvements
This commit is contained in:
+10
-92
@@ -1,95 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include "malunal/allocators.h"
|
||||
#include "malunal/allocator.h"
|
||||
#include "malunal/microtest.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)
|
||||
MICROTEST(libc_allocator, can_acquire_and_dispose) {
|
||||
malunal_mptr_t address = null;
|
||||
allocator_mptr_t allocator = libc_allocator();
|
||||
error_t result = allocator_acquire(allocator, 32, &address);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
MICROTEST_ASSERT_NOT_NULL(address);
|
||||
|
||||
|
||||
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;
|
||||
|
||||
allocation_result_t result;
|
||||
|
||||
allocator_t libc_alloc = libc_allocator();
|
||||
allocator_mptr_t allocator = &libc_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;
|
||||
result = allocator_dispose(allocator, address, 32);
|
||||
MICROTEST_EXPECT_NULL(result.domain);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user