// Copyright 2011-2020 David Robillard // SPDX-License-Identifier: ISC #undef NDEBUG #include "serd/caret.h" #include "serd/node.h" #include "serd/statement.h" #include #include #define NS_EG "http://example.org/" static void test_new(void) { SerdNode* const u = serd_node_new(NULL, serd_a_uri_string(NS_EG "s")); SerdNode* const b = serd_node_new(NULL, serd_a_blank_string("b0")); SerdNode* const l = serd_node_new(NULL, serd_a_string("str")); assert(!serd_statement_new(NULL, u, b, u, NULL, NULL)); assert(!serd_statement_new(NULL, l, u, u, NULL, NULL)); assert(!serd_statement_new(NULL, l, u, b, u, NULL)); assert(!serd_statement_new(NULL, u, l, b, NULL, NULL)); assert(!serd_statement_new(NULL, u, l, b, u, NULL)); assert(!serd_statement_new(NULL, u, u, u, l, NULL)); serd_node_free(NULL, l); serd_node_free(NULL, b); serd_node_free(NULL, u); } static void test_copy(void) { assert(!serd_statement_copy(NULL, NULL)); SerdNode* const f = serd_node_new(NULL, serd_a_string("file")); SerdNode* const s = serd_node_new(NULL, serd_a_uri_string(NS_EG "s")); SerdNode* const p = serd_node_new(NULL, serd_a_uri_string(NS_EG "p")); SerdNode* const o = serd_node_new(NULL, serd_a_uri_string(NS_EG "o")); SerdNode* const g = serd_node_new(NULL, serd_a_uri_string(NS_EG "g")); SerdCaret* const caret = serd_caret_new(NULL, f, 1, 1); SerdStatement* const statement = serd_statement_new(NULL, s, p, o, g, caret); SerdStatement* const copy = serd_statement_copy(NULL, statement); assert(serd_statement_equals(copy, statement)); assert(serd_caret_equals(serd_statement_caret(copy), caret)); serd_statement_free(NULL, copy); serd_caret_free(NULL, caret); serd_statement_free(NULL, statement); serd_node_free(NULL, g); serd_node_free(NULL, o); serd_node_free(NULL, p); serd_node_free(NULL, s); serd_node_free(NULL, f); } static void test_free(void) { serd_statement_free(NULL, NULL); } static void test_fields(void) { SerdNode* const f = serd_node_new(NULL, serd_a_string("file")); SerdNode* const s = serd_node_new(NULL, serd_a_uri_string(NS_EG "s")); SerdNode* const p = serd_node_new(NULL, serd_a_uri_string(NS_EG "p")); SerdNode* const o = serd_node_new(NULL, serd_a_uri_string(NS_EG "o")); SerdNode* const g = serd_node_new(NULL, serd_a_uri_string(NS_EG "g")); SerdCaret* const caret = serd_caret_new(NULL, f, 1, 1); SerdStatement* const statement = serd_statement_new(NULL, s, p, o, g, caret); assert(serd_statement_equals(statement, statement)); assert(!serd_statement_equals(statement, NULL)); assert(!serd_statement_equals(NULL, statement)); assert(serd_statement_node(statement, SERD_SUBJECT) == s); assert(serd_statement_node(statement, SERD_PREDICATE) == p); assert(serd_statement_node(statement, SERD_OBJECT) == o); assert(serd_statement_node(statement, SERD_GRAPH) == g); assert(serd_statement_subject(statement) == s); assert(serd_statement_predicate(statement) == p); assert(serd_statement_object(statement) == o); assert(serd_statement_graph(statement) == g); assert(serd_statement_caret(statement) != caret); assert(serd_caret_equals(serd_statement_caret(statement), caret)); assert(serd_statement_matches(statement, s, p, o, g)); assert(serd_statement_matches(statement, NULL, p, o, g)); assert(serd_statement_matches(statement, s, NULL, o, g)); assert(serd_statement_matches(statement, s, p, NULL, g)); assert(serd_statement_matches(statement, s, p, o, NULL)); assert(!serd_statement_matches(statement, o, NULL, NULL, NULL)); assert(!serd_statement_matches(statement, NULL, o, NULL, NULL)); assert(!serd_statement_matches(statement, NULL, NULL, s, NULL)); assert(!serd_statement_matches(statement, NULL, NULL, NULL, s)); SerdStatement* const diff_s = serd_statement_new(NULL, o, p, o, g, caret); assert(!serd_statement_equals(statement, diff_s)); serd_statement_free(NULL, diff_s); SerdStatement* const diff_p = serd_statement_new(NULL, s, o, o, g, caret); assert(!serd_statement_equals(statement, diff_p)); serd_statement_free(NULL, diff_p); SerdStatement* const diff_o = serd_statement_new(NULL, s, p, s, g, caret); assert(!serd_statement_equals(statement, diff_o)); serd_statement_free(NULL, diff_o); SerdStatement* const diff_g = serd_statement_new(NULL, s, p, o, s, caret); assert(!serd_statement_equals(statement, diff_g)); serd_statement_free(NULL, diff_g); serd_statement_free(NULL, statement); serd_caret_free(NULL, caret); serd_node_free(NULL, g); serd_node_free(NULL, o); serd_node_free(NULL, p); serd_node_free(NULL, s); serd_node_free(NULL, f); } int main(void) { test_new(); test_copy(); test_free(); test_fields(); return 0; }