diff options
author | David Robillard <d@drobilla.net> | 2011-12-25 07:09:29 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-12-25 07:09:29 +0000 |
commit | f3c95977f6c71b936e98f41c321265835c4fd623 (patch) | |
tree | ab275f04a3dd592c5572687230ba25f422359a36 /tests/serd_test.c | |
parent | b2effabfc5d02bab56bae00e7aa138a42bd7d3b6 (diff) | |
download | serd-f3c95977f6c71b936e98f41c321265835c4fd623.tar.gz serd-f3c95977f6c71b936e98f41c321265835c4fd623.tar.bz2 serd-f3c95977f6c71b936e98f41c321265835c4fd623.zip |
Near 100% branch coverage.
git-svn-id: http://svn.drobilla.net/serd/trunk@277 490d8e77-9747-427b-9fa3-0b8f29cee8a0
Diffstat (limited to 'tests/serd_test.c')
-rw-r--r-- | tests/serd_test.c | 160 |
1 files changed, 140 insertions, 20 deletions
diff --git a/tests/serd_test.c b/tests/serd_test.c index aca88127..7eb9a67e 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -82,7 +82,7 @@ main() }; const char* expt_test_strs[] = { - "02e18", "-5e019", "+8e20", "2E+34", "-5E-5", "8E0", "9e-0", "2e+0" + "02e18", "-5e019", "+8e20", "2E+34", "-5E-5", "8E0", "9e-0", " 2e+0" }; for (unsigned i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { @@ -99,11 +99,11 @@ main() // Test serd_node_new_decimal const double dbl_test_nums[] = { - 0.0, 42.0, .01, 8.0, 2.05, -16.00001, 5.000000005 + 0.0, 42.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001 }; const char* dbl_test_strs[] = { - "0.0", "42.0", "0.01", "8.0", "2.05", "-16.00001", "5.00000001" + "0.0", "42.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0" }; for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { @@ -155,13 +155,19 @@ main() size_t n_bytes; SerdNodeFlags flags; - const size_t len = serd_strlen(str, &n_bytes, &flags); + size_t len = serd_strlen(str, &n_bytes, &flags); if (len != 5 || n_bytes != 7 || flags != (SERD_HAS_QUOTE|SERD_HAS_NEWLINE)) { fprintf(stderr, "Bad serd_strlen(%s) len=%zu n_bytes=%zu flags=%u\n", str, len, n_bytes, flags); return 1; } + len = serd_strlen(str, NULL, &flags); + if (len != 5) { + fprintf(stderr, "Bad serd_strlen(%s) len=%zu flags=%u\n", + str, len, flags); + return 1; + } // Test serd_strerror @@ -177,6 +183,7 @@ main() return 1; } } + msg = serd_strerror((SerdStatus)-1); // Test serd_uri_to_path @@ -200,6 +207,37 @@ main() fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri); return 1; } + uri = (const uint8_t*)"file:///c:awful/system"; + if (strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")) { + fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri); + return 1; + } + uri = (const uint8_t*)"file:///0/1"; + if (strcmp((const char*)serd_uri_to_path(uri), "/0/1")) { + fprintf(stderr, "Bad path %s for %s\n", serd_uri_to_path(uri), uri); + return 1; + } + + // Test serd_node_equals + + const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; + SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str); + SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123")); + if (serd_node_equals(&lhs, &rhs)) { + fprintf(stderr, "%s == %s\n", lhs.buf, rhs.buf); + return 1; + } + + SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar")); + if (serd_node_equals(&lhs, &qnode)) { + fprintf(stderr, "%s == %s\n", lhs.buf, qnode.buf); + return 1; + } + + if (!serd_node_equals(&lhs, &lhs)) { + fprintf(stderr, "%s != %s\n", lhs.buf, lhs.buf); + return 1; + } // Test serd_node_from_string @@ -211,13 +249,31 @@ main() return 1; } + // Test serd_node_new_uri_from_string + + SerdURI base_uri; + SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), + NULL, &base_uri); + SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); + if (nil.type != SERD_URI || strcmp((const char*)nil.buf, (const char*)base.buf)) { + fprintf(stderr, "URI %s != base %s\n", nil.buf, base.buf); + return 1; + } + serd_node_free(&base); + serd_node_free(&nil); + // Test SerdEnv SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid")); - SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg:b")); + SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b")); SerdEnv* env = serd_env_new(NULL); - serd_env_set_prefix_from_strings(env, USTR("eg"), USTR("http://example.org/")); + serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); + + if (!serd_env_set_base_uri(env, &node)) { + fprintf(stderr, "Set base URI to %s\n", node.buf); + return 1; + } SerdChunk prefix, suffix; if (!serd_env_expand(env, &b, &prefix, &suffix)) { @@ -225,6 +281,12 @@ main() return 1; } + SerdNode xnode = serd_env_expand_node(env, &node); + if (!serd_node_equals(&xnode, &SERD_NODE_NULL)) { + fprintf(stderr, "Expanded %s to %s\n", c.buf, xnode.buf); + return 1; + } + SerdNode xu = serd_env_expand_node(env, &u); if (strcmp((const char*)xu.buf, "http://example.org/foo")) { fprintf(stderr, "Expanded %s to %s\n", c.buf, xu.buf); @@ -232,6 +294,13 @@ main() } serd_node_free(&xu); + SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what")); + SerdNode xbadpre = serd_env_expand_node(env, &badpre); + if (!serd_node_equals(&xbadpre, &SERD_NODE_NULL)) { + fprintf(stderr, "Expanded invalid curie %s\n", badpre.buf); + return 1; + } + SerdNode xc = serd_env_expand_node(env, &c); if (strcmp((const char*)xc.buf, "http://example.org/b")) { fprintf(stderr, "Expanded %s to %s\n", c.buf, xc.buf); @@ -251,13 +320,20 @@ main() } int n_prefixes = 0; - serd_env_set_prefix_from_strings(env, USTR("eg"), USTR("http://example.org/")); + serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); serd_env_foreach(env, count_prefixes, &n_prefixes); if (n_prefixes != 1) { fprintf(stderr, "Bad prefix count %d\n", n_prefixes); return 1; } + SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo")); + SerdNode prefix_name; + if (serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)) { + fprintf(stderr, "Qualified %s\n", shorter_uri.buf); + return 1; + } + // Test SerdReader and SerdWriter const char* path = tmpnam(NULL); @@ -277,6 +353,19 @@ main() return 1; } + serd_writer_chop_blank_prefix(writer, USTR("tmp")); + serd_writer_chop_blank_prefix(writer, NULL); + + if (!serd_writer_set_base_uri(writer, &lit)) { + fprintf(stderr, "Set base URI to %s\n", lit.buf); + return 1; + } + + if (!serd_writer_set_prefix(writer, &lit, &lit)) { + fprintf(stderr, "Set prefix %s to %s\n", lit.buf, lit.buf); + return 1; + } + if (!serd_writer_end_anon(writer, NULL)) { fprintf(stderr, "Ended non-existent anonymous node\n"); return 1; @@ -288,20 +377,43 @@ main() SerdNode o = serd_node_from_string(SERD_LITERAL, buf); // Write 3 invalid statements (should write nothing) - if (!serd_writer_write_statement(writer, 0, NULL, - &s, &p, NULL, NULL, NULL)) { - fprintf(stderr, "Successfully wrote junk statement 1\n"); - return 1; - } - if (!serd_writer_write_statement(writer, 0, NULL, - &s, &p, &SERD_NODE_NULL, NULL, NULL)) { - fprintf(stderr, "Successfully wrote junk statement 1\n"); + const SerdNode* junk[][5] = { { &s, &p, NULL, NULL, NULL }, + { &s, NULL, &o, NULL, NULL }, + { NULL, &p, &o, NULL, NULL }, + { &s, &p, &SERD_NODE_NULL, NULL, NULL }, + { &s, &SERD_NODE_NULL, &o, NULL, NULL }, + { &SERD_NODE_NULL, &p, &o, NULL, NULL }, + { &s, &o, &o, NULL, NULL }, + { &o, &p, &o, NULL, NULL }, + { NULL, NULL, NULL, NULL, NULL } }; + for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) { + if (!serd_writer_write_statement( + writer, 0, NULL, + junk[i][0], junk[i][1], junk[i][2], junk[i][3], junk[i][4])) { + fprintf(stderr, "Successfully wrote junk statement %d\n", i); return 1; + } } - if (!serd_writer_write_statement(writer, 0, NULL, - &s, &o, &o, NULL, NULL)) { - fprintf(stderr, "Successfully wrote junk statement 3\n"); - return 1; + + const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type")); + const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en")); + const SerdNode* good[][5] = { { &s, &p, &o, NULL, NULL }, + { &s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL }, + { &s, &p, &o, &t, NULL }, + { &s, &p, &o, NULL, &l }, + { &s, &p, &o, &t, &l }, + { &s, &p, &o, &t, &SERD_NODE_NULL }, + { &s, &p, &o, &SERD_NODE_NULL, &l }, + { &s, &p, &o, NULL, &SERD_NODE_NULL }, + { &s, &p, &o, &SERD_NODE_NULL, NULL }, + { &s, &p, &o, &SERD_NODE_NULL, NULL } }; + for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) { + if (serd_writer_write_statement( + writer, 0, NULL, + good[i][0], good[i][1], good[i][2], good[i][3], good[i][4])) { + fprintf(stderr, "Failed to write good statement %d\n", i); + return 1; + } } // Write 1 statement with bad UTF-8 (should be replaced) @@ -334,6 +446,9 @@ main() return 1; } + serd_reader_add_blank_prefix(reader, USTR("tmp")); + serd_reader_add_blank_prefix(reader, NULL); + if (!serd_reader_read_file(reader, USTR("http://notafile"))) { fprintf(stderr, "Apparently read an http URI\n"); return 1; @@ -348,11 +463,16 @@ main() return 1; } - if (*n_statements != 2) { + if (*n_statements != 12) { fprintf(stderr, "Bad statement count %d\n", *n_statements); return 1; } + if (!serd_reader_read_string(reader, USTR("This isn't Turtle at all."))) { + fprintf(stderr, "Parsed invalid string successfully.\n"); + return 1; + } + serd_reader_free(reader); fclose(fd); |