diff options
Diffstat (limited to 'test/test_env.c')
-rw-r--r-- | test/test_env.c | 202 |
1 files changed, 163 insertions, 39 deletions
diff --git a/test/test_env.c b/test/test_env.c index 33e41af4..89947826 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -3,6 +3,8 @@ #undef NDEBUG +#include "failing_allocator.h" + #include "serd/env.h" #include "serd/event.h" #include "serd/node.h" @@ -11,21 +13,139 @@ #include "serd/string_view.h" #include <assert.h> +#include <stdio.h> #include <string.h> #define NS_EG "http://example.org/" static void +test_new_failed_alloc(void) +{ + SerdFailingAllocator allocator = serd_failing_allocator(); + + // Successfully allocate a env to count the number of allocations + SerdEnv* const env = serd_env_new(&allocator.base, serd_empty_string()); + assert(env); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_env_new(&allocator.base, serd_empty_string())); + } + + serd_env_free(env); +} + +static void +test_copy_failed_alloc(void) +{ + static const char name[] = "eg"; + static const char uri[] = "http://example.org/"; + + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdEnv* const env = serd_env_new(&allocator.base, serd_empty_string()); + + assert(!serd_env_set_prefix(env, serd_string(name), serd_string(uri))); + assert(!serd_env_set_base_uri(env, serd_string(uri))); + + // Successfully copy an env to count the number of allocations + const size_t n_setup_allocs = allocator.n_allocations; + SerdEnv* const copy = serd_env_copy(&allocator.base, env); + assert(copy); + + // Test that each allocation failing is handled gracefully + const size_t n_copy_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_copy_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_env_copy(&allocator.base, env)); + } + + serd_env_free(copy); + serd_env_free(env); +} + +static void +test_set_prefix_absolute_failed_alloc(void) +{ + const SerdStringView base_uri = serd_string("http://example.org/"); + + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdEnv* const env = serd_env_new(&allocator.base, base_uri); + + char name[64] = "eg"; + char uri[64] = "http://example.org/"; + + SerdStatus st = SERD_SUCCESS; + const size_t n_setup_allocs = allocator.n_allocations; + + // Successfully set an absolute prefix to count the number of allocations + st = serd_env_set_prefix(env, serd_string(name), serd_string(uri)); + assert(st == SERD_SUCCESS); + + // Test that each allocation failing is handled gracefully + const size_t n_set_prefix_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_set_prefix_allocs; ++i) { + allocator.n_remaining = i; + + snprintf(name, sizeof(name), "eg%zu", i); + snprintf(uri, sizeof(name), "http://example.org/%zu", i); + + st = serd_env_set_prefix(env, serd_string(name), serd_string(uri)); + assert(st == SERD_BAD_ALLOC); + } + + serd_env_free(env); +} + +static void +test_set_prefix_relative_failed_alloc(void) +{ + const SerdStringView base_uri = serd_string("http://example.org/"); + + SerdFailingAllocator allocator = serd_failing_allocator(); + + char name[64] = "egX"; + char uri[64] = "relativeX"; + + // Successfully set an absolute prefix to count the number of allocations + SerdEnv* env = serd_env_new(&allocator.base, base_uri); + SerdStatus st = serd_env_set_prefix(env, serd_string(name), serd_string(uri)); + assert(st == SERD_SUCCESS); + serd_env_free(env); + + // Test that each allocation failing is handled gracefully + const size_t n_set_prefix_allocs = allocator.n_allocations; + for (size_t i = 0; i < n_set_prefix_allocs; ++i) { + allocator.n_remaining = i; + + snprintf(name, sizeof(name), "eg%zu", i); + snprintf(uri, sizeof(uri), "relative%zu", i); + + env = serd_env_new(&allocator.base, base_uri); + if (env) { + st = serd_env_set_prefix(env, serd_string(name), serd_string(uri)); + assert(st == SERD_BAD_ALLOC); + } + + serd_env_free(env); + } +} + +static void test_copy(void) { - assert(!serd_env_copy(NULL)); + assert(!serd_env_copy(NULL, NULL)); - SerdEnv* const env = serd_env_new(serd_string("http://example.org/base/")); + SerdEnv* const env = + serd_env_new(NULL, serd_string("http://example.org/base/")); serd_env_set_prefix( env, serd_string("eg"), serd_string("http://example.org/")); - SerdEnv* const env_copy = serd_env_copy(env); + SerdEnv* const env_copy = serd_env_copy(NULL, env); assert(serd_env_equals(env, env_copy)); @@ -46,7 +166,7 @@ test_copy(void) static void test_comparison(void) { - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_equals(env, NULL)); assert(!serd_env_equals(NULL, env)); @@ -60,7 +180,7 @@ static void test_null(void) { // "Copying" NULL returns null - assert(!serd_env_copy(NULL)); + assert(!serd_env_copy(NULL, NULL)); // Accessors are tolerant to a NULL env for convenience assert(!serd_env_base_uri(NULL)); @@ -81,8 +201,8 @@ count_prefixes(void* handle, const SerdEvent* event) static void test_base_uri(void) { - SerdEnv* const env = serd_env_new(serd_empty_string()); - SerdNode* const eg = serd_new_uri(serd_string(NS_EG)); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); + SerdNode* const eg = serd_new_uri(NULL, serd_string(NS_EG)); // Test that invalid calls work as expected assert(!serd_env_base_uri(env)); @@ -101,8 +221,8 @@ test_base_uri(void) assert(!serd_env_set_base_uri(env, serd_empty_string())); assert(!serd_env_base_uri(env)); + serd_node_free(NULL, eg); serd_env_free(env); - serd_node_free(eg); } static void @@ -114,7 +234,7 @@ test_set_prefix(void) const SerdStringView rel = serd_string("rel"); const SerdStringView base = serd_string("http://example.org/"); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); // Set a valid prefix assert(!serd_env_set_prefix(env, name1, eg)); @@ -130,7 +250,7 @@ test_set_prefix(void) size_t n_prefixes = 0; SerdSink* const count_prefixes_sink = - serd_sink_new(&n_prefixes, count_prefixes, NULL); + serd_sink_new(NULL, &n_prefixes, count_prefixes, NULL); serd_env_write_prefixes(env, count_prefixes_sink); serd_sink_free(count_prefixes_sink); @@ -142,13 +262,13 @@ test_set_prefix(void) static void test_expand_untyped_literal(void) { - SerdNode* const untyped = serd_new_string(serd_string("data")); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdNode* const untyped = serd_new_string(NULL, serd_string("data")); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_expand_node(env, untyped)); serd_env_free(env); - serd_node_free(untyped); + serd_node_free(NULL, untyped); } static void @@ -157,14 +277,14 @@ test_expand_bad_uri_datatype(void) const SerdStringView type = serd_string("Type"); SerdNode* const typed = - serd_new_literal(serd_string("data"), SERD_HAS_DATATYPE, type); + serd_new_literal(NULL, serd_string("data"), SERD_HAS_DATATYPE, type); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_expand_node(env, typed)); serd_env_free(env); - serd_node_free(typed); + serd_node_free(NULL, typed); } static void @@ -172,19 +292,19 @@ test_expand_uri(void) { const SerdStringView base = serd_string("http://example.org/b/"); - SerdEnv* const env = serd_env_new(base); - SerdNode* const rel = serd_new_uri(serd_string("rel")); + SerdEnv* const env = serd_env_new(NULL, base); + SerdNode* const rel = serd_new_uri(NULL, serd_string("rel")); SerdNode* const rel_out = serd_env_expand_node(env, rel); - SerdNode* const empty = serd_new_uri(serd_empty_string()); + SerdNode* const empty = serd_new_uri(NULL, serd_empty_string()); SerdNode* const empty_out = serd_env_expand_node(env, empty); assert(!strcmp(serd_node_string(rel_out), "http://example.org/b/rel")); assert(!strcmp(serd_node_string(empty_out), "http://example.org/b/")); - serd_node_free(empty_out); - serd_node_free(empty); - serd_node_free(rel_out); - serd_node_free(rel); + serd_node_free(NULL, empty_out); + serd_node_free(NULL, empty); + serd_node_free(NULL, rel_out); + serd_node_free(NULL, rel); serd_env_free(env); } @@ -193,27 +313,27 @@ test_expand_empty_uri_ref(void) { const SerdStringView base = serd_string("http://example.org/b/"); - SerdNode* const rel = serd_new_uri(serd_string("rel")); - SerdEnv* const env = serd_env_new(base); + SerdNode* const rel = serd_new_uri(NULL, serd_string("rel")); + SerdEnv* const env = serd_env_new(NULL, base); SerdNode* const rel_out = serd_env_expand_node(env, rel); assert(!strcmp(serd_node_string(rel_out), "http://example.org/b/rel")); - serd_node_free(rel_out); + serd_node_free(NULL, rel_out); serd_env_free(env); - serd_node_free(rel); + serd_node_free(NULL, rel); } static void test_expand_bad_uri(void) { - SerdNode* const bad_uri = serd_new_uri(serd_string("rel")); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdNode* const bad_uri = serd_new_uri(NULL, serd_string("rel")); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_expand_node(env, bad_uri)); serd_env_free(env); - serd_node_free(bad_uri); + serd_node_free(NULL, bad_uri); } static void @@ -222,7 +342,7 @@ test_expand_curie(void) const SerdStringView name = serd_string("eg.1"); const SerdStringView eg = serd_string(NS_EG); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_set_prefix(env, name, eg)); @@ -231,7 +351,7 @@ test_expand_curie(void) assert(expanded); assert(!strcmp(serd_node_string(expanded), "http://example.org/foo")); - serd_node_free(expanded); + serd_node_free(NULL, expanded); serd_env_free(env); } @@ -239,7 +359,7 @@ test_expand_curie(void) static void test_expand_bad_curie(void) { - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_expand_curie(NULL, serd_empty_string())); assert(!serd_env_expand_curie(NULL, serd_string("what:ever"))); @@ -252,13 +372,13 @@ test_expand_bad_curie(void) static void test_expand_blank(void) { - SerdNode* const blank = serd_new_blank(serd_string("b1")); - SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdNode* const blank = serd_new_blank(NULL, serd_string("b1")); + SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); assert(!serd_env_expand_node(env, blank)); serd_env_free(env); - serd_node_free(blank); + serd_node_free(NULL, blank); } static void @@ -268,8 +388,8 @@ test_equals(void) const SerdStringView base1 = serd_string(NS_EG "b1/"); const SerdStringView base2 = serd_string(NS_EG "b2/"); - SerdEnv* const env1 = serd_env_new(base1); - SerdEnv* const env2 = serd_env_new(base2); + SerdEnv* const env1 = serd_env_new(NULL, base1); + SerdEnv* const env2 = serd_env_new(NULL, base2); assert(!serd_env_equals(env1, NULL)); assert(!serd_env_equals(NULL, env1)); @@ -289,7 +409,7 @@ test_equals(void) serd_env_set_base_uri(env2, base2); assert(!serd_env_equals(env1, env2)); - SerdEnv* const env3 = serd_env_copy(env2); + SerdEnv* const env3 = serd_env_copy(NULL, env2); assert(serd_env_equals(env3, env2)); serd_env_free(env3); @@ -300,6 +420,10 @@ test_equals(void) int main(void) { + test_new_failed_alloc(); + test_copy_failed_alloc(); + test_set_prefix_absolute_failed_alloc(); + test_set_prefix_relative_failed_alloc(); test_copy(); test_comparison(); test_null(); |