test_store_ht.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "test.h"
  2. #include "store_htable.h"
  3. #include "assets/triples.h"
  4. /** @brief Test hash table store.
  5. */
  6. static int test_htstore()
  7. {
  8. LSUP_HTStore *store = LSUP_htstore_new();
  9. ASSERT (store != NULL, "Error initializing store!");
  10. LSUP_Triple *trp = create_triples();
  11. LSUP_BufferTriple *ser_trp[NUM_TRP];
  12. for (int i = 0; i < NUM_TRP; i++)
  13. ser_trp[i] = LSUP_triple_serialize (trp + i);
  14. // Test adding.
  15. LSUP_HTIterator *it = LSUP_htstore_add_init (store);
  16. for (size_t i = 0; i < NUM_TRP; i++) {
  17. LSUP_rc rc = LSUP_htstore_add_iter (it, ser_trp[i]);
  18. ASSERT (
  19. (i < 8 && rc == LSUP_OK) || rc == LSUP_NOACTION,
  20. "Wrong return code on insert!");
  21. }
  22. EXPECT_INT_EQ (LSUP_htiter_cur (it), 8);
  23. EXPECT_INT_EQ (LSUP_htstore_size (store), 8);
  24. LSUP_htstore_add_done (it);
  25. // Test lookups.
  26. LSUP_Buffer *lut[12][3] = {
  27. {NULL, NULL, NULL},
  28. {ser_trp[0]->s, NULL, NULL},
  29. {ser_trp[2]->s, NULL, NULL},
  30. {NULL, ser_trp[0]->p, NULL},
  31. {NULL, ser_trp[0]->s, NULL},
  32. {NULL, NULL, ser_trp[6]->o},
  33. {ser_trp[4]->s, ser_trp[4]->p, NULL},
  34. {NULL, ser_trp[7]->p, ser_trp[7]->o},
  35. {ser_trp[5]->s, NULL, ser_trp[5]->o},
  36. {ser_trp[5]->s, NULL, ser_trp[5]->o},
  37. {ser_trp[4]->s, ser_trp[4]->p, ser_trp[4]->o},
  38. {ser_trp[4]->s, ser_trp[4]->p, ser_trp[6]->o},
  39. };
  40. size_t results[12] = {
  41. 8,
  42. 5, 1, 1, 0, 1,
  43. 2, 1, 2, 2,
  44. 1, 0,
  45. };
  46. LSUP_BufferTriple *sspo = STRP_DUMMY;
  47. for (int i = 0; i < NUM_TRP; i++) {
  48. size_t ct = 0;
  49. log_info ("Testing triple lookup #%d.", i);
  50. LSUP_HTIterator *it = LSUP_htstore_lookup(
  51. store, lut[i][0], lut[i][1], lut[i][2]);
  52. while (LSUP_htiter_next (it, sspo) != LSUP_END) {
  53. // Verify that the internal counter aligns with an external one.
  54. ct ++;
  55. EXPECT_INT_EQ (ct, LSUP_htiter_cur(it));
  56. }
  57. EXPECT_INT_EQ (ct, results[i]);
  58. LSUP_htiter_free (it);
  59. }
  60. free (sspo);
  61. for (int i = 0; i < NUM_TRP; i++)
  62. LSUP_btriple_free (ser_trp[i]);
  63. LSUP_htstore_free (store);
  64. free_triples (trp);
  65. return 0;
  66. }
  67. int store_ht_tests()
  68. {
  69. RUN(test_htstore);
  70. return 0;
  71. }