test_graph.c 23 KB

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