From d62c7dc45af3256c6cd70d12a11e91b5b872c7db Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 20 Feb 2021 19:18:28 -0500 Subject: Replace multiple stream callbacks with SerdEvent This makes plumbing easier since everything goes through the same "stream" and only one callback is required to handling everything. It's also more easily extensible in case more event types need to be added in the future. --- test/test_reader_writer.c | 216 +++++++++++++++++++--------------------------- 1 file changed, 87 insertions(+), 129 deletions(-) (limited to 'test/test_reader_writer.c') diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index c4f8bb90..abea37b3 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -5,11 +5,11 @@ #include "serd/buffer.h" #include "serd/env.h" +#include "serd/event.h" #include "serd/memory.h" #include "serd/node.h" #include "serd/reader.h" #include "serd/sink.h" -#include "serd/statement.h" #include "serd/status.h" #include "serd/stream.h" #include "serd/string_view.h" @@ -65,46 +65,25 @@ static const char* const doc_string = "( eg:o ) eg:t eg:u .\n"; static SerdStatus -test_base_sink(void* handle, const SerdNode* uri) +test_sink(void* handle, const SerdEvent* event) { - (void)uri; - - ReaderTest* rt = (ReaderTest*)handle; - ++rt->n_base; - return SERD_SUCCESS; -} - -static SerdStatus -test_prefix_sink(void* handle, const SerdNode* name, const SerdNode* uri) -{ - (void)name; - (void)uri; - ReaderTest* rt = (ReaderTest*)handle; - ++rt->n_prefix; - return SERD_SUCCESS; -} - -static SerdStatus -test_statement_sink(void* handle, - const SerdStatementFlags flags, - const SerdStatement* statement) -{ - (void)flags; - (void)statement; - - ReaderTest* rt = (ReaderTest*)handle; - ++rt->n_statement; - return SERD_SUCCESS; -} -static SerdStatus -test_end_sink(void* handle, const SerdNode* node) -{ - (void)node; + switch (event->type) { + case SERD_BASE: + ++rt->n_base; + break; + case SERD_PREFIX: + ++rt->n_prefix; + break; + case SERD_STATEMENT: + ++rt->n_statement; + break; + case SERD_END: + ++rt->n_end; + break; + } - ReaderTest* rt = (ReaderTest*)handle; - ++rt->n_end; return SERD_SUCCESS; } @@ -158,6 +137,7 @@ test_read_nquads_chunks(const char* const path) static const char null = 0; FILE* const f = fopen(path, "w+b"); + assert(f); // Write two statements, a null separator, then another statement @@ -177,18 +157,13 @@ test_read_nquads_chunks(const char* const path) fseek(f, 0, SEEK_SET); - SerdWorld* const world = serd_world_new(); - ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdSink* const sink = serd_sink_new(rt, NULL); - SerdReader* const reader = serd_reader_new(world, SERD_NQUADS, sink, 4096); + SerdWorld* const world = serd_world_new(); + ReaderTest rt = {0, 0, 0, 0}; + SerdSink* const sink = serd_sink_new(&rt, test_sink, NULL); + assert(sink); + SerdReader* const reader = serd_reader_new(world, SERD_NQUADS, sink, 4096); assert(reader); - assert(sink); - assert(f); - serd_sink_set_base_func(sink, test_base_sink); - serd_sink_set_prefix_func(sink, test_prefix_sink); - serd_sink_set_statement_func(sink, test_statement_sink); - serd_sink_set_end_func(sink, test_end_sink); SerdStatus st = serd_reader_start_stream( reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); @@ -197,48 +172,47 @@ test_read_nquads_chunks(const char* const path) // Read first statement st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 1); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 1); + assert(rt.n_end == 0); // Read second statement st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 2); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 2); + assert(rt.n_end == 0); // Read terminator st = serd_reader_read_chunk(reader); assert(st == SERD_FAILURE); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 2); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 2); + assert(rt.n_end == 0); // Read last statement st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 3); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 3); + assert(rt.n_end == 0); // EOF st = serd_reader_read_chunk(reader); assert(st == SERD_FAILURE); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 3); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 3); + assert(rt.n_end == 0); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); serd_reader_free(reader); serd_sink_free(sink); - free(rt); serd_world_free(world); fclose(f); remove(path); @@ -250,6 +224,7 @@ test_read_turtle_chunks(const char* const path) static const char null = 0; FILE* const f = fopen(path, "w+b"); + assert(f); // Write two statements separated by null characters fprintf(f, "@base .\n"); @@ -261,18 +236,13 @@ test_read_turtle_chunks(const char* const path) fwrite(&null, sizeof(null), 1, f); fseek(f, 0, SEEK_SET); - SerdWorld* world = serd_world_new(); - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdSink* sink = serd_sink_new(rt, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdWorld* world = serd_world_new(); + ReaderTest rt = {0, 0, 0, 0}; + SerdSink* sink = serd_sink_new(&rt, test_sink, NULL); + assert(sink); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); assert(reader); - assert(sink); - assert(f); - serd_sink_set_base_func(sink, test_base_sink); - serd_sink_set_prefix_func(sink, test_prefix_sink); - serd_sink_set_statement_func(sink, test_statement_sink); - serd_sink_set_end_func(sink, test_end_sink); SerdStatus st = serd_reader_start_stream( reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); @@ -281,64 +251,63 @@ test_read_turtle_chunks(const char* const path) // Read base st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 1); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 0); - assert(rt->n_end == 0); + assert(rt.n_base == 1); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 0); + assert(rt.n_end == 0); // Read prefix st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 0); - assert(rt->n_end == 0); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 0); + assert(rt.n_end == 0); // Read first two statements st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 2); - assert(rt->n_end == 0); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 2); + assert(rt.n_end == 0); // Read terminator st = serd_reader_read_chunk(reader); assert(st == SERD_FAILURE); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 2); - assert(rt->n_end == 0); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 2); + assert(rt.n_end == 0); // Read statements after null terminator st = serd_reader_read_chunk(reader); assert(st == SERD_SUCCESS); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 4); - assert(rt->n_end == 1); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 4); + assert(rt.n_end == 1); // Read terminator st = serd_reader_read_chunk(reader); assert(st == SERD_FAILURE); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 4); - assert(rt->n_end == 1); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 4); + assert(rt.n_end == 1); // EOF st = serd_reader_read_chunk(reader); assert(st == SERD_FAILURE); - assert(rt->n_base == 1); - assert(rt->n_prefix == 1); - assert(rt->n_statement == 4); - assert(rt->n_end == 1); + assert(rt.n_base == 1); + assert(rt.n_prefix == 1); + assert(rt.n_statement == 4); + assert(rt.n_end == 1); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); serd_reader_free(reader); serd_sink_free(sink); - free(rt); serd_world_free(world); fclose(f); remove(path); @@ -347,18 +316,13 @@ test_read_turtle_chunks(const char* const path) static void test_read_string(void) { - SerdWorld* world = serd_world_new(); - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdSink* sink = serd_sink_new(rt, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); - - assert(reader); + SerdWorld* world = serd_world_new(); + ReaderTest rt = {0, 0, 0, 0}; + SerdSink* sink = serd_sink_new(&rt, test_sink, NULL); assert(sink); - serd_sink_set_base_func(sink, test_base_sink); - serd_sink_set_prefix_func(sink, test_prefix_sink); - serd_sink_set_statement_func(sink, test_statement_sink); - serd_sink_set_end_func(sink, test_end_sink); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + assert(reader); // Test reading a string that ends exactly at the end of input (no newline) assert( @@ -368,15 +332,14 @@ test_read_string(void) NULL)); assert(!serd_reader_read_document(reader)); - assert(rt->n_base == 0); - assert(rt->n_prefix == 0); - assert(rt->n_statement == 1); - assert(rt->n_end == 0); + assert(rt.n_base == 0); + assert(rt.n_prefix == 0); + assert(rt.n_statement == 1); + assert(rt.n_end == 0); assert(!serd_reader_finish(reader)); serd_reader_free(reader); serd_sink_free(sink); - free(rt); serd_world_free(world); } @@ -525,7 +488,7 @@ test_writer(const char* const path) SerdNode* const base = serd_new_uri(serd_string("http://example.org/base")); - serd_writer_set_base_uri(writer, base); + serd_sink_write_base(serd_writer_sink(writer), base); serd_node_free(base); serd_writer_free(writer); @@ -547,14 +510,9 @@ test_reader(const char* path) { SerdWorld* world = serd_world_new(); ReaderTest rt = {0, 0, 0, 0}; - SerdSink* const sink = serd_sink_new(&rt, NULL); + SerdSink* const sink = serd_sink_new(&rt, test_sink, NULL); assert(sink); - serd_sink_set_base_func(sink, test_base_sink); - serd_sink_set_prefix_func(sink, test_prefix_sink); - serd_sink_set_statement_func(sink, test_statement_sink); - serd_sink_set_end_func(sink, test_end_sink); - // Test that too little stack space fails gracefully assert(!serd_reader_new(world, SERD_TURTLE, sink, 32)); -- cgit v1.2.1