test_graph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_lookup (LSUP_StoreType type)
  109. {
  110. const LSUP_StoreInt *sif = LSUP_store_int (type);
  111. LSUP_Triple **trp = create_triples();
  112. // Lookup triples.
  113. LSUP_Term *lu_trp[N_LUT][3] = {
  114. {NULL, NULL, NULL}, // 8 matches
  115. {trp[0]->s, NULL, NULL}, // 5 matches
  116. {NULL, trp[2]->p, NULL}, // 3 matches
  117. {NULL, NULL, trp[5]->o}, // 2 matches
  118. {trp[0]->s, trp[0]->p, NULL}, // 1 match
  119. {NULL, trp[0]->p, trp[0]->o}, // 1 match
  120. {trp[0]->s, trp[2]->p, trp[5]->o}, // 1 match
  121. {trp[0]->p, NULL, NULL}, // 0 matches
  122. {NULL, trp[2]->s, NULL}, // 0 matches
  123. {NULL, NULL, trp[5]->p}, // 0 matches
  124. {trp[2]->s, trp[6]->p, NULL}, // 0 matches
  125. {NULL, trp[1]->p, trp[5]->o}, // 0 matches
  126. {trp[2]->s, trp[2]->p, trp[5]->o}, // 0 matches
  127. };
  128. // Lookup result counts.
  129. size_t lu_ct[N_LUT] = {
  130. 8,
  131. 5, 3, 2,
  132. 1, 1, 1,
  133. 0, 0, 0,
  134. 0, 0, 0
  135. };
  136. /* TODO
  137. // Index of lookup matches from trp.
  138. size_t lu_match[N_LUT][8] = {
  139. {0, 1, 2, 3, 4, 5, 6, 7},
  140. {0, 3, 4, 5, 7}, {2, 4, 7}, {5, 7},
  141. {0}, {0}, {7},
  142. {}, {}, {},
  143. {}, {}, {},
  144. };
  145. // Index of lookup non-matches from trp.
  146. size_t lu_no_match[N_LUT][8] = {
  147. {},
  148. {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6},
  149. {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6},
  150. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  151. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  152. };
  153. */
  154. if (sif->setup_fn) sif->setup_fn (NULL, true);
  155. LSUP_Graph *gr;
  156. LSUP_Store *store;
  157. if (type == LSUP_STORE_HTABLE) {
  158. gr = LSUP_graph_new (NULL, NULL, NULL);
  159. } else {
  160. store = LSUP_store_new (type, NULL, 0);
  161. gr = LSUP_graph_new (store, NULL, NULL);
  162. }
  163. size_t ct;
  164. LSUP_graph_add (gr, trp, &ct);
  165. EXPECT_INT_EQ (ct, 8);
  166. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  167. for (int i = 0; i < N_LUT; i++) {
  168. log_info ("Checking triple #%d on %d.", i, type);
  169. LSUP_GraphIterator *it = LSUP_graph_lookup (
  170. gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
  171. EXPECT_INT_EQ (ct, lu_ct[i]);
  172. // Verify that iteration count matches stat count.
  173. LSUP_Triple *spo = NULL;
  174. ct = 0;
  175. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  176. ct++;
  177. // TODO do something useful with the triple.
  178. LSUP_triple_free (spo);
  179. spo = NULL;
  180. }
  181. LSUP_triple_free (spo);
  182. EXPECT_INT_EQ (ct, lu_ct[i]);
  183. /* TODO
  184. for (int j = 0; j < 8; j++) {
  185. for (int k = 0; LSUP_graph_iter_next(it) != LSUP_END; k++) {
  186. ASSERT (
  187. LSUP_graph_contains (trp[lu_match[j]]),
  188. "Triple not found!");
  189. ASSERT (
  190. !(LSUP_graph_contains (trp[lu_no_match[j]])),
  191. "Unexpected triple found!");
  192. }
  193. }
  194. */
  195. LSUP_graph_iter_free (it);
  196. };
  197. free_triples (trp);
  198. LSUP_graph_free (gr);
  199. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  200. return 0;
  201. }
  202. static int
  203. _graph_remove (LSUP_StoreType type)
  204. {
  205. const LSUP_StoreInt *sif = LSUP_store_int (type);
  206. if (sif->setup_fn) sif->setup_fn (NULL, true);
  207. LSUP_Triple **trp = create_triples();
  208. LSUP_Graph *gr;
  209. LSUP_Store *store;
  210. if (type == LSUP_STORE_HTABLE) {
  211. gr = LSUP_graph_new (NULL, NULL, NULL);
  212. } else {
  213. store = LSUP_store_new (type, NULL, 0);
  214. gr = LSUP_graph_new (store, NULL, NULL);
  215. }
  216. size_t ct;
  217. LSUP_graph_add (gr, trp, &ct);
  218. EXPECT_INT_EQ (ct, 8);
  219. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  220. // Triples 0, 3, 4, 5, 7 will be removed.
  221. LSUP_graph_remove (gr, trp[0]->s, NULL, NULL, &ct);
  222. EXPECT_INT_EQ (ct, 5);
  223. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  224. ASSERT (!LSUP_graph_contains (gr, trp[0]), "Unexpected triple found!");
  225. ASSERT (LSUP_graph_contains (gr, trp[1]), "Triple not in graph!");
  226. ASSERT (LSUP_graph_contains (gr, trp[2]), "Triple not in graph!");
  227. ASSERT (!LSUP_graph_contains (gr, trp[3]), "Unexpected triple found!");
  228. ASSERT (!LSUP_graph_contains (gr, trp[4]), "Unexpected triple found!");
  229. ASSERT (!LSUP_graph_contains (gr, trp[5]), "Unexpected triple found!");
  230. ASSERT (LSUP_graph_contains (gr, trp[6]), "Triple not in graph!");
  231. ASSERT (!LSUP_graph_contains (gr, trp[7]), "Unexpected triple found!");
  232. free_triples (trp); // gr copied data.
  233. LSUP_graph_free (gr);
  234. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  235. // TODO Test complete removal of triples from index when they are not
  236. // in another context.
  237. return 0;
  238. }
  239. static int
  240. _graph_txn (LSUP_StoreType type)
  241. {
  242. const LSUP_StoreInt *sif = LSUP_store_int (type);
  243. if (!(sif->features & LSUP_STORE_TXN)) return 0;
  244. if (sif->setup_fn) sif->setup_fn (NULL, true);
  245. LSUP_Triple **trp = create_triples();
  246. LSUP_Graph *gr;
  247. LSUP_Store *store =
  248. type == LSUP_STORE_HTABLE ? NULL
  249. : LSUP_store_new (type, NULL, 0);
  250. gr = LSUP_graph_new (store, NULL, NULL);
  251. void *txn;
  252. size_t ct;
  253. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  254. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  255. LSUP_store_abort (store, txn);
  256. // NOTE that ct reports the count before the txn was aborted. This is
  257. // intentional.
  258. EXPECT_INT_EQ (ct, 8);
  259. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  260. EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
  261. EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
  262. LSUP_store_commit (store, txn);
  263. EXPECT_INT_EQ (ct, 8);
  264. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  265. LSUP_graph_free (gr);
  266. if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
  267. free_triples (trp); // gr copied data.
  268. return 0;
  269. }
  270. static int
  271. test_environment()
  272. {
  273. // The env should already be initialized and re-initializing it is idempotent.
  274. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  275. ASSERT (LSUP_init() > 0, "Error initializing environment!");
  276. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  277. // Tearing down is idempotent too.
  278. LSUP_done();
  279. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  280. LSUP_done();
  281. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  282. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  283. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  284. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  285. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  286. return 0;
  287. }
  288. static int test_graph_new() {
  289. #define ENTRY(a, b) \
  290. if (_graph_new (LSUP_STORE_##a) != 0) return -1;
  291. BACKEND_TBL
  292. #undef ENTRY
  293. return 0;
  294. }
  295. static int test_graph_add() {
  296. #define ENTRY(a, b) \
  297. if (_graph_add (LSUP_STORE_##a) != 0) return -1;
  298. BACKEND_TBL
  299. #undef ENTRY
  300. return 0;
  301. }
  302. static int test_graph_get() {
  303. #define ENTRY(a, b) \
  304. if (_graph_get (LSUP_STORE_##a) != 0) return -1;
  305. BACKEND_TBL
  306. #undef ENTRY
  307. return 0;
  308. }
  309. static int test_graph_lookup() {
  310. #define ENTRY(a, b) \
  311. if (_graph_lookup (LSUP_STORE_##a) != 0) return -1;
  312. BACKEND_TBL
  313. #undef ENTRY
  314. return 0;
  315. }
  316. static int test_graph_remove() {
  317. #define ENTRY(a, b) \
  318. if (_graph_remove (LSUP_STORE_##a) != 0) return -1;
  319. BACKEND_TBL
  320. #undef ENTRY
  321. return 0;
  322. }
  323. static int test_graph_txn()
  324. {
  325. /*
  326. * Test transactions only if the backend supports them.
  327. */
  328. #define ENTRY(a, b) \
  329. if (_graph_txn (LSUP_STORE_##a) != 0) return -1;
  330. BACKEND_TBL
  331. #undef ENTRY
  332. return 0;
  333. }
  334. static int test_graph_copy()
  335. {
  336. LSUP_Triple **trp = create_triples();
  337. LSUP_Graph *gr1 = LSUP_graph_new (NULL, NULL, NULL);
  338. ASSERT (gr1 != NULL, "Error creating graph!");
  339. LSUP_graph_add (gr1, trp, NULL);
  340. // Copy to graph with same store type.
  341. LSUP_Graph *gr2 = LSUP_graph_new (NULL, NULL, NULL);
  342. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr2));
  343. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  344. for (int i = 0; i < sizeof (trp); i++) {
  345. log_info ("checking triple #%d.", i);
  346. ASSERT (
  347. LSUP_graph_contains (gr2, trp[i]),
  348. "Triple not in copied graph!");
  349. }
  350. // Copy to graph with a different store type.
  351. LSUP_Graph *gr3 = LSUP_graph_new (NULL, NULL, NULL);
  352. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr3));
  353. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  354. for (int i = 0; i < sizeof (trp); i++) {
  355. log_info ("checking triple #%d.", i);
  356. ASSERT (
  357. LSUP_graph_contains (gr3, trp[i]),
  358. "Triple not in copied graph!");
  359. }
  360. LSUP_graph_free (gr3);
  361. LSUP_graph_free (gr2);
  362. LSUP_graph_free (gr1);
  363. free_triples (trp);
  364. return 0;
  365. }
  366. int graph_tests()
  367. {
  368. RUN (test_environment);
  369. RUN (test_graph_new);
  370. RUN (test_graph_add);
  371. RUN (test_graph_get);
  372. RUN (test_graph_lookup);
  373. RUN (test_graph_remove);
  374. RUN (test_graph_copy);
  375. RUN (test_graph_txn);
  376. return 0;
  377. }