diff options
author | David Robillard <d@drobilla.net> | 2021-08-14 01:51:55 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | b3892cb6e4963e1bbeb346a8124101b7c3cf379b (patch) | |
tree | 4800918b6f4db5ce0d0f4802988c1935996d6ba3 /test | |
parent | 0e739f34801ff6810064a8fac570f6be2b61ae70 (diff) | |
download | serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.tar.gz serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.tar.bz2 serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.zip |
Simplify input stream API
More or less the same rationale as the previous commit, but for reading. This
makes for nice symmetry with writing, at the cost of a slightly more annoying
reader interface since the source doesn't know its block size or name.
Diffstat (limited to 'test')
-rw-r--r-- | test/meson.build | 1 | ||||
-rw-r--r-- | test/test_byte_source.c | 40 | ||||
-rw-r--r-- | test/test_free_null.c | 1 | ||||
-rw-r--r-- | test/test_overflow.c | 15 | ||||
-rw-r--r-- | test/test_read_chunk.c | 26 | ||||
-rw-r--r-- | test/test_reader.c | 120 | ||||
-rw-r--r-- | test/test_reader_writer.c | 6 |
7 files changed, 81 insertions, 128 deletions
diff --git a/test/meson.build b/test/meson.build index ab7666e3..37bae479 100644 --- a/test/meson.build +++ b/test/meson.build @@ -8,7 +8,6 @@ run_sort_suite = find_program('run_sort_suite.py') wrapper = meson.get_cross_property('exe_wrapper', '') unit_tests = [ - 'byte_source', 'caret', 'cursor', 'env', diff --git a/test/test_byte_source.c b/test/test_byte_source.c deleted file mode 100644 index 14ef5819..00000000 --- a/test/test_byte_source.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright 2021 David Robillard <d@drobilla.net> - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ - -#undef NDEBUG - -#include "serd/serd.h" - -#include <assert.h> -#include <stddef.h> -#include <stdio.h> - -static void -test_bad_page_size(void) -{ - assert(!serd_byte_source_new_filename("file.ttl", 0)); - - assert(!serd_byte_source_new_function( - (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, NULL, NULL, 0)); -} - -int -main(void) -{ - test_bad_page_size(); - - return 0; -} diff --git a/test/test_free_null.c b/test/test_free_null.c index 51a3c2dc..85529a2b 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -24,7 +24,6 @@ int main(void) { serd_free(NULL); - serd_byte_source_free(NULL); serd_node_free(NULL); serd_world_free(NULL); serd_env_free(NULL); diff --git a/test/test_overflow.c b/test/test_overflow.c index bb2513d5..ca273b23 100644 --- a/test/test_overflow.c +++ b/test/test_overflow.c @@ -31,19 +31,24 @@ test_size(SerdWorld* const world, const SerdReaderFlags flags, const size_t stack_size) { - SerdSink* sink = serd_sink_new(NULL, NULL, NULL); - SerdByteSource* byte_source = serd_byte_source_new_string(str, NULL); - SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); + SerdSink* sink = serd_sink_new(NULL, NULL, NULL); + SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); SerdReader* const reader = serd_reader_new(world, syntax, flags, env, sink, stack_size); assert(reader); - serd_reader_start(reader, byte_source); + 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_env_free(env); - serd_byte_source_free(byte_source); serd_sink_free(sink); return st; diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index 52bb7804..a956a07d 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -94,23 +94,23 @@ main(void) SerdWorld* world = serd_world_new(); SerdSink* sink = serd_sink_new(NULL, on_event, NULL); - SerdByteSource* byte_source = - serd_byte_source_new_string("@prefix eg: <http://example.org/> .\n" - "@base <http://example.org/base> .\n" - "eg:s1 eg:p1 eg:o1 ;\n" - " eg:p2 eg:o2 ,\n" - " eg:o3 .\n" - "eg:s2 eg:p1 eg:o1 ;\n" - " eg:p2 eg:o2 .\n" - "eg:s3 eg:p1 eg:o1 .\n" - "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n", - NULL); + static const char* const string = "@prefix eg: <http://example.org/> .\n" + "@base <http://example.org/base> .\n" + "eg:s1 eg:p1 eg:o1 ;\n" + " eg:p2 eg:o2 ,\n" + " eg:o3 .\n" + "eg:s2 eg:p1 eg:o1 ;\n" + " eg:p2 eg:o2 .\n" + "eg:s3 eg:p1 eg:o1 .\n" + "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n"; SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, env, sink, 4096); assert(reader); - assert(!serd_reader_start(reader, byte_source)); + const char* position = string; + SerdInputStream in = serd_open_input_string(&position); + assert(!serd_reader_start(reader, &in, NULL, 1)); assert(!serd_reader_read_chunk(reader) && n_prefix == 1); assert(!serd_reader_read_chunk(reader) && n_base == 1); @@ -123,9 +123,9 @@ main(void) assert(serd_reader_read_chunk(reader) == SERD_FAILURE); assert(!serd_reader_finish(reader)); + serd_close_input(&in); serd_reader_free(reader); serd_env_free(env); - serd_byte_source_free(byte_source); serd_sink_free(sink); serd_world_free(world); diff --git a/test/test_reader.c b/test/test_reader.c index 32b475f9..1c28e3da 100644 --- a/test/test_reader.c +++ b/test/test_reader.c @@ -71,15 +71,17 @@ test_prepare_error(void) assert(reader); - SerdByteSource* byte_source = serd_byte_source_new_function( - prepare_test_read, prepare_test_error, NULL, f, NULL, 1); + SerdInputStream in = + serd_open_input_stream(prepare_test_read, prepare_test_error, NULL, f); - SerdStatus st = serd_reader_start(reader, byte_source); + assert(serd_reader_start(reader, &in, NULL, 0) == SERD_ERR_BAD_ARG); + + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(!st); assert(serd_reader_read_document(reader) == SERD_ERR_UNKNOWN); - serd_byte_source_free(byte_source); + serd_close_input(&in); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); @@ -101,34 +103,38 @@ test_read_string(void) assert(reader); - SerdByteSource* byte_source = - serd_byte_source_new_string("<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, byte_source)); + assert(!serd_reader_start(reader, &in, NULL, 1)); assert(!serd_reader_read_document(reader)); assert(n_statements == 1); 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 the same but as a chunk - serd_byte_source_free(byte_source); + // Test reading a chunk n_statements = 0; - byte_source = - serd_byte_source_new_string("<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> , _:blank .", - NULL); + position = string2; + in = serd_open_input_string(&position); - assert(!serd_reader_start(reader, byte_source)); + assert(!serd_reader_start(reader, &in, NULL, 1)); assert(!serd_reader_read_chunk(reader)); assert(n_statements == 2); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_env_free(env); - serd_byte_source_free(byte_source); serd_sink_free(sink); serd_world_free(world); } @@ -195,16 +201,16 @@ test_read_eof_by_page(void) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0u, env, sink, 4096); - SerdByteSource* byte_source = serd_byte_source_new_function( - (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, temp, NULL, 4096); + SerdInputStream in = serd_open_input_stream( + (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, temp); - assert(serd_reader_start(reader, byte_source) == SERD_SUCCESS); + 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_byte_source_free(byte_source); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); @@ -224,22 +230,20 @@ test_read_eof_by_byte(void) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0u, env, sink, 4096); size_t n_reads = 0u; - SerdByteSource* byte_source = - serd_byte_source_new_function((SerdReadFunc)eof_test_read, - (SerdStreamErrorFunc)eof_test_error, - NULL, - &n_reads, - NULL, - 1); - - assert(serd_reader_start(reader, byte_source) == SERD_SUCCESS); + SerdInputStream in = + serd_open_input_stream((SerdReadFunc)eof_test_read, + (SerdStreamErrorFunc)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_byte_source_free(byte_source); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); @@ -263,10 +267,10 @@ test_read_chunks(void) assert(reader); - SerdByteSource* byte_source = serd_byte_source_new_function( - (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, f, NULL, 1); + SerdInputStream in = serd_open_input_stream( + (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, f); - SerdStatus st = serd_reader_start(reader, byte_source); + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(st == SERD_SUCCESS); // Write two statement separated by null characters @@ -307,7 +311,7 @@ test_read_chunks(void) assert(st == SERD_FAILURE); assert(n_statements == 2); - serd_byte_source_free(byte_source); + assert(!serd_close_input(&in)); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); @@ -315,26 +319,6 @@ test_read_chunks(void) serd_world_free(world); } -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) @@ -352,16 +336,17 @@ test_read_empty(void) assert(reader); - SerdByteSource* byte_source = serd_byte_source_new_function( - empty_test_read, empty_test_error, NULL, f, NULL, 1); + SerdInputStream in = serd_open_input_stream( + (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, NULL, f); - SerdStatus st = serd_reader_start(reader, byte_source); + SerdStatus st = serd_reader_start(reader, &in, NULL, 1); assert(!st); assert(serd_reader_read_document(reader) == SERD_SUCCESS); assert(n_statements == 0); + assert(!serd_reader_finish(reader)); + assert(!serd_close_input(&in)); - serd_byte_source_free(byte_source); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); @@ -400,17 +385,22 @@ test_error_cursor(void) assert(sink); assert(reader); - SerdByteSource* byte_source = - serd_byte_source_new_string("<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> .", - NULL); + static const char* const string = + "<http://example.org/s> <http://example.org/p> " + "<http://example.org/o> ."; + + SerdNode* const string_name = serd_new_string(SERD_STRING("string")); + const char* position = string; + SerdInputStream in = serd_open_input_string(&position); - SerdStatus st = serd_reader_start(reader, byte_source); + SerdStatus st = serd_reader_start(reader, &in, string_name, 1); assert(!st); assert(serd_reader_read_document(reader) == SERD_SUCCESS); + assert(!serd_reader_finish(reader)); assert(called); + assert(!serd_close_input(&in)); - serd_byte_source_free(byte_source); + serd_node_free(string_name); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index bd83b082..82af9f20 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -149,12 +149,12 @@ test_reader(const char* path) assert(serd_reader_read_document(reader) == SERD_ERR_BAD_CALL); assert(serd_reader_read_chunk(reader) == SERD_ERR_BAD_CALL); - SerdByteSource* byte_source = serd_byte_source_new_filename(path, 4096); - assert(!serd_reader_start(reader, byte_source)); + SerdInputStream in = serd_open_input_file(path); + assert(!serd_reader_start(reader, &in, NULL, 4096)); assert(!serd_reader_read_document(reader)); assert(n_statements == 6); serd_reader_finish(reader); - serd_byte_source_free(byte_source); + serd_close_input(&in); serd_reader_free(reader); serd_env_free(env); |