From 30487c277ac5d4be5786733ca7b98adb4c810ae9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 27 Oct 2021 14:15:31 -0400 Subject: Add custom allocator support --- test/test_filter.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/test_filter.c (limited to 'test/test_filter.c') diff --git a/test/test_filter.c b/test/test_filter.c new file mode 100644 index 00000000..e326f9fb --- /dev/null +++ b/test/test_filter.c @@ -0,0 +1,68 @@ +/* + Copyright 2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "failing_allocator.h" +#include "serd/serd.h" + +#include +#include +#include + +static void +test_new_failed_alloc(void) +{ + static const SerdStringView s_string = SERD_STRING("http://example.org/s"); + static const SerdStringView p_string = SERD_STRING("http://example.org/p"); + static const SerdStringView o_string = SERD_STRING("http://example.org/o"); + static const SerdStringView g_string = SERD_STRING("http://example.org/g"); + + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdWorld* const world = serd_world_new(&allocator.base); + SerdNodes* const nodes = serd_nodes_new(&allocator.base); + const SerdNode* const s = serd_nodes_uri(nodes, s_string); + const SerdNode* const p = serd_nodes_uri(nodes, p_string); + const SerdNode* const o = serd_nodes_uri(nodes, o_string); + const SerdNode* const g = serd_nodes_uri(nodes, g_string); + + SerdSink* target = serd_sink_new(world, NULL, NULL, NULL); + const size_t n_setup_allocs = allocator.n_allocations; + + // Successfully allocate a filter to count the number of allocations + SerdSink* filter = serd_filter_new(world, target, s, p, o, g, true); + assert(filter); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_filter_new(world, target, s, p, o, g, true)); + } + + serd_sink_free(filter); + serd_sink_free(target); + serd_nodes_free(nodes); + serd_world_free(world); +} + +int +main(void) +{ + test_new_failed_alloc(); + return 0; +} -- cgit v1.2.1