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 --- NEWS | 3 +- src/n3.c | 2 +- src/reader.c | 4 +- src/reader.h | 3 ++ test/test_reader_writer.c | 108 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 110 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index 2299cd71..41289d06 100644 --- a/NEWS +++ b/NEWS @@ -22,13 +22,14 @@ serd (0.31.5) unstable; urgency=medium * Improve serdi man page * Improve writer error handling * Make URI writing stricter by default + * Make serd_reader_read_chunk() work with NQuads * Override pkg-config dependency within meson * Remove support for writing Turtle named inline nodes extension * Replace duplicated dox_to_sphinx script with sphinxygen dependency * Test header for warnings more strictly * Update standard test suites - -- David Robillard Sun, 30 Apr 2023 22:24:07 +0000 + -- David Robillard Wed, 03 May 2023 19:48:35 +0000 serd (0.30.16) stable; urgency=medium diff --git a/src/n3.c b/src/n3.c index c92059a5..d332504d 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1723,7 +1723,7 @@ read_turtleTrigDoc(SerdReader* const reader) return SERD_SUCCESS; } -static SerdStatus +SerdStatus read_nquads_statement(SerdReader* const reader) { SerdStatus st = SERD_SUCCESS; diff --git a/src/reader.c b/src/reader.c index 32535ceb..5d87216d 100644 --- a/src/reader.c +++ b/src/reader.c @@ -357,7 +357,9 @@ serd_reader_read_chunk(SerdReader* const reader) st = skip_byte(reader, 0); } - return st ? st : read_n3_statement(reader); + return st ? st + : (reader->syntax == SERD_NQUADS) ? read_nquads_statement(reader) + : read_n3_statement(reader); } SerdStatus diff --git a/src/reader.h b/src/reader.h index 54914ee5..9b558d1f 100644 --- a/src/reader.h +++ b/src/reader.h @@ -100,6 +100,9 @@ emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l); SerdStatus read_n3_statement(SerdReader* reader); +SerdStatus +read_nquads_statement(SerdReader* reader); + SerdStatus read_nquadsDoc(SerdReader* reader); 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