123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- #include "test.h"
- #include "store.h"
- #include "assets/triples.h"
- #define STORE_ID_MDB "file:///tmp/testdb";
- #define STORE_ID_HTABLE LSUP_NS "dummy";
- static LSUP_Store store_s;
- static LSUP_Store *store = &store_s;
- static int test_triple_store()
- {
- if (store->sif->setup_fn)
- EXPECT_PASS (store->sif->setup_fn (store->id, true));
- log_info ("Testing triple store for %s", store->id);
- store->data = store->sif->new_fn (store->id, 0);
- ASSERT (store->data != NULL, "Error creating store data back end!");
- LSUP_Triple *trp = create_triples();
- LSUP_BufferTriple ser_trp[NUM_TRP];
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
- ser_trp[i] = *tmp;
- free (tmp);
- }
-
- void *it = store->sif->add_init_fn (store->data, NULL, NULL);
- size_t ins = 0;
- for (size_t i = 0; i < NUM_TRP; i++) {
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc >= 0, "Error inserting triples!");
- if (rc == LSUP_OK) ins++;
- }
- store->sif->add_done_fn (it);
- EXPECT_INT_EQ (ins, 8);
- EXPECT_INT_EQ (store->sif->size_fn (store->data), ins);
-
- LSUP_Buffer *lut[12][3] = {
- {NULL, NULL, NULL},
- {ser_trp[0].s, NULL, NULL},
- {ser_trp[2].s, NULL, NULL},
- {NULL, ser_trp[0].p, NULL},
- {NULL, ser_trp[0].s, NULL},
- {NULL, NULL, ser_trp[6].o},
- {ser_trp[4].s, ser_trp[4].p, NULL},
- {NULL, ser_trp[7].p, ser_trp[7].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[6].o},
- };
- LSUP_Buffer *luc[12] = {
- NULL,
- NULL,
- ser_trp[2].s,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- ser_trp[2].s,
- NULL,
- NULL,
- };
- size_t results[12] = {
- 8,
- 5,
-
- store->sif->features & LSUP_STORE_CTX ? 0 : 1,
- 1, 0, 1,
- 2, 1, 2,
- store->sif->features & LSUP_STORE_CTX ? 0 : 2,
- 1, 0,
- };
- for (int i = 0; i < 12; i++) {
- size_t ct;
- log_info ("Testing triple lookup #%d.", i);
- void *it = store->sif->lookup_fn (
- store->data, lut[i][0], lut[i][1], lut[i][2], luc[i],
- NULL, &ct);
- ASSERT (it != NULL, "Error creating iterator!");
- EXPECT_INT_EQ (ct, results[i]);
- store->sif->lu_free_fn (it);
- }
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_buffer_free (ser_trp[i].s);
- LSUP_buffer_free (ser_trp[i].p);
- LSUP_buffer_free (ser_trp[i].o);
- }
- store->sif->free_fn (store->data);
- free_triples (trp);
- return 0;
- }
- static int test_quad_store()
- {
- if (!(store->sif->features & LSUP_STORE_CTX)) return 0;
- if (store->sif->setup_fn)
- EXPECT_PASS (store->sif->setup_fn (store->id, true));
- log_info ("Testing quad store for %s", store->id);
- store->data = store->sif->new_fn (store->id, 0);
- ASSERT (store->data != NULL, "Error creating store data back end!");
- LSUP_Triple *trp = create_triples();
- LSUP_BufferTriple ser_trp[NUM_TRP];
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
- ser_trp[i] = *tmp;
- free (tmp);
- }
- void *it;
- size_t ins;
-
- it = store->sif->add_init_fn (store->data, NULL, NULL);
- ins = 0;
- for (size_t i = 0; i < 6; i++) {
- log_info ("Inserting triple #%d in default context.", i);
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- EXPECT_PASS (rc);
- if (rc == LSUP_OK) ins++;
- }
- store->sif->add_done_fn (it);
- EXPECT_INT_EQ (ins, 6);
- LSUP_Buffer *sc1 = LSUP_default_ctx_buf;
- LSUP_Term *ctx2 = LSUP_iriref_new("urn:c:2", NULL);
- LSUP_Buffer *sc2 = LSUP_term_serialize (ctx2);
-
- it = store->sif->add_init_fn (store->data, sc2, NULL);
- ASSERT (it != NULL, "Error creating iterator!");
- ins = 0;
- for (size_t i = 4; i < 10; i++) {
- log_info ("Inserting triple #%d in context 2.", i);
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc == LSUP_OK || rc == LSUP_NOACTION, "Error adding triples!");
- if (rc == LSUP_OK) ins++;
- }
- store->sif->add_done_fn (it);
- EXPECT_INT_EQ (ins, 4);
-
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 10);
-
- LSUP_Term *ctx3 = LSUP_iriref_new("urn:c:3", NULL);
- LSUP_Buffer *sc3 = LSUP_term_serialize (ctx3);
-
- LSUP_Buffer *lut[41][3] = {
-
- {NULL, NULL, NULL},
- {ser_trp[0].s, NULL, NULL},
- {NULL, ser_trp[0].p, NULL},
- {NULL, ser_trp[0].s, NULL},
- {NULL, NULL, ser_trp[6].o},
- {ser_trp[4].s, ser_trp[4].p, NULL},
- {NULL, ser_trp[7].p, ser_trp[7].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
- {ser_trp[2].s, ser_trp[4].p, ser_trp[5].o},
-
- {NULL, NULL, NULL},
- {ser_trp[0].s, NULL, NULL},
- {ser_trp[2].s, NULL, NULL},
- {NULL, ser_trp[0].p, NULL},
- {NULL, ser_trp[6].p, NULL},
- {NULL, NULL, ser_trp[6].o},
- {ser_trp[4].s, ser_trp[4].p, NULL},
- {NULL, ser_trp[7].p, ser_trp[7].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
- {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o},
-
- {NULL, NULL, NULL},
- {ser_trp[0].s, NULL, NULL},
- {NULL, ser_trp[0].p, NULL},
- {NULL, ser_trp[0].s, NULL},
- {NULL, NULL, ser_trp[6].o},
- {ser_trp[4].s, ser_trp[4].p, NULL},
- {NULL, ser_trp[7].p, ser_trp[7].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
- {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o},
-
- {NULL, NULL, NULL},
- {ser_trp[0].s, NULL, NULL},
- {NULL, ser_trp[0].p, NULL},
- {NULL, ser_trp[0].s, NULL},
- {NULL, NULL, ser_trp[6].o},
- {ser_trp[4].s, ser_trp[4].p, NULL},
- {NULL, ser_trp[7].p, ser_trp[7].o},
- {ser_trp[5].s, NULL, ser_trp[5].o},
- {ser_trp[4].s, ser_trp[4].p, ser_trp[4].o},
- {ser_trp[6].s, ser_trp[6].p, ser_trp[6].o},
- };
- LSUP_Buffer *luc[41] = {
-
- NULL,
- NULL, NULL, NULL, NULL,
- NULL, NULL, NULL,
- NULL, NULL,
-
- sc1,
- sc1, sc1, sc1, sc1, sc1,
- sc1, sc1, sc1,
- sc1, sc1,
-
- sc2,
- sc2, sc2, sc2, sc2,
- sc2, sc2, sc2,
- sc2, sc2,
-
- sc3,
- sc3, sc3, sc3, sc3,
- sc3, sc3, sc3,
- sc3, sc3,
- };
- size_t results[41] = {
-
- 8,
- 5, 1, 0, 1,
- 2, 1, 2,
- 1, 0,
-
- 6,
- 4, 1, 1, 0, 0,
- 1, 0, 1,
- 1, 0,
-
- 4,
- 3, 0, 0, 1,
- 2, 1, 2,
- 1, 1,
-
- 0,
- 0, 0, 0, 0,
- 0, 0, 0,
- 0, 0,
- };
- int ctx_ct[10] = {
-
- 1, 1, 1, 1,
-
- 2, 2,
-
- 1, 1, 1, 1,
-
- };
- for (int i = 0; i < 41; i++) {
- size_t ct;
- log_info ("Checking triple #%d.", i);
- void *it = store->sif->lookup_fn (
- store->data, lut[i][0], lut[i][1], lut[i][2], luc[i],
- NULL, &ct);
- ASSERT (it != NULL, "Lookup error!");
- EXPECT_INT_EQ (ct, results[i]);
- store->sif->lu_free_fn (it);
- }
-
- for (int i = 0; i < 10; i++) {
- void *it = store->sif->lookup_fn (
- store->data, ser_trp[i].s, ser_trp[i].p, ser_trp[i].o,
- NULL, NULL, NULL);
- log_info ("Checking contexts for triple %d.", i);
- LSUP_Buffer *ctx_a;
- EXPECT_PASS (store->sif->lu_next_fn (it, NULL, &ctx_a));
- store->sif->lu_free_fn (it);
- ASSERT (ctx_a != NULL, "No contexts found!");
- size_t j = 0;
- while (ctx_a[j].addr) j++;
- free (ctx_a);
- EXPECT_INT_EQ (j, ctx_ct[i]);
- }
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_buffer_free (ser_trp[i].s);
- LSUP_buffer_free (ser_trp[i].p);
- LSUP_buffer_free (ser_trp[i].o);
- }
- LSUP_term_free (ctx2);
- LSUP_term_free (ctx3);
- LSUP_buffer_free (sc2);
- LSUP_buffer_free (sc3);
- free_triples (trp);
- store->sif->free_fn (store->data);
- return 0;
- }
- static int test_txn_commit (void)
- {
- if (!(store->sif->features & LSUP_STORE_TXN)) return 0;
- ASSERT (
- store->sif->txn_begin_fn != NULL,
- "Transaction begin function is NULL!");
- ASSERT (
- store->sif->txn_commit_fn != NULL,
- "Transaction commit function is NULL!");
- ASSERT (
- store->sif->txn_abort_fn != NULL,
- "Transaction abort function is NULL!");
- ASSERT (
- store->sif->add_abort_fn != NULL,
- "Add abort function is NULL!");
- ASSERT (
- store->sif->iter_txn_fn != NULL,
- "Iterator transaction function is NULL!");
- if (store->sif->setup_fn)
- EXPECT_PASS (store->sif->setup_fn (store->id, true));
- log_info ("Testing transaction control for %s", store->id);
- store->data = store->sif->new_fn (store->id, 0);
- ASSERT (store->data != NULL, "Error creating store data back end!");
- LSUP_Triple *trp = create_triples();
- LSUP_BufferTriple ser_trp[NUM_TRP];
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
- ser_trp[i] = *tmp;
- free (tmp);
- }
- void *it;
-
- it = store->sif->add_init_fn (store->data, NULL, NULL);
- for (size_t i = 0; i < NUM_TRP; i++) {
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc >= 0, "Error inserting triples!");
- }
- store->sif->add_abort_fn (it);
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
-
- void *txn;
- EXPECT_PASS (store->sif->txn_begin_fn (store->data, 0, &txn));
- it = store->sif->add_init_fn (store->data, NULL, txn);
- for (size_t i = 0; i < NUM_TRP; i++) {
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc >= 0, "Error inserting triples!");
- }
- store->sif->add_done_fn (it);
-
-
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
- size_t ct;
- it = store->sif->lookup_fn (
- store->data, NULL, NULL, NULL, NULL, txn, &ct);
- store->sif->lu_free_fn (it);
-
- EXPECT_INT_EQ (ct, 8);
-
- EXPECT_PASS (store->sif->txn_commit_fn (txn));
- it = store->sif->lookup_fn (
- store->data, NULL, NULL, NULL, NULL, NULL, &ct);
- store->sif->lu_free_fn (it);
- EXPECT_INT_EQ (ct, 8);
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 8);
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_buffer_free (ser_trp[i].s);
- LSUP_buffer_free (ser_trp[i].p);
- LSUP_buffer_free (ser_trp[i].o);
- }
- store->sif->free_fn (store->data);
- free_triples (trp);
- return 0;
- }
- static int test_txn_abort (void)
- {
- if (!(store->sif->features & LSUP_STORE_TXN)) return 0;
- if (store->sif->setup_fn)
- EXPECT_PASS (store->sif->setup_fn (store->id, true));
- log_info ("Testing transaction control for %s", store->id);
- store->data = store->sif->new_fn (store->id, 0);
- ASSERT (store->data != NULL, "Error creating store data back end!");
- LSUP_Triple *trp = create_triples();
- LSUP_BufferTriple ser_trp[NUM_TRP];
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_BufferTriple *tmp = LSUP_triple_serialize (trp + i);
- ser_trp[i] = *tmp;
- free (tmp);
- }
- void *it;
-
- it = store->sif->add_init_fn (store->data, NULL, NULL);
- for (size_t i = 0; i < NUM_TRP; i++) {
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc >= 0, "Error inserting triples!");
- }
- store->sif->add_abort_fn (it);
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
-
- void *txn;
- EXPECT_PASS (store->sif->txn_begin_fn (store->data, 0, &txn));
- it = store->sif->add_init_fn (store->data, NULL, txn);
- for (size_t i = 0; i < NUM_TRP; i++) {
- LSUP_rc rc = store->sif->add_iter_fn (it, ser_trp + i);
- ASSERT (rc >= 0, "Error inserting triples!");
- }
- store->sif->add_done_fn (it);
-
-
- EXPECT_INT_EQ (store->sif->size_fn (store->data), 0);
- size_t ct;
- it = store->sif->lookup_fn (
- store->data, NULL, NULL, NULL, NULL, txn, &ct);
- store->sif->lu_free_fn (it);
-
- EXPECT_INT_EQ (ct, 8);
-
- store->sif->txn_abort_fn (txn);
- it = store->sif->lookup_fn (
- store->data, NULL, NULL, NULL, NULL, NULL, &ct);
- store->sif->lu_free_fn (it);
- EXPECT_INT_EQ (ct, 0);
- for (int i = 0; i < NUM_TRP; i++) {
- LSUP_buffer_free (ser_trp[i].s);
- LSUP_buffer_free (ser_trp[i].p);
- LSUP_buffer_free (ser_trp[i].o);
- }
- store->sif->free_fn (store->data);
- free_triples (trp);
- return 0;
- }
- int store_tests()
- {
- #define ENTRY(a, b) \
- store->type = LSUP_STORE_##a; \
- store->id = STORE_ID_##a; \
- store->sif = &b; \
- RUN (test_triple_store); \
- RUN (test_quad_store); \
- RUN (test_txn_commit); \
- RUN (test_txn_abort);
- BACKEND_TBL
- #undef ENTRY
- return 0;
- }
|