diff options
author | David Robillard <d@drobilla.net> | 2021-08-13 20:31:57 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | 0e739f34801ff6810064a8fac570f6be2b61ae70 (patch) | |
tree | 4451739f8e9b00d490d2d59aa6b1f370ae99c356 /test/test_writer.c | |
parent | 63e7e57237a79d0447b0450a7fd3148c43052299 (diff) | |
download | serd-0e739f34801ff6810064a8fac570f6be2b61ae70.tar.gz serd-0e739f34801ff6810064a8fac570f6be2b61ae70.tar.bz2 serd-0e739f34801ff6810064a8fac570f6be2b61ae70.zip |
Simplify output stream API
This makes the paging mechanism an internal detail once again. While it's
conceptually elegant to simply have a single write interface and have the block
dumper just be another implementation of that, unfortunately it is not
practical. The inlining of serd_block_dumper_write() is a significant
performance boost, because it avoids a non-inlinable function call of overhead
per character.
Compared to the SerdByteSink approach, this removes the burden and overhead of
needing to dynamically allocate the structure itself.
Diffstat (limited to 'test/test_writer.c')
-rw-r--r-- | test/test_writer.c | 122 |
1 files changed, 67 insertions, 55 deletions
diff --git a/test/test_writer.c b/test/test_writer.c index 83d4087c..34eafbb2 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -26,29 +26,43 @@ #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, 0)); + + 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}; - SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&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, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); assert(writer); const SerdEvent event = {(SerdEventType)42}; assert(serd_sink_write_event(serd_writer_sink(writer), &event) == SERD_ERR_BAD_ARG); - assert(!serd_byte_sink_close(byte_sink)); + assert(!serd_close_output(&output)); char* const out = (char*)buffer.buf; + assert(out); assert(!strcmp(out, "")); serd_free(out); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); serd_env_free(env); serd_world_free(world); } @@ -56,13 +70,13 @@ test_write_bad_event(void) static void test_write_long_literal(void) { - SerdWorld* world = serd_world_new(); - SerdNodes* nodes = serd_world_nodes(world); - SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - SerdBuffer buffer = {NULL, 0}; - SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer); + SerdWorld* world = serd_world_new(); + SerdNodes* nodes = serd_world_nodes(world); + 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, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); assert(writer); const SerdNode* s = @@ -81,7 +95,7 @@ test_write_long_literal(void) assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL)); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_buffer_close(&buffer); @@ -112,13 +126,12 @@ null_sink(const void* const buf, static void test_writer_stack_overflow(void) { - SerdWorld* world = serd_world_new(); - SerdNodes* nodes = serd_world_nodes(world); - SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - SerdByteSink* byte_sink = - serd_byte_sink_new_function(null_sink, NULL, NULL, 1u); + SerdWorld* world = serd_world_new(); + SerdNodes* nodes = serd_world_nodes(world); + SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); + SerdOutputStream output = serd_open_output_stream(null_sink, NULL, NULL); - SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); const SerdSink* sink = serd_writer_sink(writer); @@ -153,7 +166,7 @@ test_writer_stack_overflow(void) assert(st == SERD_ERR_OVERFLOW); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_world_free(world); } @@ -161,18 +174,16 @@ test_writer_stack_overflow(void) static void test_strict_write(void) { - SerdWorld* world = serd_world_new(); - SerdNodes* nodes = serd_world_nodes(world); - const char* path = "serd_strict_write_test.ttl"; - FILE* fd = fopen(path, "wb"); - assert(fd); + static const char* path = "serd_strict_write_test.ttl"; - SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); + SerdWorld* world = serd_world_new(); + SerdNodes* nodes = serd_world_nodes(world); + SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - SerdByteSink* byte_sink = serd_byte_sink_new_function( - (SerdWriteFunc)fwrite, (SerdStreamCloseFunc)fclose, fd, 1); + SerdOutputStream output = serd_open_output_file(path); + assert(output.stream); - SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0, env, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0, env, &output, 1); assert(writer); const SerdSink* sink = serd_writer_sink(writer); @@ -192,7 +203,7 @@ test_strict_write(void) assert(serd_sink_write(sink, 0, s, p, bad_uri, 0) == SERD_ERR_BAD_TEXT); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_world_free(world); } @@ -233,21 +244,20 @@ test_write_error(void) // Test with setting errno - SerdByteSink* byte_sink = - serd_byte_sink_new_function(faulty_sink, NULL, NULL, 1); + SerdOutputStream output = serd_open_output_stream(faulty_sink, NULL, NULL); - SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); assert(writer); SerdStatus st = serd_sink_write(serd_writer_sink(writer), 0u, s, p, o, NULL); assert(st == SERD_ERR_BAD_WRITE); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); // Test without setting errno - byte_sink = serd_byte_sink_new_function(faulty_sink, NULL, world, 1); - writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink); + output = serd_open_output_stream(faulty_sink, NULL, world); + writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); assert(writer); @@ -255,7 +265,7 @@ test_write_error(void) SERD_ERR_BAD_WRITE); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_world_free(world); @@ -277,24 +287,25 @@ test_write_empty_syntax(void) const SerdNode* o = serd_nodes_uri(nodes, SERD_STRING("http://example.org/o")); - SerdBuffer buffer = {NULL, 0}; - SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer); + SerdBuffer buffer = {NULL, 0}; + SerdOutputStream output = serd_open_output_buffer(&buffer); SerdWriter* writer = - serd_writer_new(world, SERD_SYNTAX_EMPTY, 0u, env, byte_sink); + serd_writer_new(world, SERD_SYNTAX_EMPTY, 0u, env, &output, 1); assert(writer); assert(!serd_sink_write(serd_writer_sink(writer), 0u, s, p, o, NULL)); - assert(!serd_byte_sink_close(byte_sink)); + assert(!serd_close_output(&output)); char* const out = (char*)buffer.buf; + assert(out); assert(strlen(out) == 0); serd_free(out); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_world_free(world); } @@ -314,11 +325,11 @@ test_write_bad_uri(void) const SerdNode* rel = serd_nodes_uri(nodes, SERD_STRING("rel")); - SerdBuffer buffer = {NULL, 0}; - SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer); + SerdBuffer buffer = {NULL, 0}; + SerdOutputStream output = serd_open_output_buffer(&buffer); SerdWriter* writer = - serd_writer_new(world, SERD_NTRIPLES, 0u, env, byte_sink); + serd_writer_new(world, SERD_NTRIPLES, 0u, env, &output, 1); assert(writer); @@ -328,10 +339,10 @@ test_write_bad_uri(void) assert(st); assert(st == SERD_ERR_BAD_ARG); - serd_byte_sink_close(byte_sink); + serd_close_output(&output); serd_free(buffer.buf); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_world_free(world); } @@ -339,13 +350,13 @@ test_write_bad_uri(void) static void check_pname_escape(const char* const lname, const char* const expected) { - SerdWorld* world = serd_world_new(); - SerdNodes* nodes = serd_world_nodes(world); - SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - SerdBuffer buffer = {NULL, 0}; - SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer); + SerdWorld* world = serd_world_new(); + SerdNodes* nodes = serd_world_nodes(world); + 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, byte_sink); + SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, &output, 1); assert(writer); static const char* const prefix = "http://example.org/"; @@ -368,7 +379,7 @@ check_pname_escape(const char* const lname, const char* const expected) assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, node, NULL)); serd_writer_free(writer); - serd_byte_sink_free(byte_sink); + serd_close_output(&output); serd_env_free(env); serd_buffer_close(&buffer); @@ -415,6 +426,7 @@ test_write_pname_escapes(void) int main(void) { + test_writer_new(); test_write_bad_event(); test_write_long_literal(); test_writer_stack_overflow(); |