From 4196ab792e8f8957385fab68dd99c4cde19c870d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 3 May 2023 15:50:54 -0400 Subject: Make serd_reader_read_chunk() work with NQuads --- test/test_reader_writer.c | 108 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 83ee7b2f..cd7ca408 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -154,7 +154,95 @@ eof_test_error(void* stream) } static void -test_read_chunks(const char* const path) +test_read_nquads_chunks(const char* const path) +{ + static const char null = 0; + + FILE* const f = fopen(path, "w+b"); + + // Write two statements, a null separator, then another statement + + fprintf(f, + " " + " .\n"); + + fprintf(f, + " " + " .\n"); + + fwrite(&null, sizeof(null), 1, f); + + fprintf(f, + " " + " .\n"); + + fseek(f, 0, SEEK_SET); + + ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdReader* const reader = serd_reader_new(SERD_NQUADS, + rt, + free, + test_base_sink, + test_prefix_sink, + test_statement_sink, + test_end_sink); + + assert(reader); + assert(serd_reader_get_handle(reader) == rt); + assert(f); + + SerdStatus st = serd_reader_start_stream(reader, f, NULL, false); + assert(st == SERD_SUCCESS); + + // Read first statement + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_base == 0); + assert(rt->n_prefix == 0); + assert(rt->n_statement == 1); + assert(rt->n_end == 0); + + // Read second statement + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_base == 0); + assert(rt->n_prefix == 0); + assert(rt->n_statement == 2); + assert(rt->n_end == 0); + + // Read terminator + st = serd_reader_read_chunk(reader); + assert(st == SERD_FAILURE); + assert(rt->n_base == 0); + assert(rt->n_prefix == 0); + assert(rt->n_statement == 2); + assert(rt->n_end == 0); + + // Read last statement + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_base == 0); + assert(rt->n_prefix == 0); + assert(rt->n_statement == 3); + assert(rt->n_end == 0); + + // EOF + st = serd_reader_read_chunk(reader); + assert(st == SERD_FAILURE); + assert(rt->n_base == 0); + assert(rt->n_prefix == 0); + assert(rt->n_statement == 3); + assert(rt->n_end == 0); + + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + + serd_reader_free(reader); + fclose(f); + remove(path); +} + +static void +test_read_turtle_chunks(const char* const path) { static const char null = 0; @@ -555,16 +643,22 @@ main(void) const size_t tmp_len = strlen(tmp); #endif - const char* const name = "serd_test_reader_writer.ttl"; - const size_t name_len = strlen(name); - const size_t path_len = tmp_len + 1 + name_len; - char* const path = (char*)calloc(path_len + 1, 1); + const char* const ttl_name = "serd_test_reader_writer.ttl"; + const char* const nq_name = "serd_test_reader_writer.nq"; + const size_t ttl_name_len = strlen(ttl_name); + const size_t nq_name_len = strlen(nq_name); + const size_t path_len = tmp_len + 1 + ttl_name_len; + char* const path = (char*)calloc(path_len + 1, 1); memcpy(path, tmp, tmp_len + 1); path[tmp_len] = '/'; - memcpy(path + tmp_len + 1, name, name_len + 1); - test_read_chunks(path); + memcpy(path + tmp_len + 1, nq_name, nq_name_len + 1); + test_read_nquads_chunks(path); + + memcpy(path + tmp_len + 1, ttl_name, ttl_name_len + 1); + test_read_turtle_chunks(path); + test_read_string(); test_write_errors(); -- cgit v1.2.1