aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_reader.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-28 23:26:48 +0200
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commita083c64f506175029280ff76defa0ad7d7ae2ea0 (patch)
tree5e666749e352659d225d9c45c60bee06bd2bfe5c /test/test_reader.c
parent20eb7727954f9d8b7164146895904bbe595f5932 (diff)
downloadserd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.gz
serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.bz2
serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.zip
Simplify input stream API
Diffstat (limited to 'test/test_reader.c')
-rw-r--r--test/test_reader.c166
1 files changed, 121 insertions, 45 deletions
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));