aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_statement.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_statement.c
parent56cceb103dc633d6af957472945e792187a5dd4e (diff)
downloadserd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.gz
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.bz2
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.zip
Add custom allocator support
Diffstat (limited to 'test/test_statement.c')
-rw-r--r--test/test_statement.c164
1 files changed, 142 insertions, 22 deletions
diff --git a/test/test_statement.c b/test/test_statement.c
index 45605dd1..b3f2c483 100644
--- a/test/test_statement.c
+++ b/test/test_statement.c
@@ -16,19 +16,78 @@
#undef NDEBUG
+#include "failing_allocator.h"
+
#include "serd/serd.h"
#include <assert.h>
#include <stddef.h>
+#include <stdint.h>
#define NS_EG "http://example.org/"
static void
+test_invalid_new(void)
+{
+ SerdAllocator* const allocator = serd_default_allocator();
+
+ assert(!serd_statement_copy(allocator, NULL));
+
+ SerdNodes* const nodes = serd_nodes_new(allocator);
+
+ const SerdNode* const s = serd_nodes_string(nodes, SERD_STRING("s"));
+ const SerdNode* const u = serd_nodes_uri(nodes, SERD_STRING(NS_EG "u"));
+ const SerdNode* const b = serd_nodes_blank(nodes, SERD_STRING(NS_EG "b"));
+
+ // S, P, and G may not be strings (must be resources)
+ assert(!serd_statement_new(allocator, s, u, u, u, NULL));
+ assert(!serd_statement_new(allocator, u, s, u, u, NULL));
+ assert(!serd_statement_new(allocator, u, u, u, s, NULL));
+
+ // P may not be a blank node
+ assert(!serd_statement_new(allocator, u, b, u, u, NULL));
+
+ serd_nodes_free(nodes);
+}
+
+static void
test_copy(void)
{
- assert(!serd_statement_copy(NULL));
+ assert(!serd_statement_copy(NULL, NULL));
+
+ SerdAllocator* const allocator = serd_default_allocator();
+
+ assert(!serd_statement_copy(allocator, NULL));
+
+ SerdNodes* const nodes = serd_nodes_new(allocator);
+
+ const SerdNode* const s = serd_nodes_uri(nodes, SERD_STRING(NS_EG "s"));
+ const SerdNode* const p = serd_nodes_uri(nodes, SERD_STRING(NS_EG "p"));
+ const SerdNode* const o = serd_nodes_uri(nodes, SERD_STRING(NS_EG "o"));
+ const SerdNode* const g = serd_nodes_uri(nodes, SERD_STRING(NS_EG "g"));
+
+ SerdStatement* const statement =
+ serd_statement_new(allocator, s, p, o, g, NULL);
+
+ SerdStatement* const copy = serd_statement_copy(allocator, statement);
+
+ assert(serd_statement_equals(copy, statement));
+
+ serd_statement_free(allocator, copy);
+ serd_statement_free(allocator, statement);
+ serd_nodes_free(nodes);
+}
+
+static void
+test_copy_with_caret(void)
+{
+ assert(!serd_statement_copy(NULL, NULL));
- SerdNodes* const nodes = serd_nodes_new();
+ SerdAllocator* const allocator = serd_default_allocator();
+
+ assert(!serd_statement_copy(allocator, NULL));
+
+ SerdNodes* const nodes = serd_nodes_new(allocator);
const SerdNode* const f = serd_nodes_string(nodes, SERD_STRING("file"));
const SerdNode* const s = serd_nodes_uri(nodes, SERD_STRING(NS_EG "s"));
@@ -36,29 +95,35 @@ test_copy(void)
const SerdNode* const o = serd_nodes_uri(nodes, SERD_STRING(NS_EG "o"));
const SerdNode* const g = serd_nodes_uri(nodes, SERD_STRING(NS_EG "g"));
- SerdCaret* const caret = serd_caret_new(f, 1, 1);
- SerdStatement* const statement = serd_statement_new(s, p, o, g, caret);
- SerdStatement* const copy = serd_statement_copy(statement);
+ SerdCaret* const caret = serd_caret_new(allocator, f, 1, 1);
+
+ SerdStatement* const statement =
+ serd_statement_new(allocator, s, p, o, g, caret);
+
+ SerdStatement* const copy = serd_statement_copy(allocator, statement);
assert(serd_statement_equals(copy, statement));
assert(serd_caret_equals(serd_statement_caret(copy), caret));
- serd_statement_free(copy);
- serd_caret_free(caret);
- serd_statement_free(statement);
+ serd_statement_free(allocator, copy);
+ serd_caret_free(allocator, caret);
+ serd_statement_free(allocator, statement);
serd_nodes_free(nodes);
}
static void
test_free(void)
{
- serd_statement_free(NULL);
+ serd_statement_free(serd_default_allocator(), NULL);
+ serd_statement_free(NULL, NULL);
}
static void
test_fields(void)
{
- SerdNodes* const nodes = serd_nodes_new();
+ SerdAllocator* const allocator = serd_default_allocator();
+
+ SerdNodes* const nodes = serd_nodes_new(allocator);
const SerdNode* const f = serd_nodes_string(nodes, SERD_STRING("file"));
const SerdNode* const s = serd_nodes_uri(nodes, SERD_STRING(NS_EG "s"));
@@ -66,8 +131,10 @@ test_fields(void)
const SerdNode* const o = serd_nodes_uri(nodes, SERD_STRING(NS_EG "o"));
const SerdNode* const g = serd_nodes_uri(nodes, SERD_STRING(NS_EG "g"));
- SerdCaret* const caret = serd_caret_new(f, 1, 1);
- SerdStatement* const statement = serd_statement_new(s, p, o, g, caret);
+ SerdCaret* const caret = serd_caret_new(allocator, f, 1, 1);
+
+ SerdStatement* const statement =
+ serd_statement_new(allocator, s, p, o, g, caret);
assert(serd_statement_equals(statement, statement));
assert(!serd_statement_equals(statement, NULL));
@@ -94,33 +161,86 @@ test_fields(void)
assert(!serd_statement_matches(statement, NULL, NULL, s, NULL));
assert(!serd_statement_matches(statement, NULL, NULL, NULL, s));
- SerdStatement* const diff_s = serd_statement_new(o, p, o, g, caret);
+ SerdStatement* const diff_s =
+ serd_statement_new(allocator, o, p, o, g, caret);
assert(!serd_statement_equals(statement, diff_s));
- serd_statement_free(diff_s);
+ serd_statement_free(allocator, diff_s);
- SerdStatement* const diff_p = serd_statement_new(s, o, o, g, caret);
+ SerdStatement* const diff_p =
+ serd_statement_new(allocator, s, o, o, g, caret);
assert(!serd_statement_equals(statement, diff_p));
- serd_statement_free(diff_p);
+ serd_statement_free(allocator, diff_p);
- SerdStatement* const diff_o = serd_statement_new(s, p, s, g, caret);
+ SerdStatement* const diff_o =
+ serd_statement_new(allocator, s, p, s, g, caret);
assert(!serd_statement_equals(statement, diff_o));
- serd_statement_free(diff_o);
+ serd_statement_free(allocator, diff_o);
- SerdStatement* const diff_g = serd_statement_new(s, p, o, s, caret);
+ SerdStatement* const diff_g =
+ serd_statement_new(allocator, s, p, o, s, caret);
assert(!serd_statement_equals(statement, diff_g));
- serd_statement_free(diff_g);
+ serd_statement_free(allocator, diff_g);
+
+ serd_statement_free(allocator, statement);
+ serd_caret_free(allocator, caret);
+ serd_nodes_free(nodes);
+}
+
+static void
+test_failed_alloc(void)
+{
+ SerdNodes* const nodes = serd_nodes_new(serd_default_allocator());
+
+ const SerdNode* const f = serd_nodes_string(nodes, SERD_STRING("file"));
+ const SerdNode* const s = serd_nodes_uri(nodes, SERD_STRING(NS_EG "s"));
+ const SerdNode* const p = serd_nodes_uri(nodes, SERD_STRING(NS_EG "p"));
+ const SerdNode* const o = serd_nodes_uri(nodes, SERD_STRING(NS_EG "o"));
+ const SerdNode* const g = serd_nodes_uri(nodes, SERD_STRING(NS_EG "g"));
- serd_statement_free(statement);
- serd_caret_free(caret);
+ SerdCaret* const caret = serd_caret_new(serd_default_allocator(), f, 1, 1);
+
+ SerdFailingAllocator allocator = serd_failing_allocator();
+
+ // Successfully allocate a statement to count the number of allocations
+ SerdStatement* const statement =
+ serd_statement_new(&allocator.base, s, p, o, g, caret);
+ assert(statement);
+
+ // 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_statement_new(&allocator.base, s, p, o, g, caret));
+ }
+
+ // Successfully copy the statement to count the number of allocations
+ allocator.n_allocations = 0;
+ allocator.n_remaining = SIZE_MAX;
+ SerdStatement* const copy = serd_statement_copy(&allocator.base, statement);
+ 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_statement_copy(&allocator.base, statement));
+ }
+
+ serd_statement_free(&allocator.base, copy);
+ serd_statement_free(&allocator.base, statement);
+ serd_caret_free(&allocator.base, caret);
serd_nodes_free(nodes);
}
int
main(void)
{
+ test_invalid_new();
test_copy();
+ test_copy_with_caret();
test_free();
test_fields();
+ test_failed_alloc();
return 0;
}