test_graph.c 19 KB

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