test_graph.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_trp (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. printf ("OK.\n");
  34. }
  35. LSUP_Triple *missing_trp = LSUP_triple_new (trp[1].s, trp[6].p, trp[4].o);
  36. ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
  37. free (missing_trp);
  38. free_triples (trp); // gr copied data.
  39. LSUP_graph_free (gr);
  40. return 0;
  41. }
  42. static int
  43. _graph_lookup (LSUP_store_type type)
  44. {
  45. LSUP_Triple *trp = create_triples();
  46. // Lookup triples.
  47. LSUP_Term *lu_trp[N_LUT][3] = {
  48. {NULL, NULL, NULL}, // 8 matches
  49. {trp[0].s, NULL, NULL}, // 5 matches
  50. {NULL, trp[2].p, NULL}, // 3 matches
  51. {NULL, NULL, trp[5].o}, // 2 matches
  52. {trp[0].s, trp[0].p, NULL}, // 1 match
  53. {NULL, trp[0].p, trp[0].o}, // 1 match
  54. {trp[0].s, trp[2].p, trp[5].o}, // 1 match
  55. {trp[0].p, NULL, NULL}, // 0 matches
  56. {NULL, trp[2].s, NULL}, // 0 matches
  57. {NULL, NULL, trp[5].p}, // 0 matches
  58. {trp[2].s, trp[6].p, NULL}, // 0 matches
  59. {NULL, trp[1].p, trp[5].o}, // 0 matches
  60. {trp[2].s, trp[2].p, trp[5].o}, // 0 matches
  61. };
  62. // Lookup result counts.
  63. size_t lu_ct[N_LUT] = { 8, 5, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
  64. /* TODO
  65. // Index of lookup matches from trp.
  66. size_t lu_match[N_LUT][5] = {
  67. {0, 1, 2, 3, 4, 5, 6, 7},
  68. {0, 3, 4, 5, 7},
  69. {2, 4, 7},
  70. {5, 7},
  71. {0}, {0}, {7},
  72. {}, {}, {},
  73. };
  74. // Index of lookup non-matches from trp.
  75. size_t lu_no_match[N_LUT][8] = {
  76. {},
  77. {1, 2, 6},
  78. {0, 1, 3, 5, 6},
  79. {0, 1, 2, 3, 4, 6},
  80. {1, 2, 3, 4, 5, 6, 7},
  81. {0, 1, 2, 3, 4, 5, 6, 7},
  82. {0, 1, 2, 3, 4, 5, 6, 7},
  83. {0, 1, 2, 3, 4, 5, 6, 7},
  84. };
  85. */
  86. LSUP_Graph *gr = LSUP_graph_new (type);
  87. size_t ct;
  88. LSUP_graph_add_trp (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. printf ("done.\n");
  98. /* TODO
  99. for (int j = 0; LSUP_graph_iter_next != LSUP_END; j++) {
  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. LSUP_graph_iter_free (it);
  109. };
  110. free_triples (trp);
  111. LSUP_graph_free (gr);
  112. return 0;
  113. }
  114. static int
  115. _graph_remove (LSUP_store_type type)
  116. {
  117. LSUP_Triple *trp = create_triples();
  118. LSUP_Graph *gr = LSUP_graph_new (type);
  119. size_t ct;
  120. LSUP_graph_add_trp (gr, trp, &ct);
  121. EXPECT_INT_EQ (ct, 8);
  122. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  123. LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
  124. ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
  125. ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
  126. ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
  127. ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
  128. ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
  129. ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
  130. ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
  131. ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
  132. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  133. free_triples (trp); // gr copied data.
  134. LSUP_graph_free (gr);
  135. // TODO Test complete removal of triples from index when they are not
  136. // in another context.
  137. return 0;
  138. }
  139. static int test_graph_new() {
  140. if (_graph_new (LSUP_STORE_MEM) != 0) return -1;
  141. if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1;
  142. return 0;
  143. }
  144. static int test_graph_add() {
  145. if (_graph_add (LSUP_STORE_MEM) != 0) return -1;
  146. if (_graph_add (LSUP_STORE_MDB_TMP) != 0) return -1;
  147. return 0;
  148. }
  149. static int test_graph_lookup() {
  150. if (_graph_lookup (LSUP_STORE_MEM) != 0) return -1;
  151. if (_graph_lookup (LSUP_STORE_MDB_TMP) != 0) return -1;
  152. return 0;
  153. }
  154. static int test_graph_remove() {
  155. if (_graph_remove (LSUP_STORE_MEM) != 0) return -1;
  156. if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1;
  157. return 0;
  158. }
  159. int graph_tests()
  160. {
  161. RUN (test_graph_new);
  162. RUN (test_graph_add);
  163. RUN (test_graph_lookup);
  164. RUN (test_graph_remove);
  165. return 0;
  166. }