#include "test.h" #include "graph.h" #include "assets/triples.h" static int _graph_new (LSUP_store_type type) { LSUP_Graph *gr; gr = LSUP_graph_new (type); ASSERT (gr != NULL, "Error creating graph!"); EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1")); EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1"); ASSERT ( strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!"); EXPECT_INT_EQ (LSUP_graph_size (gr), 0); LSUP_graph_free (gr); return 0; } static int _graph_add (LSUP_store_type type) { LSUP_Triple *trp = create_triples(); LSUP_Graph *gr = LSUP_graph_new (type); ASSERT (gr != NULL, "Error creating graph!"); size_t ct; LSUP_graph_add_trp (gr, trp, &ct); EXPECT_INT_EQ (ct, 8); EXPECT_INT_EQ (LSUP_graph_size (gr), 8); for (int i = 0; i < sizeof (trp); i++) { printf ("checking triple #%d... ", i); ASSERT (LSUP_graph_contains (gr, trp + i), "Triple not in graph!"); printf ("OK.\n"); } LSUP_Triple *missing_trp = LSUP_triple_new (trp[1].s, trp[6].p, trp[4].o); ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!"); free (missing_trp); free_triples (trp); // gr copied data. LSUP_graph_free (gr); return 0; } static int _graph_remove (LSUP_store_type type) { LSUP_Triple *trp = create_triples(); LSUP_Graph *gr = LSUP_graph_new (type); size_t ct; LSUP_graph_add_trp (gr, trp, &ct); EXPECT_INT_EQ (ct, 8); EXPECT_INT_EQ (LSUP_graph_size (gr), 8); LSUP_Triple *spo = LSUP_triple_new (trp[0].s, NULL, NULL); LSUP_graph_remove (gr, spo, &ct); ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!"); ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!"); ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!"); ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!"); ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!"); ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!"); ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!"); ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!"); EXPECT_INT_EQ (LSUP_graph_size (gr), 3); LSUP_triple_free (spo); free_triples (trp); // gr copied data. LSUP_graph_free (gr); return 0; } static int test_graph_new() { if (_graph_new (LSUP_STORE_MEM) != 0) return -1; if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1; return 0; } static int test_graph_add() { if (_graph_add (LSUP_STORE_MEM) != 0) return -1; if (_graph_add (LSUP_STORE_MDB_TMP) != 0) return -1; return 0; } static int test_graph_remove() { if (_graph_remove (LSUP_STORE_MEM) != 0) return -1; if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1; return 0; } int graph_tests() { RUN (test_graph_new); RUN (test_graph_add); RUN (test_graph_remove); return 0; }