From a63a8f19c54dfee75e092819d6622b8d36fe1d39 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 22 Jul 2021 23:39:39 -0400 Subject: Expose low-level node construction API --- test/test_env.c | 2 +- test/test_node.c | 65 +++++++++++++++++++++++++++++++++++++++---------- test/test_node_syntax.c | 36 +++++++++++++++------------ 3 files changed, 74 insertions(+), 29 deletions(-) (limited to 'test') diff --git a/test/test_env.c b/test/test_env.c index 1b924ae9..9ab13ef2 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -260,7 +260,7 @@ test_expand_bad_curie(void) static void test_expand_blank(void) { - SerdNode* const blank = serd_new_blank(SERD_STRING("b1")); + SerdNode* const blank = serd_new_token(SERD_BLANK, SERD_STRING("b1")); SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); assert(!serd_env_expand_node(env, blank)); diff --git a/test/test_node.c b/test/test_node.c index efd93ddd..200791af 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -123,7 +123,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(test_values[i]); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); @@ -205,7 +205,8 @@ test_get_double(void) assert(isnan(serd_get_double(invalid))); serd_node_free(invalid); - SerdNode* const base64 = serd_new_base64(blob, sizeof(blob), NULL); + SerdNode* const base64 = + serd_new_base64(blob, sizeof(blob), SERD_EMPTY_STRING()); assert(isnan(serd_get_double(base64))); serd_node_free(base64); @@ -283,12 +284,14 @@ test_get_float(void) static void test_integer(void) { + assert(!serd_new_integer(42, SERD_STRING("notauri"))); + const int64_t test_values[] = {0, -0, -23, 23, -12340, 1000, -1000}; const char* test_strings[] = { "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], NULL); + SerdNode* node = serd_new_integer(test_values[i], SERD_EMPTY_STRING()); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, test_strings[i])); const size_t len = strlen(node_str); @@ -333,7 +336,8 @@ test_get_integer(void) static void test_base64(void) { - assert(!serd_new_base64(&SERD_URI_NULL, 0, NULL)); + assert(!serd_new_base64(&SERD_URI_NULL, 1, SERD_STRING("notauri"))); + assert(!serd_new_base64(&SERD_URI_NULL, 0, SERD_EMPTY_STRING())); // Test valid base64 blobs with a range of sizes for (size_t size = 1; size < 256; ++size) { @@ -342,7 +346,7 @@ test_base64(void) data[i] = (uint8_t)((size + i) % 256); } - SerdNode* blob = serd_new_base64(data, size, NULL); + SerdNode* blob = serd_new_base64(data, size, SERD_EMPTY_STRING()); 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); @@ -459,6 +463,28 @@ check_copy_equals(const SerdNode* const node) serd_node_free(copy); } +static void +test_new(void) +{ + assert(!serd_node_new(SERD_URI, + SERD_STRING("http://example.org/"), + SERD_HAS_DATATYPE, + SERD_STRING("http://example.org/uris/cant/have/me"))); + + assert(!serd_node_new(SERD_URI, + SERD_STRING("http://example.org/"), + SERD_HAS_LANGUAGE, + SERD_STRING("in-valid"))); + + assert(!serd_node_new(SERD_BLANK, + SERD_STRING("b0"), + SERD_HAS_DATATYPE, + SERD_STRING("http://example.org/uris/cant/have/me"))); + + assert(!serd_node_new( + SERD_BLANK, SERD_STRING("b0"), SERD_HAS_LANGUAGE, SERD_STRING("in-valid"))); +} + static void test_literal(void) { @@ -522,7 +548,7 @@ test_literal(void) static void test_blank(void) { - SerdNode* blank = serd_new_blank(SERD_STRING("b0")); + SerdNode* blank = serd_new_token(SERD_BLANK, SERD_STRING("b0")); assert(serd_node_length(blank) == 2); assert(serd_node_flags(blank) == 0); assert(!strcmp(serd_node_string(blank), "b0")); @@ -532,15 +558,14 @@ test_blank(void) static void test_compare(void) { - SerdNode* xsd_short = - serd_new_uri(SERD_STRING("http://www.w3.org/2001/XMLSchema#short")); + SerdNode* xsd_short = serd_new_token( + SERD_URI, SERD_STRING("http://www.w3.org/2001/XMLSchema#short")); SerdNode* angst = serd_new_string(SERD_STRING("angst")); SerdNode* angst_de = serd_new_literal( 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")); @@ -549,10 +574,20 @@ test_compare(void) SerdNode* hello = serd_new_string(SERD_STRING("Hello")); SerdNode* universe = serd_new_string(SERD_STRING("Universe")); - SerdNode* integer = serd_new_integer(4, NULL); - SerdNode* short_integer = serd_new_integer(4, xsd_short); - SerdNode* blank = serd_new_blank(SERD_STRING("b1")); - SerdNode* uri = serd_new_uri(SERD_STRING("http://example.org/")); + SerdNode* integer = serd_new_integer(4, SERD_EMPTY_STRING()); + SerdNode* short_integer = serd_new_integer(4, SERD_STRING(NS_XSD "short")); + SerdNode* blank = serd_new_token(SERD_BLANK, SERD_STRING("b1")); + + SerdNode* uri = serd_new_uri(SERD_STRING("http://example.org/")); + + SerdNode* aardvark = + serd_new_literal(SERD_STRING("alex"), + SERD_HAS_DATATYPE, + SERD_STRING("http://example.org/Aardvark")); + + SerdNode* badger = serd_new_literal(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(hello, uri) < 0); @@ -565,11 +600,14 @@ test_compare(void) assert(serd_node_compare(angst, angst_de) < 0); assert(serd_node_compare(angst_de, angst_en) < 0); assert(serd_node_compare(integer, short_integer) < 0); + assert(serd_node_compare(aardvark, badger) < 0); serd_node_free(uri); serd_node_free(blank); serd_node_free(short_integer); serd_node_free(integer); + serd_node_free(badger); + serd_node_free(aardvark); serd_node_free(universe); serd_node_free(hello); serd_node_free(hallo); @@ -596,6 +634,7 @@ main(void) test_node_equals(); test_node_from_syntax(); test_node_from_substring(); + test_new(); test_literal(); test_blank(); test_compare(); diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c index 26b57ff1..a9829688 100644 --- a/test/test_node_syntax.c +++ b/test/test_node_syntax.c @@ -47,8 +47,8 @@ test_common(const SerdSyntax syntax) static const SerdStringView datatype = SERD_STRING("http://example.org/Datatype"); - SerdNode* const num_type = - serd_new_uri(SERD_STRING("http://example.org/Decimal")); + static const SerdStringView num_type = + SERD_STRING("http://example.org/Decimal"); assert(test(syntax, serd_new_string(SERD_STRING("node")), "\"node\"")); @@ -61,17 +61,15 @@ test_common(const SerdSyntax syntax) serd_new_literal(SERD_STRING("X"), SERD_HAS_DATATYPE, datatype), "\"X\"^^")); - assert(test(syntax, serd_new_blank(SERD_STRING("blank")), "_:blank")); - assert(test(syntax, serd_new_blank(SERD_STRING("b0")), "_:b0")); + assert( + test(syntax, serd_new_token(SERD_BLANK, SERD_STRING("blank")), "_:blank")); + + assert(test(syntax, serd_new_token(SERD_BLANK, SERD_STRING("b0")), "_:b0")); assert(test(syntax, serd_new_uri(SERD_STRING("http://example.org/")), "")); - assert(test(syntax, - serd_new_decimal(1.25, num_type), - "\"1.25\"^^")); - assert(test(syntax, serd_new_double(1.25), "\"1.25E0\"^^")); @@ -86,10 +84,8 @@ test_common(const SerdSyntax syntax) assert( test(syntax, - serd_new_base64(data, sizeof(data), NULL), + serd_new_base64(data, sizeof(data), SERD_EMPTY_STRING()), "\"BAAAAAIAAAA=\"^^")); - - serd_node_free(num_type); } static void @@ -118,11 +114,11 @@ test_ntriples(void) } assert(test(SERD_NTRIPLES, - serd_new_decimal(1.25, NULL), + serd_new_decimal(1.25), "\"1.25\"^^")); assert(test(SERD_NTRIPLES, - serd_new_integer(1234, NULL), + serd_new_integer(1234, SERD_EMPTY_STRING()), "\"1234\"^^")); assert(test(SERD_NTRIPLES, @@ -137,10 +133,20 @@ test_ntriples(void) static void test_turtle(void) { + static const SerdStringView xsd_integer = + SERD_STRING("http://www.w3.org/2001/XMLSchema#integer"); + test_common(SERD_TURTLE); + test(SERD_TURTLE, serd_new_uri(SERD_STRING("rel/uri")), ""); - assert(test(SERD_TURTLE, serd_new_decimal(1.25, NULL), "1.25")); - assert(test(SERD_TURTLE, serd_new_integer(1234, NULL), "1234")); + + assert(test(SERD_TURTLE, serd_new_decimal(1.25), "1.25")); + + assert( + test(SERD_TURTLE, serd_new_integer(1234, SERD_EMPTY_STRING()), "1234")); + + assert(test(SERD_TURTLE, serd_new_integer(1234, xsd_integer), "1234")); + assert(test(SERD_TURTLE, serd_new_boolean(true), "true")); assert(test(SERD_TURTLE, serd_new_boolean(false), "false")); } -- cgit v1.2.1