test_graph.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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, LSUP_iriref_new ("urn:gr:1", NULL)));
  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) {
  41. gr = LSUP_graph_new (NULL, NULL, NULL);
  42. } else {
  43. store = LSUP_store_new (type, NULL, 0);
  44. gr = LSUP_graph_new (store, NULL, NULL);
  45. }
  46. ASSERT (gr != NULL, "Error creating graph!");
  47. size_t ct;
  48. LSUP_graph_add (gr, trp, &ct);
  49. EXPECT_INT_EQ (ct, 8);
  50. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  51. for (int i = 0; i < sizeof (trp); i++) {
  52. log_info ("checking triple #%d.", i);
  53. ASSERT (LSUP_graph_contains (gr, trp[i]), "Triple not in graph!");
  54. }
  55. LSUP_Triple *missing_trp = LSUP_triple_new (
  56. trp[1]->s, trp[6]->p, trp[4]->o);
  57. ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
  58. free (missing_trp);
  59. free_triples (trp); // gr copied data.
  60. LSUP_graph_free (gr);
  61. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  62. return 0;
  63. }
  64. static int
  65. _graph_get (LSUP_StoreType type)
  66. {
  67. const LSUP_StoreInt *sif = LSUP_store_int (type);
  68. // Skip if the store doesn't support contexts.
  69. if (!(sif->features & LSUP_STORE_CTX)) return 0;
  70. if (sif->setup_fn) sif->setup_fn (NULL, true);
  71. LSUP_Triple **trp = create_triples();
  72. LSUP_Store *store = LSUP_store_new (type, NULL, 0);
  73. LSUP_Graph
  74. *gr1 = LSUP_graph_new (store, NULL, NULL),
  75. *gr2 = LSUP_graph_new (store, NULL, NULL);
  76. ASSERT (gr1 != NULL, "Error creating graph!");
  77. ASSERT (gr2 != NULL, "Error creating graph!");
  78. // Add 2 groups of triples to different graphs.
  79. void *it1 = LSUP_graph_add_init (gr1);
  80. for (size_t i = 0; i < 5; i++) {
  81. LSUP_graph_add_iter(it1, trp[i]);
  82. }
  83. LSUP_graph_add_done (it1);
  84. void *it2 = LSUP_graph_add_init (gr2);
  85. for (size_t i = 5; i < NUM_TRP; i++) {
  86. LSUP_graph_add_iter(it2, trp[i]);
  87. }
  88. LSUP_graph_add_done (it2);
  89. EXPECT_INT_EQ (LSUP_graph_size (gr1), 5);
  90. EXPECT_INT_EQ (LSUP_graph_size (gr2), 3);
  91. size_t ct3, ct4;
  92. LSUP_Graph
  93. *gr3 = LSUP_graph_get (store, LSUP_graph_uri (gr1), &ct3),
  94. *gr4 = LSUP_graph_get (store, LSUP_graph_uri (gr2), &ct4);
  95. EXPECT_INT_EQ (LSUP_graph_size (gr3), LSUP_graph_size (gr1));
  96. EXPECT_INT_EQ (LSUP_graph_size (gr4), LSUP_graph_size (gr2));
  97. ASSERT (LSUP_graph_equals (gr1, gr3), "Graphs 1 and 3 are not equal!");
  98. ASSERT (LSUP_graph_equals (gr2, gr4), "Graphs 2 and 4 are not equal!");
  99. LSUP_graph_free (gr1);
  100. LSUP_graph_free (gr2);
  101. LSUP_graph_free (gr3);
  102. LSUP_graph_free (gr4);
  103. free_triples (trp);
  104. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  105. return 0;
  106. }
  107. static int
  108. _graph_bool_ops (LSUP_StoreType type)
  109. {
  110. const LSUP_StoreInt *sif = LSUP_store_int (type);
  111. // Skip if the store doesn't support contexts.
  112. if (!(sif->features & LSUP_STORE_CTX)) return 0;
  113. if (sif->setup_fn) sif->setup_fn (NULL, true);
  114. LSUP_Triple **trp = create_triples();
  115. LSUP_Store *store = LSUP_store_new (type, NULL, 0);
  116. LSUP_Graph
  117. *gr1 = LSUP_graph_new (store, NULL, NULL),
  118. *gr2 = LSUP_graph_new (store, NULL, NULL),
  119. *gr_dest;
  120. // Add 2 groups of triples to different graphs.
  121. void *it;
  122. it = LSUP_graph_add_init (gr1);
  123. for (size_t i = 0; i < 4; i++) {
  124. LSUP_graph_add_iter(it, trp[i]);
  125. }
  126. LSUP_graph_add_done (it);
  127. // Skip duplicate triples, we already tested those.
  128. // trp[3] is in both graphs.
  129. it = LSUP_graph_add_init (gr2);
  130. for (size_t i = 3; i < 8; i++) {
  131. LSUP_graph_add_iter(it, trp[i]);
  132. }
  133. LSUP_graph_add_done (it);
  134. // Test union.
  135. gr_dest = LSUP_graph_new (store, NULL, NULL);
  136. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_UNION, gr1, gr2, gr_dest));
  137. for (size_t i = 0; i < 8; i++)
  138. ASSERT (LSUP_graph_contains (gr_dest, trp[i]), "Union test failed!");
  139. LSUP_graph_free (gr_dest);
  140. // Test subtraction.
  141. gr_dest = LSUP_graph_new (store, NULL, NULL);
  142. EXPECT_PASS (LSUP_graph_bool_op (
  143. LSUP_BOOL_SUBTRACTION, gr1, gr2, gr_dest));
  144. for (size_t i = 0; i < 3; i++)
  145. ASSERT (LSUP_graph_contains (
  146. gr_dest, trp[i]), "Subtraction test is missing triples!");
  147. for (size_t i = 3; i < 8; i++)
  148. ASSERT (!LSUP_graph_contains (
  149. gr_dest, trp[i]), "Subtraction test has excess triples!");
  150. LSUP_graph_free (gr_dest);
  151. gr_dest = LSUP_graph_new (store, NULL, NULL);
  152. EXPECT_PASS (LSUP_graph_bool_op (
  153. LSUP_BOOL_SUBTRACTION, gr2, gr1, gr_dest));
  154. for (size_t i = 0; i < 4; i++)
  155. ASSERT (!LSUP_graph_contains (
  156. gr_dest, trp[i]), "Subtraction test is missing triples!");
  157. for (size_t i = 4; i < 8; i++)
  158. ASSERT (LSUP_graph_contains (
  159. gr_dest, trp[i]), "Subtraction test has excess triples!");
  160. LSUP_graph_free (gr_dest);
  161. // Test intersection.
  162. gr_dest = LSUP_graph_new (store, NULL, NULL);
  163. EXPECT_PASS (LSUP_graph_bool_op (
  164. LSUP_BOOL_INTERSECTION, gr1, gr2, gr_dest));
  165. for (size_t i = 0; i < 3; i++)
  166. ASSERT (!LSUP_graph_contains (
  167. gr_dest, trp[i]), "Intersection is missing triples!");
  168. ASSERT (LSUP_graph_contains (
  169. gr_dest, trp[3]), "Intersection test failed!");
  170. for (size_t i = 4; i < 8; i++)
  171. ASSERT (!LSUP_graph_contains (
  172. gr_dest, trp[i]), "Intersection test has excess triples!");
  173. LSUP_graph_free (gr_dest);
  174. // Test XOR.
  175. gr_dest = LSUP_graph_new (store, NULL, NULL);
  176. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_XOR, gr1, gr2, gr_dest));
  177. for (size_t i = 0; i < 3; i++)
  178. ASSERT (LSUP_graph_contains (
  179. gr_dest, trp[i]), "XOR test is missing triples!");
  180. ASSERT (!LSUP_graph_contains (
  181. gr_dest, trp[3]), "XOR test has excess triples!");
  182. for (size_t i = 4; i < 8; i++)
  183. ASSERT (LSUP_graph_contains (
  184. gr_dest, trp[i]), "XOR test is missing triples!");
  185. LSUP_graph_free (gr_dest);
  186. // Test union with result graph as one of the sources.
  187. EXPECT_PASS (LSUP_graph_bool_op (LSUP_BOOL_UNION, gr1, gr2, gr1));
  188. for (size_t i = 0; i < 8; i++)
  189. ASSERT (LSUP_graph_contains (gr1, trp[i]), "Self-union test failed!");
  190. LSUP_graph_free (gr1);
  191. LSUP_graph_free (gr2);
  192. free_triples (trp);
  193. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  194. return 0;
  195. }
  196. static int
  197. _graph_lookup (LSUP_StoreType type)
  198. {
  199. const LSUP_StoreInt *sif = LSUP_store_int (type);
  200. LSUP_Triple **trp = create_triples();
  201. // Lookup triples.
  202. LSUP_Term *lu_trp[N_LUT][3] = {
  203. {NULL, NULL, NULL}, // 8 matches
  204. {trp[0]->s, NULL, NULL}, // 5 matches
  205. {NULL, trp[2]->p, NULL}, // 3 matches
  206. {NULL, NULL, trp[5]->o}, // 2 matches
  207. {trp[0]->s, trp[0]->p, NULL}, // 1 match
  208. {NULL, trp[0]->p, trp[0]->o}, // 1 match
  209. {trp[0]->s, trp[2]->p, trp[5]->o}, // 1 match
  210. {trp[0]->p, NULL, NULL}, // 0 matches
  211. {NULL, trp[2]->s, NULL}, // 0 matches
  212. {NULL, NULL, trp[5]->p}, // 0 matches
  213. {trp[2]->s, trp[6]->p, NULL}, // 0 matches
  214. {NULL, trp[1]->p, trp[5]->o}, // 0 matches
  215. {trp[2]->s, trp[2]->p, trp[5]->o}, // 0 matches
  216. };
  217. // Lookup result counts.
  218. size_t lu_ct[N_LUT] = {
  219. 8,
  220. 5, 3, 2,
  221. 1, 1, 1,
  222. 0, 0, 0,
  223. 0, 0, 0
  224. };
  225. /* TODO
  226. // Index of lookup matches from trp.
  227. size_t lu_match[N_LUT][8] = {
  228. {0, 1, 2, 3, 4, 5, 6, 7},
  229. {0, 3, 4, 5, 7}, {2, 4, 7}, {5, 7},
  230. {0}, {0}, {7},
  231. {}, {}, {},
  232. {}, {}, {},
  233. };
  234. // Index of lookup non-matches from trp.
  235. size_t lu_no_match[N_LUT][8] = {
  236. {},
  237. {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6},
  238. {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6},
  239. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  240. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  241. };
  242. */
  243. if (sif->setup_fn) sif->setup_fn (NULL, true);
  244. LSUP_Graph *gr;
  245. LSUP_Store *store;
  246. if (type == LSUP_STORE_HTABLE) {
  247. gr = LSUP_graph_new (NULL, NULL, NULL);
  248. } else {
  249. store = LSUP_store_new (type, NULL, 0);
  250. gr = LSUP_graph_new (store, NULL, NULL);
  251. }
  252. size_t ct;
  253. LSUP_graph_add (gr, trp, &ct);
  254. EXPECT_INT_EQ (ct, 8);
  255. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  256. for (int i = 0; i < N_LUT; i++) {
  257. log_info ("Checking triple #%d on %d.", i, type);
  258. LSUP_GraphIterator *it = LSUP_graph_lookup (
  259. gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
  260. EXPECT_INT_EQ (ct, lu_ct[i]);
  261. // Verify that iteration count matches stat count.
  262. LSUP_Triple *spo = NULL;
  263. ct = 0;
  264. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  265. ct++;
  266. // TODO do something useful with the triple.
  267. LSUP_triple_free (spo);
  268. spo = NULL;
  269. }
  270. LSUP_triple_free (spo);
  271. EXPECT_INT_EQ (ct, lu_ct[i]);
  272. /* TODO
  273. for (int j = 0; j < 8; j++) {
  274. for (int k = 0; LSUP_graph_iter_next(it) != LSUP_END; k++) {
  275. ASSERT (
  276. LSUP_graph_contains (trp[lu_match[j]]),
  277. "Triple not found!");
  278. ASSERT (
  279. !(LSUP_graph_contains (trp[lu_no_match[j]])),
  280. "Unexpected triple found!");
  281. }
  282. }
  283. */
  284. LSUP_graph_iter_free (it);
  285. };
  286. free_triples (trp);
  287. LSUP_graph_free (gr);
  288. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  289. return 0;
  290. }
  291. static int
  292. _graph_remove (LSUP_StoreType type)
  293. {
  294. const LSUP_StoreInt *sif = LSUP_store_int (type);
  295. if (sif->setup_fn) sif->setup_fn (NULL, true);
  296. LSUP_Triple **trp = create_triples();
  297. LSUP_Graph *gr;
  298. LSUP_Store *store;
  299. if (type == LSUP_STORE_HTABLE) {
  300. gr = LSUP_graph_new (NULL, NULL, NULL);
  301. } else {
  302. store = LSUP_store_new (type, NULL, 0);
  303. gr = LSUP_graph_new (store, NULL, NULL);
  304. }
  305. size_t ct;
  306. LSUP_graph_add (gr, trp, &ct);
  307. EXPECT_INT_EQ (ct, 8);
  308. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  309. // Triples 0, 3, 4, 5, 7 will be removed.
  310. LSUP_graph_remove (gr, trp[0]->s, NULL, NULL, &ct);
  311. EXPECT_INT_EQ (ct, 5);
  312. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  313. ASSERT (!LSUP_graph_contains (gr, trp[0]), "Unexpected triple found!");
  314. ASSERT (LSUP_graph_contains (gr, trp[1]), "Triple not in graph!");
  315. ASSERT (LSUP_graph_contains (gr, trp[2]), "Triple not in graph!");
  316. ASSERT (!LSUP_graph_contains (gr, trp[3]), "Unexpected triple found!");
  317. ASSERT (!LSUP_graph_contains (gr, trp[4]), "Unexpected triple found!");
  318. ASSERT (!LSUP_graph_contains (gr, trp[5]), "Unexpected triple found!");
  319. ASSERT (LSUP_graph_contains (gr, trp[6]), "Triple not in graph!");
  320. ASSERT (!LSUP_graph_contains (gr, trp[7]), "Unexpected triple found!");
  321. free_triples (trp); // gr copied data.
  322. LSUP_graph_free (gr);
  323. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  324. // TODO Test complete removal of triples from index when they are not
  325. // in another context.
  326. return 0;
  327. }
  328. static int
  329. _graph_txn (LSUP_StoreType type)
  330. {
  331. const LSUP_StoreInt *sif = LSUP_store_int (type);
  332. if (!(sif->features & LSUP_STORE_TXN)) return 0;
  333. if (sif->setup_fn) sif->setup_fn (NULL, true);
  334. LSUP_Triple **trp = create_triples();
  335. LSUP_Graph *gr;
  336. LSUP_Store *store =
  337. type == LSUP_STORE_HTABLE ? NULL
  338. : LSUP_store_new (type, NULL, 0);
  339. gr = LSUP_graph_new (store, NULL, NULL);
  340. void *txn;
  341. size_t ct;
  342. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  343. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  344. LSUP_store_abort (store, txn);
  345. // NOTE that ct reports the count before the txn was aborted. This is
  346. // intentional.
  347. EXPECT_INT_EQ (ct, 8);
  348. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  349. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  350. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  351. LSUP_store_commit (store, txn);
  352. EXPECT_INT_EQ (ct, 8);
  353. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  354. LSUP_graph_free (gr);
  355. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  356. free_triples (trp); // gr copied data.
  357. return 0;
  358. }
  359. static int
  360. test_environment()
  361. {
  362. // The env should already be initialized and re-initializing it is idempotent.
  363. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  364. ASSERT (LSUP_init() > 0, "Error initializing environment!");
  365. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  366. // Tearing down is idempotent too.
  367. LSUP_done();
  368. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  369. LSUP_done();
  370. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  371. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  372. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  373. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  374. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  375. return 0;
  376. }
  377. static int test_graph_new() {
  378. #define ENTRY(a, b) \
  379. if (_graph_new (LSUP_STORE_##a) != 0) return -1;
  380. BACKEND_TBL
  381. #undef ENTRY
  382. return 0;
  383. }
  384. static int test_graph_add() {
  385. #define ENTRY(a, b) \
  386. if (_graph_add (LSUP_STORE_##a) != 0) return -1;
  387. BACKEND_TBL
  388. #undef ENTRY
  389. return 0;
  390. }
  391. static int test_graph_get() {
  392. #define ENTRY(a, b) \
  393. if (_graph_get (LSUP_STORE_##a) != 0) return -1;
  394. BACKEND_TBL
  395. #undef ENTRY
  396. return 0;
  397. }
  398. static int test_graph_bool_ops() {
  399. #define ENTRY(a, b) \
  400. if (_graph_bool_ops (LSUP_STORE_##a) != 0) return -1;
  401. BACKEND_TBL
  402. #undef ENTRY
  403. return 0;
  404. }
  405. static int test_graph_lookup() {
  406. #define ENTRY(a, b) \
  407. if (_graph_lookup (LSUP_STORE_##a) != 0) return -1;
  408. BACKEND_TBL
  409. #undef ENTRY
  410. return 0;
  411. }
  412. static int test_graph_remove() {
  413. #define ENTRY(a, b) \
  414. if (_graph_remove (LSUP_STORE_##a) != 0) return -1;
  415. BACKEND_TBL
  416. #undef ENTRY
  417. return 0;
  418. }
  419. static int test_graph_txn()
  420. {
  421. /*
  422. * Test transactions only if the backend supports them.
  423. */
  424. #define ENTRY(a, b) \
  425. if (_graph_txn (LSUP_STORE_##a) != 0) return -1;
  426. BACKEND_TBL
  427. #undef ENTRY
  428. return 0;
  429. }
  430. static int test_graph_copy()
  431. {
  432. LSUP_Triple **trp = create_triples();
  433. LSUP_Graph *gr1 = LSUP_graph_new (NULL, NULL, NULL);
  434. ASSERT (gr1 != NULL, "Error creating graph!");
  435. LSUP_graph_add (gr1, trp, NULL);
  436. // Copy to graph with same store type.
  437. LSUP_Graph *gr2 = LSUP_graph_new (NULL, NULL, NULL);
  438. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr2, NULL, NULL, NULL));
  439. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  440. for (int i = 0; i < sizeof (trp); i++) {
  441. log_info ("checking triple #%d.", i);
  442. ASSERT (
  443. LSUP_graph_contains (gr2, trp[i]),
  444. "Triple not in copied graph!");
  445. }
  446. // Copy to graph with a different store type.
  447. LSUP_Graph *gr3 = LSUP_graph_new (NULL, NULL, NULL);
  448. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr3, NULL, NULL, NULL));
  449. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  450. for (int i = 0; i < sizeof (trp); i++) {
  451. log_info ("checking triple #%d.", i);
  452. ASSERT (
  453. LSUP_graph_contains (gr3, trp[i]),
  454. "Triple not in copied graph!");
  455. }
  456. LSUP_graph_free (gr3);
  457. LSUP_graph_free (gr2);
  458. LSUP_graph_free (gr1);
  459. free_triples (trp);
  460. return 0;
  461. }
  462. static int test_graph_relative()
  463. {
  464. LSUP_Term
  465. *s = LSUP_iriref_new ("http://onto.knowledgetx.net/gr1/s1", NULL),
  466. *p = LSUP_iriref_new ("http://onto.knowledgetx.net/vocab/p1", NULL),
  467. *o = LSUP_iriref_new ("http://onto.knowledgetx.net/gr1/o1", NULL),
  468. *c = LSUP_iriref_new ("http://onto.knowledgetx.net/gr1/", NULL),
  469. *rel_s = LSUP_iriref_relative (c, s),
  470. *rel_o = LSUP_iriref_relative (c, o);
  471. LSUP_Triple *spo[2] = {
  472. LSUP_triple_new (s, p, o),
  473. NULL
  474. };
  475. LSUP_Triple *rel_spo = LSUP_triple_new (rel_s, p, rel_o);
  476. LSUP_Store *store = LSUP_store_new (LSUP_STORE_MDB, NULL, 0);
  477. LSUP_Graph *gr = LSUP_graph_new (store, c, NULL);
  478. size_t ct;
  479. LSUP_graph_add (gr, spo, &ct);
  480. // Both absolute and relative IRIs should be found.
  481. ASSERT (LSUP_graph_contains (gr, rel_spo), "Relative triple not found!");
  482. ASSERT (LSUP_graph_contains (gr, spo[0]), "Absolute triple not found!");
  483. LSUP_triple_free (spo[0]);
  484. LSUP_term_free (rel_s);
  485. LSUP_term_free (rel_o);
  486. LSUP_term_free (c);
  487. free (rel_spo);
  488. LSUP_graph_free (gr);
  489. LSUP_store_free (store);
  490. return 0;
  491. }
  492. int graph_tests()
  493. {
  494. RUN (test_environment);
  495. RUN (test_graph_new);
  496. RUN (test_graph_add);
  497. RUN (test_graph_get);
  498. RUN (test_graph_bool_ops);
  499. RUN (test_graph_lookup);
  500. RUN (test_graph_remove);
  501. RUN (test_graph_copy);
  502. RUN (test_graph_txn);
  503. RUN (test_graph_relative);
  504. return 0;
  505. }