test_store.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "test.h"
  2. #include "store.h"
  3. #include "assets/triples.h"
  4. #define STORE_ID_MDB "file:///tmp/testdb";
  5. #define STORE_ID_HTABLE LSUP_NS "dummy";
  6. static LSUP_Store store_s;
  7. static LSUP_Store *store = &store_s;
  8. /** @brief Test triple store.
  9. *
  10. * This runs context-aware stores in triple mode. They should behave exactly
  11. * in the same way as non-context stores.
  12. */
  13. static int test_triple_store()
  14. {
  15. if (store->sif->setup_fn)
  16. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  17. log_info ("Testing triple store for %s", store->id);
  18. store->data = store->sif->new_fn (store->id, 0);
  19. ASSERT (store->data != NULL, "Error creating store data back end!");
  20. LSUP_Triple *trp = create_triples();
  21. LSUP_BufferTriple ser_trp[NUM_TRP];
  22. for (int i = 0; i < NUM_TRP; i++) {
  23. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
  24. ser_trp[i] = *tmp;
  25. free (tmp);
  26. }
  27. // Test adding.
  28. void *it = store->sif->add_init_fn (store->data, NULL);
  29. size_t ins = 0;
  30. for (size_t i = 0; i < NUM_TRP; i++) {
  31. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  32. ASSERT (rc >= 0, "Error inserting triples!");
  33. if (rc == LSUP_OK) ins++;
  34. }
  35. store->sif->add_done_fn (it);
  36. EXPECT_INT_EQ (ins, 8);
  37. EXPECT_INT_EQ (store->sif->size_fn (store->data), ins);
  38. // Test lookups.
  39. LSUP_Buffer *lut[12][3] = {
  40. {NULL, NULL, NULL},
  41. {ser_trp[0].s, NULL, NULL},
  42. {ser_trp[2].s, NULL, NULL},
  43. {NULL, ser_trp[0].p, NULL},
  44. {NULL, ser_trp[0].s, NULL},
  45. {NULL, NULL, ser_trp[6].o},
  46. {ser_trp[4].s, ser_trp[4].p, NULL},
  47. {NULL, ser_trp[7].p, ser_trp[7].o},
  48. {ser_trp[5].s, NULL, ser_trp[5].o},
  49. {ser_trp[5].s, NULL, ser_trp[5].o},
  50. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
  51. {ser_trp[4].s, ser_trp[4].p, ser_trp[6].o},
  52. };
  53. LSUP_Buffer *luc[12] = {
  54. NULL,
  55. NULL,
  56. ser_trp[2].s,
  57. NULL,
  58. NULL,
  59. NULL,
  60. NULL,
  61. NULL,
  62. NULL,
  63. ser_trp[2].s,
  64. NULL,
  65. NULL,
  66. };
  67. size_t results[12] = {
  68. 8,
  69. 5,
  70. // Lookup on nonexisting context is ignored by non-context store.
  71. store->sif->features & LSUP_STORE_CTX ? 0 : 1,
  72. 1, 0, 1,
  73. 2, 1, 2,
  74. store->sif->features & LSUP_STORE_CTX ? 0 : 2,
  75. 1, 0,
  76. };
  77. for (int i = 0; i < 12; i++) {
  78. size_t ct;
  79. log_info ("Testing triple lookup #%d.", i);
  80. void *it = store->sif->lookup_fn (
  81. store->data, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
  82. ASSERT (it != NULL, "Error creating iterator!");
  83. EXPECT_INT_EQ (ct, results[i]);
  84. store->sif->lu_free_fn (it);
  85. }
  86. for (int i = 0; i < NUM_TRP; i++) {
  87. LSUP_buffer_free (ser_trp[i].s);
  88. LSUP_buffer_free (ser_trp[i].p);
  89. LSUP_buffer_free (ser_trp[i].o);
  90. }
  91. store->sif->free_fn (store->data);
  92. free_triples (trp);
  93. return 0;
  94. }
  95. /** @brief Test quad store.
  96. *
  97. * Insert the same triple set twice with different contexts.
  98. *
  99. * This is skipped for non-context stores.
  100. */
  101. static int test_quad_store()
  102. {
  103. if (!(store->sif->features & LSUP_STORE_CTX)) return 0;
  104. if (store->sif->setup_fn)
  105. EXPECT_PASS (store->sif->setup_fn (store->id, true));
  106. log_info ("Testing quad store for %s", store->id);
  107. store->data = store->sif->new_fn (store->id, 0);
  108. ASSERT (store->data != NULL, "Error creating store data back end!");
  109. LSUP_Triple *trp = create_triples();
  110. LSUP_BufferTriple ser_trp[NUM_TRP];
  111. for (int i = 0; i < NUM_TRP; i++) {
  112. LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
  113. ser_trp[i] = *tmp;
  114. free (tmp);
  115. }
  116. void *it;
  117. size_t ins;
  118. // Only triples 0÷5 in default context.
  119. it = store->sif->add_init_fn (store->data, NULL);
  120. ins = 0;
  121. for (size_t i = 0; i < 6; i++) {
  122. log_info ("Inserting triple #%d in default context.", i);
  123. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  124. EXPECT_PASS (rc);
  125. if (rc == LSUP_OK) ins++;
  126. }
  127. store->sif->add_done_fn (it);
  128. EXPECT_INT_EQ (ins, 6);
  129. LSUP_Buffer *sc1 = LSUP_default_ctx_buf;
  130. LSUP_Term *ctx2 = LSUP_iriref_new("urn:c:2", NULL);
  131. LSUP_Buffer *sc2 = LSUP_term_serialize (ctx2);
  132. // Only triples 4÷9 in context 2 (effectively 4 non-duplicates).
  133. it = store->sif->add_init_fn (store->data, sc2);
  134. ASSERT (it != NULL, "Error creating iterator!");
  135. ins = 0;
  136. for (size_t i = 4; i < 10; i++) {
  137. log_info ("Inserting triple #%d in context 2.", i);
  138. LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
  139. ASSERT (rc == LSUP_OK || rc == LSUP_NOACTION, "Error adding triples!");
  140. if (rc == LSUP_OK) ins++;
  141. }
  142. store->sif->add_done_fn (it);
  143. EXPECT_INT_EQ (ins, 4);
  144. // 6 triples in ctx1 + 6 in ctx2 - 2 duplicates
  145. EXPECT_INT_EQ (store->sif->size_fn (store->data), 10);
  146. // This context has no triples.
  147. LSUP_Term *ctx3 = LSUP_iriref_new("urn:c:3", NULL);
  148. LSUP_Buffer *sc3 = LSUP_term_serialize (ctx3);
  149. // Test lookups.
  150. LSUP_Buffer *lut[41][3] = {
  151. // Any context
  152. {NULL, NULL, NULL}, // #0
  153. {ser_trp[0].s, NULL, NULL}, // #1
  154. {NULL, ser_trp[0].p, NULL}, // #2
  155. {NULL, ser_trp[0].s, NULL}, // #3
  156. {NULL, NULL, ser_trp[6].o}, // #4
  157. {ser_trp[4].s, ser_trp[4].p, NULL}, // #5
  158. {NULL, ser_trp[7].p, ser_trp[7].o}, // #6
  159. {ser_trp[5].s, NULL, ser_trp[5].o}, // #7
  160. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #8
  161. {ser_trp[2].s, ser_trp[4].p, ser_trp[5].o}, // #9
  162. // Context 1 (trp[0÷5])
  163. {NULL, NULL, NULL}, // #10
  164. {ser_trp[0].s, NULL, NULL}, // #11
  165. {ser_trp[2].s, NULL, NULL}, // #12
  166. {NULL, ser_trp[0].p, NULL}, // #13
  167. {NULL, ser_trp[6].p, NULL}, // #14
  168. {NULL, NULL, ser_trp[6].o}, // #15
  169. {ser_trp[4].s, ser_trp[4].p, NULL}, // #16
  170. {NULL, ser_trp[7].p, ser_trp[7].o}, // #17
  171. {ser_trp[5].s, NULL, ser_trp[5].o}, // #18
  172. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #19
  173. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #20
  174. // Context 2 (trp[4÷9])
  175. {NULL, NULL, NULL}, // #21
  176. {ser_trp[0].s, NULL, NULL}, // #22
  177. {NULL, ser_trp[0].p, NULL}, // #23
  178. {NULL, ser_trp[0].s, NULL}, // #24
  179. {NULL, NULL, ser_trp[6].o}, // #25
  180. {ser_trp[4].s, ser_trp[4].p, NULL}, // #26
  181. {NULL, ser_trp[7].p, ser_trp[7].o}, // #27
  182. {ser_trp[5].s, NULL, ser_trp[5].o}, // #28
  183. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #29
  184. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #30
  185. // Non-existing context
  186. {NULL, NULL, NULL}, // #31
  187. {ser_trp[0].s, NULL, NULL}, // #32
  188. {NULL, ser_trp[0].p, NULL}, // #33
  189. {NULL, ser_trp[0].s, NULL}, // #34
  190. {NULL, NULL, ser_trp[6].o}, // #35
  191. {ser_trp[4].s, ser_trp[4].p, NULL}, // #36
  192. {NULL, ser_trp[7].p, ser_trp[7].o}, // #37
  193. {ser_trp[5].s, NULL, ser_trp[5].o}, // #38
  194. {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o}, // #39
  195. {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o}, // #40
  196. };
  197. LSUP_Buffer *luc[41] = {
  198. // Any context
  199. NULL, // #0
  200. NULL, NULL, NULL, NULL, // #1-#4
  201. NULL, NULL, NULL, // #5-#7
  202. NULL, NULL, // #8-#9
  203. // Context 1 (trp[0÷5])
  204. sc1, // #10
  205. sc1, sc1, sc1, sc1, sc1, // #11-#15
  206. sc1, sc1, sc1, // #16-#18
  207. sc1, sc1, // #19-#20
  208. // Context 2 (trp[4÷9])
  209. sc2, // #21
  210. sc2, sc2, sc2, sc2, // #22-#25
  211. sc2, sc2, sc2, // #26-#28
  212. sc2, sc2, // #29-#30
  213. // Non-existing context
  214. sc3, // #31
  215. sc3, sc3, sc3, sc3, // #32-#35
  216. sc3, sc3, sc3, // #36-#38
  217. sc3, sc3, // #39-#40
  218. };
  219. size_t results[41] = {
  220. // NULL ctx
  221. 8, // #0
  222. 5, 1, 0, 1, // #1-#4
  223. 2, 1, 2, // #5-#7
  224. 1, 0, // #8-#9
  225. // ctx1
  226. 6, // #10
  227. 4, 1, 1, 0, 0, // #11-#15
  228. 1, 0, 1, // #16-#18
  229. 1, 0, // #19-#20
  230. // ctx2
  231. 4, // #21
  232. 3, 0, 0, 1, // #22-#25
  233. 2, 1, 2, // #26-#28
  234. 1, 1, // #29-#30
  235. // ctx3
  236. 0, // #31-#32
  237. 0, 0, 0, 0, // #33-#36
  238. 0, 0, 0, // #37-#39
  239. 0, 0, // #40
  240. };
  241. int ctx_ct[10] = {
  242. // BEGIN ctx1 (triples 0÷5)
  243. 1, 1, 1, 1,
  244. // BEGIN ctx2 (triples 4÷9)
  245. 2, 2,
  246. // END ctx 1
  247. 1, 1, 1, 1,
  248. // END ctx 2
  249. };
  250. for (int i = 0; i < 41; i++) {
  251. size_t ct;
  252. log_info ("Checking triple #%d.", i);
  253. void *it = store->sif->lookup_fn (
  254. store->data, lut[i][0], lut[i][1], lut[i][2], luc[i], &ct);
  255. ASSERT (it != NULL, "Lookup error!");
  256. EXPECT_INT_EQ (ct, results[i]);
  257. store->sif->lu_free_fn (it);
  258. }
  259. // Check triple contexts.
  260. for (int i = 0; i < 10; i++) {
  261. void *it = store->sif->lookup_fn (
  262. store->data, ser_trp[i].s, ser_trp[i].p, ser_trp[i].o,
  263. NULL, NULL);
  264. log_info ("Checking contexts for triple %d.", i);
  265. LSUP_Buffer *ctx_a;
  266. EXPECT_PASS (store->sif->lu_next_fn (it, NULL, &ctx_a));
  267. store->sif->lu_free_fn (it);
  268. ASSERT (ctx_a != NULL, "No contexts found!");
  269. size_t j = 0;
  270. while (ctx_a[j].addr) j++;
  271. free (ctx_a);
  272. EXPECT_INT_EQ (j, ctx_ct[i]);
  273. }
  274. for (int i = 0; i < NUM_TRP; i++) {
  275. LSUP_buffer_free (ser_trp[i].s);
  276. LSUP_buffer_free (ser_trp[i].p);
  277. LSUP_buffer_free (ser_trp[i].o);
  278. }
  279. LSUP_term_free (ctx2);
  280. LSUP_term_free (ctx3);
  281. LSUP_buffer_free (sc2);
  282. LSUP_buffer_free (sc3);
  283. free_triples (trp);
  284. store->sif->free_fn (store->data);
  285. return 0;
  286. }
  287. int store_tests()
  288. {
  289. #define ENTRY(a, b) \
  290. store->type = LSUP_STORE_##a; \
  291. store->id = STORE_ID_##a; \
  292. store->sif = &b; \
  293. RUN (test_triple_store); \
  294. RUN (test_quad_store);
  295. BACKEND_TBL
  296. #undef ENTRY
  297. return 0;
  298. }