test_graph.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 = LSUP_graph_new (
  11. LSUP_iriref_new (NULL, NULL), type, NULL, NULL, 0);
  12. ASSERT (gr != NULL, "Error creating graph!");
  13. EXPECT_PASS (LSUP_graph_set_uri (gr, LSUP_iriref_new ("urn:gr:1", NULL)));
  14. EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
  15. // Check that setup function is idempotent with clear == false.
  16. if (sif->setup_fn) EXPECT_INT_EQ (
  17. sif->setup_fn (NULL, false), LSUP_NOACTION);
  18. ASSERT (
  19. strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0,
  20. "Graph URI mismatch!");
  21. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  22. LSUP_graph_free (gr);
  23. return 0;
  24. }
  25. static int
  26. _graph_add (LSUP_StoreType type)
  27. {
  28. const LSUP_StoreInt *sif = LSUP_store_int (type);
  29. if (sif->setup_fn) sif->setup_fn (NULL, true);
  30. LSUP_Triple *trp = create_triples();
  31. LSUP_Graph *gr = LSUP_graph_new (
  32. LSUP_iriref_new (NULL, NULL), type, NULL, NULL, 0);
  33. ASSERT (gr != NULL, "Error creating graph!");
  34. size_t ct;
  35. LSUP_graph_add (gr, trp, &ct);
  36. EXPECT_INT_EQ (ct, 8);
  37. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  38. for (int i = 0; i < sizeof (trp); i++) {
  39. log_info ("checking triple #%d.", i);
  40. ASSERT (LSUP_graph_contains (gr, trp + i), "Triple not in graph!");
  41. }
  42. LSUP_Triple *missing_trp = LSUP_triple_new (trp[1].s, trp[6].p, trp[4].o);
  43. ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
  44. free (missing_trp);
  45. free_triples (trp); // gr copied data.
  46. LSUP_graph_free (gr);
  47. return 0;
  48. }
  49. static int
  50. _graph_lookup (LSUP_StoreType type)
  51. {
  52. const LSUP_StoreInt *sif = LSUP_store_int (type);
  53. LSUP_Triple *trp = create_triples();
  54. // Lookup triples.
  55. LSUP_Term *lu_trp[N_LUT][3] = {
  56. {NULL, NULL, NULL}, // 8 matches
  57. {trp[0].s, NULL, NULL}, // 5 matches
  58. {NULL, trp[2].p, NULL}, // 3 matches
  59. {NULL, NULL, trp[5].o}, // 2 matches
  60. {trp[0].s, trp[0].p, NULL}, // 1 match
  61. {NULL, trp[0].p, trp[0].o}, // 1 match
  62. {trp[0].s, trp[2].p, trp[5].o}, // 1 match
  63. {trp[0].p, NULL, NULL}, // 0 matches
  64. {NULL, trp[2].s, NULL}, // 0 matches
  65. {NULL, NULL, trp[5].p}, // 0 matches
  66. {trp[2].s, trp[6].p, NULL}, // 0 matches
  67. {NULL, trp[1].p, trp[5].o}, // 0 matches
  68. {trp[2].s, trp[2].p, trp[5].o}, // 0 matches
  69. };
  70. // Lookup result counts.
  71. size_t lu_ct[N_LUT] = {
  72. 8,
  73. 5, 3, 2,
  74. 1, 1, 1,
  75. 0, 0, 0,
  76. 0, 0, 0
  77. };
  78. /* TODO
  79. // Index of lookup matches from trp.
  80. size_t lu_match[N_LUT][8] = {
  81. {0, 1, 2, 3, 4, 5, 6, 7},
  82. {0, 3, 4, 5, 7}, {2, 4, 7}, {5, 7},
  83. {0}, {0}, {7},
  84. {}, {}, {},
  85. {}, {}, {},
  86. };
  87. // Index of lookup non-matches from trp.
  88. size_t lu_no_match[N_LUT][8] = {
  89. {},
  90. {1, 2, 6}, {0, 1, 3, 5, 6}, {0, 1, 2, 3, 4, 6},
  91. {1, 2, 3, 4, 5, 6, 7}, {1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6},
  92. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  93. {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7}, {0, 1, 2, 3, 4, 5, 6, 7},
  94. };
  95. */
  96. if (sif->setup_fn) sif->setup_fn (NULL, true);
  97. LSUP_Graph *gr = LSUP_graph_new (
  98. LSUP_iriref_new (NULL, NULL), type, NULL, NULL, 0);
  99. size_t ct;
  100. LSUP_graph_add (gr, trp, &ct);
  101. EXPECT_INT_EQ (ct, 8);
  102. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  103. for (int i = 0; i < N_LUT; i++) {
  104. log_info ("Checking triple #%d on %d.", i, type);
  105. LSUP_GraphIterator *it = LSUP_graph_lookup (
  106. gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
  107. EXPECT_INT_EQ (ct, lu_ct[i]);
  108. /* TODO
  109. for (int j = 0; j < 8; j++) {
  110. for (int k = 0; LSUP_graph_iter_next(it) != LSUP_END; k++) {
  111. ASSERT (
  112. LSUP_graph_contains (trp[lu_match[j]]),
  113. "Triple not found!");
  114. ASSERT (
  115. !(LSUP_graph_contains (trp[lu_no_match[j]])),
  116. "Unexpected triple found!");
  117. }
  118. }
  119. */
  120. LSUP_graph_iter_free (it);
  121. };
  122. free_triples (trp);
  123. LSUP_graph_free (gr);
  124. return 0;
  125. }
  126. static int
  127. _graph_remove (LSUP_StoreType type)
  128. {
  129. const LSUP_StoreInt *sif = LSUP_store_int (type);
  130. if (sif->setup_fn) sif->setup_fn (NULL, true);
  131. LSUP_Triple *trp = create_triples();
  132. LSUP_Graph *gr = LSUP_graph_new (
  133. LSUP_iriref_new (NULL, NULL), type, NULL, NULL, 0);
  134. size_t ct;
  135. LSUP_graph_add (gr, trp, &ct);
  136. EXPECT_INT_EQ (ct, 8);
  137. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  138. // Triples 0, 3, 4, 5, 7 will be removed.
  139. LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
  140. EXPECT_INT_EQ (ct, 5);
  141. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  142. ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
  143. ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
  144. ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
  145. ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
  146. ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
  147. ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
  148. ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
  149. ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
  150. free_triples (trp); // gr copied data.
  151. LSUP_graph_free (gr);
  152. // TODO Test complete removal of triples from index when they are not
  153. // in another context.
  154. return 0;
  155. }
  156. static int
  157. test_environment()
  158. {
  159. // The env should already be initialized and re-initializing it is idempotent.
  160. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  161. ASSERT (LSUP_init() > 0, "Error initializing environment!");
  162. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  163. // Tearing down is idempotent too.
  164. LSUP_done();
  165. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  166. LSUP_done();
  167. EXPECT_INT_EQ (LSUP_IS_INIT, false);
  168. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  169. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  170. ASSERT (LSUP_init() >= 0, "Environment not initialized!");
  171. EXPECT_INT_EQ (LSUP_IS_INIT, true);
  172. return 0;
  173. }
  174. static int test_graph_new() {
  175. #define ENTRY(a, b) \
  176. if (_graph_new (LSUP_STORE_##a) != 0) return -1;
  177. BACKEND_TBL
  178. #undef ENTRY
  179. return 0;
  180. }
  181. static int test_graph_add() {
  182. #define ENTRY(a, b) \
  183. if (_graph_add (LSUP_STORE_##a) != 0) return -1;
  184. BACKEND_TBL
  185. #undef ENTRY
  186. return 0;
  187. }
  188. static int test_graph_lookup() {
  189. #define ENTRY(a, b) \
  190. if (_graph_lookup (LSUP_STORE_##a) != 0) return -1;
  191. BACKEND_TBL
  192. #undef ENTRY
  193. return 0;
  194. }
  195. static int test_graph_remove() {
  196. #define ENTRY(a, b) \
  197. if (_graph_remove (LSUP_STORE_##a) != 0) return -1;
  198. BACKEND_TBL
  199. #undef ENTRY
  200. return 0;
  201. }
  202. static int test_graph_copy()
  203. {
  204. LSUP_Triple *trp = create_triples();
  205. LSUP_Graph *gr1 = LSUP_graph_new (
  206. LSUP_iriref_new (NULL, NULL), LSUP_STORE_HTABLE, NULL, NULL, 0);
  207. ASSERT (gr1 != NULL, "Error creating graph!");
  208. LSUP_graph_add (gr1, trp, NULL);
  209. // Copy to graph with same store type.
  210. LSUP_Graph *gr2 = LSUP_graph_new (
  211. LSUP_iriref_new (NULL, NULL), LSUP_STORE_HTABLE, NULL, NULL, 0);
  212. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr2));
  213. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  214. for (int i = 0; i < sizeof (trp); i++) {
  215. log_info ("checking triple #%d.", i);
  216. ASSERT (
  217. LSUP_graph_contains (gr2, trp + i),
  218. "Triple not in copied graph!");
  219. }
  220. // Copy to graph with a different store type.
  221. LSUP_Graph *gr3 = LSUP_graph_new (
  222. LSUP_iriref_new (NULL, NULL), LSUP_STORE_MDB, NULL, NULL, 0);
  223. EXPECT_PASS (LSUP_graph_copy_contents (gr1, gr3));
  224. EXPECT_INT_EQ (LSUP_graph_size (gr1), LSUP_graph_size (gr2));
  225. for (int i = 0; i < sizeof (trp); i++) {
  226. log_info ("checking triple #%d.", i);
  227. ASSERT (
  228. LSUP_graph_contains (gr3, trp + i),
  229. "Triple not in copied graph!");
  230. }
  231. LSUP_graph_free (gr3);
  232. LSUP_graph_free (gr2);
  233. LSUP_graph_free (gr1);
  234. free_triples (trp);
  235. return 0;
  236. }
  237. int graph_tests()
  238. {
  239. RUN (test_environment);
  240. RUN (test_graph_new);
  241. RUN (test_graph_add);
  242. RUN (test_graph_lookup);
  243. RUN (test_graph_remove);
  244. RUN (test_graph_copy);
  245. return 0;
  246. }