diff options
author | David Robillard <d@drobilla.net> | 2020-08-15 20:41:15 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-08-16 13:56:40 +0200 |
commit | 9a847982aa7d4c5f77f89b5d289835b9fb174c5e (patch) | |
tree | 267637130060f1b69b0144d5f40a2f29c4363e92 | |
parent | 7221394ae12f87533261ecaae725821fab6db40d (diff) | |
download | serd-9a847982aa7d4c5f77f89b5d289835b9fb174c5e.tar.gz serd-9a847982aa7d4c5f77f89b5d289835b9fb174c5e.tar.bz2 serd-9a847982aa7d4c5f77f89b5d289835b9fb174c5e.zip |
Add a test for reading from socket-like streams
-rw-r--r-- | tests/serd_test.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/serd_test.c b/tests/serd_test.c index 3d6b6ac9..8802dc72 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -87,6 +87,33 @@ test_sink(void* handle, return SERD_SUCCESS; } +/// Returns EOF after a statement, then succeeds again (like a socket) +static size_t +eof_test_read(void* buf, size_t size, size_t nmemb, void* stream) +{ + assert(nmemb == 1); + + static const char* const string = "_:s1 <http://example.org/p> _:o1 .\n" + "_:s2 <http://example.org/p> _:o2 .\n"; + + size_t* count = (size_t*)stream; + if (*count == 34 || *count == 35 || *count + nmemb >= strlen(string)) { + ++*count; + return 0; + } + + memcpy((char*)buf, string + *count, size * nmemb); + *count += nmemb; + return nmemb; +} + +static int +eof_test_error(void* stream) +{ + (void)stream; + return 0; +} + static void test_file_uri(const char* hostname, const char* path, @@ -636,6 +663,40 @@ test_reader(const char* path) assert(serd_reader_read_string(reader, USTR("This isn't Turtle at all."))); + // A read of a big page hits EOF then fails to read chunks immediately + { + FILE* temp = tmpfile(); + assert(temp); + fprintf(temp, "_:s <http://example.org/p> _:o .\n"); + fflush(temp); + fseek(temp, 0L, SEEK_SET); + + serd_reader_start_stream(reader, temp, NULL, true); + + assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + + serd_reader_end_stream(reader); + fclose(temp); + } + + // A byte-wise reader that hits EOF once then continues (like a socket) + { + size_t n_reads = 0; + serd_reader_start_source_stream(reader, + (SerdSource)eof_test_read, + (SerdStreamErrorFunc)eof_test_error, + &n_reads, + NULL, + 1); + + 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); + } + serd_reader_free(reader); } |