test_store_ht.c 2.4 KB

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