test_graph.c 25 KB

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