cannam@226: /* cannam@226: Copyright 2011-2016 David Robillard cannam@226: cannam@226: Permission to use, copy, modify, and/or distribute this software for any cannam@226: purpose with or without fee is hereby granted, provided that the above cannam@226: copyright notice and this permission notice appear in all copies. cannam@226: cannam@226: THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES cannam@226: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF cannam@226: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR cannam@226: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES cannam@226: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN cannam@226: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF cannam@226: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. cannam@226: */ cannam@226: cannam@226: #include cannam@226: #include cannam@226: #include cannam@226: #include cannam@226: cannam@226: #include "sord/sord.h" cannam@226: cannam@226: static const int DIGITS = 3; cannam@226: static const unsigned n_objects_per = 2; cannam@226: cannam@226: static int n_expected_errors = 0; cannam@226: cannam@226: typedef struct { cannam@226: SordQuad query; cannam@226: int expected_num_results; cannam@226: } QueryTest; cannam@226: cannam@226: #define USTR(s) ((const uint8_t*)(s)) cannam@226: cannam@226: static SordNode* cannam@226: uri(SordWorld* world, int num) cannam@226: { cannam@226: if (num == 0) cannam@226: return 0; cannam@226: cannam@226: char str[] = "eg:000"; cannam@226: char* uri_num = str + 3; // First `0' cannam@226: snprintf(uri_num, DIGITS + 1, "%0*d", DIGITS, num); cannam@226: return sord_new_uri(world, (const uint8_t*)str); cannam@226: } cannam@226: cannam@226: static int cannam@226: test_fail(const char* fmt, ...) cannam@226: { cannam@226: va_list args; cannam@226: va_start(args, fmt); cannam@226: fprintf(stderr, "error: "); cannam@226: vfprintf(stderr, fmt, args); cannam@226: va_end(args); cannam@226: return 1; cannam@226: } cannam@226: cannam@226: static int cannam@226: generate(SordWorld* world, cannam@226: SordModel* sord, cannam@226: size_t n_quads, cannam@226: SordNode* graph) cannam@226: { cannam@226: fprintf(stderr, "Generating %zu (S P *) quads with %u objects each\n", cannam@226: n_quads, n_objects_per); cannam@226: cannam@226: for (size_t i = 0; i < n_quads; ++i) { cannam@226: int num = (i * n_objects_per) + 1; cannam@226: cannam@226: SordNode* ids[2 + n_objects_per]; cannam@226: for (unsigned j = 0; j < 2 + n_objects_per; ++j) { cannam@226: ids[j] = uri(world, num++); cannam@226: } cannam@226: cannam@226: for (unsigned j = 0; j < n_objects_per; ++j) { cannam@226: SordQuad tup = { ids[0], ids[1], ids[2 + j], graph }; cannam@226: if (!sord_add(sord, tup)) { cannam@226: return test_fail("Fail: Failed to add quad\n"); cannam@226: } cannam@226: } cannam@226: cannam@226: for (unsigned j = 0; j < 2 + n_objects_per; ++j) { cannam@226: sord_node_free(world, ids[j]); cannam@226: } cannam@226: } cannam@226: cannam@226: // Add some literals cannam@226: cannam@226: // (98 4 "hello") and (98 4 "hello"^^<5>) cannam@226: SordQuad tup = { 0, 0, 0, 0 }; cannam@226: tup[0] = uri(world, 98); cannam@226: tup[1] = uri(world, 4); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); cannam@226: tup[3] = graph; cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); cannam@226: if (!sord_add(sord, tup)) { cannam@226: return test_fail("Failed to add typed literal\n"); cannam@226: } cannam@226: cannam@226: // (96 4 "hello"^^<4>) and (96 4 "hello"^^<5>) cannam@226: tup[0] = uri(world, 96); cannam@226: tup[1] = uri(world, 4); cannam@226: tup[2] = sord_new_literal(world, uri(world, 4), USTR("hello"), NULL); cannam@226: tup[3] = graph; cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); cannam@226: if (!sord_add(sord, tup)) { cannam@226: return test_fail("Failed to add typed literal\n"); cannam@226: } cannam@226: cannam@226: // (94 5 "hello") and (94 5 "hello"@en-gb) cannam@226: tup[0] = uri(world, 94); cannam@226: tup[1] = uri(world, 5); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); cannam@226: tup[3] = graph; cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); cannam@226: if (!sord_add(sord, tup)) { cannam@226: return test_fail("Failed to add literal with language\n"); cannam@226: } cannam@226: cannam@226: // (92 6 "hello"@en-us) and (92 5 "hello"@en-gb) cannam@226: tup[0] = uri(world, 92); cannam@226: tup[1] = uri(world, 6); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("hello"), "en-us"); cannam@226: tup[3] = graph; cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); cannam@226: if (!sord_add(sord, tup)) { cannam@226: return test_fail("Failed to add literal with language\n"); cannam@226: } cannam@226: cannam@226: sord_node_free(world, (SordNode*)tup[0]); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[0] = uri(world, 14); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("bonjour"), "fr"); cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("salut"), "fr"); cannam@226: sord_add(sord, tup); cannam@226: cannam@226: // Attempt to add some duplicates cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Fail: Successfully added duplicate quad\n"); cannam@226: } cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Fail: Successfully added duplicate quad\n"); cannam@226: } cannam@226: cannam@226: // Add a blank node subject cannam@226: sord_node_free(world, (SordNode*)tup[0]); cannam@226: tup[0] = sord_new_blank(world, USTR("ablank")); cannam@226: sord_add(sord, tup); cannam@226: cannam@226: sord_node_free(world, (SordNode*)tup[1]); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[1] = uri(world, 6); cannam@226: tup[2] = uri(world, 7); cannam@226: sord_add(sord, tup); cannam@226: sord_node_free(world, (SordNode*)tup[0]); cannam@226: sord_node_free(world, (SordNode*)tup[1]); cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: cannam@226: return EXIT_SUCCESS; cannam@226: } cannam@226: cannam@226: #define TUP_FMT "(%6s %6s %6s)" cannam@226: #define TUP_FMT_ARGS(t) \ cannam@226: ((t)[0] ? sord_node_get_string((t)[0]) : USTR("*")), \ cannam@226: ((t)[1] ? sord_node_get_string((t)[1]) : USTR("*")), \ cannam@226: ((t)[2] ? sord_node_get_string((t)[2]) : USTR("*")) cannam@226: cannam@226: static int cannam@226: test_read(SordWorld* world, SordModel* sord, SordNode* g, cannam@226: const size_t n_quads) cannam@226: { cannam@226: int ret = EXIT_SUCCESS; cannam@226: cannam@226: SordQuad id; cannam@226: cannam@226: SordIter* iter = sord_begin(sord); cannam@226: if (sord_iter_get_model(iter) != sord) { cannam@226: return test_fail("Fail: Iterator has incorrect sord pointer\n"); cannam@226: } cannam@226: cannam@226: for (; !sord_iter_end(iter); sord_iter_next(iter)) cannam@226: sord_iter_get(iter, id); cannam@226: cannam@226: // Attempt to increment past end cannam@226: if (!sord_iter_next(iter)) { cannam@226: return test_fail("Fail: Successfully incremented past end\n"); cannam@226: } cannam@226: cannam@226: sord_iter_free(iter); cannam@226: cannam@226: const uint8_t* s = USTR("hello"); cannam@226: SordNode* plain_hello = sord_new_literal(world, 0, s, NULL); cannam@226: SordNode* type4_hello = sord_new_literal(world, uri(world, 4), s, NULL); cannam@226: SordNode* type5_hello = sord_new_literal(world, uri(world, 5), s, NULL); cannam@226: SordNode* gb_hello = sord_new_literal(world, NULL, s, "en-gb"); cannam@226: SordNode* us_hello = sord_new_literal(world, NULL, s, "en-us"); cannam@226: cannam@226: #define NUM_PATTERNS 18 cannam@226: cannam@226: QueryTest patterns[NUM_PATTERNS] = { cannam@226: { { 0, 0, 0 }, (int)(n_quads * n_objects_per) + 12 }, cannam@226: { { uri(world, 1), 0, 0 }, 2 }, cannam@226: { { uri(world, 9), uri(world, 9), uri(world, 9) }, 0 }, cannam@226: { { uri(world, 1), uri(world, 2), uri(world, 4) }, 1 }, cannam@226: { { uri(world, 3), uri(world, 4), uri(world, 0) }, 2 }, cannam@226: { { uri(world, 0), uri(world, 2), uri(world, 4) }, 1 }, cannam@226: { { uri(world, 0), uri(world, 0), uri(world, 4) }, 1 }, cannam@226: { { uri(world, 1), uri(world, 0), uri(world, 0) }, 2 }, cannam@226: { { uri(world, 1), uri(world, 0), uri(world, 4) }, 1 }, cannam@226: { { uri(world, 0), uri(world, 2), uri(world, 0) }, 2 }, cannam@226: { { uri(world, 98), uri(world, 4), plain_hello }, 1 }, cannam@226: { { uri(world, 98), uri(world, 4), type5_hello }, 1 }, cannam@226: { { uri(world, 96), uri(world, 4), type4_hello }, 1 }, cannam@226: { { uri(world, 96), uri(world, 4), type5_hello }, 1 }, cannam@226: { { uri(world, 94), uri(world, 5), plain_hello }, 1 }, cannam@226: { { uri(world, 94), uri(world, 5), gb_hello }, 1 }, cannam@226: { { uri(world, 92), uri(world, 6), gb_hello }, 1 }, cannam@226: { { uri(world, 92), uri(world, 6), us_hello }, 1 } }; cannam@226: cannam@226: SordQuad match = { uri(world, 1), uri(world, 2), uri(world, 4), g }; cannam@226: if (!sord_contains(sord, match)) { cannam@226: return test_fail("Fail: No match for " TUP_FMT "\n", cannam@226: TUP_FMT_ARGS(match)); cannam@226: } cannam@226: cannam@226: SordQuad nomatch = { uri(world, 1), uri(world, 2), uri(world, 9), g }; cannam@226: if (sord_contains(sord, nomatch)) { cannam@226: return test_fail("Fail: False match for " TUP_FMT "\n", cannam@226: TUP_FMT_ARGS(nomatch)); cannam@226: } cannam@226: cannam@226: if (sord_get(sord, NULL, NULL, uri(world, 3), g)) { cannam@226: return test_fail("Fail: Get *,*,3 succeeded\n"); cannam@226: } else if (!sord_node_equals( cannam@226: sord_get(sord, uri(world, 1), uri(world, 2), NULL, g), cannam@226: uri(world, 3))) { cannam@226: return test_fail("Fail: Get 1,2,* != 3\n"); cannam@226: } else if (!sord_node_equals( cannam@226: sord_get(sord, uri(world, 1), NULL, uri(world, 3), g), cannam@226: uri(world, 2))) { cannam@226: return test_fail("Fail: Get 1,*,3 != 2\n"); cannam@226: } else if (!sord_node_equals( cannam@226: sord_get(sord, NULL, uri(world, 2), uri(world, 3), g), cannam@226: uri(world, 1))) { cannam@226: return test_fail("Fail: Get *,2,3 != 1\n"); cannam@226: } cannam@226: cannam@226: for (unsigned i = 0; i < NUM_PATTERNS; ++i) { cannam@226: QueryTest test = patterns[i]; cannam@226: SordQuad pat = { test.query[0], test.query[1], test.query[2], g }; cannam@226: fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); cannam@226: cannam@226: iter = sord_find(sord, pat); cannam@226: int num_results = 0; cannam@226: for (; !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: sord_iter_get(iter, id); cannam@226: ++num_results; cannam@226: if (!sord_quad_match(pat, id)) { cannam@226: sord_iter_free(iter); cannam@226: return test_fail( cannam@226: "Fail: Query result " TUP_FMT " does not match pattern\n", cannam@226: TUP_FMT_ARGS(id)); cannam@226: } cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: if (num_results != test.expected_num_results) { cannam@226: return test_fail("Fail: Expected %d results, got %d\n", cannam@226: test.expected_num_results, num_results); cannam@226: } cannam@226: fprintf(stderr, "OK (%u matches)\n", test.expected_num_results); cannam@226: } cannam@226: cannam@226: // Query blank node subject cannam@226: SordQuad pat = { sord_new_blank(world, USTR("ablank")), 0, 0 }; cannam@226: if (!pat[0]) { cannam@226: return test_fail("Blank node subject lost\n"); cannam@226: } cannam@226: fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); cannam@226: iter = sord_find(sord, pat); cannam@226: int num_results = 0; cannam@226: for (; !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: sord_iter_get(iter, id); cannam@226: ++num_results; cannam@226: if (!sord_quad_match(pat, id)) { cannam@226: sord_iter_free(iter); cannam@226: return test_fail( cannam@226: "Fail: Query result " TUP_FMT " does not match pattern\n", cannam@226: TUP_FMT_ARGS(id)); cannam@226: } cannam@226: } cannam@226: fprintf(stderr, "OK\n"); cannam@226: sord_node_free(world, (SordNode*)pat[0]); cannam@226: sord_iter_free(iter); cannam@226: if (num_results != 2) { cannam@226: return test_fail("Blank node subject query failed\n"); cannam@226: } cannam@226: cannam@226: // Test nested queries cannam@226: fprintf(stderr, "Nested Queries... "); cannam@226: const SordNode* last_subject = 0; cannam@226: iter = sord_search(sord, NULL, NULL, NULL, NULL); cannam@226: for (; !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: sord_iter_get(iter, id); cannam@226: if (id[0] == last_subject) cannam@226: continue; cannam@226: cannam@226: SordQuad subpat = { id[0], 0, 0 }; cannam@226: SordIter* subiter = sord_find(sord, subpat); cannam@226: uint64_t num_sub_results = 0; cannam@226: if (sord_iter_get_node(subiter, SORD_SUBJECT) != id[0]) { cannam@226: return test_fail("Fail: Incorrect initial submatch\n"); cannam@226: } cannam@226: for (; !sord_iter_end(subiter); sord_iter_next(subiter)) { cannam@226: SordQuad subid; cannam@226: sord_iter_get(subiter, subid); cannam@226: if (!sord_quad_match(subpat, subid)) { cannam@226: sord_iter_free(iter); cannam@226: sord_iter_free(subiter); cannam@226: return test_fail( cannam@226: "Fail: Nested query result does not match pattern\n"); cannam@226: } cannam@226: ++num_sub_results; cannam@226: } cannam@226: sord_iter_free(subiter); cannam@226: if (num_sub_results != n_objects_per) { cannam@226: return test_fail( cannam@226: "Fail: Nested query " TUP_FMT " failed" cannam@226: " (%d results, expected %d)\n", cannam@226: TUP_FMT_ARGS(subpat), num_sub_results, n_objects_per); cannam@226: } cannam@226: cannam@226: uint64_t count = sord_count(sord, id[0], 0, 0, 0); cannam@226: if (count != num_sub_results) { cannam@226: return test_fail("Fail: Query " TUP_FMT " sord_count() %d" cannam@226: "does not match result count %d\n", cannam@226: TUP_FMT_ARGS(subpat), count, num_sub_results); cannam@226: } cannam@226: cannam@226: last_subject = id[0]; cannam@226: } cannam@226: fprintf(stderr, "OK\n\n"); cannam@226: sord_iter_free(iter); cannam@226: cannam@226: return ret; cannam@226: } cannam@226: cannam@226: static SerdStatus cannam@226: unexpected_error(void* handle, const SerdError* error) cannam@226: { cannam@226: fprintf(stderr, "unexpected error: "); cannam@226: vfprintf(stderr, error->fmt, *error->args); cannam@226: return SERD_SUCCESS; cannam@226: } cannam@226: cannam@226: static SerdStatus cannam@226: expected_error(void* handle, const SerdError* error) cannam@226: { cannam@226: fprintf(stderr, "expected error: "); cannam@226: vfprintf(stderr, error->fmt, *error->args); cannam@226: ++n_expected_errors; cannam@226: return SERD_SUCCESS; cannam@226: } cannam@226: cannam@226: static int cannam@226: finished(SordWorld* world, SordModel* sord, int status) cannam@226: { cannam@226: sord_free(sord); cannam@226: sord_world_free(world); cannam@226: return status; cannam@226: } cannam@226: cannam@226: int cannam@226: main(int argc, char** argv) cannam@226: { cannam@226: static const size_t n_quads = 300; cannam@226: cannam@226: sord_free(NULL); // Shouldn't crash cannam@226: cannam@226: SordWorld* world = sord_world_new(); cannam@226: cannam@226: cannam@226: // Attempt to create invalid URI cannam@226: fprintf(stderr, "expected "); cannam@226: SordNode* bad_uri = sord_new_uri(world, USTR("noscheme")); cannam@226: if (bad_uri) { cannam@226: return test_fail("Successfully created invalid URI \"noscheme\"\n"); cannam@226: } cannam@226: sord_node_free(world, bad_uri); cannam@226: cannam@226: sord_world_set_error_sink(world, expected_error, NULL); cannam@226: cannam@226: // Attempt to create invalid CURIE cannam@226: SerdNode base = serd_node_from_string(SERD_URI, USTR("http://example.org/")); cannam@226: SerdEnv* env = serd_env_new(&base); cannam@226: SerdNode sbadns = serd_node_from_string(SERD_CURIE, USTR("badns:")); cannam@226: SordNode* badns = sord_node_from_serd_node(world, env, &sbadns, NULL, NULL); cannam@226: if (badns) { cannam@226: return test_fail("Successfully created CURIE with bad namespace\n"); cannam@226: } cannam@226: sord_node_free(world, badns); cannam@226: serd_env_free(env); cannam@226: cannam@226: // Attempt to create node from garbage cannam@226: SerdNode junk = SERD_NODE_NULL; cannam@226: junk.type = (SerdType)1234; cannam@226: if (sord_node_from_serd_node(world, env, &junk, NULL, NULL)) { cannam@226: return test_fail("Successfully created node from garbage serd node\n"); cannam@226: } cannam@226: cannam@226: // Attempt to create NULL node cannam@226: SordNode* nil_node = sord_node_from_serd_node( cannam@226: world, NULL, &SERD_NODE_NULL, NULL, NULL); cannam@226: if (nil_node) { cannam@226: return test_fail("Successfully created NULL node\n"); cannam@226: } cannam@226: sord_node_free(world, nil_node); cannam@226: cannam@226: // Attempt to double-free a node cannam@226: SordNode* garbage = sord_new_uri(world, USTR("urn:garbage")); cannam@226: sord_node_free(world, garbage); cannam@226: sord_world_set_error_sink(world, expected_error, NULL); cannam@226: sord_node_free(world, garbage); cannam@226: sord_world_set_error_sink(world, unexpected_error, NULL); cannam@226: if (n_expected_errors != 2) { cannam@226: return test_fail("Successfully freed node twice\n"); cannam@226: } cannam@226: cannam@226: sord_world_set_error_sink(world, unexpected_error, NULL); cannam@226: cannam@226: // Check node flags are set properly cannam@226: SordNode* with_newline = sord_new_literal(world, NULL, USTR("a\nb"), NULL); cannam@226: if (!(sord_node_get_flags(with_newline) & SERD_HAS_NEWLINE)) { cannam@226: return test_fail("Newline flag not set\n"); cannam@226: } cannam@226: SordNode* with_quote = sord_new_literal(world, NULL, USTR("a\"b"), NULL); cannam@226: if (!(sord_node_get_flags(with_quote) & SERD_HAS_QUOTE)) { cannam@226: return test_fail("Quote flag not set\n"); cannam@226: } cannam@226: cannam@226: // Create with minimal indexing cannam@226: SordModel* sord = sord_new(world, SORD_SPO, false); cannam@226: generate(world, sord, n_quads, NULL); cannam@226: cannam@226: if (test_read(world, sord, NULL, n_quads)) { cannam@226: sord_free(sord); cannam@226: sord_world_free(world); cannam@226: return EXIT_FAILURE; cannam@226: } cannam@226: cannam@226: // Check adding tuples with NULL fields fails cannam@226: sord_world_set_error_sink(world, expected_error, NULL); cannam@226: const size_t initial_num_quads = sord_num_quads(sord); cannam@226: SordQuad tup = { 0, 0, 0, 0}; cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Added NULL tuple\n"); cannam@226: } cannam@226: tup[0] = uri(world, 1); cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Added tuple with NULL P and O\n"); cannam@226: } cannam@226: tup[1] = uri(world, 2); cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Added tuple with NULL O\n"); cannam@226: } cannam@226: cannam@226: if (sord_num_quads(sord) != initial_num_quads) { cannam@226: return test_fail("Num quads %zu != %zu\n", cannam@226: sord_num_quads(sord), initial_num_quads); cannam@226: } cannam@226: cannam@226: // Check adding tuples with an active iterator fails cannam@226: SordIter* iter = sord_begin(sord); cannam@226: tup[2] = uri(world, 3); cannam@226: if (sord_add(sord, tup)) { cannam@226: return test_fail("Added tuple with active iterator\n"); cannam@226: } cannam@226: cannam@226: // Check removing tuples with several active iterator fails cannam@226: SordIter* iter2 = sord_begin(sord); cannam@226: if (!sord_erase(sord, iter)) { cannam@226: return test_fail("Erased tuple with several active iterators\n"); cannam@226: } cannam@226: n_expected_errors = 0; cannam@226: sord_remove(sord, tup); cannam@226: if (n_expected_errors != 1) { cannam@226: return test_fail("Removed tuple with several active iterators\n"); cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: sord_iter_free(iter2); cannam@226: cannam@226: sord_world_set_error_sink(world, unexpected_error, NULL); cannam@226: cannam@226: // Check interning merges equivalent values cannam@226: SordNode* uri_id = sord_new_uri(world, USTR("http://example.org")); cannam@226: SordNode* blank_id = sord_new_blank(world, USTR("testblank")); cannam@226: SordNode* lit_id = sord_new_literal(world, uri_id, USTR("hello"), NULL); cannam@226: if (sord_node_get_type(uri_id) != SORD_URI) { cannam@226: return test_fail("URI node has incorrect type\n"); cannam@226: } else if (sord_node_get_type(blank_id) != SORD_BLANK) { cannam@226: return test_fail("Blank node has incorrect type\n"); cannam@226: } else if (sord_node_get_type(lit_id) != SORD_LITERAL) { cannam@226: return test_fail("Literal node has incorrect type\n"); cannam@226: } cannam@226: cannam@226: const size_t initial_num_nodes = sord_num_nodes(world); cannam@226: cannam@226: SordNode* uri_id2 = sord_new_uri(world, USTR("http://example.org")); cannam@226: SordNode* blank_id2 = sord_new_blank(world, USTR("testblank")); cannam@226: SordNode* lit_id2 = sord_new_literal(world, uri_id, USTR("hello"), NULL); cannam@226: if (uri_id2 != uri_id || !sord_node_equals(uri_id2, uri_id)) { cannam@226: fprintf(stderr, "Fail: URI interning failed (duplicates)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } else if (blank_id2 != blank_id cannam@226: || !sord_node_equals(blank_id2, blank_id)) { cannam@226: fprintf(stderr, "Fail: Blank node interning failed (duplicates)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } else if (lit_id2 != lit_id || !sord_node_equals(lit_id2, lit_id)) { cannam@226: fprintf(stderr, "Fail: Literal interning failed (duplicates)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: if (sord_num_nodes(world) != initial_num_nodes) { cannam@226: return test_fail("Num nodes %zu != %zu\n", cannam@226: sord_num_nodes(world), initial_num_nodes); cannam@226: } cannam@226: cannam@226: const uint8_t ni_hao[] = { 0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD }; cannam@226: SordNode* chello = sord_new_literal(world, NULL, ni_hao, "cmn"); cannam@226: cannam@226: // Test literal length cannam@226: size_t n_bytes; cannam@226: size_t n_chars; cannam@226: const uint8_t* str = sord_node_get_string_counted(lit_id2, &n_bytes); cannam@226: if (strcmp((const char*)str, "hello")) { cannam@226: return test_fail("Literal node corrupt\n"); cannam@226: } else if (n_bytes != strlen("hello")) { cannam@226: return test_fail("ASCII literal byte count incorrect\n"); cannam@226: } cannam@226: cannam@226: str = sord_node_get_string_measured(lit_id2, &n_bytes, &n_chars); cannam@226: if (n_bytes != strlen("hello") || n_chars != strlen("hello")) { cannam@226: return test_fail("ASCII literal measured length incorrect\n"); cannam@226: } cannam@226: cannam@226: str = sord_node_get_string_measured(chello, &n_bytes, &n_chars); cannam@226: if (n_bytes != 6) { cannam@226: return test_fail("Multi-byte literal byte count incorrect\n"); cannam@226: } else if (n_chars != 2) { cannam@226: return test_fail("Multi-byte literal character count incorrect\n"); cannam@226: } cannam@226: cannam@226: // Check interning doesn't clash non-equivalent values cannam@226: SordNode* uri_id3 = sord_new_uri(world, USTR("http://example.orgX")); cannam@226: SordNode* blank_id3 = sord_new_blank(world, USTR("testblankX")); cannam@226: SordNode* lit_id3 = sord_new_literal(world, uri_id, USTR("helloX"), NULL); cannam@226: if (uri_id3 == uri_id || sord_node_equals(uri_id3, uri_id)) { cannam@226: fprintf(stderr, "Fail: URI interning failed (clash)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } else if (blank_id3 == blank_id || sord_node_equals(blank_id3, blank_id)) { cannam@226: fprintf(stderr, "Fail: Blank node interning failed (clash)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } else if (lit_id3 == lit_id || sord_node_equals(lit_id3, lit_id)) { cannam@226: fprintf(stderr, "Fail: Literal interning failed (clash)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: // Check literal interning cannam@226: SordNode* lit4 = sord_new_literal(world, NULL, USTR("hello"), NULL); cannam@226: SordNode* lit5 = sord_new_literal(world, uri_id2, USTR("hello"), NULL); cannam@226: SordNode* lit6 = sord_new_literal(world, NULL, USTR("hello"), "en-ca"); cannam@226: if (lit4 == lit5 || sord_node_equals(lit4, lit5) cannam@226: || lit4 == lit6 || sord_node_equals(lit4, lit6) cannam@226: || lit5 == lit6 || sord_node_equals(lit5, lit6)) { cannam@226: fprintf(stderr, "Fail: Literal interning failed (type/lang clash)\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: // Check relative URI construction cannam@226: SordNode* reluri = sord_new_relative_uri( cannam@226: world, USTR("a/b"), USTR("http://example.org/")); cannam@226: if (strcmp((const char*)sord_node_get_string(reluri), cannam@226: "http://example.org/a/b")) { cannam@226: fprintf(stderr, "Fail: Bad relative URI constructed: <%s>\n", cannam@226: sord_node_get_string(reluri)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: SordNode* reluri2 = sord_new_relative_uri( cannam@226: world, USTR("http://drobilla.net/"), USTR("http://example.org/")); cannam@226: if (strcmp((const char*)sord_node_get_string(reluri2), cannam@226: "http://drobilla.net/")) { cannam@226: fprintf(stderr, "Fail: Bad relative URI constructed: <%s>\n", cannam@226: sord_node_get_string(reluri)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: // Check comparison with NULL cannam@226: sord_node_free(world, uri_id); cannam@226: sord_node_free(world, blank_id); cannam@226: sord_node_free(world, lit_id); cannam@226: sord_node_free(world, uri_id2); cannam@226: sord_node_free(world, blank_id2); cannam@226: sord_node_free(world, lit_id2); cannam@226: sord_node_free(world, uri_id3); cannam@226: sord_node_free(world, blank_id3); cannam@226: sord_node_free(world, lit_id3); cannam@226: sord_free(sord); cannam@226: cannam@226: static const char* const index_names[6] = { cannam@226: "spo", "sop", "ops", "osp", "pso", "pos" cannam@226: }; cannam@226: cannam@226: for (int i = 0; i < 6; ++i) { cannam@226: sord = sord_new(world, (1 << i), false); cannam@226: printf("Testing Index `%s'\n", index_names[i]); cannam@226: generate(world, sord, n_quads, 0); cannam@226: if (test_read(world, sord, 0, n_quads)) cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: sord_free(sord); cannam@226: } cannam@226: cannam@226: static const char* const graph_index_names[6] = { cannam@226: "gspo", "gsop", "gops", "gosp", "gpso", "gpos" cannam@226: }; cannam@226: cannam@226: for (int i = 0; i < 6; ++i) { cannam@226: sord = sord_new(world, (1 << i), true); cannam@226: printf("Testing Index `%s'\n", graph_index_names[i]); cannam@226: SordNode* graph = uri(world, 42); cannam@226: generate(world, sord, n_quads, graph); cannam@226: if (test_read(world, sord, graph, n_quads)) cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: sord_free(sord); cannam@226: } cannam@226: cannam@226: // Test removing cannam@226: sord = sord_new(world, SORD_SPO, true); cannam@226: tup[0] = uri(world, 1); cannam@226: tup[1] = uri(world, 2); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); cannam@226: tup[3] = 0; cannam@226: sord_add(sord, tup); cannam@226: if (!sord_ask(sord, tup[0], tup[1], tup[2], tup[3])) { cannam@226: fprintf(stderr, "Failed to add tuple\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: sord_node_free(world, (SordNode*)tup[2]); cannam@226: tup[2] = sord_new_literal(world, 0, USTR("hi"), NULL); cannam@226: sord_add(sord, tup); cannam@226: sord_remove(sord, tup); cannam@226: if (sord_num_quads(sord) != 1) { cannam@226: fprintf(stderr, "Remove failed (%zu quads, expected 1)\n", cannam@226: sord_num_quads(sord)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: iter = sord_find(sord, tup); cannam@226: if (!sord_iter_end(iter)) { cannam@226: fprintf(stderr, "Found removed tuple\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: cannam@226: // Test double remove (silent success) cannam@226: sord_remove(sord, tup); cannam@226: cannam@226: // Load a couple graphs cannam@226: SordNode* graph42 = uri(world, 42); cannam@226: SordNode* graph43 = uri(world, 43); cannam@226: generate(world, sord, 1, graph42); cannam@226: generate(world, sord, 1, graph43); cannam@226: cannam@226: // Remove one graph via iterator cannam@226: SerdStatus st; cannam@226: iter = sord_search(sord, NULL, NULL, NULL, graph43); cannam@226: while (!sord_iter_end(iter)) { cannam@226: if ((st = sord_erase(sord, iter))) { cannam@226: fprintf(stderr, "Remove by iterator failed (%s)\n", cannam@226: serd_strerror(st)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: cannam@226: // Erase the first tuple (an element in the default graph) cannam@226: iter = sord_begin(sord); cannam@226: if (sord_erase(sord, iter)) { cannam@226: return test_fail("Failed to erase begin iterator on non-empty model\n"); cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: cannam@226: // Ensure only the other graph is left cannam@226: SordQuad quad; cannam@226: SordQuad pat = { 0, 0, 0, graph42 }; cannam@226: for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: sord_iter_get(iter, quad); cannam@226: if (!sord_quad_match(quad, pat)) { cannam@226: fprintf(stderr, "Graph removal via iteration failed\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: cannam@226: // Load file into two separate graphs cannam@226: sord_free(sord); cannam@226: sord = sord_new(world, SORD_SPO, true); cannam@226: env = serd_env_new(&base); cannam@226: SordNode* graph1 = sord_new_uri(world, USTR("http://example.org/graph1")); cannam@226: SordNode* graph2 = sord_new_uri(world, USTR("http://example.org/graph2")); cannam@226: SerdReader* reader = sord_new_reader(sord, env, SERD_TURTLE, graph1); cannam@226: if ((st = serd_reader_read_string(reader, USTR("

