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_caret.c | 83 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 15 deletions(-) (limited to 'test/test_caret.c') diff --git a/test/test_caret.c b/test/test_caret.c index 54037a30..5fb6d18d 100644 --- a/test/test_caret.c +++ b/test/test_caret.c @@ -1,5 +1,5 @@ /* - Copyright 2019-2020 David Robillard + Copyright 2019-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 @@ -16,34 +16,39 @@ #undef NDEBUG +#include "failing_allocator.h" + #include "serd/serd.h" #include #include +#include -int -main(void) +static int +test_caret(void) { - SerdNodes* const nodes = serd_nodes_new(); + SerdAllocator* const allocator = serd_default_allocator(); + + SerdNodes* const nodes = serd_nodes_new(allocator); const SerdNode* const node = serd_nodes_string(nodes, SERD_STRING("node")); - SerdCaret* const caret = serd_caret_new(node, 46, 2); + SerdCaret* const caret = serd_caret_new(allocator, node, 46, 2); assert(serd_caret_equals(caret, caret)); assert(serd_caret_name(caret) == node); assert(serd_caret_line(caret) == 46); assert(serd_caret_column(caret) == 2); - SerdCaret* const copy = serd_caret_copy(caret); + SerdCaret* const copy = serd_caret_copy(allocator, caret); assert(serd_caret_equals(caret, copy)); - assert(!serd_caret_copy(NULL)); + assert(!serd_caret_copy(allocator, NULL)); const SerdNode* const other_node = serd_nodes_string(nodes, SERD_STRING("other")); - SerdCaret* const other_file = serd_caret_new(other_node, 46, 2); - SerdCaret* const other_line = serd_caret_new(node, 47, 2); - SerdCaret* const other_col = serd_caret_new(node, 46, 3); + SerdCaret* const other_file = serd_caret_new(allocator, other_node, 46, 2); + SerdCaret* const other_line = serd_caret_new(allocator, node, 47, 2); + SerdCaret* const other_col = serd_caret_new(allocator, node, 46, 3); assert(!serd_caret_equals(caret, other_file)); assert(!serd_caret_equals(caret, other_line)); @@ -51,12 +56,60 @@ main(void) assert(!serd_caret_equals(caret, NULL)); assert(!serd_caret_equals(NULL, caret)); - serd_caret_free(other_col); - serd_caret_free(other_line); - serd_caret_free(other_file); - serd_caret_free(copy); - serd_caret_free(caret); + serd_caret_free(allocator, other_col); + serd_caret_free(allocator, other_line); + serd_caret_free(allocator, other_file); + serd_caret_free(allocator, copy); + serd_caret_free(allocator, caret); serd_nodes_free(nodes); return 0; } + +static void +test_failed_alloc(void) +{ + char node_buf[32]; + + assert(!serd_node_construct_token( + sizeof(node_buf), node_buf, SERD_LITERAL, SERD_STRING("node")) + .status); + + const SerdNode* node = (const SerdNode*)node_buf; + SerdFailingAllocator allocator = serd_failing_allocator(); + + // Successfully allocate a new caret to count the number of allocations + SerdCaret* const caret = serd_caret_new(&allocator.base, node, 46, 2); + assert(caret); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations; + for (size_t i = 0u; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_caret_new(&allocator.base, node, 46, 2)); + } + + // Successfully copy the caret to count the number of allocations + allocator.n_allocations = 0; + allocator.n_remaining = SIZE_MAX; + SerdCaret* const copy = serd_caret_copy(&allocator.base, caret); + assert(copy); + + // Test that each allocation failing is handled gracefully + const size_t n_copy_allocs = allocator.n_allocations; + for (size_t i = 0u; i < n_copy_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_caret_copy(&allocator.base, caret)); + } + + serd_caret_free(&allocator.base, copy); + serd_caret_free(&allocator.base, caret); +} + +int +main(void) +{ + test_caret(); + test_failed_alloc(); + return 0; +} -- cgit v1.2.1