test_store_mdb.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <unistd.h>
  2. #include "test.h"
  3. #include "store_mdb.h"
  4. #define MDBSTORE_ID "file:///tmp/testdb"
  5. /** @brief Test context switching.
  6. */
  7. static int test_ctx_switch()
  8. {
  9. const LSUP_StoreInt *sif = LSUP_store_int (LSUP_STORE_MDB);
  10. sif->setup_fn (MDBSTORE_ID, true);
  11. LSUP_Store *store = LSUP_store_new (LSUP_STORE_MDB, MDBSTORE_ID, 0);
  12. ASSERT (store != NULL, "Error initializing store!");
  13. // Create enough triples to test a multi-page copy of triple data.
  14. // Add small buffer (4) to create a 3rd page.
  15. size_t num_trp = (getpagesize() * 2 / TRP_KLEN) + 4;
  16. LSUP_BufferTriple **tdata = malloc (num_trp * sizeof (*tdata));
  17. LSUP_Triple *trp = LSUP_triple_new (
  18. LSUP_iriref_new ("urn:s:1", NULL),
  19. LSUP_iriref_new ("urn:p:1", NULL),
  20. NULL);
  21. char *o_str = malloc (8 * sizeof (*o_str));
  22. if (UNLIKELY (!o_str)) return LSUP_MEM_ERR;
  23. for (unsigned int i = 0; i < num_trp; i++) {
  24. sprintf (o_str, "%.7d", i);
  25. if (trp->o) LSUP_term_free (trp->o);
  26. trp->o = LSUP_literal_new (o_str, NULL);
  27. tdata[i] = LSUP_triple_serialize (trp);
  28. }
  29. LSUP_triple_free (trp);
  30. free (o_str);
  31. LOG_DEBUG ("Created %lu triples.", num_trp);
  32. LSUP_Term
  33. *c1 = LSUP_iriref_new ("urn:c:1", NULL),
  34. *c2 = LSUP_iriref_new ("urn:c:2", NULL);
  35. LSUP_Buffer
  36. *sc1 = LSUP_term_serialize (c1),
  37. *sc2 = LSUP_term_serialize (c2);
  38. LSUP_term_free (c1);
  39. LSUP_term_free (c2);
  40. void *it = sif->add_init_fn (store->data, sc1, NULL);
  41. for (size_t i = 0; i < num_trp; i++)
  42. sif->add_iter_fn (it, tdata[i]);
  43. sif->add_done_fn (it);
  44. for (size_t i = 0; i < num_trp; i++)
  45. LSUP_btriple_free (tdata[i]);
  46. free (tdata);
  47. size_t check_ct;
  48. ASSERT (
  49. it = sif->lookup_fn (
  50. store->data, NULL, NULL, NULL, sc1, NULL, &check_ct
  51. ), "Error looking up triples!");
  52. EXPECT_INT_EQ (check_ct, num_trp);
  53. sif->lu_free_fn (it);
  54. RCCK (sif->update_ctx_fn (store->data, sc1, sc2, NULL));
  55. ASSERT (
  56. it = sif->lookup_fn (
  57. store->data, NULL, NULL, NULL, sc2, NULL, &check_ct
  58. ), "Error looking up triples!");
  59. EXPECT_INT_EQ (check_ct, num_trp);
  60. sif->lu_free_fn (it);
  61. LSUP_buffer_free (sc1);
  62. LSUP_buffer_free (sc2);
  63. LSUP_store_free (store);
  64. return 0;
  65. }
  66. int store_mdb_tests()
  67. {
  68. RUN (test_ctx_switch);
  69. return 0;
  70. }