summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-09-28 11:14:08 -0400
committerDavid Robillard <d@drobilla.net>2024-09-28 11:15:15 -0400
commit50c73ad346fb63eb6d057dcc7d1351c95e12e83d (patch)
tree17e1be3b38642022e582dfde58ccced85c4ca15e /test
parent19bd977e943bf37c2c24ee0116486fa9b7611f70 (diff)
downloadzix-50c73ad346fb63eb6d057dcc7d1351c95e12e83d.tar.gz
zix-50c73ad346fb63eb6d057dcc7d1351c95e12e83d.tar.bz2
zix-50c73ad346fb63eb6d057dcc7d1351c95e12e83d.zip
Improve test allocator API
Add a simple API for setting up the failing allocator to avoid manually tinkering with its members. Conveniently, this also avoids confusing cppcheck.
Diffstat (limited to 'test')
-rw-r--r--test/failing_allocator.c18
-rw-r--r--test/failing_allocator.h13
-rw-r--r--test/test_allocator.c2
-rw-r--r--test/test_btree.c4
-rw-r--r--test/test_hash.c4
-rw-r--r--test/test_ring.c4
-rw-r--r--test/test_string_view.c4
-rw-r--r--test/test_tree.c4
8 files changed, 35 insertions, 18 deletions
diff --git a/test/failing_allocator.c b/test/failing_allocator.c
index 684a8ec..53a5216 100644
--- a/test/failing_allocator.c
+++ b/test/failing_allocator.c
@@ -1,10 +1,10 @@
-// Copyright 2021 David Robillard <d@drobilla.net>
+// Copyright 2021-2024 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#include "failing_allocator.h"
-#include "zix/allocator.h"
-#include "zix/attributes.h"
+#include <zix/allocator.h>
+#include <zix/attributes.h>
#include <stdbool.h>
#include <stddef.h>
@@ -107,3 +107,15 @@ zix_failing_allocator(void)
return failing_allocator;
}
+
+size_t
+zix_failing_allocator_reset(ZixFailingAllocator* const allocator,
+ const size_t n_allowed)
+{
+ const size_t n_allocations = allocator->n_allocations;
+
+ allocator->n_allocations = 0U;
+ allocator->n_remaining = n_allowed;
+
+ return n_allocations;
+}
diff --git a/test/failing_allocator.h b/test/failing_allocator.h
index 982874d..5f6d220 100644
--- a/test/failing_allocator.h
+++ b/test/failing_allocator.h
@@ -1,8 +1,8 @@
-// Copyright 2021 David Robillard <d@drobilla.net>
+// Copyright 2021-2024 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
-#ifndef ZIX_FAILING_ALLOCATOR_H
-#define ZIX_FAILING_ALLOCATOR_H
+#ifndef ZIX_TEST_FAILING_ALLOCATOR_H
+#define ZIX_TEST_FAILING_ALLOCATOR_H
#include "zix/allocator.h"
@@ -15,7 +15,12 @@ typedef struct {
size_t n_remaining; ///< Number of remaining successful allocations
} ZixFailingAllocator;
+/// Return an allocator configured by default to succeed
ZixFailingAllocator
zix_failing_allocator(void);
-#endif // ZIX_FAILING_ALLOCATOR_H
+/// Reset an allocator to fail after some number of "allowed" allocations
+size_t
+zix_failing_allocator_reset(ZixFailingAllocator* allocator, size_t n_allowed);
+
+#endif // ZIX_TEST_FAILING_ALLOCATOR_H
diff --git a/test/test_allocator.c b/test/test_allocator.c
index 9ecbfa0..2677624 100644
--- a/test/test_allocator.c
+++ b/test/test_allocator.c
@@ -122,7 +122,7 @@ static void
test_failing_allocator(void)
{
ZixFailingAllocator allocator = zix_failing_allocator();
- allocator.n_remaining = 0;
+ zix_failing_allocator_reset(&allocator, 0);
assert(!zix_malloc(&allocator.base, 16U));
assert(!zix_calloc(&allocator.base, 16U, 1U));
diff --git a/test/test_btree.c b/test/test_btree.c
index e918799..a3183c6 100644
--- a/test/test_btree.c
+++ b/test/test_btree.c
@@ -563,9 +563,9 @@ test_failed_alloc(void)
assert(!stress(&allocator.base, 0, 4096));
// Test that each allocation failing is handled gracefully
- const size_t n_new_allocs = allocator.n_allocations;
+ const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
for (size_t i = 0U; i < n_new_allocs; ++i) {
- allocator.n_remaining = i;
+ zix_failing_allocator_reset(&allocator, i);
assert(stress(&allocator.base, 0, 4096));
}
}
diff --git a/test/test_hash.c b/test/test_hash.c
index 3d3ca95..8ee6b82 100644
--- a/test/test_hash.c
+++ b/test/test_hash.c
@@ -366,9 +366,9 @@ test_failed_alloc(void)
assert(!stress(&allocator.base, 16));
// Test that each allocation failing is handled gracefully
- const size_t n_new_allocs = allocator.n_allocations;
+ const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
for (size_t i = 0U; i < n_new_allocs; ++i) {
- allocator.n_remaining = i;
+ zix_failing_allocator_reset(&allocator, i);
assert(stress(&allocator.base, 16));
}
}
diff --git a/test/test_ring.c b/test/test_ring.c
index b1845ce..06d50ee 100644
--- a/test/test_ring.c
+++ b/test/test_ring.c
@@ -170,9 +170,9 @@ test_failed_alloc(void)
assert(ring);
// Test that each allocation failing is handled gracefully
- const size_t n_new_allocs = allocator.n_allocations;
+ const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
for (size_t i = 0U; i < n_new_allocs; ++i) {
- allocator.n_remaining = i;
+ zix_failing_allocator_reset(&allocator, i);
assert(!zix_ring_new(&allocator.base, 512));
}
diff --git a/test/test_string_view.c b/test/test_string_view.c
index 76ceab5..764edcb 100644
--- a/test/test_string_view.c
+++ b/test/test_string_view.c
@@ -95,7 +95,7 @@ test_copy(void)
ZixFailingAllocator allocator = zix_failing_allocator();
// Copying a string takes exactly one allocation
- allocator.n_remaining = 1U;
+ zix_failing_allocator_reset(&allocator, 1U);
char* const copy = zix_string_view_copy(&allocator.base, orig);
assert(copy);
@@ -103,7 +103,7 @@ test_copy(void)
zix_free(&allocator.base, copy);
// Check that allocation failure is handled gracefully
- allocator.n_remaining = 0U;
+ zix_failing_allocator_reset(&allocator, 0U);
assert(!zix_string_view_copy(&allocator.base, orig));
}
diff --git a/test/test_tree.c b/test/test_tree.c
index fae6d85..ff63999 100644
--- a/test/test_tree.c
+++ b/test/test_tree.c
@@ -211,9 +211,9 @@ test_failed_alloc(void)
assert(!stress(&allocator.base, 0, 16));
// Test that each allocation failing is handled gracefully
- const size_t n_new_allocs = allocator.n_allocations;
+ const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
for (size_t i = 0U; i < n_new_allocs; ++i) {
- allocator.n_remaining = i;
+ zix_failing_allocator_reset(&allocator, i);
assert(stress(&allocator.base, 0, 16));
}
}