aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_caret.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-10-27 14:15:31 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:24 -0500
commit30487c277ac5d4be5786733ca7b98adb4c810ae9 (patch)
treef1b00a7725d74a594fcd91de2aea924485356528 /test/test_caret.c
parent56cceb103dc633d6af957472945e792187a5dd4e (diff)
downloadserd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.gz
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.bz2
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.zip
Add custom allocator support
Diffstat (limited to 'test/test_caret.c')
-rw-r--r--test/test_caret.c83
1 files changed, 68 insertions, 15 deletions
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 <d@drobilla.net>
+ Copyright 2019-2021 David Robillard <d@drobilla.net>
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 <assert.h>
#include <stddef.h>
+#include <stdint.h>
-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;
+}