test_graph.c 7.0 KB

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