test_graph.c 7.1 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 = 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. LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
  125. ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
  126. ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
  127. ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
  128. ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
  129. ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
  130. ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
  131. ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
  132. ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
  133. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  134. free_triples (trp); // gr copied data.
  135. LSUP_graph_free (gr);
  136. // TODO Test complete removal of triples from index when they are not
  137. // in another context.
  138. return 0;
  139. }
  140. static int test_graph_new() {
  141. if (_graph_new (LSUP_STORE_MEM) != 0) return -1;
  142. if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1;
  143. return 0;
  144. }
  145. static int test_graph_add() {
  146. if (_graph_add (LSUP_STORE_MEM) != 0) return -1;
  147. if (_graph_add (LSUP_STORE_MDB_TMP) != 0) return -1;
  148. return 0;
  149. }
  150. static int test_graph_lookup() {
  151. if (_graph_lookup (LSUP_STORE_MEM) != 0) return -1;
  152. if (_graph_lookup (LSUP_STORE_MDB_TMP) != 0) return -1;
  153. return 0;
  154. }
  155. static int test_graph_remove() {
  156. if (_graph_remove (LSUP_STORE_MEM) != 0) return -1;
  157. if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1;
  158. return 0;
  159. }
  160. static int test_graph_copy()
  161. {
  162. LSUP_Triple *trp = create_triples();
  163. LSUP_Graph *gr1 = LSUP_graph_new (
  164. LSUP_iriref_new (NULL, NULL), 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. }