test_store_mdb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "test.h"
  2. #include "store_mdb.h"
  3. #include "assets.h"
  4. static char *path = "/tmp/testdb";
  5. /** @brief Test triple store.
  6. */
  7. static int test_triple_store()
  8. {
  9. EXPECT_PASS (LSUP_mdbstore_setup (&path, true));
  10. LSUP_MDBStore *store = LSUP_mdbstore_new (path, NULL); // triple store.
  11. ASSERT (store != NULL, "Error initializing store!");
  12. LSUP_Triple *trp = create_triples();
  13. LSUP_SerTriple ser_trp[NUM_TRP];
  14. for (int i = 0; i < NUM_TRP; i++) {
  15. LSUP_striple_init (ser_trp + i, BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
  16. LSUP_triple_serialize (trp + i, ser_trp + i);
  17. }
  18. // Test adding.
  19. size_t ins;
  20. EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, NUM_TRP, &ins));
  21. EXPECT_INT_EQ (ins, 8);
  22. EXPECT_INT_EQ (LSUP_mdbstore_size (store), 8);
  23. // Test lookups.
  24. LSUP_SerTriple lut[12] = {
  25. {NULL, NULL, NULL},
  26. {ser_trp[0].s, NULL, NULL},
  27. {ser_trp[2].s, NULL, NULL},
  28. {NULL, ser_trp[0].p, NULL},
  29. {NULL, ser_trp[0].s, NULL},
  30. {NULL, NULL, ser_trp[6].o},
  31. {ser_trp[4].s, ser_trp[4].p, NULL},
  32. {NULL, ser_trp[7].p, ser_trp[7].o},
  33. {ser_trp[5].s, NULL, ser_trp[5].o},
  34. {ser_trp[5].s, NULL, ser_trp[5].o},
  35. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
  36. {ser_trp[4].s, ser_trp[4].p, ser_trp[6].o},
  37. };
  38. LSUP_Buffer *luc[12] = {
  39. NULL,
  40. NULL,
  41. ser_trp[2].s,
  42. NULL,
  43. NULL,
  44. NULL,
  45. NULL,
  46. NULL,
  47. NULL,
  48. ser_trp[2].s,
  49. NULL,
  50. NULL,
  51. };
  52. size_t results[12] = {
  53. 8,
  54. 5, 1, 1, 0, 1,
  55. 2, 1, 2, 2,
  56. 1, 0,
  57. };
  58. for (int i = 0; i < 12; i++) {
  59. size_t ct;
  60. TRACE ("Testing triple lookup #%d.\n", i);
  61. LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
  62. store, lut + i, luc[i], &ct);
  63. EXPECT_INT_EQ (ct, results[i]);
  64. LSUP_mdbiter_free (it);
  65. }
  66. for (int i = 0; i < NUM_TRP; i++) {
  67. LSUP_buffer_free (ser_trp[i].s);
  68. LSUP_buffer_free (ser_trp[i].p);
  69. LSUP_buffer_free (ser_trp[i].o);
  70. }
  71. LSUP_mdbstore_free (store);
  72. free_triples (trp);
  73. return 0;
  74. }
  75. /** @brief Test quad store.
  76. *
  77. * Insert the same triple set twice with different contexts.
  78. */
  79. static int test_quad_store()
  80. {
  81. EXPECT_PASS (LSUP_mdbstore_setup (&path, true));
  82. LSUP_Term *ctx1 = LSUP_uri_new ("urn:c:1");
  83. LSUP_Buffer *sc1 = LSUP_buffer_new_from_term (ctx1);
  84. LSUP_MDBStore *store = LSUP_mdbstore_new (path, sc1); // quad store.
  85. ASSERT (store != NULL, "Error initializing store!");
  86. LSUP_Triple *trp = create_triples();
  87. LSUP_SerTriple ser_trp[NUM_TRP];
  88. for (int i = 0; i < NUM_TRP; i++) {
  89. LSUP_striple_init (ser_trp + i, BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
  90. LSUP_triple_serialize (trp + i, ser_trp + i);
  91. }
  92. // Only triples 0÷5 in default context.
  93. size_t ct;
  94. EXPECT_PASS (LSUP_mdbstore_add (store, NULL, ser_trp, 6, &ct));
  95. EXPECT_INT_EQ (ct, 6);
  96. LSUP_Term *ctx2 = LSUP_uri_new ("urn:c:2");
  97. LSUP_Buffer *sc2 = LSUP_buffer_new_from_term (ctx2);
  98. // Only triples 4÷9 in default context.
  99. EXPECT_PASS (LSUP_mdbstore_add (store, sc2, ser_trp + 4, 6, &ct));
  100. EXPECT_INT_EQ (ct, 4);
  101. // 6 triples in ctx1 + 6 in ctx2 - 2 duplicates
  102. EXPECT_INT_EQ (LSUP_mdbstore_size (store), 10);
  103. // Test lookups.
  104. // This context is not with any triple.
  105. LSUP_Term *ctx3 = LSUP_uri_new ("urn:c:3");
  106. LSUP_Buffer *sc3 = LSUP_buffer_new_from_term (ctx3);
  107. LSUP_SerTriple lut[41] = {
  108. // Any context
  109. {NULL, NULL, NULL}, // #0
  110. {ser_trp[0].s, NULL, NULL}, // #1
  111. {NULL, ser_trp[0].p, NULL}, // #2
  112. {NULL, ser_trp[0].s, NULL}, // #3
  113. {NULL, NULL, ser_trp[6].o}, // #4
  114. {ser_trp[4].s, ser_trp[4].p, NULL}, // #5
  115. {NULL, ser_trp[7].p, ser_trp[7].o}, // #6
  116. {ser_trp[5].s, NULL, ser_trp[5].o}, // #7
  117. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #8
  118. {ser_trp[2].s, ser_trp[4].p, ser_trp[5].o}, // #9
  119. // Context 1 (trp[0÷5])
  120. {NULL, NULL, NULL}, // #10
  121. {ser_trp[0].s, NULL, NULL}, // #11
  122. {ser_trp[2].s, NULL, NULL}, // #12
  123. {NULL, ser_trp[0].p, NULL}, // #13
  124. {NULL, ser_trp[6].p, NULL}, // #14
  125. {NULL, NULL, ser_trp[6].o}, // #15
  126. {ser_trp[4].s, ser_trp[4].p, NULL}, // #16
  127. {NULL, ser_trp[7].p, ser_trp[7].o}, // #17
  128. {ser_trp[5].s, NULL, ser_trp[5].o}, // #18
  129. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #19
  130. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #20
  131. // Context 2 (trp[4÷9])
  132. {NULL, NULL, NULL}, // #21
  133. {ser_trp[0].s, NULL, NULL}, // #22
  134. {NULL, ser_trp[0].p, NULL}, // #23
  135. {NULL, ser_trp[0].s, NULL}, // #24
  136. {NULL, NULL, ser_trp[6].o}, // #25
  137. {ser_trp[4].s, ser_trp[4].p, NULL}, // #26
  138. {NULL, ser_trp[7].p, ser_trp[7].o}, // #27
  139. {ser_trp[5].s, NULL, ser_trp[5].o}, // #28
  140. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #29
  141. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #30
  142. // Non-existing context
  143. {NULL, NULL, NULL}, // #31
  144. {ser_trp[0].s, NULL, NULL}, // #32
  145. {NULL, ser_trp[0].p, NULL}, // #33
  146. {NULL, ser_trp[0].s, NULL}, // #34
  147. {NULL, NULL, ser_trp[6].o}, // #35
  148. {ser_trp[4].s, ser_trp[4].p, NULL}, // #36
  149. {NULL, ser_trp[7].p, ser_trp[7].o}, // #37
  150. {ser_trp[5].s, NULL, ser_trp[5].o}, // #38
  151. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #39
  152. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #40
  153. };
  154. LSUP_Buffer *luc[41] = {
  155. // Any context
  156. NULL, // #0
  157. NULL, NULL, NULL, NULL, // #1-#4
  158. NULL, NULL, NULL, // #5-#7
  159. NULL, NULL, // #8-#9
  160. // Context 1 (trp[0÷5])
  161. sc1, // #10
  162. sc1, sc1, sc1, sc1, sc1, // #11-#15
  163. sc1, sc1, sc1, // #16-#18
  164. sc1, sc1, // #19-#20
  165. // Context 2 (trp[4÷9])
  166. sc2, // #21
  167. sc2, sc2, sc2, sc2, // #22-#25
  168. sc2, sc2, sc2, // #26-#28
  169. sc2, sc2, // #29-#30
  170. // Non-existing context
  171. sc3, // #31
  172. sc3, sc3, sc3, sc3, // #32-#35
  173. sc3, sc3, sc3, // #36-#38
  174. sc3, sc3, // #39-#40
  175. };
  176. size_t results[41] = {
  177. // NULL ctx
  178. 8, // #0
  179. 5, 1, 0, 1, // #1-#4
  180. 2, 1, 2, // #5-#7
  181. 1, 0, // #8-#9
  182. // ctx1
  183. 6, // #10
  184. 4, 1, 1, 0, 0, // #11-#15
  185. 1, 0, 1, // #16-#18
  186. 1, 0, // #19-#20
  187. // ctx2
  188. 4, // #21
  189. 3, 0, 0, 1, // #22-#25
  190. 2, 1, 2, // #26-#28
  191. 1, 1, // #29-#30
  192. // ctx3
  193. 0, // #31-#32
  194. 0, 0, 0, 0, // #33-#36
  195. 0, 0, 0, // #37-#39
  196. 0, 0, // #40
  197. };
  198. for (int i = 0; i < 41; i++) {
  199. size_t ct;
  200. printf ("Testing triple lookup #%d: {", i);
  201. if (lut[i].s) LSUP_buffer_print (lut[i].s);
  202. else printf ("NULL");
  203. printf (", ");
  204. if (lut[i].p) LSUP_buffer_print (lut[i].p);
  205. else printf ("NULL");
  206. printf (", ");
  207. if (lut[i].o) LSUP_buffer_print (lut[i].o);
  208. else printf ("NULL");
  209. printf (", ");
  210. if (luc[i]) LSUP_buffer_print (luc[i]);
  211. else printf ("NULL");
  212. printf ("}\n");
  213. LSUP_MDBIterator *it = LSUP_mdbstore_lookup(
  214. store, lut + i, luc[i], &ct);
  215. ASSERT (it != NULL, "Lookup error!");
  216. EXPECT_INT_EQ (ct, results[i]);
  217. LSUP_mdbiter_free (it);
  218. }
  219. for (int i = 0; i < NUM_TRP; i++) {
  220. LSUP_buffer_free (ser_trp[i].s);
  221. LSUP_buffer_free (ser_trp[i].p);
  222. LSUP_buffer_free (ser_trp[i].o);
  223. }
  224. LSUP_term_free (ctx1);
  225. LSUP_term_free (ctx2);
  226. LSUP_term_free (ctx3);
  227. LSUP_buffer_free (sc1);
  228. LSUP_buffer_free (sc2);
  229. LSUP_buffer_free (sc3);
  230. free_triples (trp);
  231. LSUP_mdbstore_free (store);
  232. return 0;
  233. }
  234. int store_mdb_test()
  235. {
  236. RUN (test_triple_store);
  237. RUN (test_quad_store);
  238. return 0;
  239. }