aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_reader_writer.c34
-rw-r--r--test/test_terse_write.c10
-rw-r--r--test/test_writer.c109
3 files changed, 97 insertions, 56 deletions
diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c
index 1c4a415b..aa820fa6 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -9,10 +9,10 @@
#include "serd/input_stream.h"
#include "serd/memory.h"
#include "serd/node.h"
+#include "serd/output_stream.h"
#include "serd/reader.h"
#include "serd/sink.h"
#include "serd/status.h"
-#include "serd/stream.h"
#include "serd/string_view.h"
#include "serd/syntax.h"
#include "serd/world.h"
@@ -109,9 +109,12 @@ test_write_errors(void)
ctx.n_written = 0;
ctx.error_offset = o;
- SerdEnv* const env = serd_env_new(serd_empty_string());
+ SerdEnv* const env = serd_env_new(serd_empty_string());
+ SerdOutputStream out =
+ serd_open_output_stream(faulty_sink, NULL, NULL, &ctx);
+
SerdWriter* const writer =
- serd_writer_new(world, syntax, 0U, env, faulty_sink, &ctx);
+ serd_writer_new(world, syntax, 0U, env, &out, 1U);
const SerdSink* const sink = serd_writer_sink(writer);
SerdReader* const reader = serd_reader_new(world, SERD_TRIG, 0U, sink);
@@ -137,14 +140,14 @@ test_write_errors(void)
static void
test_writer(const char* const path)
{
- FILE* fd = fopen(path, "wb");
- SerdEnv* env = serd_env_new(serd_empty_string());
- assert(fd);
-
SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+
+ SerdOutputStream output = serd_open_output_file(path);
+
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_TURTLE, SERD_WRITE_LAX, env, &output, 1U);
- SerdWriter* writer = serd_writer_new(
- world, SERD_TURTLE, SERD_WRITE_LAX, env, (SerdWriteFunc)fwrite, fd);
assert(writer);
serd_writer_chop_blank_prefix(writer, "tmp");
@@ -205,6 +208,7 @@ test_writer(const char* const path)
serd_node_free(hello);
serd_writer_free(writer);
+ serd_close_output(&output);
serd_node_free(lit);
serd_node_free(o);
@@ -212,19 +216,20 @@ test_writer(const char* const path)
serd_node_free(l);
// Test buffer sink
- SerdBuffer buffer = {NULL, 0};
- writer =
- serd_writer_new(world, SERD_TURTLE, 0, env, serd_buffer_write, &buffer);
+ SerdBuffer buffer = {NULL, 0};
+ SerdNode* const base = serd_new_uri(serd_string("http://example.org/base"));
- SerdNode* const base = serd_new_uri(serd_string("http://example.org/base"));
+ output = serd_open_output_buffer(&buffer);
+ writer = serd_writer_new(world, SERD_TURTLE, 0, env, &output, 1U);
serd_sink_write_base(serd_writer_sink(writer), base);
serd_node_free(base);
serd_writer_free(writer);
- serd_buffer_close(&buffer);
+ serd_close_output(&output);
char* const out = (char*)buffer.buf;
+ assert(out);
assert(!strcmp(out, "@base <http://example.org/base> .\n"));
serd_free(out);
@@ -233,7 +238,6 @@ test_writer(const char* const path)
serd_env_free(env);
serd_world_free(world);
- fclose(fd);
}
static void
diff --git a/test/test_terse_write.c b/test/test_terse_write.c
index 17bacc13..39e3767a 100644
--- a/test/test_terse_write.c
+++ b/test/test_terse_write.c
@@ -6,6 +6,7 @@
#include "serd/buffer.h"
#include "serd/env.h"
#include "serd/node.h"
+#include "serd/output_stream.h"
#include "serd/sink.h"
#include "serd/statement.h"
#include "serd/string_view.h"
@@ -28,6 +29,8 @@ check_output(SerdWriter* writer, SerdBuffer* buffer, const char* expected)
const char* output = (const char*)buffer->buf;
+ fprintf(stderr, "output: %s\n", output);
+ fprintf(stderr, "expected: %s\n", expected);
assert(!strcmp(output, expected));
buffer->len = 0;
@@ -53,8 +56,9 @@ test(void)
serd_env_set_prefix(env, serd_string("rdf"), serd_string(NS_RDF));
- SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0, env, serd_buffer_write, &buffer);
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
+ SerdWriter* const writer =
+ serd_writer_new(world, SERD_TURTLE, 0, env, &output, 1U);
const SerdSink* sink = serd_writer_sink(writer);
@@ -86,7 +90,6 @@ test(void)
serd_sink_write(sink, 0, l2, rdf_rest, rdf_nil, NULL);
check_output(writer, &buffer, "[] rdf:value ( \"s1\" \"s2\" ) .\n");
- serd_buffer_close(&buffer);
serd_writer_free(writer);
serd_node_free(rdf_nil);
serd_node_free(rdf_rest);
@@ -97,6 +100,7 @@ test(void)
serd_node_free(l2);
serd_node_free(l1);
serd_node_free(b1);
+ serd_close_output(&output);
serd_env_free(env);
serd_world_free(world);
free(buffer.buf);
diff --git a/test/test_writer.c b/test/test_writer.c
index fe685437..2a852562 100644
--- a/test/test_writer.c
+++ b/test/test_writer.c
@@ -8,6 +8,7 @@
#include "serd/event.h"
#include "serd/memory.h"
#include "serd/node.h"
+#include "serd/output_stream.h"
#include "serd/sink.h"
#include "serd/statement.h"
#include "serd/status.h"
@@ -23,23 +24,39 @@
#include <string.h>
static void
+test_writer_new(void)
+{
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
+
+ assert(!serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 0U));
+
+ serd_world_free(world);
+ serd_env_free(env);
+}
+
+static void
test_write_bad_event(void)
{
- SerdWorld* world = serd_world_new();
- SerdEnv* env = serd_env_new(serd_empty_string());
- SerdBuffer buffer = {NULL, 0};
- SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_write, &buffer);
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
assert(writer);
const SerdEvent event = {(SerdEventType)42};
assert(serd_sink_write_event(serd_writer_sink(writer), &event) ==
SERD_BAD_ARG);
- serd_buffer_close(&buffer);
+ assert(!serd_close_output(&output));
char* const out = (char*)buffer.buf;
+ assert(out);
assert(!strcmp(out, ""));
serd_free(out);
@@ -51,11 +68,12 @@ test_write_bad_event(void)
static void
test_write_bad_prefix(void)
{
- SerdWorld* world = serd_world_new();
- SerdEnv* env = serd_env_new(serd_empty_string());
- SerdBuffer buffer = {NULL, 0};
- SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_write, &buffer);
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
assert(writer);
@@ -81,12 +99,13 @@ test_write_bad_prefix(void)
static void
test_write_long_literal(void)
{
- SerdWorld* world = serd_world_new();
- SerdEnv* env = serd_env_new(serd_empty_string());
- SerdBuffer buffer = {NULL, 0};
- SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_write, &buffer);
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
assert(writer);
SerdNode* s = serd_new_uri(serd_string("http://example.org/s"));
@@ -99,6 +118,7 @@ test_write_long_literal(void)
serd_node_free(p);
serd_node_free(s);
serd_writer_free(writer);
+ serd_close_output(&output);
serd_env_free(env);
serd_buffer_close(&buffer);
@@ -108,7 +128,7 @@ test_write_long_literal(void)
"<http://example.org/s>\n"
"\t<http://example.org/p> \"\"\"hello \"\"\\\"world\"\"\\\"!\"\"\" .\n";
- assert(!strcmp((char*)out, expected));
+ assert(!strcmp(out, expected));
serd_free(out);
serd_world_free(world);
@@ -129,11 +149,14 @@ null_sink(const void* const buf,
static void
test_writer_cleanup(void)
{
- SerdStatus st = SERD_SUCCESS;
- SerdWorld* world = serd_world_new();
- SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdStatus st = SERD_SUCCESS;
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdOutputStream output =
+ serd_open_output_stream(null_sink, NULL, NULL, NULL);
+
SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, null_sink, NULL);
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
const SerdSink* sink = serd_writer_sink(writer);
@@ -183,8 +206,9 @@ test_strict_write(void)
SerdWorld* world = serd_world_new();
SerdEnv* const env = serd_env_new(serd_empty_string());
+ SerdOutputStream out = serd_open_output_stream(null_sink, NULL, NULL, fd);
SerdWriter* const writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, null_sink, fd);
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &out, 1U);
assert(writer);
@@ -229,15 +253,16 @@ error_sink(const void* const buf,
static void
test_write_error(void)
{
- SerdWorld* const world = serd_world_new();
- SerdEnv* const env = serd_env_new(serd_empty_string());
+ SerdWorld* const world = serd_world_new();
+ SerdEnv* const env = serd_env_new(serd_empty_string());
+ SerdOutputStream out = serd_open_output_stream(error_sink, NULL, NULL, NULL);
SerdWriter* writer = NULL;
SerdStatus st = SERD_SUCCESS;
SerdNode* u = serd_new_uri(serd_string("http://example.com/u"));
- writer = serd_writer_new(
- world, SERD_TURTLE, (SerdWriterFlags)0, env, error_sink, NULL);
+ writer =
+ serd_writer_new(world, SERD_TURTLE, (SerdWriterFlags)0, env, &out, 1U);
assert(writer);
const SerdSink* const sink = serd_writer_sink(writer);
@@ -257,8 +282,11 @@ test_writer_stack_overflow(void)
SerdWorld* world = serd_world_new();
SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdOutputStream output =
+ serd_open_output_stream(null_sink, NULL, NULL, NULL);
+
SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, null_sink, NULL);
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
const SerdSink* sink = serd_writer_sink(writer);
@@ -293,6 +321,7 @@ test_writer_stack_overflow(void)
serd_node_free(p);
serd_node_free(s);
serd_writer_free(writer);
+ serd_close_output(&output);
serd_env_free(env);
serd_world_free(world);
}
@@ -307,18 +336,19 @@ test_write_empty_syntax(void)
SerdNode* p = serd_new_uri(serd_string("http://example.org/p"));
SerdNode* o = serd_new_curie(serd_string("eg:o"));
- SerdBuffer buffer = {NULL, 0};
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
- SerdWriter* writer = serd_writer_new(
- world, SERD_SYNTAX_EMPTY, 0U, env, serd_buffer_write, &buffer);
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_SYNTAX_EMPTY, 0U, env, &output, 1U);
assert(writer);
assert(!serd_sink_write(serd_writer_sink(writer), 0U, s, p, o, NULL));
-
- serd_buffer_close(&buffer);
+ assert(!serd_close_output(&output));
char* const out = (char*)buffer.buf;
+ assert(out);
assert(strlen(out) == 0);
serd_free(out);
@@ -326,6 +356,7 @@ test_write_empty_syntax(void)
serd_node_free(o);
serd_node_free(p);
serd_node_free(s);
+ serd_close_output(&output);
serd_env_free(env);
serd_world_free(world);
}
@@ -333,13 +364,13 @@ test_write_empty_syntax(void)
static void
check_pname_escape(const char* const lname, const char* const expected)
{
- SerdWorld* world = serd_world_new();
- SerdEnv* env = serd_env_new(serd_empty_string());
- SerdBuffer buffer = {NULL, 0};
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+ SerdBuffer buffer = {NULL, 0};
+ SerdOutputStream output = serd_open_output_buffer(&buffer);
SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_write, &buffer);
-
+ serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U);
assert(writer);
static const char* const prefix = "http://example.org/";
@@ -362,11 +393,12 @@ check_pname_escape(const char* const lname, const char* const expected)
serd_node_free(p);
serd_node_free(s);
serd_writer_free(writer);
+ serd_close_output(&output);
serd_env_free(env);
serd_buffer_close(&buffer);
char* const out = (char*)buffer.buf;
- assert(!strcmp((char*)out, expected));
+ assert(!strcmp(out, expected));
serd_free(out);
serd_world_free(world);
@@ -406,6 +438,7 @@ test_write_pname_escapes(void)
int
main(void)
{
+ test_writer_new();
test_write_bad_event();
test_write_bad_prefix();
test_write_long_literal();