diff options
author | David Robillard <d@drobilla.net> | 2020-06-28 23:26:48 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | a083c64f506175029280ff76defa0ad7d7ae2ea0 (patch) | |
tree | 5e666749e352659d225d9c45c60bee06bd2bfe5c /test | |
parent | 20eb7727954f9d8b7164146895904bbe595f5932 (diff) | |
download | serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.gz serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.bz2 serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.zip |
Simplify input stream API
Diffstat (limited to 'test')
-rw-r--r-- | test/meson.build | 11 | ||||
-rw-r--r-- | test/test_overflow.c | 9 | ||||
-rw-r--r-- | test/test_reader.c | 166 | ||||
-rw-r--r-- | test/test_reader_writer.c | 20 |
4 files changed, 151 insertions, 55 deletions
diff --git a/test/meson.build b/test/meson.build index e0aea675..496296c1 100644 --- a/test/meson.build +++ b/test/meson.build @@ -289,6 +289,17 @@ if is_variable('serdi') env: test_env, suite: 'io', ) + + if host_machine.system() == 'linux' + test( + 'unreadable', + serdi, + args: ['/sys/bus/pci/rescan'], + env: test_env, + should_fail: true, + suite: 'io', + ) + endif endif ########################### diff --git a/test/test_overflow.c b/test/test_overflow.c index 6e018033..086d353b 100644 --- a/test/test_overflow.c +++ b/test/test_overflow.c @@ -27,8 +27,15 @@ test_size(SerdWorld* const world, return SERD_BAD_STACK; } - serd_reader_start_string(reader, str, NULL); + SerdNode* string_name = serd_new_string(serd_string("string")); + const char* position = str; + SerdInputStream in = serd_open_input_string(&position); + serd_reader_start(reader, &in, string_name, 1); + const SerdStatus st = serd_reader_read_document(reader); + + serd_close_input(&in); + serd_node_free(string_name); serd_reader_free(reader); serd_sink_free(sink); diff --git a/test/test_reader.c b/test/test_reader.c index f33c3429..dd090d83 100644 --- a/test/test_reader.c +++ b/test/test_reader.c @@ -4,6 +4,7 @@ #undef NDEBUG #include "serd/event.h" +#include "serd/input_stream.h" #include "serd/reader.h" #include "serd/sink.h" #include "serd/status.h" @@ -11,6 +12,7 @@ #include "serd/syntax.h" #include "serd/world.h" #include "zix/allocator.h" +#include "zix/attributes.h" #include "zix/filesystem.h" #include "zix/path.h" @@ -19,7 +21,6 @@ #endif #include <assert.h> -#include <stdbool.h> #include <stdio.h> #include <string.h> @@ -53,6 +54,64 @@ test_sink(void* handle, const SerdEvent* event) return SERD_SUCCESS; } +ZIX_PURE_FUNC static size_t +prepare_test_read(void* buf, size_t size, size_t nmemb, void* stream) +{ + assert(size == 1); + assert(nmemb == 1); + + (void)buf; + (void)size; + (void)nmemb; + (void)stream; + + return 0; +} + +static int +prepare_test_error(void* stream) +{ + (void)stream; + return 1; +} + +static void +test_prepare_error(const char* const path) +{ + SerdWorld* const world = serd_world_new(); + ReaderTest rt = {0, 0, 0, 0}; + + FILE* const f = fopen(path, "w+b"); + assert(f); + + fprintf(f, "_:s <http://example.org/p> _:o .\n"); + fflush(f); + fseek(f, 0L, SEEK_SET); + + SerdSink* const sink = serd_sink_new(&rt, test_sink, NULL); + assert(sink); + + SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, 0, sink); + assert(reader); + + SerdInputStream in = + serd_open_input_stream(prepare_test_read, prepare_test_error, NULL, f); + + assert(serd_reader_start(reader, &in, NULL, 0) == SERD_BAD_ARG); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); + assert(!st); + + assert(serd_reader_read_document(reader) == SERD_BAD_STREAM); + + serd_close_input(&in); + serd_reader_free(reader); + serd_sink_free(sink); + serd_world_free(world); + fclose(f); + assert(!zix_remove(path)); +} + static void test_read_string(void) { @@ -64,19 +123,38 @@ test_read_string(void) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0U, sink); assert(reader); - // Test reading a string that ends exactly at the end of input (no newline) - assert( - !serd_reader_start_string(reader, - "<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> .", - NULL)); + static const char* const string1 = + "<http://example.org/s> <http://example.org/p> " + "<http://example.org/o> ."; + + const char* position = string1; + SerdInputStream in = serd_open_input_string(&position); + // Test reading a string that ends exactly at the end of input (no newline) + assert(!serd_reader_start(reader, &in, NULL, 1)); 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(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); + + static const char* const string2 = + "<http://example.org/s> <http://example.org/p> " + "<http://example.org/o> , _:blank ."; + + // Test reading a chunk + rt.n_statement = 0; + position = string2; + in = serd_open_input_string(&position); + + assert(!serd_reader_start(reader, &in, NULL, 1)); + assert(!serd_reader_read_chunk(reader)); + assert(rt.n_statement == 2); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_sink_free(sink); @@ -143,12 +221,15 @@ test_read_eof_by_page(const char* const path) SerdSink* sink = serd_sink_new(&ignored, test_sink, NULL); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0U, sink); - serd_reader_start_stream( - reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 4096); + SerdInputStream in = + serd_open_input_stream((SerdReadFunc)fread, (SerdErrorFunc)ferror, NULL, f); + assert(serd_reader_start(reader, &in, NULL, 4096) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_sink_free(sink); @@ -166,18 +247,17 @@ test_read_eof_by_byte(void) SerdSink* sink = serd_sink_new(&ignored, test_sink, NULL); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0U, sink); - size_t n_reads = 0U; - serd_reader_start_stream(reader, - (SerdReadFunc)eof_test_read, - (SerdStreamErrorFunc)eof_test_error, - &n_reads, - NULL, - 1); + size_t n_reads = 0U; + SerdInputStream in = serd_open_input_stream( + (SerdReadFunc)eof_test_read, (SerdErrorFunc)eof_test_error, NULL, &n_reads); + assert(serd_reader_start(reader, &in, NULL, 1) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_sink_free(sink); @@ -218,8 +298,10 @@ test_read_nquads_chunks(const char* const path) SerdReader* const reader = serd_reader_new(world, SERD_NQUADS, 0U, sink); assert(reader); - SerdStatus st = serd_reader_start_stream( - reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); + SerdInputStream in = + serd_open_input_stream((SerdReadFunc)fread, (SerdErrorFunc)ferror, NULL, f); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(st == SERD_SUCCESS); // Read first statement @@ -264,6 +346,7 @@ test_read_nquads_chunks(const char* const path) assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_sink_free(sink); serd_world_free(world); @@ -297,8 +380,10 @@ test_read_turtle_chunks(const char* const path) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0U, sink); assert(reader); - SerdStatus st = serd_reader_start_stream( - reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, f, NULL, 1); + SerdInputStream in = + serd_open_input_stream((SerdReadFunc)fread, (SerdErrorFunc)ferror, NULL, f); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(st == SERD_SUCCESS); // Read base @@ -359,6 +444,7 @@ test_read_turtle_chunks(const char* const path) assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_sink_free(sink); serd_world_free(world); @@ -366,29 +452,9 @@ test_read_turtle_chunks(const char* const path) assert(!zix_remove(path)); } -static size_t -empty_test_read(void* buf, size_t size, size_t nmemb, void* stream) -{ - (void)buf; - (void)size; - (void)nmemb; - (void)stream; - - assert(false); - - return 0; -} - -static int -empty_test_error(void* stream) -{ - (void)stream; - return 0; -} - /// Test that reading SERD_SYNTAX_EMPTY "succeeds" without reading any input static void -test_read_empty(void) +test_read_empty(const char* const path) { SerdWorld* const world = serd_world_new(); ReaderTest rt = {0, 0, 0, 0}; @@ -399,13 +465,22 @@ test_read_empty(void) SerdReader* const reader = serd_reader_new(world, SERD_SYNTAX_EMPTY, 0, sink); assert(reader); - SerdStatus st = serd_reader_start_stream( - reader, empty_test_read, empty_test_error, &rt, NULL, 1); - assert(st == SERD_SUCCESS); + FILE* const f = fopen(path, "w+b"); + assert(f); + + SerdInputStream in = + serd_open_input_stream((SerdReadFunc)fread, (SerdErrorFunc)ferror, NULL, f); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); + assert(!st); assert(serd_reader_read_document(reader) == SERD_SUCCESS); assert(rt.n_statement == 0); + assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); + fclose(f); + assert(!zix_remove(path)); serd_reader_free(reader); serd_sink_free(sink); serd_world_free(world); @@ -422,12 +497,13 @@ main(void) test_read_nquads_chunks(nq_path); test_read_turtle_chunks(ttl_path); + test_prepare_error(ttl_path); test_read_string(); test_read_eof_by_page(ttl_path); test_read_eof_by_byte(); test_read_nquads_chunks(nq_path); test_read_turtle_chunks(ttl_path); - test_read_empty(); + test_read_empty(ttl_path); assert(!zix_remove(dir)); diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index acd43ad1..1c4a415b 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -6,6 +6,7 @@ #include "serd/buffer.h" #include "serd/env.h" #include "serd/event.h" +#include "serd/input_stream.h" #include "serd/memory.h" #include "serd/node.h" #include "serd/reader.h" @@ -22,7 +23,6 @@ #include <assert.h> #include <errno.h> -#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> @@ -116,11 +116,15 @@ test_write_errors(void) const SerdSink* const sink = serd_writer_sink(writer); SerdReader* const reader = serd_reader_new(world, SERD_TRIG, 0U, sink); - SerdStatus st = serd_reader_start_string(reader, doc_string, NULL); + const char* position = doc_string; + SerdInputStream in = serd_open_input_string(&position); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(!st); st = serd_reader_read_document(reader); assert(st == SERD_BAD_WRITE); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_writer_free(writer); serd_env_free(env); @@ -252,8 +256,8 @@ test_reader(const char* path) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0U, sink); assert(reader); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - assert(serd_reader_read_document(reader) == SERD_FAILURE); + assert(serd_reader_read_chunk(reader) == SERD_BAD_CALL); + assert(serd_reader_read_document(reader) == SERD_BAD_CALL); serd_reader_add_blank_prefix(reader, "tmp"); @@ -266,14 +270,12 @@ test_reader(const char* path) # pragma GCC diagnostic pop #endif - assert(serd_reader_start_file(reader, "http://notafile", false)); - assert(serd_reader_start_file(reader, "file://invalid", false)); - assert(serd_reader_start_file(reader, "file:///nonexistant", false)); - - assert(!serd_reader_start_file(reader, path, true)); + SerdInputStream in = serd_open_input_file(path); + assert(!serd_reader_start(reader, &in, NULL, 4096)); assert(!serd_reader_read_document(reader)); assert(rt.n_statement == 6); assert(!serd_reader_finish(reader)); + serd_close_input(&in); serd_reader_free(reader); serd_sink_free(sink); |