diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/allocator_test.c | 2 | ||||
-rw-r--r-- | test/failing_allocator.c | 68 | ||||
-rw-r--r-- | test/failing_allocator.h | 11 | ||||
-rw-r--r-- | test/ring_test.c | 11 |
4 files changed, 48 insertions, 44 deletions
diff --git a/test/allocator_test.c b/test/allocator_test.c index 6850bdc..a6087e1 100644 --- a/test/allocator_test.c +++ b/test/allocator_test.c @@ -12,7 +12,7 @@ test_allocator(void) { // Just a basic smoke test to check that things seem to be working - const ZixAllocator* const allocator = zix_default_allocator(); + ZixAllocator* const allocator = zix_default_allocator(); char* const malloced = (char*)zix_malloc(allocator, 4); malloced[0] = 0; diff --git a/test/failing_allocator.c b/test/failing_allocator.c index d00f2f1..eda762b 100644 --- a/test/failing_allocator.c +++ b/test/failing_allocator.c @@ -8,73 +8,77 @@ #include <stdbool.h> #include <stddef.h> +#include <stdint.h> static bool -attempt(ZixFailingAllocatorState* const state) +attempt(ZixFailingAllocator* const allocator) { - ++state->n_allocations; + ++allocator->n_allocations; - if (!state->n_remaining) { + if (!allocator->n_remaining) { return false; } - --state->n_remaining; + --allocator->n_remaining; return true; } ZIX_MALLOC_FUNC static void* -zix_failing_malloc(ZixAllocatorHandle* const handle, const size_t size) +zix_failing_malloc(ZixAllocator* const allocator, const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->malloc(base->handle, size) : NULL; + return attempt(state) ? base->malloc(base, size) : NULL; } ZIX_MALLOC_FUNC static void* -zix_failing_calloc(ZixAllocatorHandle* const handle, - const size_t nmemb, - const size_t size) +zix_failing_calloc(ZixAllocator* const allocator, + const size_t nmemb, + const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->calloc(base->handle, nmemb, size) : NULL; + return attempt(state) ? base->calloc(base, nmemb, size) : NULL; } static void* -zix_failing_realloc(ZixAllocatorHandle* const handle, - void* const ptr, - const size_t size) +zix_failing_realloc(ZixAllocator* const allocator, + void* const ptr, + const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->realloc(base->handle, ptr, size) : NULL; + return attempt(state) ? base->realloc(base, ptr, size) : NULL; } static void -zix_failing_free(ZixAllocatorHandle* const handle, void* const ptr) +zix_failing_free(ZixAllocator* const allocator, void* const ptr) { - (void)handle; + (void)allocator; - const ZixAllocator* const base = zix_default_allocator(); + ZixAllocator* const base = zix_default_allocator(); - base->free(base->handle, ptr); + base->free(base, ptr); } ZIX_CONST_FUNC -ZixAllocator -zix_failing_allocator(ZixFailingAllocatorState* const state) +ZixFailingAllocator +zix_failing_allocator(void) { - const ZixAllocator failing_allocator = { - state, - zix_failing_malloc, - zix_failing_calloc, - zix_failing_realloc, - zix_failing_free, + ZixFailingAllocator failing_allocator = { + { + zix_failing_malloc, + zix_failing_calloc, + zix_failing_realloc, + zix_failing_free, + }, + 0, + SIZE_MAX, }; return failing_allocator; diff --git a/test/failing_allocator.h b/test/failing_allocator.h index 89bee88..982874d 100644 --- a/test/failing_allocator.h +++ b/test/failing_allocator.h @@ -10,11 +10,12 @@ /// An allocator that fails after some number of successes for testing typedef struct { - size_t n_allocations; ///< The number of allocations that have been attempted - size_t n_remaining; ///< The number of remaining successful allocations -} ZixFailingAllocatorState; + ZixAllocator base; ///< Base allocator instance + size_t n_allocations; ///< Number of attempted allocations + size_t n_remaining; ///< Number of remaining successful allocations +} ZixFailingAllocator; -ZixAllocator -zix_failing_allocator(ZixFailingAllocatorState* state); +ZixFailingAllocator +zix_failing_allocator(void); #endif // ZIX_FAILING_ALLOCATOR_H diff --git a/test/ring_test.c b/test/ring_test.c index b4fa5f1..ba2a4d1 100644 --- a/test/ring_test.c +++ b/test/ring_test.c @@ -161,18 +161,17 @@ test_ring(const unsigned size) static void test_failed_alloc(void) { - ZixFailingAllocatorState state = {0u, SIZE_MAX}; - ZixAllocator allocator = zix_failing_allocator(&state); + ZixFailingAllocator allocator = zix_failing_allocator(); // Successfully allocate a ring to count the number of allocations - ring = zix_ring_new(&allocator, 512); + ring = zix_ring_new(&allocator.base, 512); assert(ring); // Test that each allocation failing is handled gracefully - const size_t n_new_allocs = state.n_allocations; + const size_t n_new_allocs = allocator.n_allocations; for (size_t i = 0u; i < n_new_allocs; ++i) { - state.n_remaining = i; - assert(!zix_ring_new(&allocator, 512)); + allocator.n_remaining = i; + assert(!zix_ring_new(&allocator.base, 512)); } zix_ring_free(ring); |