test_graph.c 19 KB

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