aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_env.c46
-rw-r--r--test/test_node.c71
-rw-r--r--test/test_reader_writer.c50
-rw-r--r--test/test_uri.c12
-rw-r--r--test/test_writer.c39
5 files changed, 143 insertions, 75 deletions
diff --git a/test/test_env.c b/test/test_env.c
index d470971e..3753ab35 100644
--- a/test/test_env.c
+++ b/test/test_env.c
@@ -34,13 +34,12 @@ count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri)
static void
test_env(void)
{
- SerdNode* u = serd_new_string(SERD_URI, "http://example.org/foo");
- SerdNode* b = serd_new_string(SERD_CURIE, "invalid");
- SerdNode* e = serd_new_string(SERD_URI, "");
- SerdNode* c = serd_new_string(SERD_CURIE, "eg.2:b");
- SerdNode* s = serd_new_string(SERD_LITERAL, "hello");
- SerdEnv* env = serd_env_new(NULL);
- serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/");
+ SerdNode* u = serd_new_uri(SERD_STRING("http://example.org/foo"));
+ SerdNode* b = serd_new_curie(SERD_STRING("invalid"));
+ SerdNode* e = serd_new_uri(SERD_EMPTY_STRING());
+ SerdNode* c = serd_new_curie(SERD_STRING("eg.2:b"));
+ SerdNode* s = serd_new_string(SERD_STRING("hello"));
+ SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
const SerdNode* prefix_node = NULL;
SerdStringView prefix = SERD_EMPTY_STRING();
@@ -50,7 +49,15 @@ test_env(void)
assert(serd_env_expand(env, NULL, &prefix, &suffix) == SERD_ERR_BAD_CURIE);
- assert(serd_env_set_prefix_from_strings(env, "eg.3", "rel") ==
+ assert(!serd_env_base_uri(env));
+ assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
+ assert(!serd_env_base_uri(env));
+ assert(!serd_env_base_uri(env));
+
+ serd_env_set_prefix(
+ env, SERD_STRING("eg.2"), SERD_STRING("http://example.org/"));
+
+ assert(serd_env_set_prefix(env, SERD_STRING("eg.3"), SERD_STRING("rel")) ==
SERD_ERR_BAD_ARG);
assert(!serd_env_expand_node(NULL, u));
@@ -58,13 +65,13 @@ test_env(void)
assert(!serd_env_expand_node(env, s));
assert(!serd_env_expand_node(env, e));
- assert(!serd_env_set_base_uri(env, NULL));
+ assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
SerdNode* xu = serd_env_expand_node(env, u);
assert(!strcmp(serd_node_string(xu), "http://example.org/foo"));
serd_node_free(xu);
- SerdNode* badpre = serd_new_string(SERD_CURIE, "hm:what");
+ SerdNode* badpre = serd_new_curie(SERD_STRING("hm:what"));
SerdNode* xbadpre = serd_env_expand_node(env, badpre);
assert(!xbadpre);
@@ -72,34 +79,31 @@ test_env(void)
assert(!strcmp(serd_node_string(xc), "http://example.org/b"));
serd_node_free(xc);
- SerdNode* lit = serd_new_string(SERD_LITERAL, "hello");
- assert(serd_env_set_prefix(env, b, lit));
-
- SerdNode* blank = serd_new_string(SERD_BLANK, "b1");
+ SerdNode* blank = serd_new_blank(SERD_STRING("b1"));
assert(!serd_env_expand_node(env, blank));
serd_node_free(blank);
int n_prefixes = 0;
- serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/");
+ serd_env_set_prefix(
+ env, SERD_STRING("eg.2"), SERD_STRING("http://example.org/"));
serd_env_foreach(env, count_prefixes, &n_prefixes);
assert(n_prefixes == 1);
- SerdNode* shorter_uri = serd_new_string(SERD_URI, "urn:foo");
+ SerdNode* shorter_uri = serd_new_uri(SERD_STRING("urn:foo"));
assert(!serd_env_qualify(env, shorter_uri, &prefix_node, &suffix));
- assert(!serd_env_set_base_uri(env, u));
- assert(serd_node_equals(serd_env_base_uri(env, NULL), u));
+ assert(!serd_env_set_base_uri(env, serd_node_string_view(u)));
+ assert(serd_node_equals(serd_env_base_uri(env), u));
SerdNode* xe = serd_env_expand_node(env, e);
assert(xe);
assert(!strcmp(serd_node_string(xe), "http://example.org/foo"));
serd_node_free(xe);
- assert(!serd_env_set_base_uri(env, NULL));
- assert(!serd_env_base_uri(env, NULL));
+ assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING()));
+ assert(!serd_env_base_uri(env));
serd_node_free(shorter_uri);
- serd_node_free(lit);
serd_node_free(badpre);
serd_node_free(s);
serd_node_free(c);
diff --git a/test/test_node.c b/test/test_node.c
index a16aa889..3df678cc 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -152,19 +152,22 @@ test_blob_to_node(void)
static void
test_node_equals(void)
{
- const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0};
- SerdNode* lhs =
- serd_new_string(SERD_LITERAL, (const char*)replacement_char_str);
- SerdNode* rhs = serd_new_string(SERD_LITERAL, "123");
+ static const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0};
+
+ SerdNode* const lhs =
+ serd_new_string(SERD_SUBSTRING((const char*)replacement_char_str, 3));
+
+ assert(serd_node_equals(lhs, lhs));
+
+ SerdNode* const rhs = serd_new_string(SERD_STRING("123"));
assert(!serd_node_equals(lhs, rhs));
- SerdNode* qnode = serd_new_string(SERD_CURIE, "foo:bar");
+ SerdNode* const qnode = serd_new_curie(SERD_STRING("foo:bar"));
assert(!serd_node_equals(lhs, qnode));
- assert(serd_node_equals(lhs, lhs));
+ serd_node_free(qnode);
assert(!serd_node_copy(NULL));
- serd_node_free(qnode);
serd_node_free(lhs);
serd_node_free(rhs);
}
@@ -172,29 +175,31 @@ test_node_equals(void)
static void
test_node_from_string(void)
{
- SerdNode* hello = serd_new_string(SERD_LITERAL, "hello\"");
- assert(serd_node_length(hello) == 6 &&
- serd_node_flags(hello) == SERD_HAS_QUOTE &&
- !strcmp(serd_node_string(hello), "hello\""));
-
+ SerdNode* const hello = serd_new_string(SERD_STRING("hello\""));
+ assert(serd_node_length(hello) == 6);
+ assert(serd_node_flags(hello) == SERD_HAS_QUOTE);
+ assert(!strncmp(serd_node_string(hello), "hello\"", 6));
serd_node_free(hello);
}
static void
test_node_from_substring(void)
{
- SerdNode* a_b = serd_new_substring(SERD_LITERAL, "a\"bc", 3);
- assert(serd_node_length(a_b) == 3 && serd_node_flags(a_b) == SERD_HAS_QUOTE &&
- !strncmp(serd_node_string(a_b), "a\"b", 3));
-
- serd_node_free(a_b);
- a_b = serd_new_substring(SERD_LITERAL, "a\"bc", 10);
- assert(serd_node_length(a_b) == 4 && serd_node_flags(a_b) == SERD_HAS_QUOTE &&
- !strncmp(serd_node_string(a_b), "a\"bc", 4));
+ SerdNode* const a_b = serd_new_string(SERD_SUBSTRING("a\"bc", 3));
+ assert(serd_node_length(a_b) == 3);
+ assert(serd_node_flags(a_b) == SERD_HAS_QUOTE);
+ assert(strlen(serd_node_string(a_b)) == 3);
+ assert(!strncmp(serd_node_string(a_b), "a\"b", 3));
serd_node_free(a_b);
}
static void
+test_simple_node(void)
+{
+ assert(!serd_new_simple_node(SERD_LITERAL, SERD_STRING("Literal")));
+}
+
+static void
check_copy_equals(const SerdNode* const node)
{
SerdNode* const copy = serd_node_copy(node);
@@ -207,14 +212,18 @@ check_copy_equals(const SerdNode* const node)
static void
test_literal(void)
{
- SerdNode* hello2 = serd_new_literal("hello\"", NULL, NULL);
+ SerdNode* hello2 = serd_new_literal(
+ SERD_STRING("hello\""), SERD_EMPTY_STRING(), SERD_EMPTY_STRING());
+
assert(serd_node_length(hello2) == 6 &&
serd_node_flags(hello2) == SERD_HAS_QUOTE &&
!strcmp(serd_node_string(hello2), "hello\""));
check_copy_equals(hello2);
serd_node_free(hello2);
- SerdNode* hello_l = serd_new_literal("hello_l\"", NULL, "en");
+ SerdNode* hello_l = serd_new_literal(
+ SERD_STRING("hello_l\""), SERD_EMPTY_STRING(), SERD_STRING("en"));
+
assert(serd_node_length(hello_l) == 8);
assert(!strcmp(serd_node_string(hello_l), "hello_l\""));
assert(serd_node_flags(hello_l) == (SERD_HAS_QUOTE | SERD_HAS_LANGUAGE));
@@ -225,8 +234,10 @@ test_literal(void)
check_copy_equals(hello_l);
serd_node_free(hello_l);
- SerdNode* hello_dt =
- serd_new_literal("hello_dt\"", "http://example.org/Thing", NULL);
+ SerdNode* hello_dt = serd_new_literal(SERD_STRING("hello_dt\""),
+ SERD_STRING("http://example.org/Thing"),
+ SERD_EMPTY_STRING());
+
assert(serd_node_length(hello_dt) == 9);
assert(!strcmp(serd_node_string(hello_dt), "hello_dt\""));
assert(serd_node_flags(hello_dt) == (SERD_HAS_QUOTE | SERD_HAS_DATATYPE));
@@ -239,6 +250,16 @@ test_literal(void)
serd_node_free(hello_dt);
}
+static void
+test_blank(void)
+{
+ SerdNode* blank = serd_new_blank(SERD_STRING("b0"));
+ assert(serd_node_length(blank) == 2);
+ assert(serd_node_flags(blank) == 0);
+ assert(!strcmp(serd_node_string(blank), "b0"));
+ serd_node_free(blank);
+}
+
int
main(void)
{
@@ -249,7 +270,9 @@ main(void)
test_node_equals();
test_node_from_string();
test_node_from_substring();
+ test_simple_node();
test_literal();
+ test_blank();
printf("Success\n");
return 0;
diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c
index 258b3c91..3a78889c 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -177,27 +177,28 @@ static void
test_writer(const char* const path)
{
FILE* fd = fopen(path, "wb");
- SerdEnv* env = serd_env_new(NULL);
+ SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
assert(fd);
- SerdWriter* writer =
- serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_file_sink, fd);
+ SerdWriter* writer = serd_writer_new(SERD_TURTLE, 0, env, serd_file_sink, fd);
assert(writer);
serd_writer_chop_blank_prefix(writer, "tmp");
serd_writer_chop_blank_prefix(writer, NULL);
- SerdNode* lit = serd_new_string(SERD_LITERAL, "hello");
+ SerdNode* lit = serd_new_string(SERD_STRING("hello"));
assert(serd_writer_set_base_uri(writer, lit));
assert(serd_writer_set_prefix(writer, lit, lit));
assert(serd_writer_end_anon(writer, NULL));
assert(serd_writer_env(writer) == env);
- uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0};
- SerdNode* s = serd_new_string(SERD_URI, "");
- SerdNode* p = serd_new_string(SERD_URI, "http://example.org/pred");
- SerdNode* o = serd_new_string(SERD_LITERAL, (char*)buf);
+ static const uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0};
+ const SerdStringView buf_view = {(const char*)buf, 3};
+
+ SerdNode* s = serd_new_uri(SERD_EMPTY_STRING());
+ SerdNode* p = serd_new_uri(SERD_STRING("http://example.org/pred"));
+ SerdNode* o = serd_new_string(buf_view);
// Write 3 invalid statements (should write nothing)
const SerdNode* junk[][3] = {{s, o, o}, {o, p, o}, {s, o, p}};
@@ -206,26 +207,33 @@ test_writer(const char* const path)
writer, 0, NULL, junk[i][0], junk[i][1], junk[i][2]));
}
- SerdNode* t = serd_new_literal((char*)buf, "urn:Type", NULL);
- SerdNode* l = serd_new_literal((char*)buf, NULL, "en");
+ static const SerdStringView empty = SERD_EMPTY_STRING();
+ static const SerdStringView urn_Type = SERD_STRING("urn:Type");
+ static const SerdStringView en = SERD_STRING("en");
+
+ SerdNode* const t = serd_new_literal(buf_view, urn_Type, empty);
+ SerdNode* const l = serd_new_literal(buf_view, empty, en);
const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}};
+
for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 3); ++i) {
assert(!serd_writer_write_statement(
writer, 0, NULL, good[i][0], good[i][1], good[i][2]));
}
// Write statements with bad UTF-8 (should be replaced)
- const char bad_str[] = {(char)0xFF, (char)0x90, 'h', 'i', 0};
- SerdNode* bad_lit = serd_new_string(SERD_LITERAL, bad_str);
- SerdNode* bad_uri = serd_new_string(SERD_URI, bad_str);
+ const char bad_str[] = {(char)0xFF, (char)0x90, 'h', 'i', 0};
+ const SerdStringView bad_view = {(const char*)bad_str, 4};
+ SerdNode* bad_lit = serd_new_string(bad_view);
+ SerdNode* bad_uri = serd_new_uri(bad_view);
assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_lit));
assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_uri));
- serd_node_free(bad_lit);
+
serd_node_free(bad_uri);
+ serd_node_free(bad_lit);
// Write 1 valid statement
serd_node_free(o);
- o = serd_new_string(SERD_LITERAL, "hello");
+ o = serd_new_string(SERD_STRING("hello"));
assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o));
serd_writer_free(writer);
@@ -236,13 +244,13 @@ test_writer(const char* const path)
// Test buffer sink
SerdBuffer buffer = {NULL, 0};
- writer =
- serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_buffer_sink, &buffer);
+ writer = serd_writer_new(SERD_TURTLE, 0, env, serd_buffer_sink, &buffer);
- o = serd_new_string(SERD_URI, "http://example.org/base");
- assert(!serd_writer_set_base_uri(writer, o));
+ SerdNode* const base = serd_new_uri(SERD_STRING("http://example.org/base"));
- serd_node_free(o);
+ serd_writer_set_base_uri(writer, base);
+
+ serd_node_free(base);
serd_writer_free(writer);
char* out = serd_buffer_sink_finish(&buffer);
@@ -265,7 +273,7 @@ test_reader(const char* path)
assert(reader);
assert(serd_reader_handle(reader) == rt);
- SerdNode* g = serd_new_string(SERD_URI, "http://example.org/");
+ SerdNode* g = serd_new_uri(SERD_STRING("http://example.org/"));
serd_reader_set_default_graph(reader, g);
serd_reader_add_blank_prefix(reader, "tmp");
diff --git a/test/test_uri.c b/test/test_uri.c
index 4eb4cc29..5d204a71 100644
--- a/test/test_uri.c
+++ b/test/test_uri.c
@@ -54,7 +54,9 @@ test_file_uri(const char* const hostname,
expected_path = path;
}
- SerdNode* node = serd_new_file_uri(path, hostname, 0);
+ SerdNode* node =
+ serd_new_file_uri(SERD_STRING(path), SERD_OPTIONAL_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);
@@ -160,7 +162,7 @@ check_rel_uri(const char* uri_string,
SerdNode* const rel =
is_within ? serd_new_parsed_uri(serd_relative_uri(uri, base_uri))
- : serd_new_uri(uri_string);
+ : serd_new_uri(SERD_STRING(uri_string));
const int ret = strcmp(serd_node_string(rel), expected);
serd_node_free(rel);
@@ -170,8 +172,10 @@ check_rel_uri(const char* uri_string,
static void
test_relative_uri(void)
{
- SerdNode* const root = serd_new_uri("http://example.org/a/b/ignored");
- SerdNode* const base = serd_new_uri("http://example.org/a/b/c/");
+ SerdNode* const root =
+ serd_new_uri(SERD_STRING("http://example.org/a/b/ignored"));
+
+ SerdNode* const base = serd_new_uri(SERD_STRING("http://example.org/a/b/c/"));
check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo");
check_rel_uri("http://example.org/a/", base, NULL, "../../");
diff --git a/test/test_writer.c b/test/test_writer.c
index 531e229d..756535ad 100644
--- a/test/test_writer.c
+++ b/test/test_writer.c
@@ -22,18 +22,46 @@
#include <string.h>
static void
+test_write_bad_prefix(void)
+{
+ SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
+ SerdBuffer buffer = {NULL, 0};
+ SerdWriter* writer =
+ serd_writer_new(SERD_TURTLE, 0u, env, serd_buffer_sink, &buffer);
+
+ assert(writer);
+
+ SerdNode* name = serd_new_string(SERD_STRING("eg"));
+ SerdNode* uri = serd_new_uri(SERD_STRING("rel"));
+
+ assert(serd_writer_set_prefix(writer, name, uri) == SERD_ERR_BAD_ARG);
+
+ char* const out = serd_buffer_sink_finish(&buffer);
+
+ assert(!strcmp(out, ""));
+ serd_free(out);
+
+ serd_node_free(uri);
+ serd_node_free(name);
+ serd_writer_free(writer);
+ serd_env_free(env);
+}
+
+static void
test_write_long_literal(void)
{
- SerdEnv* env = serd_env_new(NULL);
+ SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
SerdBuffer buffer = {NULL, 0};
SerdWriter* writer =
- serd_writer_new(SERD_TURTLE, 0u, env, NULL, serd_buffer_sink, &buffer);
+ serd_writer_new(SERD_TURTLE, 0u, env, serd_buffer_sink, &buffer);
assert(writer);
- SerdNode* s = serd_new_string(SERD_URI, "http://example.org/s");
- SerdNode* p = serd_new_string(SERD_URI, "http://example.org/p");
- SerdNode* o = serd_new_string(SERD_LITERAL, "hello \"\"\"world\"\"\"!");
+ SerdNode* s = serd_new_uri(SERD_STRING("http://example.org/s"));
+ SerdNode* p = serd_new_uri(SERD_STRING("http://example.org/p"));
+ SerdNode* o = serd_new_literal(SERD_STRING("hello \"\"\"world\"\"\"!"),
+ SERD_EMPTY_STRING(),
+ SERD_EMPTY_STRING());
assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o));
@@ -56,6 +84,7 @@ test_write_long_literal(void)
int
main(void)
{
+ test_write_bad_prefix();
test_write_long_literal();
return 0;