diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_env.c | 47 | ||||
-rw-r--r-- | test/test_node.c | 71 | ||||
-rw-r--r-- | test/test_reader_writer.c | 51 | ||||
-rw-r--r-- | test/test_uri.c | 13 |
4 files changed, 112 insertions, 70 deletions
diff --git a/test/test_env.c b/test/test_env.c index c64b5fed..9761a78c 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -34,15 +34,23 @@ count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) static void test_env(void) { - SerdNode* u = serd_new_string(SERD_URI, "http://example.org/foo"); - SerdNode* b = serd_new_string(SERD_CURIE, "invalid"); - SerdNode* c = serd_new_string(SERD_CURIE, "eg.2:b"); - SerdNode* s = serd_new_string(SERD_LITERAL, "hello"); - SerdEnv* env = serd_env_new(NULL); - serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); + SerdNode* u = serd_new_uri(SERD_STATIC_STRING("http://example.org/foo")); + SerdNode* b = serd_new_curie(SERD_STATIC_STRING("invalid")); + SerdNode* c = serd_new_curie(SERD_STATIC_STRING("eg.2:b")); + SerdNode* s = serd_new_string(SERD_STATIC_STRING("hello")); + SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); - assert(serd_env_set_prefix_from_strings(env, "eg.3", "rel") == - SERD_ERR_BAD_ARG); + serd_env_set_prefix( + env, SERD_STATIC_STRING("eg.2"), SERD_STATIC_STRING("http://example.org/")); + + assert(!serd_env_base_uri(env)); + assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING())); + assert(!serd_env_base_uri(env)); + assert(!serd_env_base_uri(env)); + + assert(serd_env_set_prefix(env, + SERD_STATIC_STRING("eg.3"), + SERD_STATIC_STRING("rel")) == SERD_ERR_BAD_ARG); SerdStringView prefix; SerdStringView suffix; @@ -51,13 +59,13 @@ test_env(void) assert(!serd_env_expand_node(env, b)); assert(!serd_env_expand_node(env, s)); - assert(!serd_env_set_base_uri(env, NULL)); + assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING())); SerdNode* xu = serd_env_expand_node(env, u); assert(!strcmp(serd_node_string(xu), "http://example.org/foo")); serd_node_free(xu); - SerdNode* badpre = serd_new_string(SERD_CURIE, "hm:what"); + SerdNode* badpre = serd_new_curie(SERD_STATIC_STRING("hm:what")); SerdNode* xbadpre = serd_env_expand_node(env, badpre); assert(!xbadpre); @@ -65,29 +73,26 @@ test_env(void) assert(!strcmp(serd_node_string(xc), "http://example.org/b")); serd_node_free(xc); - SerdNode* lit = serd_new_string(SERD_LITERAL, "hello"); - assert(serd_env_set_prefix(env, b, lit)); - - SerdNode* blank = serd_new_string(SERD_BLANK, "b1"); + SerdNode* blank = serd_new_blank(SERD_STATIC_STRING("b1")); assert(!serd_env_expand_node(env, blank)); serd_node_free(blank); int n_prefixes = 0; - serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); + serd_env_set_prefix( + env, SERD_STATIC_STRING("eg.2"), SERD_STATIC_STRING("http://example.org/")); serd_env_foreach(env, count_prefixes, &n_prefixes); assert(n_prefixes == 1); - SerdNode* shorter_uri = serd_new_string(SERD_URI, "urn:foo"); + SerdNode* shorter_uri = serd_new_uri(SERD_STATIC_STRING("urn:foo")); const SerdNode* prefix_name = NULL; assert(!serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)); - assert(!serd_env_set_base_uri(env, u)); - assert(serd_node_equals(serd_env_base_uri(env, NULL), u)); - assert(!serd_env_set_base_uri(env, NULL)); - assert(!serd_env_base_uri(env, NULL)); + assert(!serd_env_set_base_uri(env, serd_node_string_view(u))); + assert(serd_node_equals(serd_env_base_uri(env), u)); + assert(!serd_env_set_base_uri(env, SERD_EMPTY_STRING())); + assert(!serd_env_base_uri(env)); serd_node_free(shorter_uri); - serd_node_free(lit); serd_node_free(badpre); serd_node_free(s); serd_node_free(c); diff --git a/test/test_node.c b/test/test_node.c index 7fd4cb29..eeafe5fc 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -152,19 +152,20 @@ test_blob_to_node(void) static void test_node_equals(void) { - const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0}; - SerdNode* lhs = - serd_new_string(SERD_LITERAL, (const char*)replacement_char_str); - SerdNode* rhs = serd_new_string(SERD_LITERAL, "123"); + static const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0}; + + SerdNode* const lhs = + serd_new_string(SERD_STRING_VIEW((const char*)replacement_char_str, 3)); + + SerdNode* const rhs = serd_new_string(SERD_STATIC_STRING("123")); assert(!serd_node_equals(lhs, rhs)); - SerdNode* qnode = serd_new_string(SERD_CURIE, "foo:bar"); + SerdNode* const qnode = serd_new_curie(SERD_STATIC_STRING("foo:bar")); assert(!serd_node_equals(lhs, qnode)); - assert(serd_node_equals(lhs, lhs)); + serd_node_free(qnode); assert(!serd_node_copy(NULL)); - serd_node_free(qnode); serd_node_free(lhs); serd_node_free(rhs); } @@ -172,38 +173,45 @@ test_node_equals(void) static void test_node_from_string(void) { - SerdNode* hello = serd_new_string(SERD_LITERAL, "hello\""); - assert(serd_node_length(hello) == 6 && - serd_node_flags(hello) == SERD_HAS_QUOTE && - !strcmp(serd_node_string(hello), "hello\"")); - + SerdNode* const hello = serd_new_string(SERD_STATIC_STRING("hello\"")); + assert(serd_node_length(hello) == 6); + assert(serd_node_flags(hello) == SERD_HAS_QUOTE); + assert(!strncmp(serd_node_string(hello), "hello\"", 6)); serd_node_free(hello); } static void test_node_from_substring(void) { - SerdNode* a_b = serd_new_substring(SERD_LITERAL, "a\"bc", 3); - assert(serd_node_length(a_b) == 3 && serd_node_flags(a_b) == SERD_HAS_QUOTE && - !strncmp(serd_node_string(a_b), "a\"b", 3)); - - serd_node_free(a_b); - a_b = serd_new_substring(SERD_LITERAL, "a\"bc", 10); - assert(serd_node_length(a_b) == 4 && serd_node_flags(a_b) == SERD_HAS_QUOTE && - !strncmp(serd_node_string(a_b), "a\"bc", 4)); + SerdNode* const a_b = serd_new_string(SERD_STRING_VIEW("a\"bc", 3)); + assert(serd_node_length(a_b) == 3); + assert(serd_node_flags(a_b) == SERD_HAS_QUOTE); + assert(strlen(serd_node_string(a_b)) == 3); + assert(!strncmp(serd_node_string(a_b), "a\"b", 3)); serd_node_free(a_b); } static void +test_simple_node(void) +{ + assert(!serd_new_simple_node(SERD_LITERAL, SERD_STATIC_STRING("Literal"))); +} + +static void test_literal(void) { - SerdNode* hello2 = serd_new_literal("hello\"", NULL, NULL); + SerdNode* hello2 = serd_new_literal( + SERD_STATIC_STRING("hello\""), SERD_EMPTY_STRING(), SERD_EMPTY_STRING()); + assert(serd_node_length(hello2) == 6 && serd_node_flags(hello2) == SERD_HAS_QUOTE && !strcmp(serd_node_string(hello2), "hello\"")); serd_node_free(hello2); - SerdNode* hello_l = serd_new_literal("hello_l\"", NULL, "en"); + SerdNode* hello_l = serd_new_literal(SERD_STATIC_STRING("hello_l\""), + SERD_EMPTY_STRING(), + SERD_STATIC_STRING("en")); + assert(serd_node_length(hello_l) == 8); assert(!strcmp(serd_node_string(hello_l), "hello_l\"")); assert(serd_node_flags(hello_l) == (SERD_HAS_QUOTE | SERD_HAS_LANGUAGE)); @@ -213,8 +221,11 @@ test_literal(void) assert(!strcmp(serd_node_string(lang), "en")); serd_node_free(hello_l); - SerdNode* const hello_dt = - serd_new_literal("hello_dt\"", "http://example.org/Thing", NULL); + SerdNode* hello_dt = + serd_new_literal(SERD_STATIC_STRING("hello_dt\""), + SERD_STATIC_STRING("http://example.org/Thing"), + SERD_EMPTY_STRING()); + assert(serd_node_length(hello_dt) == 9); assert(!strcmp(serd_node_string(hello_dt), "hello_dt\"")); assert(serd_node_flags(hello_dt) == (SERD_HAS_QUOTE | SERD_HAS_DATATYPE)); @@ -225,6 +236,16 @@ test_literal(void) serd_node_free(hello_dt); } +static void +test_blank(void) +{ + SerdNode* blank = serd_new_blank(SERD_STATIC_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); +} + int main(void) { @@ -235,7 +256,9 @@ main(void) test_node_equals(); test_node_from_string(); test_node_from_substring(); + test_simple_node(); test_literal(); + test_blank(); printf("Success\n"); return 0; diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index d79fbbdd..5431462a 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -160,27 +160,28 @@ static void test_writer(const char* const path) { FILE* fd = fopen(path, "wb"); - SerdEnv* env = serd_env_new(NULL); + SerdEnv* env = serd_env_new(SERD_EMPTY_STRING()); assert(fd); - SerdWriter* writer = - serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_file_sink, fd); + SerdWriter* writer = serd_writer_new(SERD_TURTLE, 0, env, serd_file_sink, fd); assert(writer); serd_writer_chop_blank_prefix(writer, "tmp"); serd_writer_chop_blank_prefix(writer, NULL); - SerdNode* lit = serd_new_string(SERD_LITERAL, "hello"); + SerdNode* lit = serd_new_string(SERD_STATIC_STRING("hello")); assert(serd_writer_set_base_uri(writer, lit)); assert(serd_writer_set_prefix(writer, lit, lit)); assert(serd_writer_end_anon(writer, NULL)); assert(serd_writer_env(writer) == env); - uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0}; - SerdNode* s = serd_new_string(SERD_URI, ""); - SerdNode* p = serd_new_string(SERD_URI, "http://example.org/pred"); - SerdNode* o = serd_new_string(SERD_LITERAL, (char*)buf); + static const uint8_t buf[] = {0xEF, 0xBF, 0xBD, 0}; + const SerdStringView buf_view = {(const char*)buf, 3}; + + SerdNode* s = serd_new_uri(SERD_EMPTY_STRING()); + SerdNode* p = serd_new_uri(SERD_STATIC_STRING("http://example.org/pred")); + SerdNode* o = serd_new_string(buf_view); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][3] = {{s, o, o}, {o, p, o}, {s, o, p}}; @@ -189,26 +190,33 @@ test_writer(const char* const path) writer, 0, NULL, junk[i][0], junk[i][1], junk[i][2])); } - SerdNode* t = serd_new_literal((char*)buf, "urn:Type", NULL); - SerdNode* l = serd_new_literal((char*)buf, NULL, "en"); + static const SerdStringView empty = SERD_EMPTY_STRING(); + static const SerdStringView urn_Type = SERD_STATIC_STRING("urn:Type"); + static const SerdStringView en = SERD_STATIC_STRING("en"); + + SerdNode* const t = serd_new_literal(buf_view, urn_Type, empty); + SerdNode* const l = serd_new_literal(buf_view, empty, en); const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}}; + for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 3); ++i) { assert(!serd_writer_write_statement( writer, 0, NULL, good[i][0], good[i][1], good[i][2])); } // Write statements with bad UTF-8 (should be replaced) - const char bad_str[] = {(char)0xFF, (char)0x90, 'h', 'i', 0}; - SerdNode* bad_lit = serd_new_string(SERD_LITERAL, bad_str); - SerdNode* bad_uri = serd_new_string(SERD_URI, bad_str); + const char bad_str[] = {(char)0xFF, (char)0x90, 'h', 'i', 0}; + const SerdStringView bad_view = {(const char*)bad_str, 4}; + SerdNode* bad_lit = serd_new_string(bad_view); + SerdNode* bad_uri = serd_new_uri(bad_view); assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_lit)); assert(!serd_writer_write_statement(writer, 0, NULL, s, p, bad_uri)); - serd_node_free(bad_lit); + serd_node_free(bad_uri); + serd_node_free(bad_lit); // Write 1 valid statement serd_node_free(o); - o = serd_new_string(SERD_LITERAL, "hello"); + o = serd_new_string(SERD_STATIC_STRING("hello")); assert(!serd_writer_write_statement(writer, 0, NULL, s, p, o)); serd_writer_free(writer); @@ -219,13 +227,14 @@ test_writer(const char* const path) // Test buffer sink SerdBuffer buffer = {NULL, 0}; - writer = - serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_buffer_sink, &buffer); + writer = serd_writer_new(SERD_TURTLE, 0, env, serd_buffer_sink, &buffer); - o = serd_new_string(SERD_URI, "http://example.org/base"); - assert(!serd_writer_set_base_uri(writer, o)); + SerdNode* const base = + serd_new_uri(SERD_STATIC_STRING("http://example.org/base")); - serd_node_free(o); + serd_writer_set_base_uri(writer, base); + + serd_node_free(base); serd_writer_free(writer); char* out = serd_buffer_sink_finish(&buffer); @@ -248,7 +257,7 @@ test_reader(const char* path) assert(reader); assert(serd_reader_handle(reader) == rt); - SerdNode* g = serd_new_string(SERD_URI, "http://example.org/"); + SerdNode* g = serd_new_uri(SERD_STATIC_STRING("http://example.org/")); serd_reader_set_default_graph(reader, g); serd_reader_add_blank_prefix(reader, "tmp"); diff --git a/test/test_uri.c b/test/test_uri.c index d0dba037..ea70508b 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -35,7 +35,9 @@ test_file_uri(const char* hostname, expected_path = path; } - SerdNode* node = serd_new_file_uri(path, hostname, 0); + SerdNode* node = + serd_new_file_uri(SERD_MEASURE_STRING(path), SERD_MEASURE_STRING(hostname)); + const char* node_str = serd_node_string(node); char* out_hostname = NULL; char* out_path = serd_parse_file_uri(node_str, &out_hostname); @@ -143,7 +145,7 @@ check_rel_uri(const char* uri_string, SerdNode* const rel = is_within ? serd_new_parsed_uri(serd_relative_uri(uri, base_uri)) - : serd_new_uri(uri_string); + : serd_new_uri(SERD_MEASURE_STRING(uri_string)); const int ret = strcmp(serd_node_string(rel), expected); serd_node_free(rel); @@ -153,8 +155,11 @@ check_rel_uri(const char* uri_string, static void test_relative_uri(void) { - SerdNode* const root = serd_new_uri("http://example.org/a/b/ignored"); - SerdNode* const base = serd_new_uri("http://example.org/a/b/c/"); + SerdNode* const root = + serd_new_uri(SERD_STATIC_STRING("http://example.org/a/b/ignored")); + + SerdNode* const base = + serd_new_uri(SERD_STATIC_STRING("http://example.org/a/b/c/")); check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo"); check_rel_uri("http://example.org/a/", base, NULL, "../../"); |