aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NEWS3
-rw-r--r--src/n3.c2
-rw-r--r--src/reader.c4
-rw-r--r--src/reader.h3
-rw-r--r--test/test_reader_writer.c108
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 <d@drobilla.net> Sun, 30 Apr 2023 22:24:07 +0000
+ -- David Robillard <d@drobilla.net> 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
@@ -101,6 +101,9 @@ SerdStatus
read_n3_statement(SerdReader* reader);
SerdStatus
+read_nquads_statement(SerdReader* reader);
+
+SerdStatus
read_nquadsDoc(SerdReader* reader);
SerdStatus
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,
+ "<http://example.org/s> <http://example.org/p1> "
+ "<http://example.org/o1> .\n");
+
+ fprintf(f,
+ "<http://example.org/s> <http://example.org/p2> "
+ "<http://example.org/o2> .\n");
+
+ fwrite(&null, sizeof(null), 1, f);
+
+ fprintf(f,
+ "<http://example.org/s> <http://example.org/p3> "
+ "<http://example.org/o3> .\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();