From e06e795ed8a8f34ce50f27d5ecb68429386b3eec Mon Sep 17 00:00:00 2001 From: Leonardo Date: Wed, 9 Oct 2019 00:53:40 -0300 Subject: [PATCH] hashmap quickfix --- src/cmc/hashmap.h | 3 ++- tests/main/unt/hashmap.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cmc/hashmap.h b/src/cmc/hashmap.h index ddedb46..797a3e6 100644 --- a/src/cmc/hashmap.h +++ b/src/cmc/hashmap.h @@ -360,7 +360,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, \ break; \ } \ - else if (target->dist < original_pos - pos) \ + else if (target->dist < pos - original_pos) \ { \ K tmp_k = target->key; \ V tmp_v = target->value; \ @@ -527,6 +527,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, { \ return _map_->capacity; \ } \ + \ double PFX##_load(SNAME *_map_) \ { \ return _map_->load; \ diff --git a/tests/main/unt/hashmap.c b/tests/main/unt/hashmap.c index b020096..1ac5415 100644 --- a/tests/main/unt/hashmap.c +++ b/tests/main/unt/hashmap.c @@ -124,6 +124,22 @@ CMC_CREATE_UNIT(hashmap_test, true, { hm_free(map, NULL); }); + CMC_CREATE_TEST(insert[distances], { + hashmap *map = hm_new(1, 0.99, cmp, hashcapminus4); + + cmc_assert_not_equals(ptr, NULL, map); + + cmc_assert_equals(size_t, cmc_hashtable_primes[0], hm_capacity(map)); + + for (size_t i = 0; i < 6; i++) + cmc_assert(hm_insert(map, i, i)); + + for (size_t i = 0; i < 6; i++) + cmc_assert_equals(size_t, i, hm_impl_get_entry(map, i)->dist); + + hm_free(map, NULL); + }); + CMC_CREATE_TEST(insert[buffer growth and item preservation], { hashmap *map = hm_new(1, 0.99, cmp, hash);