aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/meson.build1
-rw-r--r--test/test_reader_writer.c14
-rw-r--r--test/test_sink.c44
-rw-r--r--test/test_statement.c142
4 files changed, 166 insertions, 35 deletions
diff --git a/test/meson.build b/test/meson.build
index c3f29637..3e24fea0 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -126,6 +126,7 @@ unit_tests = [
'overflow',
'reader_writer',
'sink',
+ 'statement',
'string',
'syntax',
'uri',
diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c
index 39bde443..e792f2dc 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -86,18 +86,12 @@ test_prefix_sink(void* handle, const SerdNode* name, const SerdNode* uri)
}
static SerdStatus
-test_statement_sink(void* handle,
- SerdStatementFlags flags,
- const SerdNode* graph,
- const SerdNode* subject,
- const SerdNode* predicate,
- const SerdNode* object)
+test_statement_sink(void* handle,
+ const SerdStatementFlags flags,
+ const SerdStatement* statement)
{
- (void)graph;
(void)flags;
- (void)subject;
- (void)predicate;
- (void)object;
+ (void)statement;
ReaderTest* rt = (ReaderTest*)handle;
++rt->n_statement;
diff --git a/test/test_sink.c b/test/test_sink.c
index 944dea1a..4b9a2eb2 100644
--- a/test/test_sink.c
+++ b/test/test_sink.c
@@ -18,15 +18,12 @@
#define NS_EG "http://example.org/"
typedef struct {
- const SerdNode* last_base;
- const SerdNode* last_name;
- const SerdNode* last_namespace;
- const SerdNode* last_end;
- const SerdNode* last_subject;
- const SerdNode* last_predicate;
- const SerdNode* last_object;
- const SerdNode* last_graph;
- SerdStatus return_status;
+ const SerdNode* last_base;
+ const SerdNode* last_name;
+ const SerdNode* last_namespace;
+ const SerdNode* last_end;
+ const SerdStatement* last_statement;
+ SerdStatus return_status;
} State;
static SerdStatus
@@ -49,21 +46,15 @@ on_prefix(void* handle, const SerdNode* name, const SerdNode* uri)
}
static SerdStatus
-on_statement(void* handle,
- SerdStatementFlags flags,
- const SerdNode* const graph,
- const SerdNode* const subject,
- const SerdNode* const predicate,
- const SerdNode* const object)
+on_statement(void* handle,
+ SerdStatementFlags flags,
+ const SerdStatement* const statement)
{
(void)flags;
State* state = (State*)handle;
- state->last_subject = subject;
- state->last_predicate = predicate;
- state->last_object = object;
- state->last_graph = graph;
+ state->last_statement = statement;
return state->return_status;
}
@@ -85,13 +76,17 @@ test_callbacks(void)
SerdNode* const uri = serd_new_uri(serd_string(NS_EG "uri"));
SerdNode* const blank = serd_new_blank(serd_string("b1"));
SerdEnv* env = serd_env_new(serd_node_string_view(base));
- State state = {0, 0, 0, 0, 0, 0, 0, 0, SERD_SUCCESS};
+ State state = {0, 0, 0, 0, 0, SERD_SUCCESS};
+
+ SerdStatement* const statement =
+ serd_statement_new(base, uri, blank, NULL, NULL);
// Call functions on a sink with no functions set
SerdSink* null_sink = serd_sink_new(&state, NULL);
assert(!serd_sink_write_base(null_sink, base));
assert(!serd_sink_write_prefix(null_sink, name, uri));
+ assert(!serd_sink_write_statement(null_sink, 0, statement));
assert(!serd_sink_write(null_sink, 0, base, uri, blank, NULL));
assert(!serd_sink_write_end(null_sink, blank));
serd_sink_free(null_sink);
@@ -111,16 +106,15 @@ test_callbacks(void)
assert(serd_node_equals(state.last_name, name));
assert(serd_node_equals(state.last_namespace, uri));
- assert(!serd_sink_write(sink, 0, base, uri, blank, NULL));
- assert(serd_node_equals(state.last_subject, base));
- assert(serd_node_equals(state.last_predicate, uri));
- assert(serd_node_equals(state.last_object, blank));
- assert(!state.last_graph);
+ assert(!serd_sink_write_statement(sink, 0, statement));
+ assert(serd_statement_equals(state.last_statement, statement));
assert(!serd_sink_write_end(sink, blank));
assert(serd_node_equals(state.last_end, blank));
serd_sink_free(sink);
+
+ serd_statement_free(statement);
serd_env_free(env);
serd_node_free(blank);
serd_node_free(uri);
diff --git a/test/test_statement.c b/test/test_statement.c
new file mode 100644
index 00000000..844955f8
--- /dev/null
+++ b/test/test_statement.c
@@ -0,0 +1,142 @@
+// Copyright 2011-2020 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#undef NDEBUG
+
+#include "serd/caret.h"
+#include "serd/node.h"
+#include "serd/statement.h"
+#include "serd/string_view.h"
+
+#include <assert.h>
+#include <stddef.h>
+
+#define NS_EG "http://example.org/"
+
+static void
+test_new(void)
+{
+ SerdNode* const u = serd_new_uri(serd_string(NS_EG "s"));
+ SerdNode* const c = serd_new_curie(serd_string("eg:c"));
+ SerdNode* const b = serd_new_blank(serd_string("b0"));
+ SerdNode* const l = serd_new_string(serd_string("str"));
+
+ assert(!serd_statement_new(c, b, u, NULL, NULL));
+ assert(!serd_statement_new(l, c, u, NULL, NULL));
+ assert(!serd_statement_new(l, u, c, u, NULL));
+ assert(!serd_statement_new(u, l, c, NULL, NULL));
+ assert(!serd_statement_new(u, l, u, u, NULL));
+ assert(!serd_statement_new(u, l, u, u, NULL));
+
+ serd_node_free(l);
+ serd_node_free(b);
+ serd_node_free(u);
+ serd_node_free(c);
+}
+
+static void
+test_copy(void)
+{
+ assert(!serd_statement_copy(NULL));
+
+ SerdNode* const f = serd_new_string(serd_string("file"));
+ SerdNode* const s = serd_new_uri(serd_string(NS_EG "s"));
+ SerdNode* const p = serd_new_uri(serd_string(NS_EG "p"));
+ SerdNode* const o = serd_new_uri(serd_string(NS_EG "o"));
+ SerdNode* const g = serd_new_uri(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);
+
+ 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_node_free(g);
+ serd_node_free(o);
+ serd_node_free(p);
+ serd_node_free(s);
+ serd_node_free(f);
+}
+
+static void
+test_free(void)
+{
+ serd_statement_free(NULL);
+}
+
+static void
+test_fields(void)
+{
+ SerdNode* const f = serd_new_string(serd_string("file"));
+ SerdNode* const s = serd_new_uri(serd_string(NS_EG "s"));
+ SerdNode* const p = serd_new_uri(serd_string(NS_EG "p"));
+ SerdNode* const o = serd_new_uri(serd_string(NS_EG "o"));
+ SerdNode* const g = serd_new_uri(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);
+
+ assert(serd_statement_equals(statement, statement));
+ assert(!serd_statement_equals(statement, NULL));
+ assert(!serd_statement_equals(NULL, statement));
+
+ assert(serd_statement_node(statement, SERD_SUBJECT) == s);
+ assert(serd_statement_node(statement, SERD_PREDICATE) == p);
+ assert(serd_statement_node(statement, SERD_OBJECT) == o);
+ assert(serd_statement_node(statement, SERD_GRAPH) == g);
+
+ assert(serd_statement_subject(statement) == s);
+ assert(serd_statement_predicate(statement) == p);
+ assert(serd_statement_object(statement) == o);
+ assert(serd_statement_graph(statement) == g);
+ assert(serd_statement_caret(statement) != caret);
+ assert(serd_caret_equals(serd_statement_caret(statement), caret));
+ assert(serd_statement_matches(statement, s, p, o, g));
+ assert(serd_statement_matches(statement, NULL, p, o, g));
+ assert(serd_statement_matches(statement, s, NULL, o, g));
+ assert(serd_statement_matches(statement, s, p, NULL, g));
+ assert(serd_statement_matches(statement, s, p, o, NULL));
+ assert(!serd_statement_matches(statement, o, NULL, NULL, NULL));
+ assert(!serd_statement_matches(statement, NULL, o, NULL, NULL));
+ 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);
+ assert(!serd_statement_equals(statement, diff_s));
+ serd_statement_free(diff_s);
+
+ SerdStatement* const diff_p = serd_statement_new(s, o, o, g, caret);
+ assert(!serd_statement_equals(statement, diff_p));
+ serd_statement_free(diff_p);
+
+ SerdStatement* const diff_o = serd_statement_new(s, p, s, g, caret);
+ assert(!serd_statement_equals(statement, diff_o));
+ serd_statement_free(diff_o);
+
+ SerdStatement* const diff_g = serd_statement_new(s, p, o, s, caret);
+ assert(!serd_statement_equals(statement, diff_g));
+ serd_statement_free(diff_g);
+
+ serd_statement_free(statement);
+ serd_caret_free(caret);
+ serd_node_free(g);
+ serd_node_free(o);
+ serd_node_free(p);
+ serd_node_free(s);
+ serd_node_free(f);
+}
+
+int
+main(void)
+{
+ test_new();
+ test_copy();
+ test_free();
+ test_fields();
+
+ return 0;
+}