test_store_ht.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(0);
  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. //printf("Insert #%lu\n", i);
  18. LSUP_rc rc = LSUP_htstore_add_iter (it, ser_trp[i]);
  19. ASSERT (
  20. (i < 8 && rc == LSUP_OK) || rc == LSUP_NOACTION,
  21. "Wrong return code on insert!");
  22. }
  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 = BTRP_DUMMY;
  47. for (int i = 0; i < NUM_TRP; i++) {
  48. size_t ct, ct2 = 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], &ct);
  52. EXPECT_INT_EQ (ct, results[i]);
  53. // Verify that the internal counter aligns with an external one.
  54. while (LSUP_htiter_next (it, sspo) != LSUP_END) ct2++;
  55. EXPECT_INT_EQ (ct, ct2);
  56. LSUP_htiter_free (it);
  57. }
  58. free (sspo);
  59. for (int i = 0; i < NUM_TRP; i++)
  60. LSUP_btriple_free (ser_trp[i]);
  61. LSUP_htstore_free (store);
  62. free_triples (trp);
  63. return 0;
  64. }
  65. int store_ht_tests()
  66. {
  67. RUN(test_htstore);
  68. return 0;
  69. }