test_graph.c 7.8 KB

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