test_graph.c 11 KB

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