diff options
author | David Robillard <d@drobilla.net> | 2021-03-01 19:38:01 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-13 23:03:28 -0500 |
commit | 6f362db2fbfe1b02929673aa9b0529b4328fb195 (patch) | |
tree | fe81aaace76478fcd344757397bad7efc1b4db57 /test | |
parent | 6df8ef0e9b360f8d1a37723d0492bacc4a49f957 (diff) | |
download | serd-6f362db2fbfe1b02929673aa9b0529b4328fb195.tar.gz serd-6f362db2fbfe1b02929673aa9b0529b4328fb195.tar.bz2 serd-6f362db2fbfe1b02929673aa9b0529b4328fb195.zip |
Add SerdSink interface and hide implementations
Diffstat (limited to 'test')
-rw-r--r-- | test/meson.build | 1 | ||||
-rw-r--r-- | test/test_free_null.c | 1 | ||||
-rw-r--r-- | test/test_read_chunk.c | 11 | ||||
-rw-r--r-- | test/test_reader_writer.c | 65 | ||||
-rw-r--r-- | test/test_sink.c | 159 | ||||
-rw-r--r-- | test/test_writer.c | 5 |
6 files changed, 210 insertions, 32 deletions
diff --git a/test/meson.build b/test/meson.build index 308803bb..794dec2c 100644 --- a/test/meson.build +++ b/test/meson.build @@ -10,6 +10,7 @@ unit_tests = [ 'node', 'read_chunk', 'reader_writer', + 'sink', 'string', 'uri', 'writer', diff --git a/test/test_free_null.c b/test/test_free_null.c index 99e0be1d..af1ae149 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -26,6 +26,7 @@ main(void) serd_free(NULL); serd_node_free(NULL); serd_env_free(NULL); + serd_sink_free(NULL); serd_reader_free(NULL); serd_writer_free(NULL); diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index 009cbd14..8dd176e7 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -95,8 +95,14 @@ main(void) fseek(file, 0, SEEK_SET); - SerdReader* reader = serd_reader_new( - SERD_TURTLE, NULL, NULL, on_base, on_prefix, on_statement, on_end); + SerdSink* sink = serd_sink_new(NULL, NULL); + serd_sink_set_base_func(sink, on_base); + serd_sink_set_prefix_func(sink, on_prefix); + serd_sink_set_statement_func(sink, on_statement); + serd_sink_set_end_func(sink, on_end); + + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + assert(reader); assert(reader); assert(!serd_reader_start_stream(reader, file, NULL, true)); @@ -112,6 +118,7 @@ main(void) assert(!serd_reader_end_stream(reader)); serd_reader_free(reader); + serd_sink_free(sink); fclose(file); return 0; diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 76ba87b7..ed1c87cb 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -96,15 +96,16 @@ eof_test_error(void* stream) static void test_read_chunks(void) { - ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - FILE* const f = tmpfile(); - static const char null = 0; - SerdReader* const reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + FILE* const f = tmpfile(); + static const char null = 0; + SerdSink* sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + assert(sink); assert(f); + serd_sink_set_statement_func(sink, test_sink); SerdStatus st = serd_reader_start_stream(reader, f, NULL, false); assert(st == SERD_SUCCESS); @@ -148,18 +149,22 @@ test_read_chunks(void) assert(rt->n_statements == 2); serd_reader_free(reader); + serd_sink_free(sink); fclose(f); + free(rt); } static void test_read_string(void) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdSink* sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + assert(sink); + + serd_sink_set_statement_func(sink, test_sink); // Test reading a string that ends exactly at the end of input (no newline) const SerdStatus st = @@ -171,6 +176,8 @@ test_read_string(void) assert(rt->n_statements == 1); serd_reader_free(reader); + serd_sink_free(sink); + free(rt); } static void @@ -189,10 +196,10 @@ test_writer(const char* const path) SerdNode* lit = serd_new_string(SERD_STRING("hello")); - assert(serd_writer_set_base_uri(writer, lit)); - assert(serd_writer_set_prefix(writer, lit, lit)); - assert(serd_writer_end_anon(writer, NULL)); - assert(serd_writer_env(writer) == env); + const SerdSink* const iface = serd_writer_sink(writer); + assert(serd_sink_write_base(iface, lit)); + assert(serd_sink_write_prefix(iface, lit, lit)); + assert(serd_sink_write_end(iface, lit)); static const uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0}; const SerdStringView buf_view = {(const char*)buf, 3}; @@ -204,8 +211,7 @@ test_writer(const char* const path) // Write 3 invalid statements (should write nothing) const SerdNode* junk[][3] = {{s, o, o}, {o, p, o}, {s, o, p}}; for (size_t i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 3); ++i) { - assert(serd_writer_write_statement( - writer, 0, NULL, junk[i][0], junk[i][1], junk[i][2])); + assert(serd_sink_write(iface, 0, junk[i][0], junk[i][1], junk[i][2], NULL)); } static const SerdStringView empty = SERD_EMPTY_STRING(); @@ -217,8 +223,8 @@ test_writer(const char* const path) const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}}; for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 3); ++i) { - assert(!serd_writer_write_statement( - writer, 0, NULL, good[i][0], good[i][1], good[i][2])); + assert( + !serd_sink_write(iface, 0, good[i][0], good[i][1], good[i][2], NULL)); } // Write statements with bad UTF-8 (should be replaced) @@ -226,16 +232,15 @@ test_writer(const char* const path) const SerdStringView bad_view = {(const char*)bad_str, 4}; SerdNode* bad_lit = serd_new_string(bad_view); SerdNode* bad_uri = serd_new_uri(bad_view); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_lit)); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_uri)); - + assert(!serd_sink_write(iface, 0, s, p, bad_lit, 0)); + assert(!serd_sink_write(iface, 0, s, p, bad_uri, 0)); serd_node_free(bad_uri); serd_node_free(bad_lit); // Write 1 valid statement - serd_node_free(o); - o = serd_new_string(SERD_STRING("hello")); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o)); + SerdNode* const hello = serd_new_string(SERD_STRING("hello")); + assert(!serd_sink_write(iface, 0, s, p, hello, 0)); + serd_node_free(hello); serd_writer_free(writer); serd_node_free(lit); @@ -268,11 +273,13 @@ test_writer(const char* const path) static void test_reader(const char* path) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdSink* const sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(SERD_TURTLE, sink); + + assert(sink); assert(reader); - assert(serd_reader_handle(reader) == rt); + serd_sink_set_statement_func(sink, test_sink); SerdNode* g = serd_new_uri(SERD_STRING("http://example.org/")); serd_reader_set_default_graph(reader, g); @@ -336,6 +343,8 @@ test_reader(const char* path) } serd_reader_free(reader); + serd_sink_free(sink); + free(rt); } int diff --git a/test/test_sink.c b/test/test_sink.c new file mode 100644 index 00000000..982fc1c9 --- /dev/null +++ b/test/test_sink.c @@ -0,0 +1,159 @@ +/* + Copyright 2019-2020 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include <assert.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> + +#define NS_EG "http://example.org/" + +typedef struct { + const SerdNode* last_base; + const SerdNode* last_name; + const SerdNode* last_namespace; + const SerdNode* last_end; + const SerdNode* last_subject; + const SerdNode* last_predicate; + const SerdNode* last_object; + const SerdNode* last_graph; + SerdStatus return_status; +} State; + +static SerdStatus +on_base(void* handle, const SerdNode* uri) +{ + State* state = (State*)handle; + + state->last_base = uri; + return state->return_status; +} + +static SerdStatus +on_prefix(void* handle, const SerdNode* name, const SerdNode* uri) +{ + State* state = (State*)handle; + + state->last_name = name; + state->last_namespace = uri; + return state->return_status; +} + +static SerdStatus +on_statement(void* handle, + SerdStatementFlags flags, + const SerdNode* const graph, + const SerdNode* const subject, + const SerdNode* const predicate, + const SerdNode* const object) +{ + (void)flags; + + State* state = (State*)handle; + + state->last_subject = subject; + state->last_predicate = predicate; + state->last_object = object; + state->last_graph = graph; + + return state->return_status; +} + +static SerdStatus +on_end(void* handle, const SerdNode* node) +{ + State* state = (State*)handle; + + state->last_end = node; + return state->return_status; +} + +static void +test_callbacks(void) +{ + SerdNode* const base = serd_new_uri(SERD_STRING(NS_EG)); + SerdNode* const name = serd_new_string(SERD_STRING("eg")); + SerdNode* const uri = serd_new_uri(SERD_STRING(NS_EG "uri")); + SerdNode* const blank = serd_new_blank(SERD_STRING("b1")); + SerdEnv* env = serd_env_new(serd_node_string_view(base)); + State state = {0, 0, 0, 0, 0, 0, 0, 0, SERD_SUCCESS}; + + // Call functions on a sink with no functions set + + SerdSink* null_sink = serd_sink_new(&state, NULL); + assert(!serd_sink_write_base(null_sink, base)); + assert(!serd_sink_write_prefix(null_sink, name, uri)); + assert(!serd_sink_write(null_sink, 0, base, uri, blank, NULL)); + assert(!serd_sink_write_end(null_sink, blank)); + serd_sink_free(null_sink); + + // Try again with a sink that has the event handler set + + SerdSink* sink = serd_sink_new(&state, NULL); + serd_sink_set_base_func(sink, on_base); + serd_sink_set_prefix_func(sink, on_prefix); + serd_sink_set_statement_func(sink, on_statement); + serd_sink_set_end_func(sink, on_end); + + assert(!serd_sink_write_base(sink, base)); + assert(serd_node_equals(state.last_base, base)); + + assert(!serd_sink_write_prefix(sink, name, uri)); + assert(serd_node_equals(state.last_name, name)); + assert(serd_node_equals(state.last_namespace, uri)); + + assert(!serd_sink_write(sink, 0, base, uri, blank, NULL)); + assert(serd_node_equals(state.last_subject, base)); + assert(serd_node_equals(state.last_predicate, uri)); + assert(serd_node_equals(state.last_object, blank)); + assert(!state.last_graph); + + assert(!serd_sink_write_end(sink, blank)); + assert(serd_node_equals(state.last_end, blank)); + + serd_sink_free(sink); + serd_env_free(env); + serd_node_free(blank); + serd_node_free(uri); + serd_node_free(name); + serd_node_free(base); +} + +static void +test_free(void) +{ + // Free of null should (as always) not crash + serd_sink_free(NULL); + + // Set up a sink with dynamically allocated data and a free function + uintptr_t* data = (uintptr_t*)calloc(1, sizeof(uintptr_t)); + SerdSink* sink = serd_sink_new(data, free); + + // Free the sink, which should free the data (rely on valgrind or sanitizers) + serd_sink_free(sink); +} + +int +main(void) +{ + test_callbacks(); + test_free(); + return 0; +} diff --git a/test/test_writer.c b/test/test_writer.c index 756535ad..190995b6 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -34,7 +34,8 @@ test_write_bad_prefix(void) SerdNode* name = serd_new_string(SERD_STRING("eg")); SerdNode* uri = serd_new_uri(SERD_STRING("rel")); - assert(serd_writer_set_prefix(writer, name, uri) == SERD_ERR_BAD_ARG); + assert(serd_sink_write_prefix(serd_writer_sink(writer), name, uri) == + SERD_ERR_BAD_ARG); char* const out = serd_buffer_sink_finish(&buffer); @@ -63,7 +64,7 @@ test_write_long_literal(void) SERD_EMPTY_STRING(), SERD_EMPTY_STRING()); - assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o)); + assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL)); serd_node_free(o); serd_node_free(p); |