diff options
author | David Robillard <d@drobilla.net> | 2021-07-10 22:22:09 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 2019d1581c1296cc534c731a1f454ce08b460dcc (patch) | |
tree | fa2737fbe62772806e37669aef7d56f4a10b4f7b | |
parent | b839af39ad87fbf0fe30fd500b596a5593c13a69 (diff) | |
download | serd-2019d1581c1296cc534c731a1f454ce08b460dcc.tar.gz serd-2019d1581c1296cc534c731a1f454ce08b460dcc.tar.bz2 serd-2019d1581c1296cc534c731a1f454ce08b460dcc.zip |
Use SerdNodes to simplify some tests
-rw-r--r-- | test/test_caret.c | 37 | ||||
-rw-r--r-- | test/test_reader_writer.c | 60 | ||||
-rw-r--r-- | test/test_sink.c | 26 | ||||
-rw-r--r-- | test/test_statement.c | 180 | ||||
-rw-r--r-- | test/test_terse_write.c | 44 | ||||
-rw-r--r-- | test/test_writer.c | 117 |
6 files changed, 286 insertions, 178 deletions
diff --git a/test/test_caret.c b/test/test_caret.c index 9374f65b..379f80eb 100644 --- a/test/test_caret.c +++ b/test/test_caret.c @@ -6,7 +6,9 @@ #include "failing_allocator.h" #include "serd/caret.h" +#include "serd/memory.h" #include "serd/node.h" +#include "serd/nodes.h" #include <assert.h> #include <stddef.h> @@ -15,23 +17,29 @@ static int test_caret(void) { - SerdNode* const node = serd_node_new(NULL, serd_a_string("node")); - SerdCaret* const caret = serd_caret_new(NULL, node, 46, 2); + SerdAllocator* const allocator = serd_default_allocator(); + + SerdNodes* const nodes = serd_nodes_new(allocator); + const SerdNode* const node = serd_nodes_get(nodes, serd_a_string("node")); + + SerdCaret* const caret = serd_caret_new(allocator, node, 46, 2); assert(serd_caret_equals(caret, caret)); assert(serd_caret_document(caret) == node); assert(serd_caret_line(caret) == 46); assert(serd_caret_column(caret) == 2); - SerdCaret* const copy = serd_caret_copy(NULL, caret); + SerdCaret* const copy = serd_caret_copy(allocator, caret); assert(serd_caret_equals(caret, copy)); - assert(!serd_caret_copy(NULL, NULL)); + assert(!serd_caret_copy(allocator, NULL)); + + const SerdNode* const other_node = + serd_nodes_get(nodes, serd_a_string("other")); - SerdNode* const other_node = serd_node_new(NULL, serd_a_string("other")); - SerdCaret* const other_file = serd_caret_new(NULL, other_node, 46, 2); - SerdCaret* const other_line = serd_caret_new(NULL, node, 47, 2); - SerdCaret* const other_col = serd_caret_new(NULL, node, 46, 3); + SerdCaret* const other_file = serd_caret_new(allocator, other_node, 46, 2); + SerdCaret* const other_line = serd_caret_new(allocator, node, 47, 2); + SerdCaret* const other_col = serd_caret_new(allocator, node, 46, 3); assert(!serd_caret_equals(caret, other_file)); assert(!serd_caret_equals(caret, other_line)); @@ -39,13 +47,12 @@ test_caret(void) assert(!serd_caret_equals(caret, NULL)); assert(!serd_caret_equals(NULL, caret)); - serd_caret_free(NULL, other_col); - serd_caret_free(NULL, other_line); - serd_caret_free(NULL, other_file); - serd_node_free(NULL, other_node); - serd_caret_free(NULL, copy); - serd_caret_free(NULL, caret); - serd_node_free(NULL, node); + serd_caret_free(allocator, other_col); + serd_caret_free(allocator, other_line); + serd_caret_free(allocator, other_file); + serd_caret_free(allocator, copy); + serd_caret_free(allocator, caret); + serd_nodes_free(nodes); return 0; } diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 9a4c988f..7a57daed 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -9,6 +9,7 @@ #include "serd/input_stream.h" #include "serd/memory.h" #include "serd/node.h" +#include "serd/nodes.h" #include "serd/output_stream.h" #include "serd/reader.h" #include "serd/sink.h" @@ -142,6 +143,7 @@ static void test_writer(const char* const path) { SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); SerdOutputStream output = serd_open_output_file(path); @@ -151,7 +153,7 @@ test_writer(const char* const path) assert(writer); - SerdNode* lit = serd_node_new(NULL, serd_a_string("hello")); + const SerdNode* lit = serd_nodes_get(nodes, serd_a_string("hello")); const SerdSink* const iface = serd_writer_sink(writer); assert(serd_sink_write_base(iface, lit)); @@ -161,10 +163,13 @@ test_writer(const char* const path) static const uint8_t bad_buf[] = {0xEF, 0xBF, 0xBD, 0}; const SerdStringView bad_buf_view = {(const char*)bad_buf, 3}; - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org")); - SerdNode* p = - serd_node_new(NULL, serd_a_uri_string("http://example.org/pred")); - SerdNode* bad = serd_node_new(NULL, serd_a_string_view(bad_buf_view)); + const SerdNode* s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org")); + + const SerdNode* p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/pred")); + + const SerdNode* bad = serd_nodes_get(nodes, serd_a_string_view(bad_buf_view)); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][3] = {{s, bad, bad}, {bad, p, bad}, {s, bad, p}}; @@ -172,18 +177,15 @@ test_writer(const char* const path) assert(serd_sink_write(iface, 0, junk[i][0], junk[i][1], junk[i][2], NULL)); } - serd_node_free(NULL, bad); - - const SerdStringView urn_Type = serd_string("urn:Type"); - const SerdStringView en = serd_string("en"); + const SerdStringView urn_Type = serd_string("urn:Type"); + const SerdStringView en = serd_string("en"); + const SerdNode* const o = serd_nodes_get(nodes, serd_a_string("o")); - SerdNode* const o = serd_node_new(NULL, serd_a_string("o")); + const SerdNode* const t = + serd_nodes_get(nodes, serd_a_typed_literal(serd_string("t"), urn_Type)); - SerdNode* const t = - serd_node_new(NULL, serd_a_typed_literal(serd_string("t"), urn_Type)); - - SerdNode* const l = - serd_node_new(NULL, serd_a_plain_literal(serd_string("l"), en)); + const SerdNode* const l = + serd_nodes_get(nodes, serd_a_plain_literal(serd_string("l"), en)); const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}}; @@ -198,37 +200,32 @@ test_writer(const char* const path) static const char* const bad_uri_str = (const char*)bad_uri_buf; // Write statements with bad UTF-8 (should be replaced) - SerdNode* bad_lit = serd_node_new(NULL, serd_a_string(bad_lit_str)); - SerdNode* bad_uri = serd_node_new(NULL, serd_a_uri_string(bad_uri_str)); + + const SerdNode* bad_lit = serd_nodes_get(nodes, serd_a_string(bad_lit_str)); + const SerdNode* bad_uri = + serd_nodes_get(nodes, serd_a_uri_string(bad_uri_str)); + 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(NULL, bad_uri); - serd_node_free(NULL, bad_lit); // Write 1 valid statement - SerdNode* const hello = serd_node_new(NULL, serd_a_string("hello")); + const SerdNode* const hello = serd_nodes_get(nodes, serd_a_string("hello")); assert(!serd_sink_write(iface, 0, s, p, hello, 0)); - serd_node_free(NULL, hello); serd_writer_free(writer); serd_close_output(&output); - serd_node_free(NULL, lit); - serd_node_free(NULL, o); - serd_node_free(NULL, t); - serd_node_free(NULL, l); - // Test buffer sink - SerdBuffer buffer = {NULL, NULL, 0}; - SerdNode* const base = - serd_node_new(NULL, serd_a_uri_string("http://example.org/base")); + SerdBuffer buffer = {NULL, NULL, 0}; + + const SerdNode* const base = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/base")); output = serd_open_output_buffer(&buffer); writer = serd_writer_new(world, SERD_TURTLE, 0, env, &output, 1U); serd_sink_write_base(serd_writer_sink(writer), base); - serd_node_free(NULL, base); serd_writer_free(writer); serd_close_output(&output); @@ -237,9 +234,6 @@ test_writer(const char* const path) assert(!strcmp(out, "@base <http://example.org/base> .\n")); serd_free(NULL, buffer.buf); - serd_node_free(NULL, p); - serd_node_free(NULL, s); - serd_env_free(env); serd_world_free(world); } diff --git a/test/test_sink.c b/test/test_sink.c index 72c45df6..8b6094b7 100644 --- a/test/test_sink.c +++ b/test/test_sink.c @@ -5,9 +5,9 @@ #include "failing_allocator.h" -#include "serd/env.h" #include "serd/event.h" #include "serd/node.h" +#include "serd/nodes.h" #include "serd/sink.h" #include "serd/statement.h" #include "serd/status.h" @@ -110,12 +110,17 @@ test_failed_alloc(void) static void test_callbacks(void) { - SerdNode* const base = serd_node_new(NULL, serd_a_uri_string(NS_EG)); - SerdNode* const name = serd_node_new(NULL, serd_a_string("eg")); - SerdNode* const uri = serd_node_new(NULL, serd_a_uri_string(NS_EG "uri")); - SerdNode* const blank = serd_node_new(NULL, serd_a_blank_string("b1")); - SerdEnv* env = serd_env_new(NULL, serd_node_string_view(base)); - State state = {0, 0, 0, 0, 0, SERD_SUCCESS}; + static const char* const uri_string = NS_EG "uri"; + + SerdNodes* const nodes = serd_nodes_new(NULL); + const SerdNode* const base = serd_nodes_get(nodes, serd_a_uri_string(NS_EG)); + const SerdNode* const name = serd_nodes_get(nodes, serd_a_string("eg")); + const SerdNode* const uri = + serd_nodes_get(nodes, serd_a_uri_string(uri_string)); + const SerdNode* const blank = + serd_nodes_get(nodes, serd_a_blank_string("b1")); + + State state = {0, 0, 0, 0, 0, SERD_SUCCESS}; SerdStatement* const statement = serd_statement_new(NULL, base, uri, blank, NULL, NULL); @@ -169,13 +174,8 @@ test_callbacks(void) assert(serd_sink_write_event(sink, &junk) == SERD_BAD_ARG); serd_sink_free(sink); - serd_statement_free(NULL, statement); - serd_env_free(env); - serd_node_free(NULL, blank); - serd_node_free(NULL, uri); - serd_node_free(NULL, name); - serd_node_free(NULL, base); + serd_nodes_free(nodes); } static void diff --git a/test/test_statement.c b/test/test_statement.c index 4cc3a0fa..e960002f 100644 --- a/test/test_statement.c +++ b/test/test_statement.c @@ -3,6 +3,8 @@ #undef NDEBUG +#include "failing_allocator.h" + #include "serd/caret.h" #include "serd/memory.h" #include "serd/node.h" @@ -11,6 +13,7 @@ #include <assert.h> #include <stddef.h> +#include <stdint.h> #define NS_EG "http://example.org/" @@ -24,7 +27,9 @@ test_new(void) SerdNodes* const nodes = serd_nodes_new(allocator); const SerdNode* const s = serd_nodes_get(nodes, serd_a_string("s")); + const SerdNode* const u = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "u")); + const SerdNode* const b = serd_nodes_get(nodes, serd_a_blank_string(NS_EG "b")); @@ -42,31 +47,69 @@ test_new(void) static void test_copy(void) { - SerdAllocator* const alloc = NULL; + assert(!serd_statement_copy(NULL, NULL)); + + SerdAllocator* const allocator = serd_default_allocator(); + + assert(!serd_statement_copy(allocator, NULL)); + + SerdNodes* const nodes = serd_nodes_new(allocator); + + const SerdNode* const s = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "s")); + + const SerdNode* const p = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "p")); + + const SerdNode* const o = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "o")); + const SerdNode* const g = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "g")); + + SerdStatement* const statement = + serd_statement_new(allocator, s, p, o, g, NULL); + + SerdStatement* const copy = serd_statement_copy(allocator, statement); + + assert(serd_statement_equals(copy, statement)); + + serd_statement_free(allocator, copy); + serd_statement_free(allocator, statement); + serd_nodes_free(nodes); +} + +static void +test_copy_with_caret(void) +{ assert(!serd_statement_copy(NULL, NULL)); - SerdNode* const f = serd_node_new(alloc, serd_a_string("file")); - SerdNode* const s = serd_node_new(alloc, serd_a_uri_string(NS_EG "s")); - SerdNode* const p = serd_node_new(alloc, serd_a_uri_string(NS_EG "p")); - SerdNode* const o = serd_node_new(alloc, serd_a_uri_string(NS_EG "o")); - SerdNode* const g = serd_node_new(alloc, serd_a_uri_string(NS_EG "g")); + SerdAllocator* const allocator = serd_default_allocator(); + + assert(!serd_statement_copy(allocator, NULL)); + + SerdNodes* const nodes = serd_nodes_new(allocator); + + const SerdNode* const f = serd_nodes_get(nodes, serd_a_string("file")); + + const SerdNode* const s = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "s")); + + const SerdNode* const p = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "p")); + + const SerdNode* const o = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "o")); - SerdCaret* const caret = serd_caret_new(alloc, f, 1, 1); - SerdStatement* const statement = serd_statement_new(alloc, s, p, o, g, caret); - SerdStatement* const copy = serd_statement_copy(alloc, statement); + const SerdNode* const g = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "g")); + + SerdCaret* const caret = serd_caret_new(allocator, f, 1, 1); + + SerdStatement* const statement = + serd_statement_new(allocator, s, p, o, g, caret); + + SerdStatement* const copy = serd_statement_copy(allocator, statement); assert(serd_statement_equals(copy, statement)); assert(serd_caret_equals(serd_statement_caret(copy), caret)); - serd_statement_free(NULL, copy); - serd_caret_free(NULL, caret); - serd_statement_free(NULL, statement); - serd_node_free(NULL, g); - serd_node_free(NULL, o); - serd_node_free(NULL, p); - serd_node_free(NULL, s); - serd_node_free(NULL, f); + serd_statement_free(allocator, copy); + serd_caret_free(allocator, caret); + serd_statement_free(allocator, statement); + serd_nodes_free(nodes); } static void @@ -79,17 +122,24 @@ test_free(void) static void test_fields(void) { - SerdAllocator* const alloc = NULL; + SerdAllocator* const allocator = serd_default_allocator(); - SerdNode* const f = serd_node_new(alloc, serd_a_string("file")); - SerdNode* const s = serd_node_new(alloc, serd_a_uri_string(NS_EG "s")); - SerdNode* const p = serd_node_new(alloc, serd_a_uri_string(NS_EG "p")); - SerdNode* const o = serd_node_new(alloc, serd_a_uri_string(NS_EG "o")); - SerdNode* const g = serd_node_new(alloc, serd_a_uri_string(NS_EG "g")); + SerdNodes* const nodes = serd_nodes_new(allocator); - SerdCaret* const caret = serd_caret_new(alloc, f, 1, 1); + const SerdNode* const f = serd_nodes_get(nodes, serd_a_string("file")); - SerdStatement* const statement = serd_statement_new(alloc, s, p, o, g, caret); + const SerdNode* const s = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "s")); + + const SerdNode* const p = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "p")); + + const SerdNode* const o = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "o")); + + const SerdNode* const g = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "g")); + + SerdCaret* const caret = serd_caret_new(allocator, f, 1, 1); + + SerdStatement* const statement = + serd_statement_new(allocator, s, p, o, g, caret); assert(serd_statement_equals(statement, statement)); assert(!serd_statement_equals(statement, NULL)); @@ -116,29 +166,75 @@ test_fields(void) assert(!serd_statement_matches(statement, NULL, NULL, s, NULL)); assert(!serd_statement_matches(statement, NULL, NULL, NULL, s)); - SerdStatement* const diff_s = serd_statement_new(alloc, o, p, o, g, caret); + SerdStatement* const diff_s = + serd_statement_new(allocator, o, p, o, g, caret); assert(!serd_statement_equals(statement, diff_s)); - serd_statement_free(alloc, diff_s); + serd_statement_free(allocator, diff_s); - SerdStatement* const diff_p = serd_statement_new(alloc, s, o, o, g, caret); + SerdStatement* const diff_p = + serd_statement_new(allocator, s, o, o, g, caret); assert(!serd_statement_equals(statement, diff_p)); - serd_statement_free(alloc, diff_p); + serd_statement_free(allocator, diff_p); - SerdStatement* const diff_o = serd_statement_new(alloc, s, p, s, g, caret); + SerdStatement* const diff_o = + serd_statement_new(allocator, s, p, s, g, caret); assert(!serd_statement_equals(statement, diff_o)); - serd_statement_free(alloc, diff_o); + serd_statement_free(allocator, diff_o); - SerdStatement* const diff_g = serd_statement_new(alloc, s, p, o, s, caret); + SerdStatement* const diff_g = + serd_statement_new(allocator, s, p, o, s, caret); assert(!serd_statement_equals(statement, diff_g)); - serd_statement_free(alloc, diff_g); - - serd_statement_free(alloc, statement); - serd_caret_free(alloc, caret); - serd_node_free(alloc, g); - serd_node_free(alloc, o); - serd_node_free(alloc, p); - serd_node_free(alloc, s); - serd_node_free(alloc, f); + serd_statement_free(allocator, diff_g); + + serd_statement_free(allocator, statement); + serd_caret_free(allocator, caret); + serd_nodes_free(nodes); +} + +static void +test_failed_alloc(void) +{ + SerdNodes* const nodes = serd_nodes_new(serd_default_allocator()); + + const SerdNode* const f = serd_nodes_get(nodes, serd_a_string("file")); + const SerdNode* const s = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "s")); + const SerdNode* const p = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "p")); + const SerdNode* const o = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "o")); + const SerdNode* const g = serd_nodes_get(nodes, serd_a_uri_string(NS_EG "g")); + + SerdCaret* const caret = serd_caret_new(serd_default_allocator(), f, 1, 1); + + SerdFailingAllocator allocator = serd_failing_allocator(); + + // Successfully allocate a statement to count the number of allocations + SerdStatement* const statement = + serd_statement_new(&allocator.base, s, p, o, g, caret); + assert(statement); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations; + for (size_t i = 0U; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_statement_new(&allocator.base, s, p, o, g, caret)); + } + + // Successfully copy the statement to count the number of allocations + allocator.n_allocations = 0; + allocator.n_remaining = SIZE_MAX; + SerdStatement* const copy = serd_statement_copy(&allocator.base, statement); + assert(copy); + + // Test that each allocation failing is handled gracefully + const size_t n_copy_allocs = allocator.n_allocations; + for (size_t i = 0U; i < n_copy_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_statement_copy(&allocator.base, statement)); + } + + serd_statement_free(&allocator.base, copy); + serd_statement_free(&allocator.base, statement); + serd_caret_free(serd_default_allocator(), caret); + serd_nodes_free(nodes); } int @@ -146,8 +242,10 @@ main(void) { test_new(); test_copy(); + test_copy_with_caret(); test_free(); test_fields(); + test_failed_alloc(); return 0; } diff --git a/test/test_terse_write.c b/test/test_terse_write.c index 3fa08fbd..2a516c04 100644 --- a/test/test_terse_write.c +++ b/test/test_terse_write.c @@ -7,6 +7,7 @@ #include "serd/env.h" #include "serd/memory.h" #include "serd/node.h" +#include "serd/nodes.h" #include "serd/output_stream.h" #include "serd/sink.h" #include "serd/statement.h" @@ -39,20 +40,29 @@ check_output(SerdWriter* writer, SerdBuffer* buffer, const char* expected) static int test(void) { - SerdBuffer buffer = {NULL, NULL, 0}; - SerdWorld* world = serd_world_new(NULL); - SerdEnv* env = serd_env_new(NULL, serd_empty_string()); + SerdWorld* world = serd_world_new(NULL); + SerdAllocator* alloc = serd_world_allocator(world); + SerdBuffer buffer = {NULL, NULL, 0}; + SerdEnv* env = serd_env_new(alloc, serd_empty_string()); + SerdNodes* nodes = serd_nodes_new(alloc); - SerdNode* b1 = serd_node_new(NULL, serd_a_blank_string("b1")); - SerdNode* l1 = serd_node_new(NULL, serd_a_blank_string("l1")); - SerdNode* l2 = serd_node_new(NULL, serd_a_blank_string("l2")); - SerdNode* s1 = serd_node_new(NULL, serd_a_string("s1")); - SerdNode* s2 = serd_node_new(NULL, serd_a_string("s2")); + const SerdNode* b1 = serd_nodes_get(nodes, serd_a_blank_string("b1")); + const SerdNode* l1 = serd_nodes_get(nodes, serd_a_blank_string("l1")); + const SerdNode* l2 = serd_nodes_get(nodes, serd_a_blank_string("l2")); + const SerdNode* s1 = serd_nodes_get(nodes, serd_a_string("s1")); + const SerdNode* s2 = serd_nodes_get(nodes, serd_a_string("s2")); - SerdNode* rdf_first = serd_node_new(NULL, serd_a_uri_string(NS_RDF "first")); - SerdNode* rdf_value = serd_node_new(NULL, serd_a_uri_string(NS_RDF "value")); - SerdNode* rdf_rest = serd_node_new(NULL, serd_a_uri_string(NS_RDF "rest")); - SerdNode* rdf_nil = serd_node_new(NULL, serd_a_uri_string(NS_RDF "nil")); + const SerdNode* rdf_first = + serd_nodes_get(nodes, serd_a_uri_string(NS_RDF "first")); + + const SerdNode* rdf_value = + serd_nodes_get(nodes, serd_a_uri_string(NS_RDF "value")); + + const SerdNode* rdf_rest = + serd_nodes_get(nodes, serd_a_uri_string(NS_RDF "rest")); + + const SerdNode* rdf_nil = + serd_nodes_get(nodes, serd_a_uri_string(NS_RDF "nil")); serd_env_set_prefix(env, serd_string("rdf"), serd_string(NS_RDF)); @@ -91,17 +101,9 @@ test(void) check_output(writer, &buffer, "[] rdf:value ( \"s1\" \"s2\" ) .\n"); serd_writer_free(writer); - serd_node_free(NULL, rdf_nil); - serd_node_free(NULL, rdf_rest); - serd_node_free(NULL, rdf_value); - serd_node_free(NULL, rdf_first); - serd_node_free(NULL, s2); - serd_node_free(NULL, s1); - serd_node_free(NULL, l2); - serd_node_free(NULL, l1); - serd_node_free(NULL, b1); serd_close_output(&output); serd_free(NULL, buffer.buf); + serd_nodes_free(nodes); serd_env_free(env); serd_world_free(world); diff --git a/test/test_writer.c b/test/test_writer.c index 7e72ec5c..a80783f6 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -163,6 +163,7 @@ static void test_write_long_literal(void) { SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); SerdBuffer buffer = {NULL, NULL, 0}; SerdOutputStream output = serd_open_output_buffer(&buffer); @@ -171,20 +172,21 @@ test_write_long_literal(void) serd_writer_new(world, SERD_TURTLE, 0U, env, &output, 1U); assert(writer); - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); - SerdNode* o = - serd_node_new(NULL, - serd_a_literal(serd_string("hello \"\"\"world\"\"\"!"), - SERD_IS_LONG, - serd_empty_string())); + const SerdNode* s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); + + const SerdNode* o = + serd_nodes_get(nodes, + serd_a_literal(serd_string("hello \"\"\"world\"\"\"!"), + SERD_IS_LONG, + serd_empty_string())); assert(serd_node_flags(o) & SERD_IS_LONG); assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL)); - serd_node_free(NULL, o); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_writer_free(writer); serd_close_output(&output); serd_env_free(env); @@ -219,6 +221,7 @@ test_writer_cleanup(void) { SerdStatus st = SERD_SUCCESS; SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); SerdOutputStream output = serd_open_output_stream(null_sink, NULL, NULL, NULL); @@ -228,9 +231,13 @@ test_writer_cleanup(void) const SerdSink* sink = serd_writer_sink(writer); - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); - SerdNode* o = serd_node_new(NULL, serd_a_blank_string("start")); + const SerdNode* const s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* const p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); + + const SerdNode* o = serd_nodes_get(nodes, serd_a_string("start")); st = serd_sink_write(sink, SERD_ANON_O, s, p, o, NULL); assert(!st); @@ -240,26 +247,21 @@ test_writer_cleanup(void) char buf[12] = {0}; snprintf(buf, sizeof(buf), "b%u", i); - SerdNode* next_o = serd_node_new(NULL, serd_a_blank_string(buf)); + const SerdNode* next_o = serd_nodes_get(nodes, serd_a_blank_string(buf)); st = serd_sink_write(sink, SERD_ANON_O, o, p, next_o, NULL); - - serd_node_free(NULL, o); - o = next_o; + o = next_o; } // Finish writing without terminating nodes assert(!(st = serd_writer_finish(writer))); // Set the base to an empty URI - SerdNode* empty_uri = serd_node_new(NULL, serd_a_uri_string("")); + const SerdNode* const empty_uri = + serd_nodes_get(nodes, serd_a_uri_string("")); assert(!(st = serd_sink_write_base(sink, empty_uri))); - serd_node_free(NULL, empty_uri); // Free (which could leak if the writer doesn't clean up the stack properly) - serd_node_free(NULL, o); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_writer_free(writer); serd_env_free(env); serd_world_free(world); @@ -347,9 +349,9 @@ test_write_error(void) static void test_writer_stack_overflow(void) { - SerdWorld* world = serd_world_new(NULL); - SerdEnv* env = serd_env_new(NULL, serd_empty_string()); - + SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); + SerdEnv* env = serd_env_new(NULL, serd_empty_string()); SerdOutputStream output = serd_open_output_stream(null_sink, NULL, NULL, NULL); @@ -358,12 +360,14 @@ test_writer_stack_overflow(void) const SerdSink* sink = serd_writer_sink(writer); - SerdNode* const s = - serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* const p = - serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); + const SerdNode* const s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* const p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); + + const SerdNode* o = serd_nodes_get(nodes, serd_a_blank_string("blank")); - SerdNode* o = serd_node_new(NULL, serd_a_blank_string("blank")); SerdStatus st = serd_sink_write(sink, SERD_ANON_O, s, p, o, NULL); assert(!st); @@ -372,12 +376,10 @@ test_writer_stack_overflow(void) char buf[1024]; snprintf(buf, sizeof(buf), "b%u", i); - SerdNode* next_o = serd_node_new(NULL, serd_a_blank_string(buf)); + const SerdNode* next_o = serd_nodes_get(nodes, serd_a_blank_string(buf)); st = serd_sink_write(sink, SERD_ANON_O, o, p, next_o, NULL); - - serd_node_free(NULL, o); - o = next_o; + o = next_o; if (st) { assert(st == SERD_BAD_STACK); @@ -387,9 +389,6 @@ test_writer_stack_overflow(void) assert(st == SERD_BAD_STACK); - serd_node_free(NULL, o); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_writer_free(writer); serd_close_output(&output); serd_env_free(env); @@ -400,11 +399,17 @@ static void test_write_empty_syntax(void) { SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); - SerdNode* o = serd_node_new(NULL, serd_a_uri_string("http://example.org/o")); + const SerdNode* s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); + + const SerdNode* o = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/o")); SerdBuffer buffer = {NULL, NULL, 0}; SerdOutputStream output = serd_open_output_buffer(&buffer); @@ -423,9 +428,6 @@ test_write_empty_syntax(void) serd_free(NULL, buffer.buf); serd_writer_free(writer); - serd_node_free(NULL, o); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_close_output(&output); serd_env_free(env); serd_world_free(world); @@ -435,6 +437,7 @@ static void check_pname_escape(const char* const lname, const char* const expected) { SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); SerdBuffer buffer = {NULL, NULL, 0}; SerdOutputStream output = serd_open_output_buffer(&buffer); @@ -448,20 +451,21 @@ check_pname_escape(const char* const lname, const char* const expected) serd_env_set_prefix(env, serd_string("eg"), serd_string(prefix)); - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); + const SerdNode* s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); char* const uri = (char*)calloc(1, prefix_len + strlen(lname) + 1); memcpy(uri, prefix, prefix_len + 1); memcpy(uri + prefix_len, lname, strlen(lname) + 1); - SerdNode* node = serd_node_new(NULL, serd_a_uri_string(uri)); + const SerdNode* node = serd_nodes_get(nodes, serd_a_uri_string(uri)); + assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, node, NULL)); - serd_node_free(NULL, node); free(uri); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_writer_free(writer); serd_close_output(&output); serd_env_free(env); @@ -509,10 +513,16 @@ static void test_write_bad_uri(void) { SerdWorld* world = serd_world_new(NULL); + SerdNodes* nodes = serd_world_nodes(world); SerdEnv* env = serd_env_new(NULL, serd_empty_string()); - SerdNode* s = serd_node_new(NULL, serd_a_uri_string("http://example.org/s")); - SerdNode* p = serd_node_new(NULL, serd_a_uri_string("http://example.org/p")); - SerdNode* rel = serd_node_new(NULL, serd_a_uri_string("rel")); + + const SerdNode* s = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/s")); + + const SerdNode* p = + serd_nodes_get(nodes, serd_a_uri_string("http://example.org/p")); + + const SerdNode* rel = serd_nodes_get(nodes, serd_a_uri_string("rel")); SerdBuffer buffer = {NULL, NULL, 0}; SerdOutputStream output = serd_open_output_buffer(&buffer); @@ -530,9 +540,6 @@ test_write_bad_uri(void) serd_writer_free(writer); serd_close_output(&output); serd_free(NULL, buffer.buf); - serd_node_free(NULL, rel); - serd_node_free(NULL, p); - serd_node_free(NULL, s); serd_env_free(env); serd_world_free(world); } |