From acfb9ce1762d5136f846cdfb1356c0126d05f332 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 10 Jul 2021 15:05:14 -0400 Subject: Clean up socket-like stream reading test --- test/test_reader_writer.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 134c7a8e..233a3871 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -55,24 +55,36 @@ test_sink(void* handle, return SERD_SUCCESS; } -/// Returns EOF after a statement, then succeeds again (like a socket) +/// Reads a null byte 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(size == 1); assert(nmemb == 1); + (void)size; static const char* const string = "_:s1 _:o1 .\n" "_:s2 _:o2 .\n"; - size_t* count = (size_t*)stream; - if (*count == 34 || *count == 35 || *count + nmemb >= strlen(string)) { + size_t* const count = (size_t*)stream; + + // Normal reading for the first statement + if (*count < 35) { + *(char*)buf = string[*count]; + ++*count; + return nmemb; + } + + // EOF for the first read at the start of the second statement + if (*count == 35) { + assert(string[*count] == '_'); ++*count; return 0; } - memcpy((char*)buf, string + *count, size * nmemb); - *count += nmemb; + // Normal reading after the EOF, adjusting for the skipped index 35 + *(char*)buf = string[*count - 1]; + ++*count; return nmemb; } -- cgit v1.2.1