123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "test.h"
- #include "htable.h"
- #define _CT 8
- static inline uint64_t id_hash_fn(const void *key, ksize_t size, uint64_t seed)
- { return *(uint64_t*)key; }
- static inline bool buffer_eq_fn(const void *a, const void *b, ksize_t size)
- { return memcmp(a, b, size) == 0; }
- static int htable_idx()
- {
- LSUP_Key keys[_CT] = {5, 8, 13, 21, 34, 55, 89, 5};
- LSUP_HTable *ht = LSUP_htable_new(
- _CT, sizeof(LSUP_Key), sizeof(LSUP_Buffer),
- id_hash_fn, buffer_eq_fn);
- LSUP_Buffer *sterm = BUF_DUMMY;
- for (int i = 0; i < _CT; i++) {
- char tmp[64];
- sprintf(tmp, "<%lu>", keys[i]);
- LSUP_buffer_reset(sterm, strlen(tmp) + 1, tmp);
- printf("Buffer to insert: ");
- LSUP_buffer_print(sterm);
- if (LSUP_htable_put(ht, keys + i, sterm) != LSUP_OK)
- LSUP_buffer_done(sterm);
- }
- LSUP_buffer_free(sterm);
- EXPECT_INT_EQ(LSUP_htable_size(ht), 7);
- for (int i = 0; i < _CT; i++) {
- LSUP_Buffer* vtmp;
- char ptmp[64];
- LSUP_htable_get(ht, keys + i, (void**)&vtmp);
- printf("Key in get: <%lu>: ", keys[i]);
- LSUP_buffer_print(vtmp);
- sprintf(ptmp, "<%lu>", keys[i]);
- EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
- }
- LSUP_Key *ktmp;
- LSUP_Buffer *vtmp;
- htsize_t cur = 0;
- while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
- printf("Key in iter: <%lu>: ", *ktmp);
- LSUP_buffer_print(vtmp);
- char ptmp[64];
- sprintf(ptmp, "<%lu>", *ktmp);
- EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
- }
- cur = 0;
- while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
- LSUP_buffer_done(vtmp);
- }
- printf("Freeing hash table.\n");
- LSUP_htable_free(ht);
- return 0;
- }
- static int htable_keys()
- {
- LSUP_TripleKey keys[_CT] = {
- {1, 1, 1},
- {2, 1, 1},
- {1, 2, 3},
- {1, 9, 9},
- {5, 6, 7},
- {7, 6, 5},
- {1, 1, 1}, // Duplicate.
- {2, 1, 1}, // Duplicate.
- };
- LSUP_HTable *ht = LSUP_htable_new(
- _CT, sizeof(LSUP_TripleKey), sizeof(LSUP_Buffer),
- id_hash_fn, buffer_eq_fn);
- LSUP_Buffer *sterm = BUF_DUMMY;
- for (int i = 0; i < _CT; i++) {
- char tmp[64];
- sprintf(tmp, "<%lu : %lu : %lu>", keys[i][0], keys[i][1], keys[i][2]);
- LSUP_buffer_reset(sterm, strlen(tmp) + 1, tmp);
- TRACE(STR, "Buffer to insert: ");
- LSUP_buffer_print(sterm);
- if (LSUP_htable_put(ht, keys + i, sterm) != LSUP_OK)
- LSUP_buffer_done(sterm);
- }
- LSUP_buffer_free(sterm);
- EXPECT_INT_EQ(LSUP_htable_size(ht), 6);
- for (int i = 0; i < _CT; i++) {
- LSUP_Buffer* vtmp;
- char ptmp[64];
- LSUP_htable_get(ht, keys[i], (void**)&vtmp);
- printf(
- "Key in get: <%lu : %lu : %lu>: ",
- keys[i][0], keys[i][1], keys[i][2]);
- LSUP_buffer_print(vtmp);
- sprintf(ptmp, "<%lu : %lu : %lu>", keys[i][0], keys[i][1], keys[i][2]);
- EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
- }
- LSUP_TripleKey *ktmp;
- LSUP_Buffer *vtmp;
- htsize_t cur = 0;
- while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
- printf(
- "Key in iter: <%lu : %lu : %lu>: ",
- (*ktmp)[0], (*ktmp)[1], (*ktmp)[2]);
- LSUP_buffer_print(vtmp);
- char ptmp[64];
- sprintf(ptmp, "<%lu : %lu : %lu>", (*ktmp)[0], (*ktmp)[1], (*ktmp)[2]);
- EXPECT_INT_EQ(memcmp(ptmp, vtmp->addr, vtmp->size), 0);
- }
- cur = 0;
- while(LSUP_htable_iter(ht, &cur, (void**)&ktmp, (void**)&vtmp) == LSUP_OK) {
- LSUP_buffer_done(vtmp);
- }
- printf("Freeing hash table.\n");
- LSUP_htable_free(ht);
- return 0;
- }
- int htable_tests()
- {
- RUN(htable_idx);
- RUN(htable_keys);
- return 0;
- }
|