aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_uri.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_uri.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_uri.c')
-rw-r--r--test/test_uri.c86
1 files changed, 60 insertions, 26 deletions
diff --git a/test/test_uri.c b/test/test_uri.c
index 74b732fd..79d3f73e 100644
--- a/test/test_uri.c
+++ b/test/test_uri.c
@@ -3,6 +3,8 @@
#undef NDEBUG
+#include "failing_allocator.h"
+
#include "serd/memory.h"
#include "serd/node.h"
#include "serd/string_view.h"
@@ -14,6 +16,35 @@
#include <string.h>
static void
+test_file_uri_failed_alloc(void)
+{
+ static const char* const string = "file://host/path/spacey%20dir/100%%.ttl";
+
+ SerdFailingAllocator allocator = serd_failing_allocator();
+
+ // Successfully parse a URI to count the number of allocations
+ char* hostname = NULL;
+ char* path = serd_parse_file_uri(&allocator.base, string, &hostname);
+
+ assert(!strcmp(path, "/path/spacey dir/100%.ttl"));
+ assert(!strcmp(hostname, "host"));
+ serd_free(&allocator.base, path);
+ serd_free(&allocator.base, hostname);
+
+ // 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;
+
+ path = serd_parse_file_uri(&allocator.base, string, &hostname);
+ assert(!path || !hostname);
+
+ serd_free(&allocator.base, path);
+ serd_free(&allocator.base, hostname);
+ }
+}
+
+static void
test_uri_string_has_scheme(void)
{
assert(!serd_uri_string_has_scheme("relative"));
@@ -44,19 +75,21 @@ test_file_uri(const char* const hostname,
expected_path = path;
}
- SerdNode* node = serd_new_file_uri(serd_string(path), serd_string(hostname));
+ SerdNode* node =
+ serd_new_file_uri(NULL, serd_string(path), serd_string(hostname));
const char* node_str = serd_node_string(node);
char* out_hostname = NULL;
- char* out_path = serd_parse_file_uri(node_str, &out_hostname);
+ char* const out_path = serd_parse_file_uri(NULL, node_str, &out_hostname);
+
assert(!strcmp(node_str, expected_uri));
assert((hostname && out_hostname) || (!hostname && !out_hostname));
assert(!hostname || !strcmp(hostname, out_hostname));
assert(!strcmp(out_path, expected_path));
- serd_free(out_path);
- serd_free(out_hostname);
- serd_node_free(node);
+ serd_free(NULL, out_path);
+ serd_free(NULL, out_hostname);
+ serd_node_free(NULL, node);
}
static void
@@ -107,22 +140,22 @@ test_uri_parsing(void)
#endif
// Missing trailing '/' after authority
- assert(!serd_parse_file_uri("file://truncated", NULL));
+ assert(!serd_parse_file_uri(NULL, "file://truncated", NULL));
// Check that NULL hostname doesn't crash
- char* out_path = serd_parse_file_uri("file://me/path", NULL);
+ char* out_path = serd_parse_file_uri(NULL, "file://me/path", NULL);
assert(!strcmp(out_path, "/path"));
- serd_free(out_path);
+ serd_free(NULL, out_path);
// Invalid first escape character
- out_path = serd_parse_file_uri("file:///foo/%0Xbar", NULL);
+ out_path = serd_parse_file_uri(NULL, "file:///foo/%0Xbar", NULL);
assert(!strcmp(out_path, "/foo/bar"));
- serd_free(out_path);
+ serd_free(NULL, out_path);
// Invalid second escape character
- out_path = serd_parse_file_uri("file:///foo/%X0bar", NULL);
+ out_path = serd_parse_file_uri(NULL, "file:///foo/%X0bar", NULL);
assert(!strcmp(out_path, "/foo/bar"));
- serd_free(out_path);
+ serd_free(NULL, out_path);
}
static void
@@ -134,12 +167,12 @@ test_parse_uri(void)
const SerdURIView empty_uri = serd_parse_uri("");
SerdNode* const nil =
- serd_new_parsed_uri(serd_resolve_uri(empty_uri, base_uri));
+ serd_new_parsed_uri(NULL, serd_resolve_uri(empty_uri, base_uri));
assert(serd_node_type(nil) == SERD_URI);
assert(!strcmp(serd_node_string(nil), base.data));
- serd_node_free(nil);
+ serd_node_free(NULL, nil);
}
static void
@@ -194,22 +227,22 @@ check_relative_uri(const char* const uri_string,
assert(base_string);
assert(expected_string);
- SerdNode* const uri_node = serd_new_uri(serd_string(uri_string));
+ SerdNode* const uri_node = serd_new_uri(NULL, serd_string(uri_string));
const SerdURIView uri = serd_node_uri_view(uri_node);
- SerdNode* const base_node = serd_new_uri(serd_string(base_string));
+ SerdNode* const base_node = serd_new_uri(NULL, serd_string(base_string));
const SerdURIView base = serd_node_uri_view(base_node);
SerdNode* result_node = NULL;
if (!root_string) {
- result_node = serd_new_parsed_uri(serd_relative_uri(uri, base));
+ result_node = serd_new_parsed_uri(NULL, serd_relative_uri(uri, base));
} else {
- SerdNode* const root_node = serd_new_uri(serd_string(root_string));
+ SerdNode* const root_node = serd_new_uri(NULL, serd_string(root_string));
const SerdURIView root = serd_node_uri_view(root_node);
result_node = serd_uri_is_within(uri, root)
- ? serd_new_parsed_uri(serd_relative_uri(uri, base))
- : serd_new_uri(serd_string(uri_string));
- serd_node_free(root_node);
+ ? serd_new_parsed_uri(NULL, serd_relative_uri(uri, base))
+ : serd_new_uri(NULL, serd_string(uri_string));
+ serd_node_free(NULL, root_node);
}
assert(!strcmp(serd_node_string(result_node), expected_string));
@@ -223,9 +256,9 @@ check_relative_uri(const char* const uri_string,
assert(chunk_equals(&result.query, &expected.query));
assert(chunk_equals(&result.fragment, &expected.fragment));
- serd_node_free(result_node);
- serd_node_free(base_node);
- serd_node_free(uri_node);
+ serd_node_free(NULL, result_node);
+ serd_node_free(NULL, base_node);
+ serd_node_free(NULL, uri_node);
}
static void
@@ -326,9 +359,9 @@ test_relative_uri(void)
static void
check_uri_string(const SerdURIView uri, const char* const expected)
{
- SerdNode* const node = serd_new_parsed_uri(uri);
+ SerdNode* const node = serd_new_parsed_uri(NULL, uri);
assert(!strcmp(serd_node_string(node), expected));
- serd_node_free(node);
+ serd_node_free(NULL, node);
}
static void
@@ -379,6 +412,7 @@ test_uri_resolution(void)
int
main(void)
{
+ test_file_uri_failed_alloc();
test_uri_string_has_scheme();
test_uri_parsing();
test_parse_uri();