#include "test.h" #include "graph.h" #define NUM_TRP 10 static int _create_triples(LSUP_Triple *trp) { // These constitute overall 10 individual triples, 8 unique. trp[0].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL); trp[0].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:0", NULL, NULL); trp[0].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:0", NULL, NULL); trp[1].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:1", NULL, NULL); trp[1].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:1", NULL, NULL); trp[1].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:1", NULL, NULL); trp[2].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:2", NULL, NULL); trp[2].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:2", NULL, NULL); trp[2].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:2", NULL, NULL); trp[3].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL); trp[3].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:1", NULL, NULL); trp[3].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:2", NULL, NULL); trp[4].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL); trp[4].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:2", NULL, NULL); trp[4].o = LSUP_term_new( LSUP_TERM_LITERAL, "String 1", NULL, NULL); trp[5].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL); trp[5].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:5", NULL, NULL); trp[5].o = LSUP_term_new( LSUP_TERM_LITERAL, "String 2", "xsd:string", NULL); trp[6].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:1", NULL, NULL); trp[6].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:6", NULL, NULL); trp[6].o = LSUP_term_new( LSUP_TERM_LITERAL, "String 3", "xsd:string", "es-ES"); // Let's reuse pointers. Do not double-free. trp[7].s = trp[0].s; // trp[7].p = trp[2].p; // trp[7].o = trp[5].o; // "String 2"^^xsd:string // Duplicate of trp[7]. Do not double-free. trp[8].s = trp[0].s; trp[8].p = trp[2].p; trp[8].o = trp[5].o; // Duplicate of trp[7] from different pointers with same value. // Do not double-free. trp[9].s = trp[5].s; trp[9].p = trp[4].p; trp[9].o = trp[5].o; return 0; } static void _free_triples(LSUP_Triple *trp) { // Last three triples are second pointers. for(int i=0; i < NUM_TRP - 3; i++) { LSUP_term_free(trp[i].s); LSUP_term_free(trp[i].p); LSUP_term_free(trp[i].o); } free(trp); } static int test_graph_heap() { LSUP_Graph *gr = LSUP_graph_new(10, "urn:gr:1", LSUP_STORE_MEM); ASSERT(strcmp(LSUP_graph_uri(gr), "urn:gr:1") == 0, "Graph URI mismatch!"); EXPECT_INT_EQ(LSUP_graph_capacity(gr), 10); EXPECT_INT_EQ(LSUP_graph_size(gr), 0); LSUP_graph_free(gr); return 0; } static int test_graph_add() { LSUP_Triple *trp = malloc(NUM_TRP * sizeof(LSUP_Triple)); _create_triples(trp); LSUP_Graph *gr = LSUP_graph_new(NUM_TRP + 2, "urn:gr:2", LSUP_STORE_MEM); LSUP_graph_add(gr, trp, NUM_TRP); _free_triples(trp); // gr takes ownership of data. EXPECT_INT_EQ(LSUP_graph_capacity(gr), NUM_TRP + 2); EXPECT_INT_EQ(LSUP_graph_size(gr), 8); LSUP_graph_free(gr); return 0; } static int test_graph_add_100k() { size_t nt = 10000; LSUP_Triple *trp = malloc(nt * sizeof(LSUP_Triple)); for (size_t i; i < nt; i++) { //printf("i: %lu\n", i); trp[i].s = LSUP_term_new( LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL); trp[i].p = LSUP_term_new( LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL); trp[i].o = LSUP_term_new( LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL); } TRACE(STR, "Triples generated."); LSUP_Graph *gr = LSUP_graph_new(nt, NULL, LSUP_STORE_MEM); LSUP_graph_add(gr, trp, nt); TRACE(STR, "Graph populated."); _free_triples(trp); // gr takes ownership of data. LSUP_graph_free(gr); return 0; } int graph_tests() { RUN(test_graph_heap); RUN(test_graph_add); RUN(test_graph_add_100k); return 0; }