aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-12-02 17:11:23 -0500
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commitb13ad41a4d65b577b4db67660a9edf3056bdf7af (patch)
treee89535f6098a1b860f38ec8a7a1b698b30a72545 /test
parent0db18e483f11ac2f9518d96e137d217040ed1339 (diff)
downloadserd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.tar.gz
serd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.tar.bz2
serd-b13ad41a4d65b577b4db67660a9edf3056bdf7af.zip
Use SerdNodes instead of manual memory management in tests
Diffstat (limited to 'test')
-rw-r--r--test/meson.build8
-rw-r--r--test/test_env.c61
-rw-r--r--test/test_node.c43
-rw-r--r--test/test_overflow.c11
-rw-r--r--test/test_reader.c11
-rw-r--r--test/test_uri.c37
-rw-r--r--test/test_writer.c33
7 files changed, 133 insertions, 71 deletions
diff --git a/test/meson.build b/test/meson.build
index 6bb0e033..e86f34e9 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -278,6 +278,14 @@ if is_variable('serd_pipe')
# Base URI options
test(
+ 'good_rebase',
+ serd_pipe,
+ args: ['-B', 'rebase', '-o', 'serd_rebased.ttl', serd_ttl],
+ env: test_env,
+ suite: cmd_suite,
+ )
+
+ test(
'bad_rebase',
serd_pipe,
args: ['-B', 'rebase', serd_ttl],
diff --git a/test/test_env.c b/test/test_env.c
index cc3a524f..5a4c95df 100644
--- a/test/test_env.c
+++ b/test/test_env.c
@@ -8,6 +8,7 @@
#include "serd/env.h"
#include "serd/event.h"
#include "serd/node.h"
+#include "serd/nodes.h"
#include "serd/sink.h"
#include "serd/status.h"
#include "serd/string_view.h"
@@ -201,8 +202,9 @@ count_prefixes(void* handle, const SerdEvent* event)
static void
test_base_uri(void)
{
- SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
- SerdNode* const eg = serd_node_new(NULL, serd_a_uri_string(NS_EG));
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
+ const SerdNode* const eg = serd_nodes_get(nodes, serd_a_uri_string(NS_EG));
// Test that invalid calls work as expected
assert(!serd_env_base_uri(env));
@@ -221,8 +223,8 @@ test_base_uri(void)
assert(!serd_env_set_base_uri(env, serd_empty_string()));
assert(!serd_env_base_uri(env));
- serd_node_free(NULL, eg);
serd_env_free(env);
+ serd_nodes_free(nodes);
}
static void
@@ -262,13 +264,14 @@ test_set_prefix(void)
static void
test_expand_untyped_literal(void)
{
- SerdNode* const untyped = serd_node_new(NULL, serd_a_string("data"));
- SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ const SerdNode* const untyped = serd_nodes_get(nodes, serd_a_string("data"));
+ SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
assert(!serd_env_expand_node(env, untyped));
serd_env_free(env);
- serd_node_free(NULL, untyped);
+ serd_nodes_free(nodes);
}
static void
@@ -276,15 +279,17 @@ test_expand_bad_uri_datatype(void)
{
const SerdStringView type = serd_string("Type");
- SerdNode* const typed =
- serd_node_new(NULL, serd_a_typed_literal(serd_string("data"), type));
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+
+ const SerdNode* const typed =
+ serd_nodes_get(nodes, serd_a_typed_literal(serd_string("data"), type));
SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
assert(!serd_env_expand_node(env, typed));
serd_env_free(env);
- serd_node_free(NULL, typed);
+ serd_nodes_free(nodes);
}
static void
@@ -292,20 +297,21 @@ test_expand_uri(void)
{
const SerdStringView base = serd_string("http://example.org/b/");
- SerdEnv* const env = serd_env_new(NULL, base);
- SerdNode* const rel = serd_node_new(NULL, serd_a_uri_string("rel"));
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ SerdEnv* const env = serd_env_new(NULL, base);
+ const SerdNode* const rel = serd_nodes_get(nodes, serd_a_uri_string("rel"));
+ const SerdNode* const empty = serd_nodes_get(nodes, serd_a_uri_string(""));
+
SerdNode* const rel_out = serd_env_expand_node(env, rel);
- SerdNode* const empty = serd_node_new(NULL, serd_a_uri_string(""));
SerdNode* const empty_out = serd_env_expand_node(env, empty);
assert(!strcmp(serd_node_string(rel_out), "http://example.org/b/rel"));
assert(!strcmp(serd_node_string(empty_out), "http://example.org/b/"));
serd_node_free(NULL, empty_out);
- serd_node_free(NULL, empty);
serd_node_free(NULL, rel_out);
- serd_node_free(NULL, rel);
serd_env_free(env);
+ serd_nodes_free(nodes);
}
static void
@@ -313,27 +319,31 @@ test_expand_empty_uri_ref(void)
{
const SerdStringView base = serd_string("http://example.org/b/");
- SerdNode* const rel = serd_node_new(NULL, serd_a_uri_string("rel"));
- SerdEnv* const env = serd_env_new(NULL, base);
- SerdNode* const rel_out = serd_env_expand_node(env, rel);
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ SerdEnv* const env = serd_env_new(NULL, base);
+ const SerdNode* const rel = serd_nodes_get(nodes, serd_a_uri_string("rel"));
+ SerdNode* const rel_out = serd_env_expand_node(env, rel);
assert(!strcmp(serd_node_string(rel_out), "http://example.org/b/rel"));
serd_node_free(NULL, rel_out);
serd_env_free(env);
- serd_node_free(NULL, rel);
+ serd_nodes_free(nodes);
}
static void
test_expand_bad_uri(void)
{
- SerdNode* const bad_uri = serd_node_new(NULL, serd_a_uri_string("rel"));
- SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
+
+ const SerdNode* const bad_uri =
+ serd_nodes_get(nodes, serd_a_uri_string("rel"));
assert(!serd_env_expand_node(env, bad_uri));
serd_env_free(env);
- serd_node_free(NULL, bad_uri);
+ serd_nodes_free(nodes);
}
static void
@@ -372,13 +382,16 @@ test_expand_bad_curie(void)
static void
test_expand_blank(void)
{
- SerdNode* const blank = serd_node_new(NULL, serd_a_blank_string("b1"));
- SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ const SerdNode* const blank =
+ serd_nodes_get(nodes, serd_a_blank(serd_string("b1")));
+
+ SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
assert(!serd_env_expand_node(env, blank));
serd_env_free(env);
- serd_node_free(NULL, blank);
+ serd_nodes_free(nodes);
}
static void
diff --git a/test/test_node.c b/test/test_node.c
index c1b83140..7ed6e3c5 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -158,6 +158,7 @@ test_boolean(void)
{
SerdNode* const true_node =
serd_node_new(NULL, serd_a_primitive(serd_bool(true)));
+
assert(true_node);
assert(!strcmp(serd_node_string(true_node), "true"));
assert(serd_node_value(true_node).data.as_bool);
@@ -170,6 +171,7 @@ test_boolean(void)
{
SerdNode* const false_node =
serd_node_new(NULL, serd_a_primitive(serd_bool(false)));
+
assert(false_node);
assert(!strcmp(serd_node_string(false_node), "false"));
assert(!serd_node_value(false_node).data.as_bool);
@@ -264,8 +266,9 @@ test_double(void)
"0.0E0", "-0.0E0", "1.2E0", "-2.3E0", "4.56789E6"};
for (size_t i = 0; i < sizeof(test_values) / sizeof(double); ++i) {
- SerdNode* node =
+ SerdNode* const node =
serd_node_new(NULL, serd_a_primitive(serd_double(test_values[i])));
+
const char* node_str = serd_node_string(node);
assert(!strcmp(node_str, test_strings[i]));
@@ -336,8 +339,9 @@ test_float(void)
"0.0E0", "-0.0E0", "1.5E0", "-2.5E0", "4.56789E6"};
for (size_t i = 0; i < sizeof(test_values) / sizeof(float); ++i) {
- SerdNode* node =
+ SerdNode* const node =
serd_node_new(NULL, serd_a_primitive(serd_float(test_values[i])));
+
const char* node_str = serd_node_string(node);
assert(!strcmp(node_str, test_strings[i]));
@@ -635,6 +639,7 @@ static void
test_node_from_syntax(void)
{
SerdNode* const hello = serd_node_new(NULL, serd_a_string("hello\""));
+
assert(serd_node_length(hello) == 6);
assert(!serd_node_flags(hello));
assert(!strncmp(serd_node_string(hello), "hello\"", 6));
@@ -656,6 +661,7 @@ test_node_from_substring(void)
{
SerdNode* const a_b =
serd_node_new(NULL, serd_a_string_view(serd_substring("a\"bc", 3)));
+
assert(serd_node_length(a_b) == 3);
assert(!serd_node_flags(a_b));
assert(strlen(serd_node_string(a_b)) == 3);
@@ -674,16 +680,45 @@ check_copy_equals(const SerdNode* const node)
}
static void
+test_uri(void)
+{
+ const SerdStringView base = serd_string("http://example.org/base/");
+ const SerdStringView rel = serd_string("a/b");
+ const SerdStringView abs = serd_string("http://example.org/base/a/b");
+
+ const SerdURIView base_uri = serd_parse_uri(base.data);
+ const SerdURIView rel_uri = serd_parse_uri(rel.data);
+ const SerdURIView abs_uri = serd_resolve_uri(rel_uri, base_uri);
+
+ SerdNode* const from_string = serd_node_new(NULL, serd_a_uri(abs));
+
+ SerdNode* const from_uri = serd_node_new(NULL, serd_a_parsed_uri(abs_uri));
+
+ assert(from_string);
+ assert(from_uri);
+ assert(!strcmp(serd_node_string(from_string), serd_node_string(from_uri)));
+
+ serd_node_free(NULL, from_uri);
+ serd_node_free(NULL, from_string);
+}
+
+static void
test_literal(void)
{
const SerdStringView hello_str = serd_string("hello");
const SerdStringView empty_str = serd_empty_string();
+ assert(!serd_node_new(NULL,
+ serd_a_literal(hello_str,
+ SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE,
+ serd_string("whatever"))));
+
assert(!serd_node_new(NULL, serd_a_typed_literal(hello_str, empty_str)));
assert(!serd_node_new(NULL, serd_a_plain_literal(hello_str, empty_str)));
assert(
!serd_node_new(NULL, serd_a_typed_literal(hello_str, serd_string("Type"))));
+
assert(
!serd_node_new(NULL, serd_a_typed_literal(hello_str, serd_string("de"))));
@@ -756,12 +791,11 @@ test_compare(void)
SerdNode* angst_de = serd_node_new(
NULL, serd_a_plain_literal(serd_string("angst"), serd_string("de")));
- assert(angst_de);
SerdNode* angst_en = serd_node_new(
NULL, serd_a_plain_literal(serd_string("angst"), serd_string("en")));
SerdNode* hallo = serd_node_new(
- NULL, serd_a_typed_literal(serd_string("Hallo"), serd_string("de")));
+ NULL, serd_a_plain_literal(serd_string("Hallo"), serd_string("de")));
SerdNode* hello = serd_node_new(NULL, serd_a_string("Hello"));
SerdNode* universe = serd_node_new(NULL, serd_a_string("Universe"));
@@ -828,6 +862,7 @@ main(void)
test_node_equals();
test_node_from_syntax();
test_node_from_substring();
+ test_uri();
test_literal();
test_blank();
test_compare();
diff --git a/test/test_overflow.c b/test/test_overflow.c
index edabd4eb..abc7d989 100644
--- a/test/test_overflow.c
+++ b/test/test_overflow.c
@@ -22,14 +22,16 @@ test_size(SerdWorld* const world,
limits.reader_stack_size = stack_size;
serd_world_set_limits(world, limits);
- SerdSink* sink = serd_sink_new(NULL, NULL, NULL, NULL);
- SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
- SerdReader* const reader = serd_reader_new(world, syntax, flags, env, sink);
+ SerdNodes* const nodes = serd_world_nodes(world);
+ SerdAllocator* const alloc = serd_world_allocator(world);
+ SerdSink* sink = serd_sink_new(alloc, NULL, NULL, NULL);
+ SerdEnv* const env = serd_env_new(alloc, serd_empty_string());
+ SerdReader* const reader = serd_reader_new(world, syntax, flags, env, sink);
if (!reader) {
return SERD_BAD_STACK;
}
- SerdNode* string_name = serd_node_new(NULL, serd_a_string("string"));
+ const SerdNode* string_name = serd_nodes_get(nodes, serd_a_string("string"));
const char* position = str;
SerdInputStream in = serd_open_input_string(&position);
serd_reader_start(reader, &in, string_name, 1);
@@ -37,7 +39,6 @@ test_size(SerdWorld* const world,
const SerdStatus st = serd_reader_read_document(reader);
serd_close_input(&in);
- serd_node_free(NULL, string_name);
serd_reader_free(reader);
serd_env_free(env);
serd_sink_free(sink);
diff --git a/test/test_reader.c b/test/test_reader.c
index 54dbca9f..3d59af8e 100644
--- a/test/test_reader.c
+++ b/test/test_reader.c
@@ -10,6 +10,7 @@
#include "serd/event.h"
#include "serd/input_stream.h"
#include "serd/node.h"
+#include "serd/nodes.h"
#include "serd/reader.h"
#include "serd/sink.h"
#include "serd/statement.h"
@@ -613,6 +614,7 @@ static void
test_error_cursor(void)
{
SerdWorld* const world = serd_world_new(NULL);
+ SerdNodes* const nodes = serd_world_nodes(world);
bool called = false;
SerdSink* const sink = serd_sink_new(NULL, &called, check_cursor, NULL);
SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
@@ -624,9 +626,11 @@ test_error_cursor(void)
"<http://example.org/s> <http://example.org/p> "
"<http://example.org/o> .";
- SerdNode* const string_name = serd_node_new(NULL, serd_a_string("string"));
- const char* position = string;
- SerdInputStream in = serd_open_input_string(&position);
+ const SerdNode* const string_name =
+ serd_nodes_get(nodes, serd_a_string("string"));
+
+ const char* position = string;
+ SerdInputStream in = serd_open_input_string(&position);
SerdStatus st = serd_reader_start(reader, &in, string_name, 1);
assert(!st);
@@ -635,7 +639,6 @@ test_error_cursor(void)
assert(called);
assert(!serd_close_input(&in));
- serd_node_free(NULL, string_name);
serd_reader_free(reader);
serd_env_free(env);
serd_sink_free(sink);
diff --git a/test/test_uri.c b/test/test_uri.c
index 294b00ec..b1af8a02 100644
--- a/test/test_uri.c
+++ b/test/test_uri.c
@@ -169,13 +169,15 @@ test_parse_uri(void)
const SerdURIView base_uri = serd_parse_uri(base.data);
const SerdURIView empty_uri = serd_parse_uri("");
- SerdNode* const nil = serd_node_new(
- NULL, serd_a_parsed_uri(serd_resolve_uri(empty_uri, base_uri)));
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+
+ const SerdNode* const nil = serd_nodes_get(
+ nodes, serd_a_parsed_uri(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(NULL, nil);
+ serd_nodes_free(nodes);
}
static void
@@ -230,27 +232,27 @@ check_relative_uri(const char* const uri_string,
assert(base_string);
assert(expected_string);
- SerdNode* const uri_node = serd_node_new(NULL, serd_a_uri_string(uri_string));
- const SerdURIView uri = serd_node_uri_view(uri_node);
- SerdNode* const base_node =
- serd_node_new(NULL, serd_a_uri_string(base_string));
-
+ SerdNodes* const nodes = serd_nodes_new(NULL);
+ const SerdNode* uri_node =
+ serd_nodes_get(nodes, serd_a_uri_string(uri_string));
+ const SerdURIView uri = serd_node_uri_view(uri_node);
+ const SerdNode* base_node =
+ serd_nodes_get(nodes, serd_a_uri_string(base_string));
const SerdURIView base = serd_node_uri_view(base_node);
- SerdNode* result_node = NULL;
+ const SerdNode* result_node = NULL;
if (!root_string) {
result_node =
- serd_node_new(NULL, serd_a_parsed_uri(serd_relative_uri(uri, base)));
+ serd_nodes_get(nodes, serd_a_parsed_uri(serd_relative_uri(uri, base)));
} else {
- SerdNode* const root_node =
- serd_node_new(NULL, serd_a_uri_string(root_string));
+ const SerdNode* root_node =
+ serd_nodes_get(nodes, serd_a_uri_string(root_string));
const SerdURIView root = serd_node_uri_view(root_node);
result_node =
serd_uri_is_within(uri, root)
- ? serd_node_new(NULL, serd_a_parsed_uri(serd_relative_uri(uri, base)))
- : serd_node_new(NULL, serd_a_uri_string(uri_string));
- serd_node_free(NULL, root_node);
+ ? serd_nodes_get(nodes, serd_a_parsed_uri(serd_relative_uri(uri, base)))
+ : serd_nodes_get(nodes, serd_a_uri_string(uri_string));
}
assert(!strcmp(serd_node_string(result_node), expected_string));
@@ -263,10 +265,7 @@ check_relative_uri(const char* const uri_string,
assert(chunk_equals(&result.path, &expected.path));
assert(chunk_equals(&result.query, &expected.query));
assert(chunk_equals(&result.fragment, &expected.fragment));
-
- serd_node_free(NULL, result_node);
- serd_node_free(NULL, base_node);
- serd_node_free(NULL, uri_node);
+ serd_nodes_free(nodes);
}
static void
diff --git a/test/test_writer.c b/test/test_writer.c
index a80783f6..60f50308 100644
--- a/test/test_writer.c
+++ b/test/test_writer.c
@@ -237,7 +237,7 @@ test_writer_cleanup(void)
const SerdNode* const p =
serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p"));
- const SerdNode* o = serd_nodes_get(nodes, serd_a_string("start"));
+ const SerdNode* o = serd_nodes_get(nodes, serd_a_blank(serd_string("start")));
st = serd_sink_write(sink, SERD_ANON_O, s, p, o, NULL);
assert(!st);
@@ -257,9 +257,8 @@ test_writer_cleanup(void)
assert(!(st = serd_writer_finish(writer)));
// Set the base to an empty URI
- const SerdNode* const empty_uri =
- serd_nodes_get(nodes, serd_a_uri_string(""));
- assert(!(st = serd_sink_write_base(sink, empty_uri)));
+ assert(!(st = serd_sink_write_base(
+ sink, serd_nodes_get(nodes, serd_a_uri_string("")))));
// Free (which could leak if the writer doesn't clean up the stack properly)
serd_writer_free(writer);
@@ -275,6 +274,7 @@ test_strict_write(void)
assert(fd);
SerdWorld* world = serd_world_new(NULL);
+ SerdNodes* nodes = serd_world_nodes(world);
SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
SerdOutputStream out = serd_open_output_stream(null_sink, NULL, NULL, fd);
SerdWriter* const writer =
@@ -286,21 +286,23 @@ test_strict_write(void)
const uint8_t bad_str[] = {0xFF, 0x90, 'h', 'i', 0};
- SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s"));
- SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p"));
+ const SerdNode* const s =
+ serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s"));
+
+ const SerdNode* const p =
+ serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p"));
+
+ const SerdNode* const bad_lit =
+ serd_nodes_get(nodes, serd_a_string((const char*)bad_str));
- SerdNode* bad_lit = serd_node_new(NULL, serd_a_string((const char*)bad_str));
- SerdNode* bad_uri =
- serd_node_new(NULL, serd_a_uri_string((const char*)bad_str));
+ const SerdNode* const bad_uri =
+ serd_nodes_get(nodes, serd_a_uri_string((const char*)bad_str));
assert(serd_sink_write(sink, 0, s, p, bad_lit, NULL) == SERD_BAD_TEXT);
assert(serd_sink_write(sink, 0, s, p, bad_uri, NULL) == SERD_BAD_TEXT);
- serd_node_free(NULL, bad_uri);
- serd_node_free(NULL, bad_lit);
- serd_node_free(NULL, p);
- serd_node_free(NULL, s);
serd_writer_free(writer);
+ serd_close_output(&out);
serd_env_free(env);
serd_world_free(world);
fclose(fd);
@@ -325,11 +327,13 @@ static void
test_write_error(void)
{
SerdWorld* const world = serd_world_new(NULL);
+ SerdNodes* const nodes = serd_world_nodes(world);
SerdEnv* const env = serd_env_new(NULL, serd_empty_string());
SerdOutputStream out = serd_open_output_stream(error_sink, NULL, NULL, NULL);
SerdStatus st = SERD_SUCCESS;
- SerdNode* u = serd_node_new(NULL, serd_a_uri_string("http://example.com/u"));
+ const SerdNode* const u =
+ serd_nodes_get(nodes, serd_a_uri_string("http://example.com/u"));
SerdWriter* const writer =
serd_writer_new(world, SERD_TURTLE, (SerdWriterFlags)0, env, &out, 1U);
@@ -341,7 +345,6 @@ test_write_error(void)
assert(st == SERD_BAD_WRITE);
serd_writer_free(writer);
- serd_node_free(NULL, u);
serd_env_free(env);
serd_world_free(world);
}