aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_sink.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-09-10 15:06:42 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commit248a874d7425749d29cf900a1c3783c624ea8d8c (patch)
treeaed59f5a484a815cd254506866e98a947858904d /test/test_sink.c
parent0bd10132c6707353dba80bd89cf0102ee7ca4e34 (diff)
downloadserd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.gz
serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.bz2
serd-248a874d7425749d29cf900a1c3783c624ea8d8c.zip
Add support for custom allocators
This makes it explicit in the API where memory is allocated, and allows the user to provide a custom allocator to avoid the use of the default system allocator for whatever reason.
Diffstat (limited to 'test/test_sink.c')
-rw-r--r--test/test_sink.c52
1 files changed, 37 insertions, 15 deletions
diff --git a/test/test_sink.c b/test/test_sink.c
index b0756fcc..9c7959de 100644
--- a/test/test_sink.c
+++ b/test/test_sink.c
@@ -1,8 +1,10 @@
-// Copyright 2019-2020 David Robillard <d@drobilla.net>
+// Copyright 2019-2021 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#undef NDEBUG
+#include "failing_allocator.h"
+
#include "serd/env.h"
#include "serd/event.h"
#include "serd/node.h"
@@ -88,17 +90,36 @@ on_event(void* const handle, const SerdEvent* const event)
}
static void
+test_failed_alloc(void)
+{
+ SerdFailingAllocator allocator = serd_failing_allocator();
+
+ // Successfully allocate a sink to count the number of allocations
+ SerdSink* const sink = serd_sink_new(&allocator.base, NULL, NULL, NULL);
+ assert(sink);
+
+ // Test that each allocation failing is handled gracefully
+ const size_t n_allocs = allocator.n_allocations;
+ for (size_t i = 0; i < n_allocs; ++i) {
+ allocator.n_remaining = i;
+ assert(!serd_sink_new(&allocator.base, NULL, NULL, NULL));
+ }
+
+ serd_sink_free(sink);
+}
+
+static void
test_callbacks(void)
{
- SerdNode* const base = serd_new_uri(serd_string(NS_EG));
- SerdNode* const name = serd_new_string(serd_string("eg"));
- SerdNode* const uri = serd_new_uri(serd_string(NS_EG "uri"));
- SerdNode* const blank = serd_new_blank(serd_string("b1"));
- SerdEnv* env = serd_env_new(serd_node_string_view(base));
+ SerdNode* const base = serd_new_uri(NULL, serd_string(NS_EG));
+ SerdNode* const name = serd_new_string(NULL, serd_string("eg"));
+ SerdNode* const uri = serd_new_uri(NULL, serd_string(NS_EG "uri"));
+ SerdNode* const blank = serd_new_blank(NULL, serd_string("b1"));
+ SerdEnv* env = serd_env_new(NULL, serd_node_string_view(base));
State state = {0, 0, 0, 0, 0, SERD_SUCCESS};
SerdStatement* const statement =
- serd_statement_new(base, uri, blank, NULL, NULL);
+ serd_statement_new(NULL, base, uri, blank, NULL, NULL);
const SerdBaseEvent base_event = {SERD_BASE, uri};
const SerdPrefixEvent prefix_event = {SERD_PREFIX, name, uri};
@@ -107,7 +128,7 @@ test_callbacks(void)
// Call functions on a sink with no functions set
- SerdSink* null_sink = serd_sink_new(&state, NULL, NULL);
+ SerdSink* null_sink = serd_sink_new(NULL, &state, NULL, NULL);
assert(!serd_sink_write_base(null_sink, base));
assert(!serd_sink_write_prefix(null_sink, name, uri));
@@ -130,7 +151,7 @@ test_callbacks(void)
// Try again with a sink that has the event handler set
- SerdSink* sink = serd_sink_new(&state, on_event, NULL);
+ SerdSink* sink = serd_sink_new(NULL, &state, on_event, NULL);
assert(!serd_sink_write_base(sink, base));
assert(serd_node_equals(state.last_base, base));
@@ -150,12 +171,12 @@ test_callbacks(void)
serd_sink_free(sink);
- serd_statement_free(statement);
+ serd_statement_free(NULL, statement);
serd_env_free(env);
- serd_node_free(blank);
- serd_node_free(uri);
- serd_node_free(name);
- serd_node_free(base);
+ serd_node_free(NULL, blank);
+ serd_node_free(NULL, uri);
+ serd_node_free(NULL, name);
+ serd_node_free(NULL, base);
}
static void
@@ -166,7 +187,7 @@ test_free(void)
// Set up a sink with dynamically allocated data and a free function
uintptr_t* data = (uintptr_t*)calloc(1, sizeof(uintptr_t));
- SerdSink* sink = serd_sink_new(data, NULL, free);
+ SerdSink* sink = serd_sink_new(NULL, data, NULL, free);
// Free the sink, which should free the data (rely on valgrind or sanitizers)
serd_sink_free(sink);
@@ -175,6 +196,7 @@ test_free(void)
int
main(void)
{
+ test_failed_alloc();
test_callbacks();
test_free();
return 0;