test_graph.c 8.6 KB

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