#include "test.h" #include "graph.h" #include "assets/triples.h" #define N_LUT 12 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_lookup (LSUP_store_type type) { LSUP_Triple *trp = create_triples(); // Lookup triples. LSUP_Term *lu_trp[N_LUT][3] = { {trp[0].s, NULL, NULL}, // 5 matches {NULL, trp[2].p, NULL}, // 3 matches {NULL, NULL, trp[5].o}, // 2 matches {trp[0].s, trp[0].p, NULL}, // 1 match {NULL, trp[0].p, trp[0].o}, // 1 match {trp[0].s, trp[2].p, trp[5].o}, // 1 match {trp[0].p, NULL, NULL}, // 0 matches {NULL, trp[2].s, NULL}, // 0 matches {NULL, NULL, trp[5].p}, // 0 matches {trp[2].s, trp[6].p, NULL}, // 0 matches {NULL, trp[1].p, trp[5].o}, // 0 matches {trp[2].s, trp[2].p, trp[5].o}, // 0 matches }; // Lookup result counts. size_t lu_ct[N_LUT] = { 5, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; /* TODO // Index of lookup matches from trp. size_t lu_match[N_LUT][5] = { {0, 3, 4, 5, 7}, {2, 4, 7}, {5, 7}, {0}, {0}, {7}, {}, {}, {}, }; // Index of lookup non-matches from trp. size_t lu_no_match[N_LUT][8] = { {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, }; */ 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; for (int i = 0; i < N_LUT; i++) { printf ("Checking tiple #%d on %d... ", i, type); LSUP_GraphIterator *it = LSUP_graph_lookup ( gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct); if (type != LSUP_STORE_MEM) // TODO not implemented in htable. EXPECT_INT_EQ (ct, lu_ct[i]); printf ("done.\n"); /* TODO for (int j = 0; LSUP_graph_iter_next != LSUP_END; j++) { ASSERT ( LSUP_graph_contains (trp[lu_match[j]]), "Triple not found!"); ASSERT ( !(LSUP_graph_contains (trp[lu_no_match[j]])), "Unexpected triple found!"); } */ LSUP_graph_iter_free (it); }; free_triples (trp); 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_graph_remove (gr, trp[0].s, NULL, NULL, &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); free_triples (trp); // gr copied data. LSUP_graph_free (gr); // TODO Test complete removal of triples from index when they are not // in another context. 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_lookup() { if (_graph_lookup (LSUP_STORE_MEM) != 0) return -1; if (_graph_lookup (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_lookup); RUN (test_graph_remove); return 0; }