.")))) { cannam@226: fprintf(stderr, "Failed to read string (%s)\n", serd_strerror(st)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: serd_reader_free(reader); cannam@226: reader = sord_new_reader(sord, env, SERD_TURTLE, graph2); cannam@226: if ((st = serd_reader_read_string(reader, USTR("

.")))) { cannam@226: fprintf(stderr, "Failed to re-read string (%s)\n", serd_strerror(st)); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: serd_reader_free(reader); cannam@226: serd_env_free(env); cannam@226: cannam@226: // Ensure we only see triple once cannam@226: size_t n_triples = 0; cannam@226: for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: fprintf(stderr, "%s %s %s %s\n", cannam@226: sord_node_get_string(sord_iter_get_node(iter, SORD_SUBJECT)), cannam@226: sord_node_get_string(sord_iter_get_node(iter, SORD_PREDICATE)), cannam@226: sord_node_get_string(sord_iter_get_node(iter, SORD_OBJECT)), cannam@226: sord_node_get_string(sord_iter_get_node(iter, SORD_GRAPH))); cannam@226: cannam@226: ++n_triples; cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: if (n_triples != 1) { cannam@226: fprintf(stderr, "Found duplicate triple\n"); cannam@226: return finished(world, sord, EXIT_FAILURE); cannam@226: } cannam@226: cannam@226: // Test SPO iteration on an SOP indexed store cannam@226: sord_free(sord); cannam@226: sord = sord_new(world, SORD_SOP, false); cannam@226: generate(world, sord, 1, graph42); cannam@226: for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { cannam@226: ++n_triples; cannam@226: } cannam@226: sord_iter_free(iter); cannam@226: cannam@226: return finished(world, sord, EXIT_SUCCESS); cannam@226: }