test_graph.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. #include "lsup/graph.h"
  2. #include "test.h"
  3. #include "assets/triples.h"
  4. #define N_LUT 13
  5. static int
  6. _graph_new (LSUP_StoreType type)
  7. {
  8. LSUP_Graph *gr;
  9. LSUP_Store *store = NULL;
  10. if (type == LSUP_STORE_HTABLE) {
  11. gr = LSUP_graph_new (NULL, NULL, NULL);
  12. } else {
  13. store = LSUP_store_new (type, NULL, 0, true);
  14. gr = LSUP_graph_new (store, NULL, NULL);
  15. }
  16. ASSERT (gr != NULL, "Error creating graph!");
  17. EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1"));
  18. EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
  19. ASSERT (
  20. strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0,
  21. "Graph URI mismatch!");
  22. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  23. // Check that setup function is idempotent with clear == false.
  24. const LSUP_StoreInt *sif = LSUP_store_int (type);
  25. if (sif->setup_fn) EXPECT_INT_EQ (
  26. sif->setup_fn (NULL, false), LSUP_NOACTION);
  27. LSUP_graph_free (gr);
  28. LSUP_store_free (store);
  29. return 0;
  30. }
  31. /// Test creating a graph with a namespaced URI.
  32. static int
  33. _graph_ns_uri (LSUP_StoreType type)
  34. {
  35. const LSUP_StoreInt *sif = LSUP_store_int (type);
  36. const char *nsdata[][2] = {
  37. {"ns1", "urn:ns1#"},
  38. {"ns2", "urn:ns2#"},
  39. {"ns3", "urn:ns3#"},
  40. {NULL}
  41. };
  42. LSUP_Store *store = NULL;
  43. if (sif->features & LSUP_STORE_PERM)
  44. store = LSUP_store_new (type, NULL, 0, true);
  45. log_info("Testing NSM for graph store type: %d", type);
  46. LSUP_NSMap *nsm = LSUP_nsmap_new();
  47. for (int i = 0; nsdata[i][0]; i++)
  48. EXPECT_PASS (LSUP_nsmap_add (nsm, nsdata[i][0], nsdata[i][1]));
  49. LSUP_Graph *gr = LSUP_graph_new (store, "ns1:gr1", nsm);
  50. ASSERT (gr != NULL, "Error creating graph!");
  51. ASSERT (
  52. LSUP_graph_uri (gr)->type == LSUP_TERM_NS_IRIREF,
  53. "Namespace prefixing failed!");
  54. LSUP_Term *comp = LSUP_iriref_new ("urn:ns1#gr1", NULL);
  55. ASSERT (
  56. LSUP_term_equals (LSUP_graph_uri (gr), comp),
  57. "Namespaced graph URI is incorrect!" );
  58. LSUP_term_free (comp);
  59. LSUP_graph_free (gr);
  60. LSUP_nsmap_free (nsm);
  61. LSUP_store_free (store);
  62. return 0;
  63. }
  64. static int
  65. _graph_add (LSUP_StoreType type)
  66. {
  67. LSUP_Triple **trp = create_triples();
  68. LSUP_Graph *gr;
  69. LSUP_Store *store;
  70. if (type == LSUP_STORE_HTABLE) store = NULL;
  71. else store = LSUP_store_new (type, NULL, 0, true);
  72. gr = LSUP_graph_new (store, NULL, NULL);
  73. ASSERT (gr != NULL, "Error creating graph!");
  74. size_t ct;
  75. LSUP_graph_add (gr, trp, &ct);
  76. EXPECT_INT_EQ (ct, 8);
  77. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  78. for (int i = 0; i < sizeof (trp); i++) {
  79. log_info ("checking triple #%d.", i);
  80. ASSERT (LSUP_graph_contains (gr, trp[i]), "Triple not in graph!");
  81. }
  82. LSUP_Triple *missing_trp = LSUP_triple_new (
  83. trp[1]->s, trp[6]->p, trp[4]->o);
  84. ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
  85. free (missing_trp);
  86. free_triples (trp); // gr copied data.
  87. // Check size again after freeing triples.
  88. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  89. LSUP_Graph *gr2 = LSUP_graph_new (NULL, NULL, NULL);
  90. // Test equality against empty graph.
  91. ASSERT (!LSUP_graph_equals (gr, gr2), "Graphs should not be equal!");
  92. ASSERT (!LSUP_graph_equals (gr2, gr), "Graphs should not be equal!");
  93. LSUP_graph_free (gr);
  94. LSUP_graph_free (gr2);
  95. LSUP_store_free (store);
  96. return 0;
  97. }
  98. static int
  99. _graph_get (LSUP_StoreType type)
  100. {
  101. const LSUP_StoreInt *sif = LSUP_store_int (type);
  102. // Skip if the store doesn't support contexts.
  103. if (!(sif->features & LSUP_STORE_CTX)) return 0;
  104. LSUP_Triple **trp = create_triples();
  105. LSUP_Store *store;
  106. if (type == LSUP_STORE_HTABLE) store = NULL;
  107. else store = LSUP_store_new (type, NULL, 0, true);
  108. LSUP_Graph
  109. *gr1 = LSUP_graph_new (store, NULL, NULL),
  110. *gr2 = LSUP_graph_new (store, NULL, NULL);
  111. ASSERT (gr1 != NULL, "Error creating graph!");
  112. ASSERT (gr2 != NULL, "Error creating graph!");
  113. // Add 2 groups of triples to different graphs.
  114. void *it1 = LSUP_graph_add_init (gr1);
  115. for (size_t i = 0; i < 5; i++) {
  116. LSUP_graph_add_iter(it1, trp[i]);
  117. }
  118. LSUP_graph_add_done (it1);
  119. void *it2 = LSUP_graph_add_init (gr2);
  120. for (size_t i = 5; i < NUM_TRP; i++) {
  121. LSUP_graph_add_iter(it2, trp[i]);
  122. }
  123. LSUP_graph_add_done (it2);
  124. free_triples (trp);
  125. EXPECT_INT_EQ (LSUP_graph_size (gr1), 5);
  126. EXPECT_INT_EQ (LSUP_graph_size (gr2), 3);
  127. size_t ct3, ct4;
  128. LSUP_Graph
  129. *gr3 = LSUP_graph_get (store, LSUP_graph_uri (gr1), &ct3),
  130. *gr4 = LSUP_graph_get (store, LSUP_graph_uri (gr2), &ct4);
  131. EXPECT_INT_EQ (LSUP_graph_size (gr3), LSUP_graph_size (gr1));
  132. EXPECT_INT_EQ (LSUP_graph_size (gr4), LSUP_graph_size (gr2));
  133. ASSERT (!LSUP_graph_equals (gr1, gr2), "Graphs 1 and 2 are equal!");
  134. ASSERT (!LSUP_graph_equals (gr3, gr4), "Graphs 3 and 4 are equal!");
  135. ASSERT (LSUP_graph_equals (gr1, gr3), "Graphs 1 and 3 are not equal!");
  136. ASSERT (LSUP_graph_equals (gr2, gr4), "Graphs 2 and 4 are not equal!");
  137. LSUP_graph_free (gr1);
  138. LSUP_graph_free (gr2);
  139. LSUP_graph_free (gr3);
  140. LSUP_graph_free (gr4);
  141. LSUP_store_free (store);
  142. return 0;
  143. }
  144. static int
  145. _graph_link_map (LSUP_StoreType type)
  146. {
  147. LSUP_Triple **trp = create_triples();
  148. LSUP_Store *store;
  149. if (type == LSUP_STORE_HTABLE) store = NULL;
  150. else store = LSUP_store_new (type, NULL, 0, true);
  151. LSUP_Graph *gr = LSUP_graph_new (store, NULL, NULL);
  152. size_t ct;
  153. LSUP_graph_add (gr, trp, &ct);
  154. LSUP_LinkMap *lmap;
  155. LSUP_LinkMapIterator *lmit ;
  156. //LSUP_Term *k = NULL;
  157. LSUP_TermSet *ts = NULL; // Term set being iterated in link map loop.
  158. LSUP_Term *k_res[9] = {NULL}; // Collected keys.
  159. LSUP_Term *ts_res[9] = {NULL}; // Collected terms in term set in loop.
  160. size_t i = 0, j = 0;
  161. /*
  162. LSUP_Term *po[6][4] = {
  163. {trp[0]->p, trp[0]->o, NULL},
  164. {trp[3]->p, trp[3]->o, NULL},
  165. {trp[4]->p, trp[4]->o, trp[7]->o, NULL},
  166. {trp[5]->p, trp[5]->o, NULL},
  167. {NULL}
  168. };
  169. */
  170. // terms connected to subject, urn:s:0
  171. lmap = LSUP_graph_connections (gr, trp[0]->s, LSUP_LINK_OUTBOUND);
  172. lmit = LSUP_link_map_iter_new (lmap);
  173. while (LSUP_link_map_next (lmit, k_res + i, &ts) == LSUP_OK) {
  174. while (LSUP_term_set_next (ts, &j, ts_res + j) == LSUP_OK);
  175. /*
  176. // TODO test exact terms. This requires a cross-check.
  177. ASSERT (
  178. LSUP_term_equals (k_res[i], po[i][0]),
  179. "Wrong term in link map!");
  180. for (size_t k = 1; po[i][k]; k++)
  181. ASSERT (
  182. LSUP_term_equals (ts_res[k - 1], po[i][k]),
  183. "Wrong term in term set!");
  184. */
  185. i++;
  186. }
  187. LSUP_link_map_iter_free (lmit);
  188. LSUP_link_map_free (lmap);
  189. EXPECT_INT_EQ (i, 4);
  190. i = 0; j = 0;
  191. memset (k_res, 0, sizeof (k_res));
  192. memset (ts_res, 0, sizeof (ts_res));
  193. /*
  194. LSUP_Term *so[3][3] = {
  195. {trp[1]->s, trp[1]->o, NULL},
  196. {trp[3]->s, trp[3]->o, NULL},
  197. };
  198. */
  199. // terms connected to predicate, urn:p:1
  200. lmap = LSUP_graph_connections (gr, trp[1]->p, LSUP_LINK_EDGE);
  201. lmit = LSUP_link_map_iter_new (lmap);
  202. while (LSUP_link_map_next (lmit, k_res + i, &ts) == LSUP_OK) {
  203. while (LSUP_term_set_next (ts, &j, ts_res + j) == LSUP_OK);
  204. /*
  205. ASSERT (
  206. LSUP_term_equals (k_res[i], so[i][0]),
  207. "Wrong term in link map!");
  208. for (size_t k = 1; so[i][k]; k++)
  209. ASSERT (
  210. LSUP_term_equals (ts_res[k - 1], so[i][k]),
  211. "Wrong term in term set!");
  212. */
  213. i++;
  214. }
  215. LSUP_link_map_iter_free (lmit);
  216. LSUP_link_map_free (lmap);
  217. EXPECT_INT_EQ (i, 2);
  218. i = 0; j = 0;
  219. memset (k_res, 0, sizeof (k_res));
  220. memset (ts_res, 0, sizeof (ts_res));
  221. /*
  222. LSUP_Term *sp[1][3] = {
  223. {trp[6]->s, trp[6]->p, NULL},
  224. };
  225. */
  226. // terms connected to object, "String 1"@es-ES
  227. lmap = LSUP_graph_connections (gr, trp[6]->o, LSUP_LINK_INBOUND);
  228. lmit = LSUP_link_map_iter_new (lmap);
  229. while (LSUP_link_map_next (lmit, k_res + i, &ts) == LSUP_OK) {
  230. while (LSUP_term_set_next (ts, &j, ts_res + j) == LSUP_OK);
  231. /*
  232. ASSERT (
  233. LSUP_term_equals (k_res[i], sp[i][0]),
  234. "Wrong term in link map!");
  235. for (size_t k = 1; sp[i][k]; k++)
  236. ASSERT (
  237. LSUP_term_equals (ts_res[k - 1], sp[i][k]),
  238. "Wrong term in term set!");
  239. */
  240. i++;
  241. }
  242. LSUP_link_map_iter_free (lmit);
  243. LSUP_link_map_free (lmap);
  244. EXPECT_INT_EQ (i, 1);
  245. free_triples (trp);
  246. LSUP_graph_free (gr);
  247. LSUP_store_free (store);
  248. return 0;
  249. }
  250. static int
  251. _graph_bool_ops (LSUP_StoreType type)
  252. {
  253. const LSUP_StoreInt *sif = LSUP_store_int (type);
  254. // Skip if the store doesn't support contexts.
  255. if (!(sif->features & LSUP_STORE_CTX)) return 0;
  256. LSUP_Triple **trp = create_triples();
  257. LSUP_Store *store;
  258. if (type == LSUP_STORE_HTABLE) store = NULL;
  259. else store = LSUP_store_new (type, NULL, 0, true);
  260. LSUP_Graph
  261. *gr1 = LSUP_graph_new (store, NULL, NULL),
  262. *gr2 = LSUP_graph_new (store, NULL, NULL),
  263. *gr_dest;
  264. // Add 2 groups of triples to different graphs.
  265. void *it;
  266. it = LSUP_graph_add_init (gr1);
  267. for (size_t i = 0; i < 4; i++) {
  268. LSUP_graph_add_iter(it, trp[i]);
  269. }
  270. LSUP_graph_add_done (it);
  271. // Skip duplicate triples, we already tested those.
  272. // trp[3] is in both graphs.
  273. it = LSUP_graph_add_init (gr2);
  274. for (size_t i = 3; i < 8; i++) {
  275. LSUP_graph_add_iter(it, trp[i]);
  276. }
  277. LSUP_graph_add_done (it);
  278. // Test union.
  279. gr_dest = LSUP_graph_new (store, NULL, NULL);
  280. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_UNION, gr1, gr2, gr_dest));
  281. for (size_t i = 0; i < 8; i++)
  282. ASSERT (LSUP_graph_contains (gr_dest, trp[i]), "Union test failed!");
  283. LSUP_graph_free (gr_dest);
  284. // Test subtraction.
  285. gr_dest = LSUP_graph_new (store, NULL, NULL);
  286. EXPECT_PASS (LSUP_graph_bool_op (
  287. LSUP_BOOL_SUBTRACTION, gr1, gr2, gr_dest));
  288. for (size_t i = 0; i < 3; i++)
  289. ASSERT (LSUP_graph_contains (
  290. gr_dest, trp[i]), "Subtraction test is missing triples!");
  291. for (size_t i = 3; i < 8; i++)
  292. ASSERT (!LSUP_graph_contains (
  293. gr_dest, trp[i]), "Subtraction test has excess triples!");
  294. LSUP_graph_free (gr_dest);
  295. gr_dest = LSUP_graph_new (store, NULL, NULL);
  296. EXPECT_PASS (LSUP_graph_bool_op (
  297. LSUP_BOOL_SUBTRACTION, gr2, gr1, gr_dest));
  298. for (size_t i = 0; i < 4; i++)
  299. ASSERT (!LSUP_graph_contains (
  300. gr_dest, trp[i]), "Subtraction test is missing triples!");
  301. for (size_t i = 4; i < 8; i++)
  302. ASSERT (LSUP_graph_contains (
  303. gr_dest, trp[i]), "Subtraction test has excess triples!");
  304. LSUP_graph_free (gr_dest);
  305. // Test intersection.
  306. gr_dest = LSUP_graph_new (store, NULL, NULL);
  307. EXPECT_PASS (LSUP_graph_bool_op (
  308. LSUP_BOOL_INTERSECTION, gr1, gr2, gr_dest));
  309. for (size_t i = 0; i < 3; i++)
  310. ASSERT (!LSUP_graph_contains (
  311. gr_dest, trp[i]), "Intersection is missing triples!");
  312. ASSERT (LSUP_graph_contains (
  313. gr_dest, trp[3]), "Intersection test failed!");
  314. for (size_t i = 4; i < 8; i++)
  315. ASSERT (!LSUP_graph_contains (
  316. gr_dest, trp[i]), "Intersection test has excess triples!");
  317. LSUP_graph_free (gr_dest);
  318. // Test XOR.
  319. gr_dest = LSUP_graph_new (store, NULL, NULL);
  320. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, gr_dest));
  321. for (size_t i = 0; i < 3; i++)
  322. ASSERT (LSUP_graph_contains (
  323. gr_dest, trp[i]), "XOR test is missing triples!");
  324. ASSERT (!LSUP_graph_contains (
  325. gr_dest, trp[3]), "XOR test has excess triples!");
  326. for (size_t i = 4; i < 8; i++)
  327. ASSERT (LSUP_graph_contains (
  328. gr_dest, trp[i]), "XOR test is missing triples!");
  329. LSUP_graph_free (gr_dest);
  330. // Test union with result graph as one of the sources.
  331. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_UNION, gr1, gr2, gr1));
  332. for (size_t i = 0; i < 8; i++)
  333. ASSERT (LSUP_graph_contains (gr1, trp[i]), "Self-union test failed!");
  334. LSUP_graph_free (gr1);
  335. LSUP_graph_free (gr2);
  336. free_triples (trp);
  337. LSUP_store_free (store);
  338. return 0;
  339. }
  340. static int
  341. _graph_lookup (LSUP_StoreType type)
  342. {
  343. LSUP_Triple **trp = create_triples();
  344. // Lookup triples.
  345. LSUP_Term *lu_trp[N_LUT][3] = {
  346. {NULL, NULL, NULL}, // 8 matches
  347. {trp[0]->s, NULL, NULL}, // 5 matches
  348. {NULL, trp[2]->p, NULL}, // 3 matches
  349. {NULL, NULL, trp[5]->o}, // 2 matches
  350. {trp[0]->s, trp[0]->p, NULL}, // 1 match
  351. {NULL, trp[0]->p, trp[0]->o}, // 1 match
  352. {trp[0]->s, trp[2]->p, trp[5]->o}, // 1 match
  353. {trp[0]->p, NULL, NULL}, // 0 matches
  354. {NULL, trp[2]->s, NULL}, // 0 matches
  355. {NULL, NULL, trp[5]->p}, // 0 matches
  356. {trp[2]->s, trp[6]->p, NULL}, // 0 matches
  357. {NULL, trp[1]->p, trp[5]->o}, // 0 matches
  358. {trp[2]->s, trp[2]->p, trp[5]->o}, // 0 matches
  359. };
  360. // Lookup result counts.
  361. size_t lu_ct[N_LUT] = {
  362. 8,
  363. 5, 3, 2,
  364. 1, 1, 1,
  365. 0, 0, 0,
  366. 0, 0, 0
  367. };
  368. /* TODO
  369. // Index of lookup matches from trp.
  370. size_t lu_match[N_LUT][8] = {
  371. {0, 1, 2, 3, 4, 5, 6, 7},
  372. {0, 3, 4, 5, 7}, {2, 4, 7}, {5, 7},
  373. {0}, {0}, {7},
  374. {}, {}, {},
  375. {}, {}, {},
  376. };
  377. // Index of lookup non-matches from trp.
  378. size_t lu_no_match[N_LUT][8] = {
  379. {},
  380. {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6},
  381. {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6},
  382. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  383. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  384. };
  385. */
  386. LSUP_Graph *gr;
  387. LSUP_Store *store = NULL;
  388. if (type == LSUP_STORE_HTABLE) {
  389. gr = LSUP_graph_new (NULL, NULL, NULL);
  390. } else {
  391. store = LSUP_store_new (type, NULL, 0, true);
  392. gr = LSUP_graph_new (store, NULL, NULL);
  393. }
  394. size_t ct;
  395. LSUP_graph_add (gr, trp, &ct);
  396. EXPECT_INT_EQ (ct, 8);
  397. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  398. for (int i = 0; i < N_LUT; i++) {
  399. log_info ("Checking triple #%d on %d.", i, type);
  400. LSUP_GraphIterator *it = LSUP_graph_lookup (
  401. gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
  402. EXPECT_INT_EQ (ct, lu_ct[i]);
  403. // Verify that iteration count matches stat count.
  404. LSUP_Triple *spo = NULL;
  405. ct = 0;
  406. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  407. ct++;
  408. // TODO do something useful with the triple.
  409. LSUP_triple_free (spo);
  410. spo = NULL;
  411. }
  412. LSUP_triple_free (spo);
  413. EXPECT_INT_EQ (ct, lu_ct[i]);
  414. /* TODO
  415. for (int j = 0; j < 8; j++) {
  416. for (int k = 0; LSUP_graph_iter_next(it) != LSUP_END; k++) {
  417. ASSERT (
  418. LSUP_graph_contains (trp[lu_match[j]]),
  419. "Triple not found!");
  420. ASSERT (
  421. !(LSUP_graph_contains (trp[lu_no_match[j]])),
  422. "Unexpected triple found!");
  423. }
  424. }
  425. */
  426. LSUP_graph_iter_free (it);
  427. };
  428. #if 0
  429. // Enable to test print functionality and exit early.
  430. LSUP_graph_print (gr);
  431. return 1;
  432. #endif
  433. free_triples (trp);
  434. LSUP_graph_free (gr);
  435. LSUP_store_free (store);
  436. return 0;
  437. }
  438. static int
  439. _graph_remove (LSUP_StoreType type)
  440. {
  441. LSUP_Triple **trp = create_triples();
  442. LSUP_Graph *gr;
  443. LSUP_Store *store = NULL;
  444. if (type == LSUP_STORE_HTABLE) {
  445. gr = LSUP_graph_new (NULL, NULL, NULL);
  446. } else {
  447. store = LSUP_store_new (type, NULL, 0, true);
  448. gr = LSUP_graph_new (store, NULL, NULL);
  449. }
  450. size_t ct;
  451. LSUP_graph_add (gr, trp, &ct);
  452. EXPECT_INT_EQ (ct, 8);
  453. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  454. // Triples 0, 3, 4, 5, 7 will be removed.
  455. LSUP_graph_remove (gr, trp[0]->s, NULL, NULL, &ct);
  456. EXPECT_INT_EQ (ct, 5);
  457. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  458. ASSERT (!LSUP_graph_contains (gr, trp[0]), "Unexpected triple found!");
  459. ASSERT (LSUP_graph_contains (gr, trp[1]), "Triple not in graph!");
  460. ASSERT (LSUP_graph_contains (gr, trp[2]), "Triple not in graph!");
  461. ASSERT (!LSUP_graph_contains (gr, trp[3]), "Unexpected triple found!");
  462. ASSERT (!LSUP_graph_contains (gr, trp[4]), "Unexpected triple found!");
  463. ASSERT (!LSUP_graph_contains (gr, trp[5]), "Unexpected triple found!");
  464. ASSERT (LSUP_graph_contains (gr, trp[6]), "Triple not in graph!");
  465. ASSERT (!LSUP_graph_contains (gr, trp[7]), "Unexpected triple found!");
  466. free_triples (trp); // gr copied data.
  467. LSUP_graph_free (gr);
  468. LSUP_store_free (store);
  469. // TODO Test complete removal of triples from index when they are not
  470. // in another context.
  471. return 0;
  472. }
  473. static int
  474. _graph_txn (LSUP_StoreType type)
  475. {
  476. const LSUP_StoreInt *sif = LSUP_store_int (type);
  477. if (!(sif->features & LSUP_STORE_TXN)) return 0;
  478. LSUP_Triple **trp = create_triples();
  479. LSUP_Graph *gr;
  480. LSUP_Store *store =
  481. type == LSUP_STORE_HTABLE ? NULL
  482. : LSUP_store_new (type, NULL, 0, true);
  483. gr = LSUP_graph_new (store, NULL, NULL);
  484. void *txn;
  485. size_t ct;
  486. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  487. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  488. LSUP_store_abort (store, txn);
  489. // NOTE that ct reports the count before the txn was aborted. This is
  490. // intentional.
  491. EXPECT_INT_EQ (ct, 8);
  492. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  493. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  494. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  495. LSUP_store_commit (store, txn);
  496. EXPECT_INT_EQ (ct, 8);
  497. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  498. LSUP_graph_free (gr);
  499. LSUP_store_free (store);
  500. free_triples (trp); // gr copied data.
  501. return 0;
  502. }
  503. static int
  504. _graph_relative (LSUP_StoreType type)
  505. {
  506. LSUP_Term
  507. *s = LSUP_iriref_new ("http://onto.knowledgetx.com/gr1/s1", NULL),
  508. *s2 = LSUP_iriref_new ("http://onto.knowledgetx.com/gr2/s1", NULL),
  509. *p = LSUP_iriref_new ("http://onto.knowledgetx.com/vocab/p1", NULL),
  510. *o = LSUP_iriref_new ("http://onto.knowledgetx.com/gr1/o1", NULL),
  511. *o2 = LSUP_iriref_new ("http://onto.knowledgetx.com/gr2/o1", NULL),
  512. *c = LSUP_iriref_new ("http://onto.knowledgetx.com/gr1/", NULL),
  513. *rel_s = LSUP_iriref_relative (c, s),
  514. *rel_o = LSUP_iriref_relative (c, o);
  515. LSUP_Triple *spo[2] = {
  516. LSUP_triple_new (s, p, o),
  517. NULL
  518. };
  519. LSUP_Triple *rel_spo = LSUP_triple_new (rel_s, p, rel_o);
  520. LSUP_Store *store =
  521. type == LSUP_STORE_HTABLE ? NULL
  522. : LSUP_store_new (type, NULL, 0, true);
  523. LSUP_Graph *gr = LSUP_graph_new (store, c->data, NULL);
  524. size_t ct;
  525. LSUP_graph_add (gr, spo, &ct);
  526. // Both absolute and relative IRIs should be found.
  527. ASSERT (LSUP_graph_contains (gr, rel_spo), "Relative triple not found!");
  528. ASSERT (LSUP_graph_contains (gr, spo[0]), "Absolute triple not found!");
  529. // Change graph URI and verify that relative URIs are still found, and
  530. // that absolute URIs change with it.
  531. spo[0]->s = s2;
  532. LSUP_term_free (s);
  533. spo[0]->o = o2;
  534. LSUP_term_free (o);
  535. LSUP_graph_set_uri (gr, "http://onto.knowledgetx.com/gr2/");
  536. LSUP_term_free (c);
  537. ASSERT (LSUP_graph_contains (gr, rel_spo), "Relative triple not found!");
  538. ASSERT (LSUP_graph_contains (gr, spo[0]), "Absolute triple not found!");
  539. LSUP_triple_free (spo[0]);
  540. LSUP_term_free (rel_s);
  541. LSUP_term_free (rel_o);
  542. free (rel_spo);
  543. LSUP_graph_free (gr);
  544. LSUP_store_free (store);
  545. return 0;
  546. }
  547. static int
  548. _graph_list (LSUP_StoreType type)
  549. {
  550. const LSUP_StoreInt *sif = LSUP_store_int (type);
  551. if (!(sif->features & LSUP_STORE_CTX)) return 0;
  552. LSUP_Store *store = LSUP_store_new (type, NULL, 0, true);
  553. LSUP_Graph *gg[3] = {
  554. LSUP_graph_new (store, "urn:gr:1", NULL),
  555. LSUP_graph_new (store, "urn:gr:2", NULL),
  556. LSUP_graph_new (store, "urn:gr:3", NULL),
  557. };
  558. LSUP_Triple **trp = create_triples();
  559. LSUP_graph_add (gg[0], trp, NULL); // Add some triples to the 1st graph.
  560. LSUP_graph_add (gg[1], trp + 4, NULL); // Same with the 2nd graph.
  561. free_triples (trp);
  562. LSUP_TermSet *ts = LSUP_graph_list (store);
  563. ASSERT (ts != NULL, "Error creating context list!");
  564. EXPECT_INT_EQ (hashmap_count (ts), 2);
  565. // Check if first 2 graphs (with triples) are in the context set.
  566. LSUP_Hash key;
  567. key = LSUP_term_hash (LSUP_graph_uri (gg[0]));
  568. ASSERT (LSUP_term_set_get (ts, key) != NULL, "Context #1 not found!");
  569. key = LSUP_term_hash (LSUP_graph_uri (gg[1]));
  570. ASSERT (LSUP_term_set_get (ts, key) != NULL, "Context #2 not found!");
  571. key = LSUP_term_hash (LSUP_graph_uri (gg[2]));
  572. ASSERT (
  573. LSUP_term_set_get (ts, key) == NULL,
  574. "Empty context shoud not be here!");
  575. for (size_t i = 0; i < 3; i++) LSUP_graph_free (gg[i]);
  576. LSUP_term_set_free (ts);
  577. LSUP_store_free (store);
  578. return 0;
  579. }
  580. static int
  581. test_environment()
  582. {
  583. // The env should already be initialized and re-initializing it is idempotent.
  584. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  585. ASSERT (LSUP_init() > 0, "Error initializing environment!");
  586. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  587. // Tearing down is idempotent too.
  588. LSUP_done();
  589. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  590. LSUP_done();
  591. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  592. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  593. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  594. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  595. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  596. return 0;
  597. }
  598. static int test_graph_new() {
  599. #define ENTRY(a, b) \
  600. if (_graph_new (LSUP_STORE_##a) != 0) return -1;
  601. BACKEND_TBL
  602. #undef ENTRY
  603. return 0;
  604. }
  605. static int test_graph_ns_uri() {
  606. #define ENTRY(a, b) \
  607. if (_graph_ns_uri (LSUP_STORE_##a) != 0) return -1;
  608. BACKEND_TBL
  609. #undef ENTRY
  610. return 0;
  611. }
  612. static int test_graph_add() {
  613. #define ENTRY(a, b) \
  614. if (_graph_add (LSUP_STORE_##a) != 0) return -1;
  615. BACKEND_TBL
  616. #undef ENTRY
  617. return 0;
  618. }
  619. static int test_graph_get() {
  620. #define ENTRY(a, b) \
  621. if (_graph_get (LSUP_STORE_##a) != 0) return -1;
  622. BACKEND_TBL
  623. #undef ENTRY
  624. return 0;
  625. }
  626. static int test_graph_link_map() {
  627. #define ENTRY(a, b) \
  628. if (_graph_link_map (LSUP_STORE_##a) != 0) return -1;
  629. BACKEND_TBL
  630. #undef ENTRY
  631. return 0;
  632. }
  633. static int test_graph_bool_ops() {
  634. #define ENTRY(a, b) \
  635. if (_graph_bool_ops (LSUP_STORE_##a) != 0) return -1;
  636. BACKEND_TBL
  637. #undef ENTRY
  638. return 0;
  639. }
  640. static int test_graph_lookup() {
  641. #define ENTRY(a, b) \
  642. if (_graph_lookup (LSUP_STORE_##a) != 0) return -1;
  643. BACKEND_TBL
  644. #undef ENTRY
  645. return 0;
  646. }
  647. static int test_graph_remove() {
  648. #define ENTRY(a, b) \
  649. if (_graph_remove (LSUP_STORE_##a) != 0) return -1;
  650. BACKEND_TBL
  651. #undef ENTRY
  652. return 0;
  653. }
  654. static int test_graph_txn()
  655. {
  656. /*
  657. * Test transactions only if the backend supports them.
  658. */
  659. #define ENTRY(a, b) \
  660. if (_graph_txn (LSUP_STORE_##a) != 0) return -1;
  661. BACKEND_TBL
  662. #undef ENTRY
  663. return 0;
  664. }
  665. static int test_graph_relative()
  666. {
  667. /*
  668. * Test relative URIs in graphs.
  669. */
  670. #define ENTRY(a, b) \
  671. if (_graph_relative (LSUP_STORE_##a) != 0) return -1;
  672. BACKEND_TBL
  673. #undef ENTRY
  674. return 0;
  675. }
  676. static int test_graph_list()
  677. {
  678. /*
  679. * Test relative URIs in graphs.
  680. */
  681. #define ENTRY(a, b) \
  682. if (_graph_list (LSUP_STORE_##a) != 0) return -1;
  683. BACKEND_TBL
  684. #undef ENTRY
  685. return 0;
  686. }
  687. static int test_graph_copy()
  688. {
  689. LSUP_Triple **trp = create_triples();
  690. LSUP_Graph *gr1 = LSUP_graph_new (NULL, NULL, NULL);
  691. ASSERT (gr1 != NULL, "Error creating graph!");
  692. LSUP_graph_add (gr1, trp, NULL);
  693. // Copy to graph with same store type.
  694. LSUP_Graph *gr2 = LSUP_graph_new (NULL, NULL, NULL);
  695. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr2, NULL, NULL, NULL));
  696. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  697. for (int i = 0; i < sizeof (trp); i++) {
  698. log_info ("checking triple #%d.", i);
  699. ASSERT (
  700. LSUP_graph_contains (gr2, trp[i]),
  701. "Triple not in copied graph!");
  702. }
  703. // Copy to graph with a different store type.
  704. LSUP_Graph *gr3 = LSUP_graph_new (NULL, NULL, NULL);
  705. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr3, NULL, NULL, NULL));
  706. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  707. for (int i = 0; i < sizeof (trp); i++) {
  708. log_info ("checking triple #%d.", i);
  709. ASSERT (
  710. LSUP_graph_contains (gr3, trp[i]),
  711. "Triple not in copied graph!");
  712. }
  713. LSUP_graph_free (gr3);
  714. LSUP_graph_free (gr2);
  715. LSUP_graph_free (gr1);
  716. free_triples (trp);
  717. return 0;
  718. }
  719. int graph_tests()
  720. {
  721. RUN (test_environment);
  722. RUN (test_graph_new);
  723. RUN (test_graph_ns_uri);
  724. RUN (test_graph_add);
  725. RUN (test_graph_get);
  726. RUN (test_graph_link_map);
  727. RUN (test_graph_bool_ops);
  728. RUN (test_graph_lookup);
  729. RUN (test_graph_remove);
  730. RUN (test_graph_copy);
  731. RUN (test_graph_txn);
  732. RUN (test_graph_relative);
  733. RUN (test_graph_list);
  734. return 0;
  735. }