diff options
Diffstat (limited to 'test/test_node_syntax.c')
-rw-r--r-- | test/test_node_syntax.c | 143 |
1 files changed, 97 insertions, 46 deletions
diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c index d9b0afbb..582ba8aa 100644 --- a/test/test_node_syntax.c +++ b/test/test_node_syntax.c @@ -16,12 +16,53 @@ #undef NDEBUG +#include "failing_allocator.h" + #include "serd/serd.h" #include <assert.h> #include <stdbool.h> #include <string.h> +static void +test_failed_alloc(void) +{ + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdNode* const node = serd_new_string(&allocator.base, SERD_STRING("node")); + + // Successfully convert a node to count the number of allocations + + const size_t n_setup_allocs = allocator.n_allocations; + + char* const str = + serd_node_to_syntax(&allocator.base, node, SERD_TURTLE, NULL); + + SerdNode* const copy = + serd_node_from_syntax(&allocator.base, str, SERD_TURTLE, NULL); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + + char* const s = + serd_node_to_syntax(&allocator.base, node, SERD_TURTLE, NULL); + + SerdNode* const c = + serd_node_from_syntax(&allocator.base, str, SERD_TURTLE, NULL); + + assert(!s || !c); + + serd_free(&allocator.base, s); + serd_node_free(&allocator.base, c); + } + + serd_node_free(&allocator.base, copy); + serd_free(&allocator.base, str); + serd_node_free(&allocator.base, node); +} + static bool check(SerdWorld* const world, const SerdSyntax syntax, @@ -31,14 +72,14 @@ check(SerdWorld* const world, SerdEnv* const env = serd_env_new(world, SERD_STRING("http://example.org/base/")); - char* const str = serd_node_to_syntax(node, syntax, env); - SerdNode* const copy = serd_node_from_syntax(str, syntax, env); + char* const str = serd_node_to_syntax(NULL, node, syntax, env); + SerdNode* const copy = serd_node_from_syntax(NULL, str, syntax, env); const bool success = !strcmp(str, expected) && serd_node_equals(copy, node); - serd_node_free(copy); - serd_free(str); - serd_node_free(node); + serd_node_free(serd_world_allocator(world), copy); + serd_free(serd_world_allocator(world), str); + serd_node_free(NULL, node); serd_env_free(env); return success; } @@ -54,107 +95,111 @@ test_common(SerdWorld* const world, const SerdSyntax syntax) static const SerdStringView num_type = SERD_STRING("http://example.org/Decimal"); + assert(check( + world, syntax, serd_new_string(NULL, SERD_STRING("node")), "\"node\"")); + assert( - check(world, syntax, serd_new_string(SERD_STRING("node")), "\"node\"")); + check(world, + syntax, + serd_new_literal( + NULL, SERD_STRING("hallo"), SERD_HAS_LANGUAGE, SERD_STRING("de")), + "\"hallo\"@de")); - assert(check(world, - syntax, - serd_new_literal( - SERD_STRING("hallo"), SERD_HAS_LANGUAGE, SERD_STRING("de")), - "\"hallo\"@de")); + assert( + check(world, + syntax, + serd_new_literal(NULL, SERD_STRING("X"), SERD_HAS_DATATYPE, datatype), + "\"X\"^^<http://example.org/Datatype>")); assert(check(world, syntax, - serd_new_literal(SERD_STRING("X"), SERD_HAS_DATATYPE, datatype), - "\"X\"^^<http://example.org/Datatype>")); + serd_new_token(NULL, SERD_BLANK, SERD_STRING("blank")), + "_:blank")); assert(check(world, syntax, - serd_new_token(SERD_BLANK, SERD_STRING("blank")), - "_:blank")); - - assert(check( - world, syntax, serd_new_token(SERD_BLANK, SERD_STRING("b0")), "_:b0")); + serd_new_token(NULL, SERD_BLANK, SERD_STRING("b0")), + "_:b0")); assert(check(world, syntax, - serd_new_token(SERD_BLANK, SERD_STRING("named1")), + serd_new_token(NULL, SERD_BLANK, SERD_STRING("named1")), "_:named1")); assert(check(world, syntax, - serd_new_uri(SERD_STRING("http://example.org/")), + serd_new_uri(NULL, SERD_STRING("http://example.org/")), "<http://example.org/>")); assert(check(world, syntax, - serd_new_double(1.25), + serd_new_double(NULL, 1.25), "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#double>")); assert(check(world, syntax, - serd_new_float(1.25), + serd_new_float(NULL, 1.25), "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#float>")); assert(check(world, syntax, - serd_new_integer(1234, num_type), + serd_new_integer(NULL, 1234, num_type), "\"1234\"^^<http://example.org/Decimal>")); assert( check(world, syntax, - serd_new_base64(data, sizeof(data), SERD_EMPTY_STRING()), + serd_new_base64(NULL, data, sizeof(data), SERD_EMPTY_STRING()), "\"BAAAAAIAAAA=\"^^<http://www.w3.org/2001/XMLSchema#base64Binary>")); } static void test_ntriples(void) { - SerdWorld* const world = serd_world_new(); + SerdWorld* const world = serd_world_new(NULL); test_common(world, SERD_NTRIPLES); { // No relative URIs in NTriples, so converting one fails without an env - SerdNode* const rel = serd_new_uri(SERD_STRING("rel/uri")); - assert(!serd_node_to_syntax(rel, SERD_NTRIPLES, NULL)); - assert(!serd_node_from_syntax("<rel/uri>", SERD_NTRIPLES, NULL)); + SerdNode* const rel = serd_new_uri(NULL, SERD_STRING("rel/uri")); + assert(!serd_node_to_syntax(NULL, rel, SERD_NTRIPLES, NULL)); + assert(!serd_node_from_syntax(NULL, "<rel/uri>", SERD_NTRIPLES, NULL)); // If a relative URI can be expanded then all's well SerdEnv* const env = serd_env_new(world, SERD_STRING("http://example.org/base/")); - char* const str = serd_node_to_syntax(rel, SERD_NTRIPLES, env); + char* const str = serd_node_to_syntax(NULL, rel, SERD_NTRIPLES, env); assert(!strcmp(str, "<http://example.org/base/rel/uri>")); - SerdNode* const copy = serd_node_from_syntax(str, SERD_NTRIPLES, env); + SerdNode* const copy = serd_node_from_syntax(NULL, str, SERD_NTRIPLES, env); assert(!strcmp(serd_node_string(copy), "http://example.org/base/rel/uri")); - serd_node_free(copy); + serd_node_free(serd_world_allocator(world), copy); serd_env_free(env); - serd_free(str); - serd_node_free(rel); + serd_free(serd_world_allocator(world), str); + serd_node_free(NULL, rel); } assert(check(world, SERD_NTRIPLES, - serd_new_decimal(1.25), + serd_new_decimal(NULL, 1.25), "\"1.25\"^^<http://www.w3.org/2001/XMLSchema#decimal>")); assert(check(world, SERD_NTRIPLES, - serd_new_integer(1234, SERD_EMPTY_STRING()), + serd_new_integer(NULL, 1234, SERD_EMPTY_STRING()), "\"1234\"^^<http://www.w3.org/2001/XMLSchema#integer>")); assert(check(world, SERD_NTRIPLES, - serd_new_boolean(true), + serd_new_boolean(NULL, true), "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>")); assert(check(world, SERD_NTRIPLES, - serd_new_boolean(false), + serd_new_boolean(NULL, false), "\"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>")); serd_world_free(world); @@ -166,22 +211,27 @@ test_turtle(void) static const SerdStringView xsd_integer = SERD_STRING("http://www.w3.org/2001/XMLSchema#integer"); - SerdWorld* const world = serd_world_new(); + SerdWorld* const world = serd_world_new(NULL); test_common(world, SERD_TURTLE); - check(world, SERD_TURTLE, serd_new_uri(SERD_STRING("rel/uri")), "<rel/uri>"); + check(world, + SERD_TURTLE, + serd_new_uri(NULL, SERD_STRING("rel/uri")), + "<rel/uri>"); - assert(check(world, SERD_TURTLE, serd_new_decimal(1.25), "1.25")); + assert(check(world, SERD_TURTLE, serd_new_decimal(NULL, 1.25), "1.25")); - assert(check( - world, SERD_TURTLE, serd_new_integer(1234, SERD_EMPTY_STRING()), "1234")); + assert(check(world, + SERD_TURTLE, + serd_new_integer(NULL, 1234, SERD_EMPTY_STRING()), + "1234")); - assert( - check(world, SERD_TURTLE, serd_new_integer(1234, xsd_integer), "1234")); + assert(check( + world, SERD_TURTLE, serd_new_integer(NULL, 1234, xsd_integer), "1234")); - assert(check(world, SERD_TURTLE, serd_new_boolean(true), "true")); - assert(check(world, SERD_TURTLE, serd_new_boolean(false), "false")); + assert(check(world, SERD_TURTLE, serd_new_boolean(NULL, true), "true")); + assert(check(world, SERD_TURTLE, serd_new_boolean(NULL, false), "false")); serd_world_free(world); } @@ -189,6 +239,7 @@ test_turtle(void) int main(void) { + test_failed_alloc(); test_ntriples(); test_turtle(); |