diff options
Diffstat (limited to 'test/test_model.c')
-rw-r--r-- | test/test_model.c | 87 |
1 files changed, 63 insertions, 24 deletions
diff --git a/test/test_model.c b/test/test_model.c index 72db88ae..827bd638 100644 --- a/test/test_model.c +++ b/test/test_model.c @@ -16,14 +16,16 @@ #undef NDEBUG +#include "failing_allocator.h" + +#include "serd/serd.h" + #include <assert.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "serd/serd.h" - #define WILDCARD_NODE NULL #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -147,7 +149,9 @@ test_read(SerdWorld* world, const SerdNode* g, const unsigned n_quads) { - SerdNodes* const nodes = serd_nodes_new(); + SerdAllocator* const allocator = serd_default_allocator(); + + SerdNodes* const nodes = serd_nodes_new(allocator); SerdCursor* cursor = serd_model_begin(model); const SerdStatement* prev = NULL; @@ -345,6 +349,32 @@ ignore_only_index_error(void* const handle, } static int +test_failed_new_alloc(SerdWorld* ignored_world, const unsigned n_quads) +{ + (void)ignored_world; + (void)n_quads; + + SerdFailingAllocator allocator = serd_failing_allocator(); + SerdWorld* const world = serd_world_new(&allocator.base); + const size_t n_world_allocs = allocator.n_allocations; + + // Successfully allocate a model to count the number of allocations + SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); + assert(model); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations - n_world_allocs; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_model_new(world, SERD_ORDER_SPO, 0u)); + } + + serd_model_free(model); + serd_world_free(world); + return 0; +} + +static int test_free_null(SerdWorld* world, const unsigned n_quads) { (void)world; @@ -564,7 +594,9 @@ test_inserter(SerdWorld* world, const unsigned n_quads) { (void)n_quads; - SerdNodes* const nodes = serd_nodes_new(); + SerdAllocator* const allocator = serd_default_allocator(); + + SerdNodes* const nodes = serd_nodes_new(allocator); SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); SerdSink* inserter = serd_inserter_new(model, NULL); @@ -615,7 +647,9 @@ test_erase_with_iterator(SerdWorld* world, const unsigned n_quads) assert(serd_cursor_advance(iter2) == SERD_BAD_CURSOR); // Check that erasing the end iterator does nothing - SerdCursor* const end = serd_cursor_copy(serd_model_end(model)); + SerdCursor* const end = + serd_cursor_copy(serd_world_allocator(world), serd_model_end(model)); + assert(serd_model_erase(model, end) == SERD_FAILURE); serd_cursor_free(end); @@ -630,7 +664,9 @@ test_add_erase(SerdWorld* world, const unsigned n_quads) { (void)n_quads; - SerdNodes* const nodes = serd_nodes_new(); + SerdAllocator* const allocator = serd_default_allocator(); + + SerdNodes* const nodes = serd_nodes_new(allocator); SerdModel* const model = serd_model_new(world, SERD_ORDER_SPO, 0u); // Add (s p "hello") @@ -667,7 +703,9 @@ test_add_bad_statement(SerdWorld* world, const unsigned n_quads) { (void)n_quads; - SerdNodes* const nodes = serd_nodes_new(); + SerdAllocator* const allocator = serd_world_allocator(world); + + SerdNodes* const nodes = serd_nodes_new(allocator); const SerdNode* s = serd_nodes_uri(nodes, SERD_STRING("urn:s")); const SerdNode* p = serd_nodes_uri(nodes, SERD_STRING("urn:p")); const SerdNode* o = serd_nodes_uri(nodes, SERD_STRING("urn:o")); @@ -675,7 +713,7 @@ test_add_bad_statement(SerdWorld* world, const unsigned n_quads) const SerdNode* f = serd_nodes_uri(nodes, SERD_STRING("file:///tmp/file.ttl")); - SerdCaret* caret = serd_caret_new(f, 16, 18); + SerdCaret* caret = serd_caret_new(allocator, f, 16, 18); SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); assert(!serd_model_add_with_caret(model, s, p, o, NULL, caret)); @@ -699,7 +737,7 @@ test_add_bad_statement(SerdWorld* world, const unsigned n_quads) serd_cursor_free(begin); serd_model_free(model); - serd_caret_free(caret); + serd_caret_free(allocator, caret); serd_nodes_free(nodes); return 0; } @@ -709,7 +747,7 @@ test_add_with_caret(SerdWorld* world, const unsigned n_quads) { (void)n_quads; - SerdNodes* const nodes = serd_nodes_new(); + SerdNodes* const nodes = serd_nodes_new(serd_world_allocator(world)); const SerdNode* lit = serd_nodes_string(nodes, SERD_STRING("string")); const SerdNode* uri = serd_nodes_uri(nodes, SERD_STRING("urn:uri")); const SerdNode* blank = serd_nodes_blank(nodes, SERD_STRING("b1")); @@ -762,7 +800,7 @@ test_copy(SerdWorld* world, const unsigned n_quads) SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); generate(world, model, n_quads, NULL); - SerdModel* copy = serd_model_copy(model); + SerdModel* copy = serd_model_copy(serd_world_allocator(world), model); assert(serd_model_equals(model, copy)); serd_model_free(model); @@ -1039,7 +1077,7 @@ test_write_flat_range(SerdWorld* world, const unsigned n_quads) (void)n_quads; SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, SERD_STORE_GRAPHS); - SerdNodes* nodes = serd_nodes_new(); + SerdNodes* nodes = serd_nodes_new(serd_world_allocator(world)); const SerdNode* s = serd_nodes_uri(nodes, SERD_STRING("urn:s")); const SerdNode* p = serd_nodes_uri(nodes, SERD_STRING("urn:p")); @@ -1052,7 +1090,7 @@ test_write_flat_range(SerdWorld* world, const unsigned n_quads) serd_model_add(model, s, p, b2, NULL); serd_model_add(model, b2, p, o, NULL); - SerdBuffer buffer = {NULL, 0}; + SerdBuffer buffer = {NULL, NULL, 0}; SerdEnv* env = serd_env_new(world, SERD_EMPTY_STRING()); SerdOutputStream out = serd_open_output_buffer(&buffer); @@ -1083,7 +1121,7 @@ test_write_flat_range(SerdWorld* world, const unsigned n_quads) assert(str); assert(!strcmp(str, expected)); - serd_free(buffer.buf); + serd_free(NULL, buffer.buf); serd_writer_free(writer); serd_model_free(model); serd_env_free(env); @@ -1097,7 +1135,7 @@ test_write_bad_list(SerdWorld* world, const unsigned n_quads) (void)n_quads; SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, SERD_STORE_GRAPHS); - SerdNodes* nodes = serd_nodes_new(); + SerdNodes* nodes = serd_nodes_new(serd_world_allocator(world)); serd_model_add_index(model, SERD_ORDER_OPS); @@ -1123,7 +1161,7 @@ test_write_bad_list(SerdWorld* world, const unsigned n_quads) serd_model_add(model, list2, prest, norest, NULL); serd_model_add(model, norest, pfirst, val2, NULL); - SerdBuffer buffer = {NULL, 0}; + SerdBuffer buffer = {NULL, NULL, 0}; SerdEnv* env = serd_env_new(world, SERD_EMPTY_STRING()); SerdOutputStream out = serd_open_output_buffer(&buffer); @@ -1149,7 +1187,7 @@ test_write_bad_list(SerdWorld* world, const unsigned n_quads) assert(str); assert(!strcmp(str, expected)); - free(buffer.buf); + serd_free(NULL, buffer.buf); serd_writer_free(writer); serd_close_output(&out); serd_model_free(model); @@ -1164,7 +1202,7 @@ test_write_infinite_list(SerdWorld* world, const unsigned n_quads) (void)n_quads; SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, SERD_STORE_GRAPHS); - SerdNodes* nodes = serd_nodes_new(); + SerdNodes* nodes = serd_nodes_new(serd_world_allocator(world)); serd_model_add_index(model, SERD_ORDER_OPS); @@ -1185,7 +1223,7 @@ test_write_infinite_list(SerdWorld* world, const unsigned n_quads) serd_model_add(model, list2, pfirst, val2, NULL); serd_model_add(model, list2, prest, list1, NULL); - SerdBuffer buffer = {NULL, 0}; + SerdBuffer buffer = {NULL, NULL, 0}; SerdEnv* env = serd_env_new(world, SERD_EMPTY_STRING()); SerdOutputStream out = serd_open_output_buffer(&buffer); @@ -1217,7 +1255,7 @@ test_write_infinite_list(SerdWorld* world, const unsigned n_quads) assert(str); assert(!strcmp(str, expected)); - free(buffer.buf); + serd_free(NULL, buffer.buf); serd_writer_free(writer); serd_close_output(&out); serd_model_free(model); @@ -1253,7 +1291,7 @@ test_write_error_in_list_subject(SerdWorld* world, const unsigned n_quads) serd_set_log_func(world, expected_error, NULL); SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); - SerdNodes* nodes = serd_nodes_new(); + SerdNodes* nodes = serd_nodes_new(serd_world_allocator(world)); serd_model_add_index(model, SERD_ORDER_OPS); @@ -1310,7 +1348,7 @@ test_write_error_in_list_object(SerdWorld* world, const unsigned n_quads) serd_set_log_func(world, expected_error, NULL); SerdModel* model = serd_model_new(world, SERD_ORDER_SPO, 0u); - SerdNodes* nodes = serd_nodes_new(); + SerdNodes* nodes = serd_nodes_new(serd_world_allocator(world)); serd_model_add_index(model, SERD_ORDER_OPS); @@ -1367,7 +1405,8 @@ main(void) typedef int (*TestFunc)(SerdWorld*, unsigned); - const TestFunc tests[] = {test_free_null, + const TestFunc tests[] = {test_failed_new_alloc, + test_free_null, test_get_world, test_get_default_order, test_get_flags, @@ -1401,7 +1440,7 @@ main(void) test_write_error_in_list_object, NULL}; - SerdWorld* world = serd_world_new(); + SerdWorld* world = serd_world_new(NULL); int ret = 0; for (const TestFunc* t = tests; *t; ++t) { |