test_graph.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "test.h"
  2. #include "graph.h"
  3. #include "assets/triples.h"
  4. #define N_LUT 12
  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. printf ("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. {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] = { 5, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
  63. /* TODO
  64. // Index of lookup matches from trp.
  65. size_t lu_match[N_LUT][5] = {
  66. {0, 3, 4, 5, 7},
  67. {2, 4, 7},
  68. {5, 7},
  69. {0}, {0}, {7},
  70. {}, {}, {},
  71. };
  72. // Index of lookup non-matches from trp.
  73. size_t lu_no_match[N_LUT][8] = {
  74. {1, 2, 6},
  75. {0, 1, 3, 5, 6},
  76. {0, 1, 2, 3, 4, 6},
  77. {1, 2, 3, 4, 5, 6, 7},
  78. {0, 1, 2, 3, 4, 5, 6, 7},
  79. {0, 1, 2, 3, 4, 5, 6, 7},
  80. {0, 1, 2, 3, 4, 5, 6, 7},
  81. };
  82. */
  83. LSUP_Graph *gr = LSUP_graph_new (type);
  84. size_t ct;
  85. LSUP_graph_add_trp (gr, trp, &ct);
  86. EXPECT_INT_EQ (ct, 8);
  87. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  88. LSUP_Triple *spo;
  89. for (int i = 0; i < N_LUT; i++) {
  90. printf ("Checking tiple #%d on %d... ", i, type);
  91. LSUP_GraphIterator *it = LSUP_graph_lookup (
  92. gr, lu_trp[i][0], lu_trp[i][1], lu_trp[i][2], &ct);
  93. if (type != LSUP_STORE_MEM) // TODO not implemented in htable.
  94. EXPECT_INT_EQ (ct, lu_ct[i]);
  95. printf ("done.\n");
  96. /* TODO
  97. for (int j = 0; LSUP_graph_iter_next != LSUP_END; j++) {
  98. ASSERT (
  99. LSUP_graph_contains (trp[lu_match[j]]),
  100. "Triple not found!");
  101. ASSERT (
  102. !(LSUP_graph_contains (trp[lu_no_match[j]])),
  103. "Unexpected triple found!");
  104. }
  105. */
  106. LSUP_graph_iter_free (it);
  107. };
  108. free_triples (trp);
  109. LSUP_graph_free (gr);
  110. return 0;
  111. }
  112. static int
  113. _graph_remove (LSUP_store_type type)
  114. {
  115. LSUP_Triple *trp = create_triples();
  116. LSUP_Graph *gr = LSUP_graph_new (type);
  117. size_t ct;
  118. LSUP_graph_add_trp (gr, trp, &ct);
  119. EXPECT_INT_EQ (ct, 8);
  120. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  121. LSUP_graph_remove (gr, trp[0].s, NULL, NULL, &ct);
  122. ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
  123. ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
  124. ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
  125. ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
  126. ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
  127. ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
  128. ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
  129. ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
  130. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  131. free_triples (trp); // gr copied data.
  132. LSUP_graph_free (gr);
  133. // TODO Test complete removal of triples from index when they are not
  134. // in another context.
  135. return 0;
  136. }
  137. static int test_graph_new() {
  138. if (_graph_new (LSUP_STORE_MEM) != 0) return -1;
  139. if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1;
  140. return 0;
  141. }
  142. static int test_graph_add() {
  143. if (_graph_add (LSUP_STORE_MEM) != 0) return -1;
  144. if (_graph_add (LSUP_STORE_MDB_TMP) != 0) return -1;
  145. return 0;
  146. }
  147. static int test_graph_lookup() {
  148. if (_graph_lookup (LSUP_STORE_MEM) != 0) return -1;
  149. if (_graph_lookup (LSUP_STORE_MDB_TMP) != 0) return -1;
  150. return 0;
  151. }
  152. static int test_graph_remove() {
  153. if (_graph_remove (LSUP_STORE_MEM) != 0) return -1;
  154. if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1;
  155. return 0;
  156. }
  157. int graph_tests()
  158. {
  159. RUN (test_graph_new);
  160. RUN (test_graph_add);
  161. RUN (test_graph_lookup);
  162. RUN (test_graph_remove);
  163. return 0;
  164. }