diff options
author | David Robillard <d@drobilla.net> | 2023-09-10 15:06:42 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 248a874d7425749d29cf900a1c3783c624ea8d8c (patch) | |
tree | aed59f5a484a815cd254506866e98a947858904d /test/test_node.c | |
parent | 0bd10132c6707353dba80bd89cf0102ee7ca4e34 (diff) | |
download | serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.gz serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.bz2 serd-248a874d7425749d29cf900a1c3783c624ea8d8c.zip |
Add support for custom allocators
This makes it explicit in the API where memory is allocated, and allows the
user to provide a custom allocator to avoid the use of the default system
allocator for whatever reason.
Diffstat (limited to 'test/test_node.c')
-rw-r--r-- | test/test_node.c | 218 |
1 files changed, 119 insertions, 99 deletions
diff --git a/test/test_node.c b/test/test_node.c index 07ea4973..23d1a45a 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -3,7 +3,6 @@ #undef NDEBUG -#include "serd/memory.h" #include "serd/node.h" #include "serd/status.h" #include "serd/string_view.h" @@ -51,23 +50,23 @@ static void test_boolean(void) { - SerdNode* const true_node = serd_new_boolean(true); + SerdNode* const true_node = serd_new_boolean(NULL, true); assert(!strcmp(serd_node_string(true_node), "true")); assert(serd_get_boolean(true_node)); const SerdNode* const true_datatype = serd_node_datatype(true_node); assert(true_datatype); assert(!strcmp(serd_node_string(true_datatype), NS_XSD "boolean")); - serd_node_free(true_node); + serd_node_free(NULL, true_node); - SerdNode* const false_node = serd_new_boolean(false); + SerdNode* const false_node = serd_new_boolean(NULL, false); assert(!strcmp(serd_node_string(false_node), "false")); assert(!serd_get_boolean(false_node)); const SerdNode* const false_datatype = serd_node_datatype(false_node); assert(false_datatype); assert(!strcmp(serd_node_string(false_datatype), NS_XSD "boolean")); - serd_node_free(false_node); + serd_node_free(NULL, false_node); } static void @@ -76,12 +75,12 @@ check_get_boolean(const char* string, const bool expected) { SerdNode* const node = serd_new_literal( - serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); + NULL, serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); assert(node); assert(serd_get_boolean(node) == expected); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -115,7 +114,7 @@ test_decimal(void) "0.0000000001"}; for (size_t i = 0; i < sizeof(test_values) / sizeof(double); ++i) { - SerdNode* node = serd_new_decimal(test_values[i], NULL); + SerdNode* node = serd_new_decimal(NULL, test_values[i], NULL); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); @@ -128,7 +127,7 @@ test_decimal(void) const double value = serd_get_double(node); assert(!memcmp(&value, &test_values[i], sizeof(value))); - serd_node_free(node); + serd_node_free(NULL, node); } } @@ -140,7 +139,7 @@ test_double(void) "0.0E0", "-0.0E0", "1.2E0", "-2.3E0", "4.56789E6"}; for (size_t i = 0; i < sizeof(test_values) / sizeof(double); ++i) { - SerdNode* node = serd_new_double(test_values[i]); + SerdNode* node = serd_new_double(NULL, test_values[i]); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); @@ -153,7 +152,7 @@ test_double(void) const double value = serd_get_double(node); assert(!memcmp(&value, &test_values[i], sizeof(value))); - serd_node_free(node); + serd_node_free(NULL, node); } } @@ -163,14 +162,14 @@ check_get_double(const char* string, const double expected) { SerdNode* const node = serd_new_literal( - serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); + NULL, serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); assert(node); const double value = serd_get_double(node); assert(!memcmp(&value, &expected, sizeof(value))); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -187,20 +186,22 @@ test_get_double(void) SERD_DISABLE_CONVERSION_WARNINGS - SerdNode* const nan = serd_new_string(serd_string("unknown")); + SerdNode* const nan = serd_new_string(NULL, serd_string("unknown")); assert(isnan(serd_get_double(nan))); - serd_node_free(nan); + serd_node_free(NULL, nan); - SerdNode* const invalid = serd_new_literal( - serd_string("!invalid"), SERD_HAS_DATATYPE, serd_string(NS_XSD "long")); + SerdNode* const invalid = serd_new_literal(NULL, + serd_string("!invalid"), + SERD_HAS_DATATYPE, + serd_string(NS_XSD "long")); assert(isnan(serd_get_double(invalid))); - serd_node_free(invalid); + serd_node_free(NULL, invalid); - SerdNode* const base64 = serd_new_base64(blob, sizeof(blob)); + SerdNode* const base64 = serd_new_base64(NULL, blob, sizeof(blob)); assert(isnan(serd_get_double(base64))); - serd_node_free(base64); + serd_node_free(NULL, base64); SERD_RESTORE_WARNINGS } @@ -213,7 +214,7 @@ test_float(void) "0.0E0", "-0.0E0", "1.5E0", "-2.5E0", "4.56789E6"}; for (size_t i = 0; i < sizeof(test_values) / sizeof(float); ++i) { - SerdNode* node = serd_new_float(test_values[i]); + SerdNode* node = serd_new_float(NULL, test_values[i]); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); @@ -226,7 +227,7 @@ test_float(void) const float value = serd_get_float(node); assert(!memcmp(&value, &test_values[i], sizeof(value))); - serd_node_free(node); + serd_node_free(NULL, node); } } @@ -236,14 +237,14 @@ check_get_float(const char* string, const float expected) { SerdNode* const node = serd_new_literal( - serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); + NULL, serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); assert(node); const float value = serd_get_float(node); assert(!memcmp(&value, &expected, sizeof(value))); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -258,18 +259,20 @@ test_get_float(void) SERD_DISABLE_CONVERSION_WARNINGS - SerdNode* const nan = serd_new_string(serd_string("unknown")); + SerdNode* const nan = serd_new_string(NULL, serd_string("unknown")); assert(isnan(serd_get_float(nan))); - serd_node_free(nan); + serd_node_free(NULL, nan); - SerdNode* const invalid = serd_new_literal( - serd_string("!invalid"), SERD_HAS_DATATYPE, serd_string(NS_XSD "long")); + SerdNode* const invalid = serd_new_literal(NULL, + serd_string("!invalid"), + SERD_HAS_DATATYPE, + serd_string(NS_XSD "long")); assert(isnan(serd_get_double(invalid))); SERD_RESTORE_WARNINGS - serd_node_free(invalid); + serd_node_free(NULL, invalid); } static void @@ -280,7 +283,7 @@ test_integer(void) "0", "0", "-23", "23", "-12340", "1000", "-1000"}; for (size_t i = 0; i < sizeof(test_values) / sizeof(double); ++i) { - SerdNode* node = serd_new_integer(test_values[i]); + SerdNode* node = serd_new_integer(NULL, test_values[i]); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); const size_t len = strlen(node_str); @@ -291,7 +294,7 @@ test_integer(void) assert(!strcmp(serd_node_string(datatype), NS_XSD "integer")); assert(serd_get_integer(node) == test_values[i]); - serd_node_free(node); + serd_node_free(NULL, node); } } @@ -301,12 +304,12 @@ check_get_integer(const char* string, const int64_t expected) { SerdNode* const node = serd_new_literal( - serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); + NULL, serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); assert(node); assert(serd_get_integer(node) == expected); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -325,7 +328,7 @@ test_get_integer(void) static void test_base64(void) { - assert(!serd_new_base64(&SERD_URI_NULL, 0)); + assert(!serd_new_base64(NULL, &SERD_URI_NULL, 0)); // Test valid base64 blobs with a range of sizes for (size_t size = 1; size < 256; ++size) { @@ -334,7 +337,7 @@ test_base64(void) data[i] = (uint8_t)((size + i) % 256); } - SerdNode* blob = serd_new_base64(data, size); + SerdNode* blob = serd_new_base64(NULL, data, size); const char* blob_str = serd_node_string(blob); const size_t max_size = serd_get_base64_size(blob); uint8_t* out = (uint8_t*)calloc(1, max_size); @@ -353,8 +356,8 @@ test_base64(void) assert(datatype); assert(!strcmp(serd_node_string(datatype), NS_XSD "base64Binary")); - serd_node_free(blob); - serd_free(out); + serd_node_free(NULL, blob); + free(out); free(data); } } @@ -365,7 +368,7 @@ check_get_base64(const char* string, const char* expected) { SerdNode* const node = serd_new_literal( - serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); + NULL, serd_string(string), SERD_HAS_DATATYPE, serd_string(datatype_uri)); assert(node); @@ -380,7 +383,7 @@ check_get_base64(const char* string, assert(strlen(decoded) <= max_size); free(decoded); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -390,14 +393,16 @@ test_get_base64(void) check_get_base64("Zm9vYg==", NS_XSD "base64Binary", "foob"); check_get_base64(" \f\n\r\t\vZm9v \f\n\r\t\v", NS_XSD "base64Binary", "foo"); - SerdNode* const node = serd_new_literal( - serd_string("Zm9v"), SERD_HAS_DATATYPE, serd_string(NS_XSD "base64Binary")); + SerdNode* const node = serd_new_literal(NULL, + serd_string("Zm9v"), + SERD_HAS_DATATYPE, + serd_string(NS_XSD "base64Binary")); char small[2] = {0}; const SerdWriteResult r = serd_get_base64(node, sizeof(small), small); assert(r.status == SERD_OVERFLOW); - serd_node_free(node); + serd_node_free(NULL, node); } static void @@ -408,56 +413,56 @@ test_node_equals(void) static const SerdStringView replacement_char = { (const char*)replacement_char_str, 3}; - SerdNode* lhs = serd_new_string(replacement_char); - SerdNode* rhs = serd_new_string(serd_string("123")); + SerdNode* lhs = serd_new_string(NULL, replacement_char); + SerdNode* rhs = serd_new_string(NULL, serd_string("123")); assert(serd_node_equals(lhs, lhs)); assert(!serd_node_equals(lhs, rhs)); - assert(!serd_node_copy(NULL)); + assert(!serd_node_copy(NULL, NULL)); - serd_node_free(lhs); - serd_node_free(rhs); + serd_node_free(NULL, lhs); + serd_node_free(NULL, rhs); } static void test_node_from_string(void) { - SerdNode* const hello = serd_new_string(serd_string("hello\"")); + SerdNode* const hello = serd_new_string(NULL, serd_string("hello\"")); assert(serd_node_length(hello) == 6); assert(!serd_node_flags(hello)); assert(!strncmp(serd_node_string(hello), "hello\"", 6)); assert(!strcmp(serd_node_string_view(hello).data, "hello\"")); assert(serd_node_string_view(hello).length == 6); - serd_node_free(hello); + serd_node_free(NULL, hello); - SerdNode* const uri = serd_new_uri(serd_string("http://example.org/")); + SerdNode* const uri = serd_new_uri(NULL, serd_string("http://example.org/")); assert(serd_node_length(uri) == 19); assert(!strcmp(serd_node_string(uri), "http://example.org/")); assert(serd_node_uri_view(uri).authority.length == 11); assert(!strncmp(serd_node_uri_view(uri).authority.data, "example.org", 11)); - serd_node_free(uri); + serd_node_free(NULL, uri); } static void test_node_from_substring(void) { - SerdNode* const a_b = serd_new_string(serd_substring("a\"bc", 3)); + SerdNode* const a_b = serd_new_string(NULL, serd_substring("a\"bc", 3)); assert(serd_node_length(a_b) == 3); assert(!serd_node_flags(a_b)); assert(strlen(serd_node_string(a_b)) == 3); assert(!strncmp(serd_node_string(a_b), "a\"b", 3)); - serd_node_free(a_b); + serd_node_free(NULL, a_b); } static void check_copy_equals(const SerdNode* const node) { - SerdNode* const copy = serd_node_copy(node); + SerdNode* const copy = serd_node_copy(NULL, node); assert(serd_node_equals(node, copy)); - serd_node_free(copy); + serd_node_free(NULL, copy); } static void @@ -466,35 +471,46 @@ test_literal(void) const SerdStringView hello_str = serd_string("hello"); const SerdStringView empty_str = serd_empty_string(); - assert(!serd_new_literal( - hello_str, SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE, serd_string("whatever"))); + assert(!serd_new_literal(NULL, + hello_str, + SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE, + serd_string("whatever"))); - assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, empty_str)); - assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, empty_str)); + assert(!serd_new_literal(NULL, hello_str, SERD_HAS_DATATYPE, empty_str)); + assert(!serd_new_literal(NULL, hello_str, SERD_HAS_LANGUAGE, empty_str)); - assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, serd_string("Type"))); - assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, serd_string("de"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_DATATYPE, serd_string("Type"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_DATATYPE, serd_string("de"))); - assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, serd_string("3n"))); - assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, serd_string("d3"))); - assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, serd_string("d3"))); - assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, serd_string("en-!"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_LANGUAGE, serd_string("3n"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_LANGUAGE, serd_string("d3"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_LANGUAGE, serd_string("d3"))); + assert( + !serd_new_literal(NULL, hello_str, SERD_HAS_LANGUAGE, serd_string("en-!"))); - SerdNode* hello2 = serd_new_string(serd_string("hello\"")); + SerdNode* hello2 = serd_new_string(NULL, serd_string("hello\"")); assert(serd_node_length(hello2) == 6 && !strcmp(serd_node_string(hello2), "hello\"")); check_copy_equals(hello2); - assert(!serd_new_literal( - serd_string("plain"), SERD_HAS_DATATYPE, serd_string(NS_RDF "langString"))); + assert(!serd_new_literal(NULL, + serd_string("plain"), + SERD_HAS_DATATYPE, + serd_string(NS_RDF "langString"))); - serd_node_free(hello2); + serd_node_free(NULL, hello2); const char* lang_lit_str = "\"Hello\"@en-ca"; SerdNode* sliced_lang_lit = - serd_new_literal(serd_substring(lang_lit_str + 1, 5), + serd_new_literal(NULL, + serd_substring(lang_lit_str + 1, 5), SERD_HAS_LANGUAGE, serd_substring(lang_lit_str + 8, 5)); @@ -504,11 +520,12 @@ test_literal(void) assert(lang); assert(!strcmp(serd_node_string(lang), "en-ca")); check_copy_equals(sliced_lang_lit); - serd_node_free(sliced_lang_lit); + serd_node_free(NULL, sliced_lang_lit); const char* type_lit_str = "\"Hallo\"^^<http://example.org/Greeting>"; SerdNode* sliced_type_lit = - serd_new_literal(serd_substring(type_lit_str + 1, 5), + serd_new_literal(NULL, + serd_substring(type_lit_str + 1, 5), SERD_HAS_DATATYPE, serd_substring(type_lit_str + 10, 27)); @@ -517,53 +534,56 @@ test_literal(void) const SerdNode* const datatype = serd_node_datatype(sliced_type_lit); assert(datatype); assert(!strcmp(serd_node_string(datatype), "http://example.org/Greeting")); - serd_node_free(sliced_type_lit); + serd_node_free(NULL, sliced_type_lit); } static void test_blank(void) { - SerdNode* blank = serd_new_blank(serd_string("b0")); + SerdNode* blank = serd_new_blank(NULL, serd_string("b0")); assert(serd_node_length(blank) == 2); assert(serd_node_flags(blank) == 0); assert(!strcmp(serd_node_string(blank), "b0")); - serd_node_free(blank); + serd_node_free(NULL, blank); } static void test_compare(void) { SerdNode* xsd_short = - serd_new_uri(serd_string("http://www.w3.org/2001/XMLSchema#short")); + serd_new_uri(NULL, serd_string("http://www.w3.org/2001/XMLSchema#short")); - SerdNode* angst = serd_new_string(serd_string("angst")); + SerdNode* angst = serd_new_string(NULL, serd_string("angst")); SerdNode* angst_de = serd_new_literal( - serd_string("angst"), SERD_HAS_LANGUAGE, serd_string("de")); + NULL, serd_string("angst"), SERD_HAS_LANGUAGE, serd_string("de")); assert(angst_de); SerdNode* angst_en = serd_new_literal( - serd_string("angst"), SERD_HAS_LANGUAGE, serd_string("en")); + NULL, serd_string("angst"), SERD_HAS_LANGUAGE, serd_string("en")); SerdNode* hallo = serd_new_literal( - serd_string("Hallo"), SERD_HAS_LANGUAGE, serd_string("de")); + NULL, serd_string("Hallo"), SERD_HAS_LANGUAGE, serd_string("de")); - SerdNode* hello = serd_new_string(serd_string("Hello")); - SerdNode* universe = serd_new_string(serd_string("Universe")); - SerdNode* integer = serd_new_integer(4); - SerdNode* blank = serd_new_blank(serd_string("b1")); - SerdNode* uri = serd_new_uri(serd_string("http://example.org/")); + SerdNode* hello = serd_new_string(NULL, serd_string("Hello")); + SerdNode* universe = serd_new_string(NULL, serd_string("Universe")); + SerdNode* integer = serd_new_integer(NULL, 4); + SerdNode* blank = serd_new_blank(NULL, serd_string("b1")); + SerdNode* uri = serd_new_uri(NULL, serd_string("http://example.org/")); SerdNode* aardvark = - serd_new_literal(serd_string("alex"), + serd_new_literal(NULL, + serd_string("alex"), SERD_HAS_DATATYPE, serd_string("http://example.org/Aardvark")); - SerdNode* badger = serd_new_literal(serd_string("bobby"), + SerdNode* badger = serd_new_literal(NULL, + serd_string("bobby"), SERD_HAS_DATATYPE, serd_string("http://example.org/Badger")); // Types are ordered according to their SerdNodeType (more or less arbitrary) + assert(serd_node_compare(integer, hello) < 0); assert(serd_node_compare(hello, uri) < 0); assert(serd_node_compare(uri, blank) < 0); @@ -575,18 +595,18 @@ test_compare(void) assert(serd_node_compare(angst_de, angst_en) < 0); assert(serd_node_compare(aardvark, badger) < 0); - serd_node_free(uri); - serd_node_free(blank); - serd_node_free(integer); - serd_node_free(badger); - serd_node_free(aardvark); - serd_node_free(universe); - serd_node_free(hello); - serd_node_free(hallo); - serd_node_free(angst_en); - serd_node_free(angst_de); - serd_node_free(angst); - serd_node_free(xsd_short); + serd_node_free(NULL, uri); + serd_node_free(NULL, blank); + serd_node_free(NULL, integer); + serd_node_free(NULL, badger); + serd_node_free(NULL, aardvark); + serd_node_free(NULL, universe); + serd_node_free(NULL, hello); + serd_node_free(NULL, hallo); + serd_node_free(NULL, angst_en); + serd_node_free(NULL, angst_de); + serd_node_free(NULL, angst); + serd_node_free(NULL, xsd_short); } int |